mirror of
https://github.com/Kakune55/PyGetGPT.git
synced 2025-09-15 03:39:31 +08:00
feat:完成简易的的前端和后端api
This commit is contained in:
21
router/chat.py
Normal file
21
router/chat.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from fastapi import APIRouter
|
||||
from model.util import InputData, getModels, getModelsInfo, requestModel
|
||||
from pydantic import BaseModel
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class ChatRequest(BaseModel):
|
||||
model: str
|
||||
message: str
|
||||
@router.post("/api/chat")
|
||||
def chat(req: ChatRequest):
|
||||
model_name = req.model
|
||||
input_data = InputData(req.message)
|
||||
#调用模型
|
||||
ret = requestModel(model_name, input_data)
|
||||
return ret
|
||||
|
||||
|
||||
@router.get("/api/chat/models")
|
||||
def get_models():
|
||||
return {"models": getModelsInfo()}
|
30
router/page.py
Normal file
30
router/page.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from fastapi import APIRouter
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from starlette.responses import FileResponse
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# 将 Vue 构建后的 dist 目录中的 assets 作为静态资源目录
|
||||
#router.mount("/assets", StaticFiles(directory="frontend/dist/assets"), name="assets")
|
||||
@router.get("/assets/{path:path}")
|
||||
async def serve_static(path: str):
|
||||
if path.endswith(".css"):
|
||||
return FileResponse(f"frontend/dist/assets/{path}",media_type="text/css")
|
||||
elif path.endswith(".js"):
|
||||
return FileResponse(f"frontend/dist/assets/{path}",media_type="text/javascript")
|
||||
elif path.endswith(".png"):
|
||||
return FileResponse(f"frontend/dist/assets/{path}",media_type="image/png")
|
||||
else:
|
||||
return FileResponse(f"frontend/dist/assets/{path}")
|
||||
|
||||
|
||||
# 根路径提供 index.html
|
||||
@router.get("/")
|
||||
async def serve_vue_app():
|
||||
return FileResponse("frontend/dist/index.html")
|
||||
|
||||
# 通用路由处理,确保 history 模式下的路由正常工作
|
||||
@router.get("/{full_path:path}")
|
||||
async def serve_vue_app_catch_all(full_path: str):
|
||||
return FileResponse("frontend/dist/index.html")
|
19
router/user.py
Normal file
19
router/user.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import APIRouter, Response
|
||||
from pydantic import BaseModel
|
||||
|
||||
import dao.db.user as user
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class LoginRequest(BaseModel):
|
||||
userinfo: str | None = None
|
||||
password: str
|
||||
|
||||
@router.post("/api/user/login")
|
||||
def login(response: Response,LoginRequest: LoginRequest):
|
||||
#if not user.checkUser(uid, password_hash):
|
||||
if False:
|
||||
return {"code": 401, "message": "Invalid credentials"}
|
||||
response.set_cookie(key="auth_token", value="1234567890")
|
||||
return {"code": 200, "message": "Login successful"}
|
Reference in New Issue
Block a user