聊天接口(函数调用)
简洁描述:支持模型生成并调用外部函数(工具)的聊天接口,模型可在对话中返回函数调用建议,平台或调用方可据此执行实际函数。
概览
- 请求方法:
POST - 路径:
/v1/chat/completions - 内容类型:
application/json
认证方式
支持常见的 Header 认证格式:
- OpenAI:
Authorization: Bearer sk-xxx - Anthropic:
x-api-key: sk-xxx(并可能需要anthropic-version) - Google:
x-goog-api-key: sk-xxx
请求示例
Body 参数说明
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| model | string | 是 | 模型名称,例如 gpt-4o |
| messages | array<object> | 是 | 聊天消息列表,常见项 `{ role: 'user' |
| tools | array<object> | 是 | 可供模型调用的工具(函数)列表;每项类型为 function,包含函数名、描述与参数 JSON Schema |
工具项(示例结构):
json
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": { "type": "string", "description": "City and country e.g. Bogotá, Colombia" }
},
"required": ["location"],
"additionalProperties": false
},
"strict": true
}
}模型接收到 tools 列表后,可能在回答中返回 tool_calls,表示建议调用某函数并附带 JSON 字符串形式的 arguments。
curl 示例
bash
curl -X POST "https://api.gpt.ge/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxx" \
-d '{
"model": "gpt-4o",
"messages": [{"role":"user","content":"What is the weather like in Paris today?"}],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current temperature for a given location.",
"parameters": { "type": "object", "properties": { "location": { "type": "string" } }, "required": ["location"], "additionalProperties": false },
"strict": true
}
}
]
}'JavaScript (fetch) 示例
javascript
fetch('https://api.gpt.ge/v1/chat/completions', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer sk-xxxx' },
body: JSON.stringify({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'What is the weather like in Paris today?' }],
tools: [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get current temperature for a given location.',
parameters: { type: 'object', properties: { location: { type: 'string' } }, required: ['location'], additionalProperties: false },
strict: true
}
}
]
})
}).then(r => r.json()).then(console.log)Python 示例(requests)
python
import requests
payload = {
'model': 'gpt-4o',
'messages': [{'role': 'user', 'content': 'What is the weather like in Paris today?'}],
'tools': [
{
'type': 'function',
'function': {
'name': 'get_weather',
'description': 'Get current temperature for a given location.',
'parameters': { 'type': 'object', 'properties': { 'location': { 'type': 'string' } }, 'required': ['location'], 'additionalProperties': False },
'strict': True
}
}
]
}
resp = requests.post('https://api.gpt.ge/v1/chat/completions', headers={'Content-Type':'application/json','Authorization':'Bearer sk-xxxx'}, json=payload)
print(resp.json())返回示例(200)
json
{
"id": "chatcmpl-Ax2bU1RFE8P0Y9uqKcErQNLcx4dDe",
"object": "chat.completion",
"created": 1738634724,
"model": "gpt-4o-2024-08-06",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_FezxjoWuDV1CL3dITVFOjUzK",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"location\":\"Paris, France\"}"
}
}
],
"refusal": null
},
"logprobs": null,
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 65,
"completion_tokens": 16,
"total_tokens": 81,
"prompt_tokens_details": { "cached_tokens": 0, "audio_tokens": 0 },
"completion_tokens_details": { "reasoning_tokens": 0, "audio_tokens": 0, "accepted_prediction_tokens": 0, "rejected_prediction_tokens": 0 }
},
"system_fingerprint": "fp_f3927aa00d"
}