From 7bbb1bf15258cd501e7018da3301db55edad9a0f Mon Sep 17 00:00:00 2001 From: root <953994191@qq.com> Date: Thu, 7 May 2026 01:41:51 +0800 Subject: [PATCH] =?UTF-8?q?debug:=20=E7=BB=99=20agent=20=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=A6=E7=BB=86=E6=97=A5=E5=BF=97=E5=92=8C?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/main_graph/nodes/agent.py | 44 ++++++++++++++++++--------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/backend/app/main_graph/nodes/agent.py b/backend/app/main_graph/nodes/agent.py index 225468f..7248269 100644 --- a/backend/app/main_graph/nodes/agent.py +++ b/backend/app/main_graph/nodes/agent.py @@ -68,22 +68,36 @@ def create_agent_node(llm_with_tools, llm): """ info(f"[Agent] 第 {state.current_step} 步推理") - # 组装完整消息:系统提示 + 历史消息 - full_messages = [SystemMessage(content=SYSTEM_PROMPT)] + state.messages + try: + # 组装完整消息:系统提示 + 历史消息 + full_messages = [SystemMessage(content=SYSTEM_PROMPT)] + state.messages + + info(f"[Agent] 消息数量: {len(full_messages)}, 最后一条: {type(full_messages[-1]).__name__}") - # 判断是否达到步数上限 - if state.current_step >= state.max_steps: - info(f"[Agent] 达到步数上限 {state.max_steps},强制结束,不绑定工具") - llm_no_tools = llm.bind_tools([]) - response = await llm_no_tools.ainvoke(full_messages) - else: - response = await llm_with_tools.ainvoke(full_messages) + # 判断是否达到步数上限 + if state.current_step >= state.max_steps: + info(f"[Agent] 达到步数上限 {state.max_steps},强制结束,不绑定工具") + llm_no_tools = llm.bind_tools([]) + response = await llm_no_tools.ainvoke(full_messages) + else: + info(f"[Agent] 调用带工具的 LLM...") + response = await llm_with_tools.ainvoke(full_messages) + + info(f"[Agent] LLM 调用成功!响应类型: {type(response).__name__}") + if hasattr(response, 'tool_calls') and response.tool_calls: + info(f"[Agent] 检测到工具调用: {[tc['name'] for tc in response.tool_calls]}") - # 返回状态更新(注意:不原地修改 state,返回字典让 LangGraph 处理 - return { - "messages": [response], - "current_step": state.current_step + 1, - "llm_calls": state.llm_calls + 1 - } + # 返回状态更新(注意:不原地修改 state,返回字典让 LangGraph 处理 + return { + "messages": [response], + "current_step": state.current_step + 1, + "llm_calls": state.llm_calls + 1 + } + + except Exception as e: + error(f"[Agent] ❌ 第 {state.current_step} 步推理出错: {e}") + import traceback + error(f"[Agent] 堆栈: {traceback.format_exc()}") + raise return agent_node