mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 17:49:19 +08:00
feat: 工具调用改为独立 SSE 事件并以中文状态条展示
This commit is contained in:
@@ -8,7 +8,8 @@ from typing import Any, AsyncIterator
|
||||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
from domain.models import ChatMessage, LlmRuntimeConfig, MessageRole
|
||||
from agent.tools.labels import tool_status_label
|
||||
from domain.models import ChatMessage, LlmRuntimeConfig, MessageRole, StreamEvent
|
||||
from domain.ports import ChatStreamer
|
||||
|
||||
_BASE_SYSTEM = (
|
||||
@@ -39,7 +40,7 @@ class OpenAICompatibleStreamer(ChatStreamer):
|
||||
tools: list[Any] | None = None,
|
||||
memory_count: int = 0,
|
||||
memory_just_saved: str | None = None,
|
||||
) -> AsyncIterator[str]:
|
||||
) -> AsyncIterator[StreamEvent]:
|
||||
system = _BASE_SYSTEM
|
||||
if memory_count > 0:
|
||||
system += f"\n\n当前用户约有 {memory_count} 条长期记忆,需要时用 memory_search / memory_load。"
|
||||
@@ -66,7 +67,7 @@ class OpenAICompatibleStreamer(ChatStreamer):
|
||||
async for chunk in model.astream(messages):
|
||||
text = _chunk_text(chunk.content)
|
||||
if text:
|
||||
yield text
|
||||
yield StreamEvent.token(text)
|
||||
return
|
||||
|
||||
# 工具轮:每轮用 astream 边生成边推 token;出现 tool_call 再执行工具后继续
|
||||
@@ -84,16 +85,28 @@ class OpenAICompatibleStreamer(ChatStreamer):
|
||||
continue
|
||||
text = _chunk_text(getattr(chunk, "content", None))
|
||||
if text:
|
||||
yield text
|
||||
yield StreamEvent.token(text)
|
||||
if gathered is None:
|
||||
yield "(模型未返回内容)"
|
||||
yield StreamEvent.token("(模型未返回内容)")
|
||||
return
|
||||
messages.append(gathered)
|
||||
tool_calls = getattr(gathered, "tool_calls", None) or []
|
||||
if not tool_calls:
|
||||
return
|
||||
|
||||
yield f"\n[调用工具: {', '.join(tc.get('name', '?') for tc in tool_calls)}]\n"
|
||||
# 去重保序的工具状态(同轮多次同名只显示一次文案)
|
||||
seen: set[str] = set()
|
||||
for tc in tool_calls:
|
||||
name = tc.get("name") or ""
|
||||
if name in seen:
|
||||
continue
|
||||
seen.add(name)
|
||||
yield StreamEvent.tool(
|
||||
phase="start",
|
||||
name=name,
|
||||
label=tool_status_label(name),
|
||||
)
|
||||
|
||||
for tc in tool_calls:
|
||||
name = tc.get("name") or ""
|
||||
args = tc.get("args") or {}
|
||||
@@ -104,12 +117,23 @@ class OpenAICompatibleStreamer(ChatStreamer):
|
||||
result = f"未知工具: {name}"
|
||||
else:
|
||||
raw = await tool.ainvoke(args)
|
||||
result = raw if isinstance(raw, str) else json.dumps(raw, ensure_ascii=False, default=str)
|
||||
result = (
|
||||
raw
|
||||
if isinstance(raw, str)
|
||||
else json.dumps(raw, ensure_ascii=False, default=str)
|
||||
)
|
||||
except Exception as e: # noqa: BLE001
|
||||
result = f"工具执行失败: {e}"
|
||||
messages.append(ToolMessage(content=str(result)[:8000], tool_call_id=call_id))
|
||||
|
||||
yield "工具调用轮次过多,请缩小问题范围后重试。"
|
||||
for name in seen:
|
||||
yield StreamEvent.tool(
|
||||
phase="end",
|
||||
name=name,
|
||||
label=tool_status_label(name),
|
||||
)
|
||||
|
||||
yield StreamEvent.token("工具调用轮次过多,请缩小问题范围后重试。")
|
||||
|
||||
|
||||
def _chunk_text(content: Any) -> str:
|
||||
|
||||
Reference in New Issue
Block a user