feat(v2): 模型设置(用户可调小模型/大模型/管线,/config + UI)
This commit is contained in:
+58
-3
@@ -39,6 +39,7 @@ _router: Optional[Router] = None
|
||||
_pipeline: Optional["CollaborativePipeline"] = None
|
||||
_v2stats = V2Stats() if _V2_OK else None
|
||||
_review = None
|
||||
_settings = None
|
||||
|
||||
|
||||
def get_router() -> Router:
|
||||
@@ -56,8 +57,23 @@ def get_review() -> "ReviewQueue":
|
||||
return _review
|
||||
|
||||
|
||||
def settings_store():
|
||||
"""用户可调整设置(懒加载单例)。"""
|
||||
global _settings
|
||||
if _settings is None:
|
||||
from gateway.settings import load_settings
|
||||
_settings = load_settings()
|
||||
return _settings
|
||||
|
||||
|
||||
def rebuild_pipeline() -> None:
|
||||
"""清除管线单例,下次调用重建(配置改动后生效)。"""
|
||||
global _pipeline
|
||||
_pipeline = None
|
||||
|
||||
|
||||
def build_v2_pipeline(worker_cfg_override: Optional[dict] = None):
|
||||
"""从配置构建 v2 协作管线(architect + worker + pipeline)。
|
||||
"""从配置 + 用户设置构建 v2 协作管线(architect + worker + pipeline)。
|
||||
|
||||
worker_cfg_override 可注入(测试/演示用 mock)。无 API key 时 /chat 会走
|
||||
本地降级路径(不崩溃)。
|
||||
@@ -65,13 +81,27 @@ def build_v2_pipeline(worker_cfg_override: Optional[dict] = None):
|
||||
global _pipeline
|
||||
if _pipeline is None:
|
||||
cfg = load_config()
|
||||
s = settings_store().to_dict() if _V2_OK else {}
|
||||
kb = KnowledgeBase()
|
||||
architect = build_architect(cfg.get("architect", {}))
|
||||
|
||||
# architect(合并用户设置)
|
||||
acfg = dict(cfg.get("architect", {}))
|
||||
acfg.update(s.get("architect", {}))
|
||||
architect = build_architect(acfg)
|
||||
|
||||
# worker(合并用户设置;backend 可 mock/openai/llama_server)
|
||||
wcfg = dict(cfg.get("worker", {}))
|
||||
wcfg.update(s.get("worker", {}))
|
||||
if worker_cfg_override:
|
||||
wcfg.update(worker_cfg_override)
|
||||
worker = build_worker(wcfg, kb=kb)
|
||||
_pipeline = build_pipeline(cfg, architect, worker)
|
||||
|
||||
# pipeline(合并用户设置)
|
||||
cfg2 = dict(cfg)
|
||||
pcfg = dict(cfg.get("pipeline", {}))
|
||||
pcfg.update(s.get("pipeline", {}))
|
||||
cfg2["pipeline"] = pcfg
|
||||
_pipeline = build_pipeline(cfg2, architect, worker)
|
||||
return _pipeline
|
||||
|
||||
|
||||
@@ -221,6 +251,31 @@ try:
|
||||
raise HTTPException(status_code=404, detail=f"审核记录不存在或已审核: {review_id}")
|
||||
return {"ok": True, "review_id": review_id, "verdict": verdict}
|
||||
|
||||
# ---------------- 模型设置(用户可调整) ----------------
|
||||
@app.get("/config", tags=["settings"])
|
||||
async def get_config():
|
||||
"""读取当前可调整设置(小模型 / 大模型 / 管线)。"""
|
||||
return settings_store().to_dict()
|
||||
|
||||
@app.put("/config", tags=["settings"])
|
||||
async def put_config(patch: dict):
|
||||
"""部分更新设置并重建管线。示例:
|
||||
{"worker": {"backend": "openai", "base_url": "http://127.0.0.1:11434/v1", "temperature": 0.4}}
|
||||
"""
|
||||
try:
|
||||
merged = settings_store().update(patch)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=f"设置非法: {e}")
|
||||
rebuild_pipeline()
|
||||
return merged
|
||||
|
||||
@app.post("/config/reset", tags=["settings"])
|
||||
async def reset_config():
|
||||
"""恢复默认设置并重建管线。"""
|
||||
merged = settings_store().reset()
|
||||
rebuild_pipeline()
|
||||
return merged
|
||||
|
||||
# ---------------- metrics ----------------
|
||||
@app.get("/metrics", tags=["system"])
|
||||
async def metrics():
|
||||
|
||||
Reference in New Issue
Block a user