需求未定稿时可先跑通追问/判定/出方案;槽位与判定树外置 YAML,后续可接 Ragflow。 Co-authored-by: haijd <haijd@163.com>
76 lines
1.9 KiB
Python
76 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI, HTTPException
|
|
from fastapi.responses import FileResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from pydantic import BaseModel, Field
|
|
|
|
from .config_loader import business_config
|
|
from .scheme_engine import create_session, handle_message
|
|
from .settings import settings
|
|
|
|
STATIC_DIR = Path(__file__).resolve().parent / "static"
|
|
|
|
app = FastAPI(title=settings.app_name, version="0.1.0")
|
|
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
|
|
|
|
|
class ChatRequest(BaseModel):
|
|
message: str = Field(..., min_length=1)
|
|
session_id: str | None = None
|
|
|
|
|
|
class SessionResponse(BaseModel):
|
|
session_id: str
|
|
|
|
|
|
@app.get("/")
|
|
def index() -> FileResponse:
|
|
return FileResponse(STATIC_DIR / "index.html")
|
|
|
|
|
|
@app.get("/api/health")
|
|
def health() -> dict:
|
|
return {
|
|
"ok": True,
|
|
"app": settings.app_name,
|
|
"retrieval_backend": settings.retrieval_backend,
|
|
"slots": len(business_config.slots),
|
|
"rules": len(business_config.rules),
|
|
}
|
|
|
|
|
|
@app.get("/api/config/slots")
|
|
def get_slots() -> dict:
|
|
return business_config.slots_doc
|
|
|
|
|
|
@app.get("/api/config/decision-tree")
|
|
def get_tree() -> dict:
|
|
return business_config.tree_doc
|
|
|
|
|
|
@app.post("/api/admin/reload-config")
|
|
def reload_config() -> dict:
|
|
business_config.reload()
|
|
return {"reloaded": True, "slots": len(business_config.slots), "rules": len(business_config.rules)}
|
|
|
|
|
|
@app.post("/api/sessions", response_model=SessionResponse)
|
|
def api_create_session() -> SessionResponse:
|
|
session = create_session()
|
|
return SessionResponse(session_id=session.id)
|
|
|
|
|
|
@app.post("/api/chat")
|
|
def api_chat(body: ChatRequest) -> dict:
|
|
session_id = body.session_id
|
|
if not session_id:
|
|
session_id = create_session().id
|
|
try:
|
|
return handle_message(session_id, body.message)
|
|
except KeyError:
|
|
raise HTTPException(status_code=404, detail="session_not_found") from None
|