OpenAI API 提供 GPT 系列模型的程序化访问,支持文本生成、对话、函数调用、图像生成、语音合成与识别、嵌入向量等功能。所有接口兼容 OpenAI 格式,可通过 REST API 或官方 SDK 调用。
所有请求需在 HTTP Header 中携带 API Key:
Authorization: Bearer sk-xxxxxxxxxxxxxxxx
API Key 在 platform.openai.com/api-keys 创建,建议通过环境变量管理:
export OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxx
https://api.openai.com/v1
| 模型ID | 类型 | 上下文窗口 | 训练数据 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| gpt-5.2 | 旗舰对话 | 256K | 2026年4月 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gpt-4o | 通用对话 | 128K | 2025年10月 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gpt-4o-mini | 轻量对话 | 128K | 2025年10月 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| o3 | 推理专用 | 200K | 2026年4月 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| o4-mini | 轻量推理 | 128K | 2026年3月 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dall-e-3 | 图像生成 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| whisper-1 | 语音识别 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tts-1 | 语音合成 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text-embedding-3-large | 文本嵌入 | 8191 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
POST /v1/chat/completions
{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"temperature": 0.7,
"top_p": 1,
"max_tokens": 4096,
"stream": false,
"stop": null,
"presence_penalty": 0,
"frequency_penalty": 0
}
| 参数 | 类型 | 必填 | 说明 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| model | string | ✅ | 模型ID | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| messages | array | ✅ | 消息数组,每条含 role + content | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| temperature | float | ❌ | 0-2,默认1。越低越确定性 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| top_p | float | ❌ | 核采样,0-1,默认1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_tokens | int | ❌ | 最大生成token数 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stream | bool | ❌ | 是否流式输出,默认false | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stop | string/array | ❌ | 停止序列 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| presence_penalty | float | ❌ | -2到2,正值鼓励新话题 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| frequency_penalty | float | ❌ | -2到2,正值降低重复 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tools | array | ❌ | 函数定义列表 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tool_choice | string/object | ❌ | "auto"/"none"/"required"/指定函数 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response_format | object | ❌ | {"type":"json_object"} 强制JSON输出 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| seed | int | ❌ | 随机种子,可复现结果 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1717100000,
"model": "gpt-4o",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 8,
"total_tokens": 28
}
}
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的当前天气",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
}]
// 模型返回 tool_calls
if (response.choices[0].finish_reason === "tool_calls") {
const toolCall = response.choices[0].message.tool_calls[0];
// 执行实际函数
const result = await getWeather(toolCall.function.arguments);
// 将结果返回给模型
const followUp = await client.chat.completions.create({
model: "gpt-4o",
messages: [
...previousMessages,
response.choices[0].message,
{
role: "tool",
tool_call_id: toolCall.id,
content: JSON.stringify(result)
}
]
});
}
POST /v1/images/generations
{
"model": "dall-e-3",
"prompt": "A white siamese cat in a cyberpunk city",
"n": 1,
"size": "1024x1024",
"quality": "hd",
"style": "vivid"
}
可选尺寸:1024x1024, 1792x1024, 1024x1792
POST /v1/audio/transcriptions
Content-Type: multipart/form-data
file: audio.mp3
model: whisper-1
language: zh
POST /v1/audio/speech
{
"model": "tts-1",
"input": "你好,世界!",
"voice": "alloy"
}
可用声音:alloy, echo, fable, onyx, nova, shimmer
POST /v1/embeddings
{
"model": "text-embedding-3-large",
"input": "The food was delicious"
}
| 层级 | RPM | TPM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Free | 3 | 40,000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Tier 1 ($5+) | 500 | 200,000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Tier 2 ($50+) | 5,000 | 2,000,000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
遇到 429 错误时使用指数退避重试。
| 状态码 | 含义 | 处理建议 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 400 | 请求格式错误 | 检查参数 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | API Key 无效 | 检查密钥 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 速率限制 | 退避重试 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 服务器错误 | 稍后重试 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 服务过载 | 稍后重试 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
评论区