什么是 Function Calling

Function Calling 让 LLM 能够调用外部工具和 API,从”只会说话”变成”会做事”。

基本用法

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "获取指定城市的天气",
            "parameters": {
                "city": {"type": "string"}
            }
        }
    }
]

response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "北京今天天气怎么样?"}],
    tools=tools
)

Function Calling 是构建 AI Agent 的基础能力,让模型能够主动获取信息而不是仅仅依赖训练数据。