主题
从最近一次 Crew Kickoff 重放任务
从最近的 crew.kickoff(...) 重放任务
引言
CrewAI 支持从最近一次 crew kickoff 中指定的任务开始重放。当你完成一次 kickoff 后,可能想重试某些任务,或者不需要重新获取数据,而你的智能体已经从 kickoff 执行中保存了上下文,因此你只需重放想要的任务即可——这时该功能尤其有用。
NOTE
在重放任务之前,必须先运行 crew.kickoff()。 目前仅支持最近一次的 kickoff,因此如果你使用 kickoff_for_each,就只能从最近一次 crew 运行中重放。
以下是从某个任务开始重放的示例:
使用 CLI 从指定任务重放
要使用重放功能,请按以下步骤操作:
Open your terminal or command prompt.
Navigate to the directory where your CrewAI project is located.
Run the following commands:
要查看最近一次 kickoff 的任务_ids,请使用:
shell
crewai log-tasks-outputs拿到要重放的 task_id 后,使用:
shell
crewai replay -t <task_id>NOTE
确保你的开发环境中已正确安装并配置 crewai。
以编程方式从任务重放
要以编程方式从某个任务重放,请按以下步骤操作:
Specify the `task_id` and input parameters for the replay process.
指定用于重放流程的 task_id 和输入参数。
Execute the replay command within a try-except block to handle potential errors.
在 try-except 代码块中执行重放命令,以处理可能出现的错误。
python
def replay():
"""
Replay the crew execution from a specific task.
"""
task_id = '<task_id>'
inputs = {"topic": "CrewAI Training"} # This is optional; you can pass in the inputs you want to replay; otherwise, it uses the previous kickoff's inputs.
try:
YourCrewName_Crew().crew().replay(task_id=task_id, inputs=inputs)
except subprocess.CalledProcessError as e:
raise Exception(f"An error occurred while replaying the crew: {e}")
except Exception as e:
raise Exception(f"An unexpected error occurred: {e}")结论
借助上述增强功能和详细说明,在 CrewAI 中重放特定任务变得更加高效和健壮。 请严格遵循命令和步骤,充分利用这些功能。