Skip to main content
在大多数生产场景中,您都需要编写自己的工具。这就是为什么 Agno 专注于提供最佳的工具使用体验。 规则很简单:
  • 任何 Python 函数都可以被 Agent 用作工具。
  • 使用 @tool 装饰器来修改该工具调用之前和之后发生的事情。

任何 Python 函数都可以用作工具

例如,以下是如何将 get_top_hackernews_stories 函数用作工具:
hn_agent.py

@tool 装饰器的魔力

要修改工具的行为,请使用 @tool 装饰器。一些值得注意的特性:
  • requires_confirmation=True: 在执行前需要用户确认。
  • requires_user_input=True: 在执行前需要用户输入。使用 user_input_fields 指定需要用户输入的字段。
  • external_execution=True: 工具将在 Agent 的控制之外执行。
  • show_result=True: 在 Agent 的响应中显示工具调用的输出。没有此标志,工具调用的结果将被发送到模型进行进一步处理。
  • stop_after_tool_call=True: 在工具调用后停止 Agent 运行。
  • tool_hooks: 在此工具调用之前和之后运行自定义逻辑。
  • cache_results=True: 缓存工具结果以避免重复相同的调用。使用 cache_dircache_ttl 配置缓存。
这是一个使用 @tool 装饰器上许多可能参数的示例。
advanced_tool.py

@tool 参数参考