title: “Understanding OpenAI Function Calling” tags:
- AI slug: 324b9f7e date: 2024-08-11 17:24:33 summary: “What function calling is, how the schema-driven tool flow works, and a minimal weather example end-to-end.”
Many “agent” products have launched in China — Tencent’s 元宝/元器, ByteDance’s 豆包, Moonshot’s Kimi+, etc. A key technology behind them is function calling. So what is function calling?
这里我实际使用了下混元和OpenAI的,来梳理下function call怎么玩的。

Function Call
先说概念,顾名思义,即函数调用。
Note: from the API’s perspective, the model doesn’t directly run the function. It selects a function and arguments based on your predefined schema and the user’s input.
So function calling is a form of intent recognition.
Interaction flow
定义工具列表
tools: [{ type: "function", function: { name: "get_realtime_weather", summary: "获取当前的天气情况", parameters: { type: "object", properties: { city: { type: "string", summary: "城市名称" } }, required: ["city"], additionalProperties: true, } } }], // 工具选项,当为string类型时,可选参数为"none"/"auto", "none"表示不会使用工具,"auto"表示模型自行选择生成回复或调用工具;当为object类型时,强制模型调用指定的function tool_choice: "auto"
In this example, the function requires only a city parameter — and it’s marked as required.
The user asks “What should I wear tomorrow?”. The model infers it’s weather‑related and, since the city is required, prompts for it.
为了给您提供合适的穿衣建议,我需要知道您所在的城市。请问您能告诉我您所在的城市名称吗?(城市名称为必填项)User replies “北京 (Beijing)”.
The model emits a tool call for
get_realtime_weatherwith arguments.{ id: 'call_cqs8e7k2c3m5dtj9a9tg', type: 'function', function: { name: 'get_realtime_weather', arguments: '{"city":"北京"}' } }Your code executes the function and returns the result back to the model.
{ "role": "tool", "content": "北京 天气晴朗,微风,最高气温27摄氏度,最低气温17摄氏度" }The model composes a final answer for the user based on the tool result.
{"created":1723369252,"id":"b097e75efe3d328fa0f6ecc1f884e908","model":"hunyuan","version":"202403121316","choices":[{"finish_reason":"stop","message":{"role":"assistant","content":"根据查询到的天气情况,北京明天天气晴朗,微风,最高气温27摄氏度,最低气温17摄氏度。建议您可以穿着长袖衬衫或薄型外套,搭配长裤。如果您对温度比较敏感,可以随身携带一件轻薄的外01,"completion_tokens":50,"total_tokens":151}}
That’s a complete function‑calling round‑trip.
这里贴下完整的message信息
[
{ role: 'user', content: '我明天应该穿什么?' },
{
role: 'assistant',
content: '为了给您提供合适的穿衣建议,我需要知道您所在的城市。请问您能告诉我您所在的城市名称吗?(城市名称为必填项)'
},
{ role: 'user', content: '北京' },
{
role: 'assistant',
content: '调用天气查询工具,输入参数为用户所在的城市北京。\n' +
'\t\n' +
'\t现在已经获得了用户所在的城市信息,可以使用天气查询工具来获取明天的天气情况。根据用户提供的城市信息和当前日期,可以计算出明天的日期,并查询那天的天气情况。',
tool_calls: [ [Object] ]
},
{ role: 'tool', content: '北京 天气晴朗,微风,最高气温27摄氏度,最低气温17摄氏度' },
{
role: 'assistant',
content: '根据查询到的天气情况,明天北京天气晴朗,微风,最高气温27摄氏度,最低气温17摄氏度。建议您可以穿着长袖衬衫或薄型外套,搭配长裤。如果您对温度比较敏感,可以随身携带一件轻薄的外套以防温度变化。'
}
]

Agent
AI platforms support visual agent builders that compose multiple capabilities; function calling is the key for intent recognition and tool invocation.
With function calling, you can integrate and call any tool/function you expose.
Final Thoughts
If you haven’t tried function calling yet, give it a spin.

