feat: aichat 改用 tdesign chat

This commit is contained in:
2026-09-19 22:48:13 +08:00
parent ffb9f2ca90
commit 609fdaf71f
7 changed files with 1782 additions and 311 deletions
+18 -10
View File
@@ -59,20 +59,28 @@ class OpenAICompatibleStreamer(ChatStreamer):
yield text
return
# 工具轮:先非流式执行 tool_calls,最后一轮流式输出正文
# 工具轮:每轮用 astream 边生成边推 token;出现 tool_call 再执行工具后继续
bound = model.bind_tools(tools)
tool_map = {t.name: t for t in tools if getattr(t, "name", None)}
for _ in range(5):
ai = await bound.ainvoke(messages)
messages.append(ai)
tool_calls = getattr(ai, "tool_calls", None) or []
gathered = None
saw_tool = False
async for chunk in bound.astream(messages):
gathered = chunk if gathered is None else gathered + chunk
if getattr(chunk, "tool_call_chunks", None) or getattr(chunk, "tool_calls", None):
saw_tool = True
continue
if saw_tool:
continue
text = _chunk_text(getattr(chunk, "content", None))
if text:
yield text
if gathered is None:
yield "(模型未返回内容)"
return
messages.append(gathered)
tool_calls = getattr(gathered, "tool_calls", None) or []
if not tool_calls:
content = ai.content if isinstance(ai.content, str) else _chunk_text(ai.content)
if content:
# 按小段 yield,便于前端渐进展示
step = 24
for i in range(0, len(content), step):
yield content[i : i + step]
return
yield f"\n[调用工具: {', '.join(tc.get('name', '?') for tc in tool_calls)}]\n"