Skip to Content
接口调用OpenAI GPT API

OpenAI GPT API

鉴权方式

  • Header:Authorization: Bearer $OPENAI_API_KEY
  • Header:Content-Type: application/json

Chat completions

  • 方法:POST
  • 地址:https://api.openai.com/v1/chat/completions
  • 用途:兼容式 Chat Completions 接口

Chat completions 请求参数

参数类型必填说明
modelstring是模型 ID。
messagesarray是消息数组。
messages[].rolestring是角色,例如 developer、system、user、assistant、tool、function。
messages[].contentstring | array是消息内容。
storeboolean否是否存储 chat completion。
metadataobject否元数据。
modalitiesarray否输出模态,例如 ["text"]、["text", "audio"]。
audioobject否音频输出配置。
audio.formatstring否音频格式,例如 wav、mp3、flac、opus、pcm16。
audio.voicestring | object否音频语音。
predictionobject否Predicted output 配置。
temperaturenumber否温度。
top_pnumber否top-p。
ninteger否返回候选数量。
streamboolean否是否流式返回。
stream_optionsobject否流式输出选项。
stopstring | array否停止序列。
max_tokensinteger否最大输出 token 数。
max_completion_tokensinteger否最大 completion token 数。
presence_penaltynumber否presence penalty。
frequency_penaltynumber否frequency penalty。
reasoning_effortstring否推理强度。
logit_biasobject否logit bias。
logprobsboolean否是否返回 logprobs。
top_logprobsinteger否top logprobs 数量。
response_formatobject否响应格式。
response_format.typestring否响应格式类型,例如 text、json_object、json_schema。
seedinteger否随机种子。
web_search_optionsobject否Web search 选项。
service_tierstring否服务层。
toolsarray否工具数组。
tool_choicestring | object否工具选择策略。
functionsarray否旧式函数定义数组。
function_callstring | object否旧式函数调用策略。
parallel_tool_callsboolean否是否允许并行工具调用。
safety_identifierstring否安全标识符。
userstring否用户标识。

Chat completions 请求示例

curl https://api.openai.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "VAR_chat_model_id", "messages": [ { "role": "developer", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello!" } ] }'

Chat completions 请求返回结果

参数类型说明
idstringcompletion ID。
objectstring对象类型,通常为 chat.completion。
createdinteger创建时间戳。
request_idstring请求 ID。
modelstring实际使用模型。
tool_choicestring | object | null实际工具选择配置。
toolsarray | null实际工具列表。
metadataobject元数据。
choicesarray候选数组。
choices[].indexinteger候选索引。
choices[].messageobjectassistant 消息对象。
choices[].message.rolestring角色,通常为 assistant。
choices[].message.contentstring | null返回文本。
choices[].message.tool_callsarray工具调用数组。
choices[].message.function_callobject | null旧式函数调用对象。
choices[].finish_reasonstring停止原因。
choices[].logprobsobject | nulllogprobs 对象。
usage.prompt_tokensintegerprompt token 数。
usage.completion_tokensintegercompletion token 数。
usage.total_tokensinteger总 token 数。
seedinteger | null随机种子。
temperaturenumber | null实际温度。
top_pnumber | null实际 top-p。
presence_penaltynumber | null实际 presence penalty。
frequency_penaltynumber | null实际 frequency penalty。
input_userstring | null输入用户标识。
service_tierstring | null服务层。
system_fingerprintstring | null系统指纹。
response_formatobject | null实际响应格式。

Chat completions 请求返回示例

