Skip to content

参考

基于 OpenAI API 2026 + GPT-5 系列编写。完整 API Reference 见 platform.openai.com/docs/api-reference

API Endpoints

Endpoint用途
POST /v1/chat/completions旧式对话(仍主力)
POST /v1/responses新式 Agent / 内置工具
POST /v1/embeddings向量嵌入
POST /v1/images/generations图像生成
POST /v1/audio/speechTTS
POST /v1/audio/transcriptionsWhisper STT
POST /v1/audio/translations音频翻译
POST /v1/files文件上传
POST /v1/batchesBatch processing
WS /v1/realtimeRealtime API(语音)
POST /v1/vector_storesRAG vector store
POST /v1/threadsAssistants API(已 deprecated)
GET /v1/models模型列表

Chat Completions Schema

ts
interface ChatCompletionParams {
  model: string;
  messages: ChatMessage[];
  max_completion_tokens?: number;
  temperature?: number;
  top_p?: number;
  n?: number;
  stream?: boolean;
  stop?: string | string[];
  presence_penalty?: number;
  frequency_penalty?: number;
  logprobs?: boolean;
  top_logprobs?: number;
  logit_bias?: Record<string, number>;
  seed?: number;
  service_tier?: "auto" | "default" | "flex" | "priority";
  user?: string;
  tools?: Tool[];
  tool_choice?: "auto" | "none" | "required" | { type: "function"; function: { name: string } };
  parallel_tool_calls?: boolean;
  response_format?: { type: "text" } | { type: "json_object" } | { type: "json_schema"; json_schema: JsonSchemaSpec };
  reasoning_effort?: "low" | "medium" | "high";   // o-series only
  metadata?: Record<string, string>;
  store?: boolean;
}

interface ChatMessage {
  role: "system" | "user" | "assistant" | "tool" | "function" | "developer";
  content: string | ContentBlock[];
  name?: string;
  tool_calls?: ToolCall[];
  tool_call_id?: string;
}

type ContentBlock =
  | { type: "text"; text: string }
  | { type: "image_url"; image_url: { url: string; detail?: "auto" | "low" | "high" } }
  | { type: "input_audio"; input_audio: { data: string; format: "wav" | "mp3" } };

Responses Schema

ts
interface ResponsesCreateParams {
  model: string;
  input?: string | InputItem[];
  instructions?: string;          // 替代 system
  previous_response_id?: string;  // 接续上次(替代 messages 历史)
  tools?: Tool[];
  tool_choice?: ToolChoice;
  text?: { format: TextFormat };  // 输出格式
  reasoning?: { effort: "low" | "medium" | "high" };  // o-series
  max_output_tokens?: number;
  stream?: boolean;
  store?: boolean;
  metadata?: Record<string, string>;
  truncation?: "auto" | "disabled";
  parallel_tool_calls?: boolean;
}

type Tool =
  | { type: "web_search"; search_context_size?: "low" | "medium" | "high" }
  | { type: "code_interpreter"; container: { type: "auto" } }
  | { type: "image_generation"; size?: string; quality?: string }
  | { type: "file_search"; vector_store_ids: string[] }
  | { type: "function"; name: string; parameters: object; strict?: boolean };

模型 ID 速查

GPT-5 系列(旗舰,2025-2026)

Model上下文输出上限价格输入价格输出
gpt-5256K32K$5/M$25/M
gpt-5-mini256K32K$0.50/M$2.50/M
gpt-5-nano128K16K$0.10/M$0.40/M

o-series(推理增强)

Model上下文价格输入价格输出
o3200K$15/M$60/M
o4-mini200K$1.50/M$6/M

GPT-4o 系列(多模态)

Model上下文价格输入价格输出
gpt-4o128K$2.50/M$10/M
gpt-4o-mini128K$0.15/M$0.60/M
gpt-4o-realtime-preview128K$5/M(文)+ $100/M(音输入)+ $200/M(音输出)-
gpt-4o-audio-preview128K同上-

旧版(仍可用,建议迁)

Model上下文状态
gpt-4-turbo128K可用
gpt-48K可用
gpt-3.5-turbo16K可用,建议迁 mini
o1200K可用,建议迁 o3
o1-mini128K可用,建议迁 o4-mini

图像 / 音频模型

Model用途
gpt-image-1图像生成(DALL-E 继任者)
dall-e-3图像生成(仍可用)
dall-e-2图像生成 + 编辑
whisper-1Speech-to-Text
tts-1 / tts-1-hdText-to-Speech

Embedding 模型

Model维度价格
text-embedding-3-large3072$0.13/M
text-embedding-3-small1536$0.02/M
text-embedding-ada-0021536$0.10/M(已 legacy)

内置工具完整字段

json
{
  "type": "web_search",
  "user_location": {"country": "US"},
  "search_context_size": "medium"
}
字段说明
user_location.countryISO 3166 国家代码
user_location.region地区
user_location.city城市
search_context_sizelow(1 page) / medium(默认 5)/ high(10+)

