可以向代理和团队添加工具。这使您可以在初始化后灵活地向现有的代理或团队实例添加工具,这对于动态工具管理或根据运行时需求有条件地添加工具非常有用。 还可以通过调用 set_tools 来更新代理或团队可用的全部工具。请注意,这会移除已分配给您的代理或团队的任何其他工具,并用提供给 set_tools 的工具列表进行覆盖。

代理示例

创建您自己的工具,例如 get_weather。然后调用 add_tool 将其附加到您的代理。

add_agent_tool_post_initialization.py
import random

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools import tool


@tool(show_result=True, stop_after_tool_call=True)
def get_weather(city: str) -> str:
    """Get the weather for a city."""
    # In a real implementation, this would call a weather API
    weather_conditions = ["sunny", "cloudy", "rainy", "snowy", "windy"]
    random_weather = random.choice(weather_conditions)

    return f"The weather in {city} is {random_weather}."


agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    markdown=True,
)

agent.print_response("What can you do?", stream=True)

agent.add_tool(get_weather)

agent.print_response("What is the weather in San Francisco?", stream=True)

团队示例

创建工具列表,并使用 set_tools 将它们分配给您的团队

add_team_tool_post_initialization.py
import random

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.team.team import Team
from agno.tools import tool
from agno.tools.calculator import CalculatorTools


agent1 = Agent(
    name="Stock Searcher",
    model=OpenAIChat("gpt-4o"),
)

agent2 = Agent(
    name="Company Info Searcher",
    model=OpenAIChat("gpt-4o"),
)

team = Team(
    name="Stock Research Team",
    mode="route",
    model=OpenAIChat("gpt-4o"),
    members=[agent1, agent2],
    tools=[CalculatorTools()],
    markdown=True,
    show_members_responses=True,
)


@tool
def get_stock_price(stock_symbol: str) -> str:
    """Get the current stock price of a stock."""
    return f"The current stock price of {stock_symbol} is {random.randint(100, 1000)}."

@tool
def get_stock_availability(stock_symbol: str) -> str:
    """Get the current availability of a stock."""
    return f"The current stock available of {stock_symbol} is {random.randint(100, 1000)}."


team.set_tools([get_stock_price, get_stock_availability])

team.print_response("What is the current stock price of NVDA?", stream=True)
team.print_response("How much stock NVDA stock is available?", stream=True)

add_tool 方法允许您动态扩展代理或团队的功能。当您希望根据用户输入或其他运行时条件添加工具时,这特别有用。 set_tool 方法允许您覆盖代理或团队的功能。请注意,这将移除先前分配给您的代理或团队的任何现有工具。

相关文档