Skip to Content
文本生成Responses API

Responses API

OpenAI Responses
接口POST /v1/responses

文本输入使用 input,输出长度使用 max_output_tokens。

请求参数

参数类型必填说明
modelstring是模型名称,使用 GET /v1/models 返回的值。
inputstring | array是文本输入,可以是字符串或结构化输入项数组。
instructionsstring否为模型提供的系统级指令。
max_output_tokensinteger否限制输出 token 数量。
streamboolean否是否以事件流返回增量结果,默认 false。
stream_optionsobject否事件流选项。
temperaturenumber否采样温度;数值越高,输出随机性越强。
top_pnumber否核采样参数,通常与 temperature 二选一调整。
reasoningobject否推理配置,例如推理强度。
textobject否文本输出格式配置。
toolsarray否可供模型调用的工具定义。
tool_choicestring | object否控制是否自动调用工具或指定工具。
truncationstring否上下文超出限制时的截断策略。
previous_response_idstring否关联上一轮响应,实现多轮上下文衔接。
metadataobject否附加到响应的业务元数据。
storeboolean否是否保存响应。
userstring否用于标识终端用户的业务标识。

响应参数

字段类型说明
idstring本次响应的唯一 ID。
objectstring响应对象类型,通常为 response。
created_atinteger响应创建时间,Unix 时间戳。
statusstring响应状态,例如 in_progress、completed 或 failed。
modelstring实际处理请求的模型名称。
outputarray输出项列表,文本消息的 type 为 message。
output[].idstring输出项 ID。
output[].rolestring输出消息角色,通常为 assistant。
output[].statusstring输出项状态。
output[].contentarray消息内容列表。
output[].content[].typestring文本内容类型,通常为 output_text。
output[].content[].textstring生成的文本内容。
usageobject | nulltoken 用量信息。
usage.input_tokensinteger输入 token 数量。
usage.output_tokensinteger输出 token 数量。
usage.total_tokensinteger输入与输出 token 总数。
errorobject | null失败时的错误信息。
incomplete_detailsobject | null响应未完成时的原因说明。

调用示例

非流式请求:

curl "https://api.example.com/v1/responses" \ -H "Authorization: Bearer <YOUR_API_KEY>" \ -H "Content-Type: application/json" \ -d '{ "model": "deepseek-v4-pro", "instructions": "你是一个简洁、准确的助手。", "input": "请用一句话介绍你自己", "temperature": 0.7, "max_output_tokens": 512 }'

流式请求:

curl --no-buffer "https://api.example.com/v1/responses" \ -H "Authorization: Bearer <YOUR_API_KEY>" \ -H "Content-Type: application/json" \ -d '{ "model": "deepseek-v4-pro", "input": "列出三个提高代码可读性的建议", "stream": true, "max_output_tokens": 512 }'

响应示例

非流式响应:

{ "id": "resp_example", "object": "response", "created_at": 1710000000, "status": "completed", "model": "deepseek-v4-pro", "output": [ { "type": "message", "id": "msg_example", "status": "completed", "role": "assistant", "content": [ { "type": "output_text", "text": "我是一个文本生成助手。", "annotations": [] } ] } ], "usage": { "input_tokens": 24, "output_tokens": 12, "total_tokens": 36 } }

流式响应以事件逐段返回,文本增量位于 response.output_text.delta 事件的 delta 字段:

event: response.output_text.delta data: {"type":"response.output_text.delta","delta":"我是","output_index":0,"content_index":0} event: response.output_text.delta data: {"type":"response.output_text.delta","delta":"一个文本生成助手。","output_index":0,"content_index":0}
Last updated on