code_interpreter

json
{
  "type": "code_interpreter",
  "container": {
    "type": "auto"   // 或具体 container_id
  }
}

容器内置 Python 3.12 + pandas / numpy / matplotlib / scipy 等常用库。每次会话独立沙箱,结束销毁。

image_generation

json
{
  "type": "image_generation",
  "model": "gpt-image-1",   // 默认
  "size": "1024x1024",       // 256x256 / 512x512 / 1024x1024 / 1024x1536 / 1536x1024
  "quality": "high",         // low / medium / high
  "n": 1,                     // 1-4
  "background": "transparent" // opaque / transparent
}
json
{
  "type": "file_search",
  "vector_store_ids": ["vs_xxx", "vs_yyy"],
  "max_num_results": 20,
  "filters": {
    "type": "and",
    "filters": [
      {"type": "eq", "key": "category", "value": "docs"}
    ]
  }
}

Realtime API Schema

ts
// WebSocket URL
wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview

// 客户端事件
type ClientEvent =
  | { type: "session.update"; session: SessionConfig }
  | { type: "input_audio_buffer.append"; audio: string /* base64 */ }
  | { type: "input_audio_buffer.commit" }
  | { type: "conversation.item.create"; item: Item }
  | { type: "response.create"; response: ResponseConfig }
  | { type: "response.cancel" };

// 服务端事件
type ServerEvent =
  | { type: "session.created"; session: Session }
  | { type: "input_audio_buffer.speech_started" }
  | { type: "response.audio.delta"; delta: string /* base64 */ }
  | { type: "response.audio_transcript.delta"; delta: string }
  | { type: "response.function_call_arguments.done"; name: string; arguments: string; call_id: string }
  | { type: "response.done" }
  | { type: "error"; error: Error };

Structured Outputs JSON Schema 支持

支持不支持
string / number / integer / booleannot
enumanyOf 部分场景
array + itemsrecursive >5 层
object + properties + requiredadditionalProperties: true(必须 false)
additionalProperties: false(必须)properties > 100
nested objectpattern(部分)
Pydantic models (SDK)string format 部分

Rate Limits(Tier 系统)

Tier月消费RPMTPM
Free$03200K
1$5+50030K
2$50+5000450K
3$100+50001M
4$250+10K2M
5$1000+30K8M

按 model + endpoint 独立限速。

价格速查(2026)

ModelInput $/MOutput $/MCached $/M
GPT-5$5$25$2.50
GPT-5-mini$0.50$2.50$0.25
GPT-5-nano$0.10$0.40$0.05
o3$15$60$7.50
o4-mini$1.50$6$0.75
GPT-4o$2.50$10$1.25
GPT-4o-mini$0.15$0.60$0.075

Batches: 50% off。Flex: 50% off(但延迟可能慢)。

SDK 列表

语言维护
Pythonopenai官方
TypeScript / JSopenai官方
.NETOpenAI (NuGet)官方
Javacom.openai.api-client官方
Gogithub.com/openai/openai-go官方
Rubyruby-openai社区
SwiftMacPaw/OpenAI社区

Azure OpenAI 差异

维度OpenAI 直连Azure OpenAI
模型 IDgpt-5部署名(自定义)
鉴权API keyAzure AD / Key Vault
Endpointapi.openai.com<resource>.openai.azure.com
Region全球按 Azure region(含香港 / 新加坡)
SLA无承诺Enterprise SLA
内容审核默认开可关
模型更新即时滞后 1-2 月
python
# Azure SDK
from openai import AzureOpenAI

client = AzureOpenAI(
    azure_endpoint="https://my.openai.azure.com/",
    api_key=os.getenv("AZURE_OPENAI_KEY"),
    api_version="2025-12-01-preview",
)

错误码

HTTP类型含义
400invalid_request_error参数错
401authentication_errorAPI key 错
403permission_errormodel 无权限
404not_found_error资源不存在
429rate_limit_exceeded超 RPM/TPM
500server_errorOpenAI 内部错
503service_unavailable过载

response header:

x-ratelimit-limit-requests: 5000
x-ratelimit-remaining-requests: 4999
x-ratelimit-reset-requests: 0s

与 Claude / Gemini 接口能力对比

详见 Claude reference 已列。简要:

能力GPTClaudeGemini
内置 web_search-(需 MCP)✓(grounding)
内置 code_interpreter-(需 MCP)
内置 image_generation✓(Imagen)
内置 file_search RAG-(需自建)✓(Semantic Retrieval)
Realtime 双向语音✓(Live API)
音频输入✓ Whisper
TTS
Video 帧-(仅图)✓ 原生
Structured Outputs✓ 一类 strict-(Tool Use 模拟)✓ 一类
Prompt Cache自动 5-15min / 50%手动 5min / 90%implicit + explicit
Tool Use 严格 schema✓(strict: true
MCP-(社区)✓ 一类-(社区)
上下文128-256K200K / 1M1M / 2M
推理模型o-seriesExtended ThinkingthinkingConfig

资源链接