OpenAI Function Calling 实战
2026-05-09
·
OpenAI
## OpenAI Function Calling 实战
### 什么是Function Calling
让GPT模型能够调用外部函数,实现与真实世界交互。
### 基本用法
```python
from openai import OpenAI
client = OpenAI()
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取城市天气",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}
}]
response = client.chat.completions.create(
model="gpt-4.5-turbo",
messages=[{"role": "user", "content": "北京天气?"}],
tools=tools
)
```
评论区