工具

函数工具

灵活定义您的工具

VeADK 支持通过函数定义的方式快速创建自定义工具,使 Agent 能够调用本地业务逻辑代码完成特定任务。以下示例展示了 Agent 调用一个自定义的模拟天气查询的函数工具 get_city_weather

agent_with_tool.py
def get_city_weather(city: str) -> dict[str, str]:
    """Retrieves the weather information of a given city. the args must in English"""
    fixed_weather = {
        "beijing": {"condition": "Sunny", "temperature": 25},
        "shanghai": {"condition": "Cloudy", "temperature": 22},
        "guangzhou": {"condition": "Rainy", "temperature": 28}
    }

    city = city.lower().strip()
    if city in fixed_weather:
        info = fixed_weather[city]
        return {"result": f"{info['condition']}, {info['temperature']}°C"}
    else:
        return {"result": f"Weather information not found for {city}"}

session_id = "..."
prompt = "..."
agent = Agent(tools=[get_city_weather])
runner = Runner(agent=agent, short_term_memory=ShortTermMemory())
response = await runner.run(messages=prompt, session_id=session_id)