响应生成
最后更新:2026-11-14
响应生成 API(/responses),以结构化"响应"为中心的模型调用接口:一次调用返回一个可持久化、可续接的 Response 对象,内置工具调用与思考过程,支持多轮续接与流式输出,适用于复杂任务编排与智能体(Agent)场景。
接口鉴权方式请参考:API接入与鉴权
当前展示通用接口文档,各模型支持的参数存在差异。请点击下方模型标签查看专属文档。
请求参数
请求头
| 参数名称 | 描述 |
|---|---|
*Content-Type string |
可选值:application/json application/json |
*Authorization string |
鉴权令牌,格式为 Bearer ${AIGATEWAY_TOKEN} |
Body 参数
| 参数名称 | 描述 |
|---|---|
*model string |
模型名称,例如 "gpt-4.1" |
instructions string |
系统指令(System Prompt),设定助手的角色和行为 |
*input object |
用户输入内容。支持简单文本字符串或对话历史/多模态数组。简单文本示例:"input": "Write a story about a unicorn.";对话历史/多模态示例:"input": [{"role": "developer", "content": "You are a helpful assistant."}, {"role": "user", "content": [{"type": "input_text", "text": "Parse this file"}, {"type": "input_file", "file_url": "https://..."}]}] |
prompt object |
提示词模版参数,包含 id(提示模板ID)和 variables(变量键值对)。模版示例:"prompt": {"id": "xxxx", "variables": {"key1": "value1", "key2": "value2"}}(id 维护于 example 控制台的提示模板ID,variables 为提示模板内的变量动态赋值) |
id string |
提示模板ID |
variables object |
提示模板内的变量,在此动态赋值 |
tools array |
工具配置列表,支持类型:web_search_preview(联网搜索)、file_search(文件搜索RAG)、mcp(MCP工具)、image_generation(图片生成)、function(自定义函数调用) |
type string |
可选值:web_search_preview、file_search、mcp、image_generation、function 工具类型 |
stream boolean |
可选值:true、false 是否开启流式输出 |
max_output_tokens integer |
最大输出token数 |
请求示例
curl -v 'https://www.sagelib.com/responses' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5",
"input": [
{
"role": "developer",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
}'
import json
import requests
base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/responses"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
"model": "gpt-5",
"input": [
{
"role": "developer",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
}
response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/responses";
String jsonRequestBody = """
{
"model": "gpt-5",
"input": [
{
"role": "developer",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/responses"
jsonRequestBody := `
{
"model": "gpt-5",
"input": [
{
"role": "developer",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/responses' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "what is in this image?"
},
{
"type": "input_image",
"image_url": "https://example.com/image.jpg"
}
]
}
]
}'
import json
import requests
base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/responses"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
"model": "gpt-5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "what is in this image?"
},
{
"type": "input_image",
"image_url": "https://example.com/image.jpg"
}
]
}
]
}
response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/responses";
String jsonRequestBody = """
{
"model": "gpt-5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "what is in this image?"
},
{
"type": "input_image",
"image_url": "https://example.com/image.jpg"
}
]
}
]
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/responses"
jsonRequestBody := `
{
"model": "gpt-5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "what is in this image?"
},
{
"type": "input_image",
"image_url": "https://example.com/image.jpg"
}
]
}
]
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/responses' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5",
"prompt": {
"id": "pmpt_abc123",
"variables": {
"customer_name": "Jane Doe",
"product": "40oz juice box"
}
}
}'
import json
import requests
base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/responses"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
"model": "gpt-5",
"prompt": {
"id": "pmpt_abc123",
"variables": {
"customer_name": "Jane Doe",
"product": "40oz juice box"
}
}
}
response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/responses";
String jsonRequestBody = """
{
"model": "gpt-5",
"prompt": {
"id": "pmpt_abc123",
"variables": {
"customer_name": "Jane Doe",
"product": "40oz juice box"
}
}
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/responses"
jsonRequestBody := `
{
"model": "gpt-5",
"prompt": {
"id": "pmpt_abc123",
"variables": {
"customer_name": "Jane Doe",
"product": "40oz juice box"
}
}
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/responses' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5",
"tools": [
{
"type": "web_search_preview"
}
],
"input": "What was a positive news story from today?"
}'
import json
import requests
base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/responses"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
"model": "gpt-5",
"tools": [
{
"type": "web_search_preview"
}
],
"input": "What was a positive news story from today?"
}
response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/responses";
String jsonRequestBody = """
{
"model": "gpt-5",
"tools": [
{
"type": "web_search_preview"
}
],
"input": "What was a positive news story from today?"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/responses"
jsonRequestBody := `
{
"model": "gpt-5",
"tools": [
{
"type": "web_search_preview"
}
],
"input": "What was a positive news story from today?"
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/responses' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5",
"input": "What is the weather like in Boston today?",
"tools": [
{
"type": "function",
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
"x-i18n-description": {
"zh-CN": "The city and state, e.g. San Francisco, CA",
"en-US": "The city and state, e.g. San Francisco, CA"
}
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location",
"unit"
]
},
"x-i18n-description": {
"zh-CN": "Get the current weather in a given location",
"en-US": "Get the current weather in a given location"
}
}
],
"tool_choice": "auto"
}'
import json
import requests
base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/responses"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
"model": "gpt-5",
"input": "What is the weather like in Boston today?",
"tools": [
{
"type": "function",
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
"x-i18n-description": {
"zh-CN": "The city and state, e.g. San Francisco, CA",
"en-US": "The city and state, e.g. San Francisco, CA"
}
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location",
"unit"
]
},
"x-i18n-description": {
"zh-CN": "Get the current weather in a given location",
"en-US": "Get the current weather in a given location"
}
}
],
"tool_choice": "auto"
}
response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/responses";
String jsonRequestBody = """
{
"model": "gpt-5",
"input": "What is the weather like in Boston today?",
"tools": [
{
"type": "function",
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
"x-i18n-description": {
"zh-CN": "The city and state, e.g. San Francisco, CA",
"en-US": "The city and state, e.g. San Francisco, CA"
}
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location",
"unit"
]
},
"x-i18n-description": {
"zh-CN": "Get the current weather in a given location",
"en-US": "Get the current weather in a given location"
}
}
],
"tool_choice": "auto"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/responses"
jsonRequestBody := `
{
"model": "gpt-5",
"input": "What is the weather like in Boston today?",
"tools": [
{
"type": "function",
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
"x-i18n-description": {
"zh-CN": "The city and state, e.g. San Francisco, CA",
"en-US": "The city and state, e.g. San Francisco, CA"
}
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location",
"unit"
]
},
"x-i18n-description": {
"zh-CN": "Get the current weather in a given location",
"en-US": "Get the current weather in a given location"
}
}
],
"tool_choice": "auto"
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
响应参数
Body 参数
| 参数名称 | 描述 |
|---|---|
id string |
响应ID |
status string |
可选值:completed、incomplete 响应状态 |
model string |
使用的模型 |
output array |
输出内容列表 |
type string |
内容类型,如 message 或 image_generation_call |
content array |
消息内容列表 |
type string |
内容类型,如 output_text |
text string |
响应的文本内容 |
result string |
当 type 为 image_generation_call 时,返回图片的Base64编码 |
usage object |
Token消耗统计 |
input_tokens integer |
输入Token数 |
output_tokens integer |
输出Token数 |
incomplete_details object |
被截断的详情 (如 reason: "max_output_tokens") |
流式响应(SSE),content-type 为 text/event-stream。采用事件驱动的JSON结构,type 字段标识事件类型。事件包括 response.created、response.output_text.delta、response.web_search_call.*、response.output_item.done、response.completed 等。
响应示例
{
"id": "resp_057006...",
"status": "completed",
"model": "gpt-4.1-2025-04-14",
"output": [
{
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "I'm ChatGPT..."
}
]
}
],
"usage": {
"input_tokens": 20,
"output_tokens": 47,
"total_tokens": 67
}
}
{
"id": "resp_059666...",
"status": "completed",
"model": "gpt-4.1-2025-04-14",
"output": [
{
"type": "image_generation_call",
"status": "completed",
"result": "iVBORw0KGgoAAAANSUhEUgAABAAAA..."
}
]
}
data: {"type":"response.created", "sequence_number":0, "response":{...}}
data: {"type":"response.output_item.added", "sequence_number":2, "item":{...}}
// Text delta output
data: {"type":"response.output_text.delta", "sequence_number":9, "delta":" Today's weather in London..."}
// Search tool status
data: {"type":"response.web_search_call.searching", "sequence_number":4, ...}
// Response end
data: {"type":"response.completed", "sequence_number":54, "response":{...}}
data: [DONE]
状态码
| HTTP状态码 | 描述 |
|---|---|
| 200 | 请求成功 |
| 401 | 访问被拒绝 (缺少Authorization请求头或token错误) |
| 403 | 访问被拒绝 (请求主体格式错误等) |
| 500 | 服务器内部错误 |
OpenAI 直连模式的响应生成端点,以 OpenAI 原生 /v1/responses 接口格式访问模型。除 AI 网关专属鉴权、URL 路径前缀外,其余请求参数均使用 OpenAI 原生规范。客户端只需将原厂 SDK 的 base URL 替换为网关地址 + openai 前缀,请求体无需修改。
接口鉴权方式请参考:API接入与鉴权
当前展示通用接口文档,各模型支持的参数存在差异。请点击下方模型标签查看专属文档。
请求参数
请求头
| 参数名称 | 描述 |
|---|---|
*Content-Type string |
可选值:application/json application/json |
*Authorization string |
鉴权令牌,格式为 Bearer ${AIGATEWAY_TOKEN} |
Body 参数
| 参数名称 | 描述 |
|---|---|
*model string |
模型名称,例如 "gpt-4.1" |
instructions string |
系统指令(System Prompt),设定助手的角色和行为 |
*input object |
用户输入内容。支持简单文本字符串或对话历史/多模态数组。简单文本示例:"input": "Write a story about a unicorn.";对话历史/多模态示例:"input": [{"role": "developer", "content": "You are a helpful assistant."}, {"role": "user", "content": [{"type": "input_text", "text": "Parse this file"}, {"type": "input_file", "file_url": "https://..."}]}] |
prompt object |
提示词模版参数,包含 id(提示模板ID)和 variables(变量键值对)。模版示例:"prompt": {"id": "xxxx", "variables": {"key1": "value1", "key2": "value2"}}(id 维护于 example 控制台的提示模板ID,variables 为提示模板内的变量动态赋值) |
id string |
提示模板ID |
variables object |
提示模板内的变量,在此动态赋值 |
tools array |
工具配置列表,支持类型:web_search_preview(联网搜索)、file_search(文件搜索RAG)、mcp(MCP工具)、image_generation(图片生成)、function(自定义函数调用) |
type string |
可选值:web_search_preview、file_search、mcp、image_generation、function 工具类型 |
stream boolean |
可选值:true、false 是否开启流式输出 |
max_output_tokens integer |
最大输出token数 |
请求示例
curl -v 'https://www.sagelib.com/openai/responses' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5",
"input": [
{
"role": "developer",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
}'
import json
import requests
base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/openai/responses"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
"model": "gpt-5",
"input": [
{
"role": "developer",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
}
response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/openai/responses";
String jsonRequestBody = """
{
"model": "gpt-5",
"input": [
{
"role": "developer",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/openai/responses"
jsonRequestBody := `
{
"model": "gpt-5",
"input": [
{
"role": "developer",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/openai/responses' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "what is in this image?"
},
{
"type": "input_image",
"image_url": "https://example.com/image.jpg"
}
]
}
]
}'
import json
import requests
base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/openai/responses"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
"model": "gpt-5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "what is in this image?"
},
{
"type": "input_image",
"image_url": "https://example.com/image.jpg"
}
]
}
]
}
response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/openai/responses";
String jsonRequestBody = """
{
"model": "gpt-5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "what is in this image?"
},
{
"type": "input_image",
"image_url": "https://example.com/image.jpg"
}
]
}
]
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/openai/responses"
jsonRequestBody := `
{
"model": "gpt-5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "what is in this image?"
},
{
"type": "input_image",
"image_url": "https://example.com/image.jpg"
}
]
}
]
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/openai/responses' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5",
"prompt": {
"id": "pmpt_abc123",
"variables": {
"customer_name": "Jane Doe",
"product": "40oz juice box"
}
}
}'
import json
import requests
base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/openai/responses"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
"model": "gpt-5",
"prompt": {
"id": "pmpt_abc123",
"variables": {
"customer_name": "Jane Doe",
"product": "40oz juice box"
}
}
}
response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/openai/responses";
String jsonRequestBody = """
{
"model": "gpt-5",
"prompt": {
"id": "pmpt_abc123",
"variables": {
"customer_name": "Jane Doe",
"product": "40oz juice box"
}
}
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/openai/responses"
jsonRequestBody := `
{
"model": "gpt-5",
"prompt": {
"id": "pmpt_abc123",
"variables": {
"customer_name": "Jane Doe",
"product": "40oz juice box"
}
}
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/openai/responses' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5",
"tools": [
{
"type": "web_search_preview"
}
],
"input": "What was a positive news story from today?"
}'
import json
import requests
base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/openai/responses"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
"model": "gpt-5",
"tools": [
{
"type": "web_search_preview"
}
],
"input": "What was a positive news story from today?"
}
response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/openai/responses";
String jsonRequestBody = """
{
"model": "gpt-5",
"tools": [
{
"type": "web_search_preview"
}
],
"input": "What was a positive news story from today?"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/openai/responses"
jsonRequestBody := `
{
"model": "gpt-5",
"tools": [
{
"type": "web_search_preview"
}
],
"input": "What was a positive news story from today?"
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/openai/responses' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5",
"input": "What is the weather like in Boston today?",
"tools": [
{
"type": "function",
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
"x-i18n-description": {
"zh-CN": "The city and state, e.g. San Francisco, CA",
"en-US": "The city and state, e.g. San Francisco, CA"
}
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location",
"unit"
]
},
"x-i18n-description": {
"zh-CN": "Get the current weather in a given location",
"en-US": "Get the current weather in a given location"
}
}
],
"tool_choice": "auto"
}'
import json
import requests
base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/openai/responses"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
"model": "gpt-5",
"input": "What is the weather like in Boston today?",
"tools": [
{
"type": "function",
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
"x-i18n-description": {
"zh-CN": "The city and state, e.g. San Francisco, CA",
"en-US": "The city and state, e.g. San Francisco, CA"
}
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location",
"unit"
]
},
"x-i18n-description": {
"zh-CN": "Get the current weather in a given location",
"en-US": "Get the current weather in a given location"
}
}
],
"tool_choice": "auto"
}
response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/openai/responses";
String jsonRequestBody = """
{
"model": "gpt-5",
"input": "What is the weather like in Boston today?",
"tools": [
{
"type": "function",
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
"x-i18n-description": {
"zh-CN": "The city and state, e.g. San Francisco, CA",
"en-US": "The city and state, e.g. San Francisco, CA"
}
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location",
"unit"
]
},
"x-i18n-description": {
"zh-CN": "Get the current weather in a given location",
"en-US": "Get the current weather in a given location"
}
}
],
"tool_choice": "auto"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code: " + response.statusCode());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/openai/responses"
jsonRequestBody := `
{
"model": "gpt-5",
"input": "What is the weather like in Boston today?",
"tools": [
{
"type": "function",
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
"x-i18n-description": {
"zh-CN": "The city and state, e.g. San Francisco, CA",
"en-US": "The city and state, e.g. San Francisco, CA"
}
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location",
"unit"
]
},
"x-i18n-description": {
"zh-CN": "Get the current weather in a given location",
"en-US": "Get the current weather in a given location"
}
}
],
"tool_choice": "auto"
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
响应参数
Body 参数
| 参数名称 | 描述 |
|---|---|
id string |
响应ID |
status string |
可选值:completed、incomplete 响应状态 |
model string |
使用的模型 |
output array |
输出内容列表 |
type string |
内容类型,如 message 或 image_generation_call |
content array |
消息内容列表 |
type string |
内容类型,如 output_text |
text string |
响应的文本内容 |
result string |
当 type 为 image_generation_call 时,返回图片的Base64编码 |
usage object |
Token消耗统计 |
input_tokens integer |
输入Token数 |
output_tokens integer |
输出Token数 |
incomplete_details object |
被截断的详情 (如 reason: "max_output_tokens") |
流式响应(SSE),content-type 为 text/event-stream。采用事件驱动的JSON结构,type 字段标识事件类型。事件包括 response.created、response.output_text.delta、response.web_search_call.*、response.output_item.done、response.completed 等。
响应示例
{
"id": "resp_057006...",
"status": "completed",
"model": "gpt-4.1-2025-04-14",
"output": [
{
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "I'm ChatGPT..."
}
]
}
],
"usage": {
"input_tokens": 20,
"output_tokens": 47,
"total_tokens": 67
}
}
{
"id": "resp_059666...",
"status": "completed",
"model": "gpt-4.1-2025-04-14",
"output": [
{
"type": "image_generation_call",
"status": "completed",
"result": "iVBORw0KGgoAAAANSUhEUgAABAAAA..."
}
]
}
data: {"type":"response.created", "sequence_number":0, "response":{...}}
data: {"type":"response.output_item.added", "sequence_number":2, "item":{...}}
// Text delta output
data: {"type":"response.output_text.delta", "sequence_number":9, "delta":" Today's weather in London..."}
// Search tool status
data: {"type":"response.web_search_call.searching", "sequence_number":4, ...}
// Response end
data: {"type":"response.completed", "sequence_number":54, "response":{...}}
data: [DONE]
状态码
| HTTP状态码 | 描述 |
|---|---|
| 200 | 请求成功 |
| 401 | 访问被拒绝 (缺少Authorization请求头或token错误) |
| 403 | 访问被拒绝 (请求主体格式错误等) |
| 500 | 服务器内部错误 |