创建对话请求(OpenAI)
为给定的对话创建模型响应。
端点
POST https://gateway.mytokengate.com/v1/chat/completions请求示例
curl --request POST \
--url https://gateway.mytokengate.com/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "中国大模型行业2025年将会迎来哪些机遇和挑战?"
}
]
}'认证
所有请求需要在 Authorization 头中包含 Bearer token:
Authorization: Bearer YOUR_API_KEY请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型名称,详见模型列表 |
| messages | array | 是 | 对话消息列表 |
| max_tokens | integer | 否 | 最大生成 token 数,需确保输入+输出不超过模型上下文窗口 |
| stream | boolean | 否 | 是否流式输出,true 时以 Server-Sent Events 返回,以 data: [DONE] 结束 |
| temperature | float | 否 | 控制随机性,范围 0-2,默认值因模型而异 |
| top_p | float | 否 | 核采样参数,默认 0.7 |
| top_k | float | 否 | Top-k 采样参数 |
| frequency_penalty | float | 否 | 重复惩罚,范围 -2.0 到 2.0 |
| stop | string 或 array | 否 | 最多 4 个停止序列,生成遇到这些序列时会停止 |
| n | integer | 否 | 返回的完成数量,默认 1 |
| response_format | object | 否 | 指定输出格式,如 { "type": "json_object" } |
| tools | array | 否 | 工具列表,最多 128 个函数 |
响应
{
"id": "chatcmpl-xxx",
"choices": [
{
"message": {
"role": "assistant",
"content": "响应内容...",
"tool_calls": [
{
"id": "call_xxx",
"type": "function",
"function": {
"name": "function_name",
"arguments": "{\"arg\": \"value\"}"
}
}
]
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 123,
"completion_tokens": 456,
"total_tokens": 579
},
"created": 1234567890,
"model": "gpt-4o",
"object": "chat.completion"
}错误码
| 状态码 | 说明 |
|---|---|
| 400 | 请求参数无效 |
| 401 | API Key 无效或缺失 |
| 404 | 模型不存在 |
| 429 | 触发限流 |
| 503/504 | 服务暂时不可用 |
Last updated on