工具

函数工具

灵活定义您的工具

VeADK 支持通过函数定义的方式快速创建自定义工具,使 Agent 能够调用本地业务逻辑代码完成特定任务。

以下示例展示了 Agent 调用一个自定义的模拟天气查询的函数工具 get_city_weather

函数的 docstring 尤为重要,它将作为模型提示词。
agent_with_tool.py
import asyncio

from veadk import Agent, Runner


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}"}


agent = Agent(tools=[get_city_weather])

runner = Runner(agent=agent)

response = asyncio.run(runner.run(messages="北京的天气"))

print(response)