{ "id": "chatcmpl-B9MBs8CjcvOU2jLn4n570S5qMJKcT", "object": "chat.completion", "created": 1741569952, "model": "gpt-5.4", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Hello! How can I assist you today?", "refusal": null, "annotations": [] }, "logprobs": null, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 19, "completion_tokens": 10, "total_tokens": 29, "prompt_tokens_details": { "cached_tokens": 0, "audio_tokens": 0 }, "completion_tokens_details": { "reasoning_tokens": 0, "audio_tokens": 0, "accepted_prediction_tokens": 0, "rejected_prediction_tokens": 0 } } , "service_tier": "default" }

Responses

  • 方法:POST
  • 地址:https://api.openai.com/v1/responses
  • 用途:统一文本生成、多模态输入、结构化输出、工具调用、流式输出

Responses 请求参数

参数类型必填说明
modelstring是模型 ID,例如 gpt-5。
inputstring | array否输入内容。
instructionsstring否高层指令。
conversationstring | object否会话上下文。
includearray否额外返回字段。
max_output_tokensinteger否最大输出 token 数。
metadataobject否自定义元数据。
parallel_tool_callsboolean否是否允许并行工具调用。
previous_response_idstring否上一个 response 的 ID。
promptobject否Prompt 模板引用。
reasoningobject否推理配置。
service_tierstring否服务层级。
storeboolean否是否存储。
streamboolean否是否流式输出。
temperaturenumber否温度。
textobject否文本输出配置。
tool_choicestring | object否工具选择策略。
toolsarray否工具数组。
top_pnumber否top-p。
truncationstring否截断策略。
userstring否用户标识。
backgroundboolean否是否后台执行。

Responses 请求示例

curl https://api.openai.com/v1/responses \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-5", "input": [ { "role": "user", "content": [ { "type": "input_text", "text": "Write a concise product launch announcement for GGPU Platform." } ] } ], "instructions": "Use a professional but energetic tone.", "max_output_tokens": 300, "temperature": 0.7, "text": { "format": { "type": "text" } } }'

Responses 请求返回结果

参数类型说明
idstring响应对象 ID。
objectstring对象类型,通常为 response。
created_atinteger创建时间戳。
statusstring当前状态。
errorobject | null错误对象。
incomplete_detailsobject | null未完成详情。
instructionsstring | null实际使用指令。
max_output_tokensinteger | null实际最大输出 token。
modelstring实际使用模型。
outputarray输出项数组。
output_textstring扁平文本输出。
parallel_tool_callsboolean是否允许并行工具调用。
previous_response_idstring | null上一个 response ID。
reasoningobject推理元信息。
service_tierstring | null服务层。
storeboolean是否存储。
temperaturenumber | null实际温度。
textobject文本输出配置。
tool_choicestring | object工具选择。
toolsarray工具列表。
top_pnumber | null实际 top-p。
truncationstring | null截断策略。
usage.input_tokensinteger输入 token 数。
usage.output_tokensinteger输出 token 数。
usage.total_tokensinteger总 token 数。
usage.output_tokens_details.reasoning_tokensinteger推理 token 数。
userstring | null用户标识。
metadataobject元数据。

Responses 请求返回示例

{ "id": "resp_67cb71b351908190a308f3859487620d06981a8637e6bc44", "object": "response", "created_at": 1741386163, "status": "completed", "error": null, "incomplete_details": null, "instructions": "Use a professional but energetic tone.", "max_output_tokens": 300, "model": "gpt-5", "output": [ { "id": "msg_67cb71b4223c8190a8f3859487620d06", "type": "message", "role": "assistant", "content": [ { "type": "output_text", "text": "GGPU Platform is now available, bringing teams a faster and more reliable way to build, run, and scale intelligent workflows.", "annotations": [] } ] } ], "output_text": "GGPU Platform is now available, bringing teams a faster and more reliable way to build, run, and scale intelligent workflows.", "parallel_tool_calls": true, "previous_response_id": null, "reasoning": { "effort": null, "summary": null }, "service_tier": "default", "store": true, "temperature": 0.7, "text": { "format": { "type": "text" } }, "tool_choice": "auto", "tools": [], "top_p": null, "truncation": "disabled", "usage": { "input_tokens": 31, "output_tokens": 28, "total_tokens": 59, "output_tokens_details": { "reasoning_tokens": 0 } }, "user": null, "metadata": {} }