Responses API
OpenAI 最新推荐的 API 方式,提供更简洁的接口和更好的缓存支持
端点信息
POST/v1/responses
推荐说明
推荐
Responses API 是 OpenAI 最新的 API,推荐使用
支持独立的系统指令、Prompt 缓存、原生函数调用等功能,相比 Chat Completions 有更好的性能和成本优势
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型 ID,例如 gpt-4o、gpt-4o-mini |
| instructions | string | 否 | 系统指令,独立于 input 之外,可享受缓存加速 |
| input | string / array | 是 | 输入内容,可以是字符串或字符串数组 |
| input_format | string | 否 | 输入格式,支持 text、audio 等 |
| temperature | number | 否 | 采样温度,范围 0-2,较高的值会使输出更加随机默认: 1 |
| max_output_tokens | integer | 否 | 最大输出 token 数量 |
| stream | boolean | 否 | 是否启用流式响应,默认 false默认: false |
请求示例
POST /v1/responses
curl https://api.mintcloud.ai/v1/responses \
-H "Authorization: Bearer $MINT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"instructions": "你是一个专业的技术写作助手",
"input": "请介绍一下什么是 RESTful API"
}'响应格式
响应字段
id- 响应唯一标识符object- 对象类型,固定为 "response"created- 创建时间戳output- 输出数组,替代了 Chat Completions 中的 choicesusage- Token 使用量统计
output 结构
output 数组包含一个或多个输出内容块,每个内容块有 type 和 content 数组。
文本内容的访问方式为:output[0].content[0].text
usage 结构
包含 input_tokens、output_tokens 和 total_tokens,统计本次请求的 Token 消耗
与 Chat Completions 对比
| 特性 | Responses API | Chat Completions |
|---|---|---|
| 指令处理 | 独立 instructions,享受缓存 | 内嵌 messages |
| 输出格式 | output[0].content[0].text | choices[0].message.content |
| Prompt Caching | 原生支持 | 不支持 |
流式响应
启用 stream: true 后,服务器将返回 SSE (Server-Sent Events) 格式的流式响应
SSE 事件类型
response.created- 响应创建事件response.output_item.created- 输出项创建事件response.content_part.added- 内容片段添加事件response.output_item.done- 输出项完成事件response.done- 响应完成事件,包含 usage 信息
流式响应示例
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.MINT_API_KEY,
baseURL: "https://api.mintcloud.ai/v1",
});
async function main() {
const stream = await client.responses.create({
model: "gpt-4o",
instructions: "你是一个专业的技术写作助手",
input: "请介绍一下什么是 RESTful API",
stream: true,
});
for await (const event of stream) {
console.log(event);
}
}
main();