This commit is contained in:
hailin 2025-08-01 11:30:15 +08:00
parent d33a596dfa
commit 452a2ed902
1 changed files with 50 additions and 17 deletions

View File

@ -152,29 +152,62 @@ def chat(
# yield "⏳ 正在生成中...", log_state
if api_suffix == "/v1/chat/completions":
while True:
# ⚠️ 线程已结束且队列已空 → 直接 return 让生成器终止
if not thread.is_alive() and result_q.empty():
break
# return # ← 新增这一行
try:
result = result_q.get(timeout=0.1)
except Empty:
continue
# 统一格式
if isinstance(result, str):
result = {"text": result}
elif not isinstance(result, dict) or "text" not in result:
result = {"text": str(result)}
# yield result, log_state # 第一次真正把模型回复丢给前端
if api_suffix == "/v1/chat/completions":
history.append({"role": "assistant", "content": result["text"]})
yield result["text"], None
yield result["text"], history # ✅ 显示模型输出,同时更新 history
return
else:
yield result["text"], log_state
while thread.is_alive():
try:
result = result_q.get(timeout=0.1)
break
except Empty:
continue
if isinstance(result, str):
result = {"text": result}
elif not isinstance(result, dict) or "text" not in result:
result = {"text": str(result)}
yield result["text"], log_state # ✅ 其它接口只输出文本,不更新 history
return
# while True:
# # ⚠️ 线程已结束且队列已空 → 直接 return 让生成器终止
# if not thread.is_alive() and result_q.empty():
# break
# # return # ← 新增这一行
# try:
# result = result_q.get(timeout=0.1)
# except Empty:
# continue
# # 统一格式
# if isinstance(result, str):
# result = {"text": result}
# elif not isinstance(result, dict) or "text" not in result:
# result = {"text": str(result)}
# # yield result, log_state # 第一次真正把模型回复丢给前端
# if api_suffix == "/v1/chat/completions":
# history.append({"role": "assistant", "content": result["text"]})
# yield result["text"], None
# else:
# yield result["text"], log_state
# return # ← 把旧的 break 换成 return