Responses API 完整使用文档
概述
Response API 是一个轻量、灵活的对话接口,支持多模态输入、多种内置工具调用、推理模式等高级特性。
完整的请求协议见:https://platform.openai.com/docs/api-reference/responses/create
完整的响应协议见:https://platform.openai.com/docs/api-reference/responses/object
全部流式事件见:https://platform.openai.com/docs/api-reference/responses-streaming
文件输入以及file_search工具需要的file-id,以
purpose为assistants的方式,上传到file-api,见 文件上传
为什么选择Responses API?
核心端点
创建响应
端点: POST /v1/responses
认证: Bearer Token
获取响应详情
端点: GET /v1/responses/{response_id}
认证: Bearer Token
核心功能详解
1. 基础对话
简单文本对话
{
"model": "gpt-5-nano",
"input": "How much gold would it take to coat the Statue of Liberty in a 1mm layer?",
"stream": true
}
带推理模式的对话
{
"model": "gpt-5-nano",
"input": [{
"role": "user",
"content": "复杂问题..."
}],
"store": true,
"reasoning": {
"effort": "medium",
"summary": "auto"
},
"stream": true
}
2. 会话管理
续接上次对话
{
"model": "gpt-5-nano",
"previous_response_id": "resp_1000012001",
"input": "继续上面的话题...",
"store": true,
"stream": true
}
基于历史会话
{
"model": "gpt-5-nano",
"conversation": "conv_xxx",
"input": "基于之前的讨论...",
"store": true
}
3. 多模态输入
Responses API支持多种输入模态的组合使用:
- 文本(input_text)
- 图片(input_image)
- 音频(input_audio)
- 文件(input_file)
文件输入
{
"model": "gpt-5-nano",
"input": [{
"role": "user",
"content": [
{
"type": "input_file",
"file_id": "file-2509221659180022489862-1989906366"
},
{
"type": "input_text",
"text": "他的学历是什么?"
}
]
}],
"store": false,
"stream": true
}
图片输入(Base64)
{
"model": "gpt-5-nano",
"input": [{
"role": "user",
"content": [
{
"type": "input_image",
"image_url": "data:image;base64,/9j/4AAQSkZJRgAB..."
},
{
"type": "input_text",
"text": "描述这张图片"
}
]
}],
"stream": true
}
音频输入
{
"model": "gpt-5-nano",
"input": [{
"role": "user",
"content": [
{
"type": "input_audio",
"input_audio": {
"format": "wav",
"data": "UklGRv////9XQVZFZm10..."
}
},
{
"type": "input_text",
"text": "这段语音是什么内容?"
}
]
}],
"reasoning": {
"effort": "medium",
"summary": "auto"
},
"stream": true
}
4. 工具调用
函数工具(Function Tools)
基础函数定义
{
"model": "gpt-5",
"input": [{
"type": "message",
"role": "user",
"content": "北京和上海现在的天气怎么样?"
}],
"tools": [{
"type": "function",
"name": "get_weather",
"description": "Retrieves current weather for the given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "object",
"properties": {
"x": {"type": "string"},
"y": {"type": "string"}
},
"required": ["x", "y"]
},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location", "units"]
},
"strict": true
}]
}
包含函数调用历史的对话
{
"model": "gpt-5-nano",
"input": [
{
"type": "message",
"role": "user",
"content": "北京现在的天气怎么样?"
},
{
"type": "function_call",
"status": "completed",
"call_id": "call_weather456",
"name": "get_weather",
"arguments": "{\"city\": \"北京\", \"unit\": \"celsius\"}"
},
{
"status": "completed",
"type": "function_call_output",
"call_id": "call_weather456",
"output": "{\"temperature\": 15, \"condition\": \"晴朗\"}"
},
{
"type": "message",
"role": "user",
"content": "比较一下上海的天气"
}
],
"tools": [{
"type": "function",
"name": "get_weather",
"description": "Retrieves current weather for the given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "object",
"properties": {
"x": {"type": "string"},
"y": {"type": "string"}
},
"required": ["x", "y"]
},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location", "units"]
},
"strict": true
}]
}
网络搜索工具(Web Search)
{
"model": "gpt-5-nano",
"input": [{
"type": "message",
"role": "user",
"content": "使用网络搜索分别查询今年的房价和贝壳的股价"
}],
"tools": [{
"type": "web_search_preview"
}],
"reasoning": {
"effort": "medium"
},
"store": true,
"stream": true
}
图像生成工具
{
"model": "gpt-5-nano",
"input": [{
"type": "message",
"role": "user",
"content": "生成一张夏日午后森林的图片和一张冬天雪后森林的图片"
}],
"tools": [{
"type": "image_generation",
"model": "doubao-seedream-3-0-t2i"
}],
"stream": true
}
文件搜索工具
{
"model": "gpt-5-nano",
"input": [{
"type": "message",
"role": "user",
"content": "他的技能有哪些?"
}],
"tools": [{
"type": "file_search",
"vector_store_ids": ["file-2509221659180022489862-1989906366"]
}],
"stream": true
}
本地Shell工具
{
"model": "gpt-5-nano",
"input": "使用local_shell工具,列出当前目录下所有以'test_'开头的txt文件",
"tools": [{
"type": "local_shell"
}],
"store": false,
"stream": true
}
MCP工具(Model Context Protocol)
MCP (Model Context Protocol) 是一个开放协议,允许AI模型通过标准化接口与外部工具和数据源交互。Response API支持动态连接MCP服务器,自动发现和调用服务器提供的工具。
基础MCP配置
{
"model": "gpt-5-nano",
"input": [{
"type": "message",
"role": "user",
"content": "使用Context7查询React文档"
}],
"tools": [{
"type": "mcp",
"server_label": "context7",
"server_url": "https://mcp.context7.com/mcp",
"server_description": "Context7 documentation search service"
}],
"stream": true
}
核心字段说明:
type: 固定为"mcp"server_label: MCP服务器的唯一标识符(必需)server_url: MCP服务器的HTTP端点URL(与connector_id二选一)connector_id: 预定义的连接器ID(与server_url二选一)server_description: 服务器描述,帮助模型理解服务器用途authorization: OAuth令牌(可选,格式:"Bearer token123")headers: 自定义HTTP请求头(可选)
带认证的MCP服务器
{
"model": "gpt-5-nano",
"input": "查询私有仓库的代码文档",
"tools": [{
"type": "mcp",
"server_label": "github_private",
"server_url": "https://api.github.com/mcp",
"authorization": "Bearer ghp_xxxxxxxxxxxx",
"headers": {
"X-GitHub-Api-Version": "2022-11-28"
},
"server_description": "GitHub private repository access"
}]
}