CrewAI自定义AskHumanInputTool缓存人类输入问题求助
解决CrewAI自定义AskHumanInputTool重复填充历史输入的问题
1. 清除并禁用__pycache__
- 直接删除项目下所有
__pycache__文件夹:find . -name "__pycache__" -type d -exec rm -rf {} + - 运行Python脚本时禁用字节码生成,避免生成新的pycache:
PYTHONDONTWRITEBYTECODE=1 python your_script.py
2. 显式禁用工具级缓存
虽然你在Crew中设置了cache=False,但可以在工具类中显式添加缓存禁用配置,确保底层不会复用结果:
from crewai_tools import BaseTool class AskHumanInputTool(BaseTool): name: str = "AskHumanInputTool" description: str = ("""This tool is used for asking a human input. This tool takes a question as an input and ask the input. The tool return a the answer received.""") # 显式禁用工具自身的缓存 cache: bool = False def _run(self, question_for_human: str) -> str: human_input = input(question_for_human) return human_input
3. 避免终端自动补全干扰
如果是终端的readline历史导致自动填充,可以在工具调用时临时禁用历史记录:
import readline from crewai_tools import BaseTool class AskHumanInputTool(BaseTool): name: str = "AskHumanInputTool" description: str = ("""This tool is used for asking a human input. This tool takes a question as an input and ask the input. The tool return a the answer received.""") cache: bool = False def _run(self, question_for_human: str) -> str: # 临时禁用readline历史 readline.clear_history() readline.set_auto_history(False) # 获取输入 human_input = input(question_for_human) # 恢复历史设置 readline.set_auto_history(True) return human_input
4. 检查智能体记忆配置
如果你的多智能体系统使用了记忆组件,确保记忆不会缓存工具的输入输出。比如在定义智能体时,明确设置记忆为无或禁用相关缓存:
from crewai import Agent agent = Agent( role='Your Agent Role', goal='Your Agent Goal', backstory='Your Agent Backstory', # 禁用智能体记忆的缓存行为 memory=False, tools=[AskHumanInputTool()] )
5. 强制每次调用生成新工具实例
在每次智能体任务执行时,不要复用同一个工具实例,而是创建新的实例:
# 避免复用同一个工具实例,每次任务创建新实例 agent = Agent( # ...其他配置 tools=[AskHumanInputTool()] )
内容的提问来源于stack exchange,提问作者Rohan Prasad
相关产品推荐
相关产品推荐

