LlamaIndex ToolInteractiveReflectionAgentWorker修正性反思失效及报错问题
解决LlamaIndex内省代理的UnboundLocalError及修正失效问题
一、修复UnboundLocalError崩溃问题
这个错误是因为主代理工作线程(main_agent_worker)为None或未生成有效响应时,original_response变量未被赋值就传给了反思代理。
解决方法:
- 确保
main_agent_worker正确初始化:必须绑定有效的LLM实例和工具集合,绝对不能设为None。 - 添加响应前置检查:在执行反思代理逻辑前,先判断主代理是否生成了有效响应,避免触发未定义变量错误。示例代码片段:
# 执行主代理步骤并获取响应 original_response = main_agent_worker.run_step(step_state) # 检查响应是否有效 if not original_response or not hasattr(original_response, 'response'): return AgentFinish(response="无法生成有效响应,请重试", is_done=True) # 再执行反思代理的批评与修正逻辑 reflective_agent_response = reflective_agent.chat(original_response.response)
二、修复代理修正失效及设备间行为不一致问题
核心原因:
- 反思代理的提示词模板偏离官方示例,导致批评逻辑未正确触发
- 不同设备的LLM参数(温度、模型版本)不一致,影响响应生成
- 替换工具后,主代理的响应场景与反思代理的评判标准不匹配
解决步骤:
- 严格复用官方提示词模板:不要自定义修改反思代理的批评/修正提示词,确保逻辑对齐示例要求。示例模板:
REFLECTIVE_CRITIQUE_PROMPT = """ You are a critic tasked with evaluating the original assistant response against [你的评判标准,比如毒性、合规性]. Provide a clear, concise critique pointing out issues with the original response. """ REFLECTIVE_CORRECTION_PROMPT = """ You are a corrector. Using the provided critique, revise the original response to fix all identified issues. """ - 统一LLM配置:所有设备使用相同的LLM模型、温度(建议设为0.1,减少随机性)、上下文窗口参数,消除设备间差异。
- 验证工具调用逻辑:确保替换后的工具能被主代理正常调用,且主代理的响应能触发反思代理的评判逻辑(比如测试时故意输入会触发违规的问题,验证批评和修正流程)。
三、版本兼容补充检查
即使执行了指定版本的降级安装,仍需确认:
- 执行
pip freeze检查依赖版本是否完全匹配:llama-index-agent-introspective==0.1.0 llama-index-llms-openai==0.1.19 llama-index-agent-openai==0.2.5 llama-index-core==0.10.37 - 清理pip缓存后重新安装,避免旧版本缓存干扰:
pip cache purge pip install --upgrade --force-reinstall \ llama-index-agent-introspective==0.1.0 \ llama-index-llms-openai==0.1.19 \ llama-index-agent-openai==0.2.5 \ llama-index-core==0.10.37
内容的提问来源于stack exchange,提问作者Burny
相关产品推荐
相关产品推荐

