官方 Function Calling 调用
POST/v1/chat/completions Function Calling 调用
POST https://www.vortapapi.com/v1/chat/completions
使用 Chat Completions 接口向模型发送对话消息,并可通过 tools 参数声明可调用的函数。模型会根据上下文判断是否需要调用函数,并在响应中返回对应的工具调用信息。
该接口兼容 OpenAI API 格式。
请求头
| 名称 | 必填 | 说明 |
|---|---|---|
Content-Type | 是 | 请求体格式,通常为 application/json |
Accept | 是 | 响应格式,通常为 application/json |
Authorization | 否 | API Key 鉴权信息,格式为 Bearer sk-*** |
请求示例
curl https://www.vortapapi.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer sk-***" \
-d '{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": "北京今天的天气怎么样?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
}
}
]
}'
响应示例
普通文本回复示例:
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
当模型决定调用函数时,响应中的 message 可能包含 tool_calls:
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"北京\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 60,
"completion_tokens": 18,
"total_tokens": 78
}
}
字段说明
| 字段 | 说明 |
|---|---|
id | 本次补全结果的唯一标识 |
object | 对象类型,通常为 chat.completion |
created | 响应创建时间,Unix 时间戳 |
choices | 模型生成的候选结果列表 |
choices[].index | 当前候选结果的序号 |
choices[].message | 模型返回的消息内容 |
choices[].message.role | 消息角色,通常为 assistant |
choices[].message.content | 模型生成的文本内容;如果触发工具调用,可能为 null |
choices[].message.tool_calls | 模型请求调用的工具列表 |
choices[].finish_reason | 生成结束原因,例如 stop 或 tool_calls |
usage.prompt_tokens | 输入消耗的 token 数 |
usage.completion_tokens | 输出消耗的 token 数 |
usage.total_tokens | 本次请求总 token 数 |