|
@@ -40,8 +40,8 @@ def decide_next(llm, scene: dict, history: list, prompt_fn) -> dict:
|
|
|
prompt = prompt_fn(scene, "", history) # 知识在 router 中检索后注入,见 retrieve_and_decide
|
|
prompt = prompt_fn(scene, "", history) # 知识在 router 中检索后注入,见 retrieve_and_decide
|
|
|
last_err = None
|
|
last_err = None
|
|
|
for attempt in range(2):
|
|
for attempt in range(2):
|
|
|
- resp = llm.invoke([{"role": "human", "content": prompt}])
|
|
|
|
|
try:
|
|
try:
|
|
|
|
|
+ resp = llm.invoke([{"role": "human", "content": prompt}])
|
|
|
data = _extract_json(resp.content)
|
|
data = _extract_json(resp.content)
|
|
|
action = data.get("action")
|
|
action = data.get("action")
|
|
|
if action == "ask":
|
|
if action == "ask":
|
|
@@ -64,8 +64,8 @@ def generate_profile(llm, scene: dict, history: list, kb_context: Optional[list]
|
|
|
if kb_context:
|
|
if kb_context:
|
|
|
kb_text = "\n---\n".join(d.get("content", "") for d in kb_context[:5])
|
|
kb_text = "\n---\n".join(d.get("content", "") for d in kb_context[:5])
|
|
|
prompt = prompt_fn(scene, kb_text, history)
|
|
prompt = prompt_fn(scene, kb_text, history)
|
|
|
- resp = llm.invoke([{"role": "human", "content": prompt}])
|
|
|
|
|
try:
|
|
try:
|
|
|
|
|
+ resp = llm.invoke([{"role": "human", "content": prompt}])
|
|
|
data = _extract_json(resp.content)
|
|
data = _extract_json(resp.content)
|
|
|
up = data.get("user_profile", [])
|
|
up = data.get("user_profile", [])
|
|
|
np = data.get("need_profile", [])
|
|
np = data.get("need_profile", [])
|
|
@@ -73,4 +73,23 @@ def generate_profile(llm, scene: dict, history: list, kb_context: Optional[list]
|
|
|
item["score"] = max(0, min(100, int(item.get("score", 0))))
|
|
item["score"] = max(0, min(100, int(item.get("score", 0))))
|
|
|
return {"user_profile": up, "need_profile": np}, bool(kb_text)
|
|
return {"user_profile": up, "need_profile": np}, bool(kb_text)
|
|
|
except Exception:
|
|
except Exception:
|
|
|
- return {"user_profile": [], "need_profile": []}, False
|
|
|
|
|
|
|
+ return {"user_profile": [], "need_profile": []}, False
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def retrieve_and_decide(llm, scene, history, kb_context, prompt_fn) -> dict:
|
|
|
|
|
+ """知识注入版出题:把 kb_context 转文本后调用 LLM 决策(供 router 使用)"""
|
|
|
|
|
+ kb_text = ""
|
|
|
|
|
+ if kb_context:
|
|
|
|
|
+ kb_text = "\n---\n".join(d.get("content", "") for d in kb_context[:5])
|
|
|
|
|
+ prompt = prompt_fn(scene, kb_text, history)
|
|
|
|
|
+ try:
|
|
|
|
|
+ resp = llm.invoke([{"role": "human", "content": prompt}])
|
|
|
|
|
+ data = _extract_json(resp.content)
|
|
|
|
|
+ if data.get("action") == "finish":
|
|
|
|
|
+ return {"action": "finish", "reason": data.get("reason", "信息已足够")}
|
|
|
|
|
+ q = schemas.Question(**data["question"])
|
|
|
|
|
+ return {"action": "ask", "question": q.model_dump(), "reason": data.get("reason", "")}
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ return {"fallback": True, "reason": f"出题失败: {e}",
|
|
|
|
|
+ "question": {"id": f"fb{len(history)+1}", "type": "text",
|
|
|
|
|
+ "text": "请简单描述您最近一周的饮食和作息情况。"}}
|