feat: 多专业小模型+路由模型系统 MVP(mock 全链路 + FastAPI 网关 + 论文调研)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""API 网关:FastAPI 服务。"""␍
|
||||
@@ -0,0 +1,94 @@
|
||||
"""FastAPI 网关:对外提供 /chat /health /metrics 接口。
|
||||
|
||||
启动:
|
||||
uvicorn gateway.api:app --host 0.0.0.0 --port 8000
|
||||
或:
|
||||
python -m gateway.api
|
||||
|
||||
依赖:fastapi, uvicorn, pydantic(见 requirements.txt)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from router_system.config import load_config
|
||||
from router_system.router import Router, build_router
|
||||
|
||||
# ---- 全局单例 ----
|
||||
_router: Optional[Router] = None
|
||||
|
||||
|
||||
def get_router() -> Router:
|
||||
global _router
|
||||
if _router is None:
|
||||
_router = build_router()
|
||||
return _router
|
||||
|
||||
|
||||
# ---- 请求/响应模型 ----
|
||||
class QueryRequest(BaseModel):
|
||||
query: str = Field(..., min_length=1, max_length=8000, description="用户查询")
|
||||
|
||||
|
||||
class QueryResponse(BaseModel):
|
||||
response: str
|
||||
domain: str
|
||||
difficulty: str
|
||||
confidence: float
|
||||
upgraded: bool
|
||||
quality_score: float
|
||||
model_used: str
|
||||
route: List[str]
|
||||
latency_ms: float
|
||||
cache_hit: bool
|
||||
cache_level: Optional[str]
|
||||
cost_est: float
|
||||
error: Optional[str] = None
|
||||
|
||||
|
||||
class HealthResponse(BaseModel):
|
||||
status: str
|
||||
domains: List[str]
|
||||
classifier: str
|
||||
judge: str
|
||||
fallback: str
|
||||
|
||||
|
||||
# ---- FastAPI 应用 ----
|
||||
try:
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI(
|
||||
title="Multi-Expert Router API",
|
||||
description="多专业小模型 + 路由模型系统(MVP)",
|
||||
version="0.1.0",
|
||||
)
|
||||
|
||||
@app.get("/health", response_model=HealthResponse, tags=["system"])
|
||||
async def health():
|
||||
return get_router().health()
|
||||
|
||||
@app.post("/chat", response_model=QueryResponse, tags=["chat"])
|
||||
async def chat(req: QueryRequest):
|
||||
result = await get_router().route(req.query)
|
||||
return QueryResponse(**result.to_dict())
|
||||
|
||||
@app.get("/metrics", tags=["system"])
|
||||
async def metrics():
|
||||
r = get_router()
|
||||
return {
|
||||
"router": r.stats.summary(),
|
||||
"cache": r.cache.stats(),
|
||||
}
|
||||
|
||||
except ImportError:
|
||||
# fastapi 未安装时,提供 CLI 入口提示
|
||||
app = None
|
||||
print("[gateway] 未安装 fastapi,请执行: pip install -r requirements.txt")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run("gateway.api:app", host="0.0.0.0", port=8000, reload=False)␍
|
||||
Reference in New Issue
Block a user