介绍

在 CrewAI 中,您可以强制将工具的输出作为代理任务的结果。当您希望确保工具输出被捕获并作为任务结果返回,避免在任务执行期间被代理修改时,此功能非常有用。

强制将工具输出作为结果

要强制将工具输出作为代理任务的结果,您需要在将工具添加到代理时将参数 result_as_answer 设置为 True。此参数可确保工具输出被捕获并作为任务结果返回,而不会被代理进行任何修改。

以下是强制将工具输出作为代理任务结果的示例

代码
from crewai.agent import Agent
from my_tool import MyCustomTool

# Create a coding agent with the custom tool
coding_agent = Agent(
        role="Data Scientist",
        goal="Produce amazing reports on AI",
        backstory="You work with data and AI",
        tools=[MyCustomTool(result_as_answer=True)],
    )

# Assuming the tool's execution and result population occurs within the system
task_result = coding_agent.execute_task(task)

实际工作流程

1

任务执行

代理使用提供的工具执行任务。

2

工具输出

工具生成输出,并将其捕获为任务结果。

3

代理交互

代理可能会反思并从工具中学习,但输出不会被修改。

4

结果返回

工具输出作为任务结果返回,没有任何修改。