Skip to content
EN

OpenAI Chat(Gemini 预设tools调用)

方法: POST路径: /v1/chat/completions

支持Gemini 预设 tools: googleSearch codeExecution urlContext

请求参数

Header 参数

text
Content-Type
string
必需
示例:
application/json
Accept
string
必需
示例:
application/json
Authorization
string
可选
默认值:
Bearer {{YOUR_API_KEY}}

Body 参数 application/json

text
model
string
必需
messages
array
[object]
必需
role
string
可选
content
string
可选
tools
array
[object]
必需
type
string
可选
function
object
可选
示例
{
"model"
:
"gemini-3.1-flash-lite-preview"
,
"messages"
:
[
{
"role"
:
"user"
,
"content"
:
"今天北京天气怎么样?"
}
]
,
"tools"
:
[
{
"type"
:
"function"
,
"function"
:
{
"name"
:
"googleSearch"
}
}
]
}

请求示例代码

Shell

bash
curl --location --request POST '/v1/chat/completions' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer {{YOUR_API_KEY}}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "model": "gemini-3.1-flash-lite-preview",
    "messages": [
        {
            "role": "user",
            "content": "今天北京天气怎么样?"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "googleSearch"
            }
        }
    ]
}'

返回响应

🟢 200 OK

内容类型: application/json

响应结构

text
id
string
必需
object
string
必需
created
integer
必需
choices
array
[object]
必需
index
integer
可选
message
object
可选
finish_reason
string
可选
usage
object
必需
prompt_tokens
integer
必需
completion_tokens
integer
必需
total_tokens
integer
必需

示例

json
{
    "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
    }
}