fix: 修复 _generate_node 中 messages[-3] 硬编码索引在多工具调用场景下的潜在问题#79
Open
KEDADA wants to merge 4 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
naive_rag_agent.py、graph_agent.py、hybrid_agent.py 和 deep_research_agent.py 的 _generate_node 等方法中,使用
messages[-3].content获取用户原始问题。该写法假定 messages 结构固定为 3 条:
[HumanMessage, AIMessage(tool_calls), ToolMessage]潜在问题
当 LLM 在单次调用中返回多个 tool_calls 时,
ToolNode会为每个 tool_call 生成独立的 ToolMessage,导致 messages 长度 > 3,此时messages[-3]不再指向 HumanMessage,可能取到错误内容。修复方案
在 BaseAgent 中新增 _find_last_human_message() 静态方法,通过类型匹配(
isinstance(msg, HumanMessage))遍历查找最后一条用户消息,代替硬编码索引。修改文件
messages[-3]messages[-3]messages[-3]messages[-3]影响范围
该修改仅改变获取用户问题的方式(从索引查找改为类型查找),不影响任何业务逻辑和返回结果。在当前单工具调用场景下行为完全一致,同时为未来多工具调用场景提供了安全保障。