This commit is contained in:
hailin 2025-08-01 13:53:06 +08:00
parent ebe7f87009
commit e71c4823ef
1 changed files with 13 additions and 6 deletions

View File

@ -148,14 +148,21 @@ def chat(
except Empty:
continue
# 你之前在 worker() 中把完整字符串放进 result_q所以这里只是字符串
if isinstance(result, str):
result = {"text": result}
elif not isinstance(result, dict) or "text" not in result:
result = {"text": str(result)}
# 假设是 JSON 字符串(但你原来实际放的是 content 文本)
try:
parsed = json.loads(result)
txt = parsed["choices"][0]["message"]["content"].strip()
except Exception:
txt = result.strip()
elif isinstance(result, dict):
txt = result.get("text", "").strip()
else:
txt = str(result).strip()
# ❌ 不 append 到 history让前端 UI 不显示之前的历史)
# ✅ 但我们已经在前面把 history 全部传给 LLM 推理了
yield result["text"], None # UI 只显示当前回复
# ✅ 推荐返回结构,自动渲染 + 自动 history 追加
yield {"text": txt}, log_state
return
else:
while thread.is_alive():