Responses API 思考模式
思考模式允许模型展示其推理过程,帮助用户理解AI是如何得出答案的。gpt-5 模型支持原生的思考推理模式。重要提示:根据内容安全管理政策,必须同时设置"effort"和"summary": "detailed"参数才能开启思考过程展示功能。
快速开始
只需要替换<API-KEY> 为你的实际API密钥即可运行。
curl -X POST "https://api.tokenops.ai/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <API-KEY>" \
-d '{
"model": "gpt-5-2025-08-07",
"input": "请解决这个数学问题:一个圆的面积是78.54平方厘米,求这个圆的周长。请详细说明解题思路。",
"reasoning": {
"effort": "medium",
"summary": "detailed"
}
}'
import requests
import json
# 配置API密钥和基础URL
API_KEY = "<API-KEY>"
BASE_URL = "https://api.tokenops.ai/v1"
def create_thinking_response():
"""使用gpt-5模型的思考模式"""
url = f"{BASE_URL}/responses"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
payload = {
"model": "gpt-5-2025-08-07",
"input": "请解决这个数学问题:一个圆的面积是78.54平方厘米,求这个圆的周长。请详细说明解题思路。",
"reasoning": {
"effort": "medium", # 推理强度:low, medium, high
"summary": "detailed" # 必需参数:开启思考过程展示
}
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
result = response.json()
# 获取推理过程(如果有)
if "output" in result:
for output_item in result["output"]:
if output_item.get("type") == "reasoning" and "summary" in output_item:
print("=== 推理摘要 ===")
for summary_item in output_item["summary"]:
if summary_item.get("type") == "summary_text":
print(summary_item["text"])
print()
# 获取最终回答
if "output" in result:
for output_item in result["output"]:
if output_item.get("type") == "message" and "content" in output_item:
print("=== 最终回答 ===")
for content_item in output_item["content"]:
if content_item.get("type") == "output_text":
print(content_item["text"])
# 显示使用统计
usage = result.get("usage", {})
reasoning_tokens = usage.get("output_tokens_details", {}).get("reasoning_tokens", 0)
print(f"\nToken使用: {usage.get('total_tokens', 0)} (输入: {usage.get('input_tokens', 0)}, 输出: {usage.get('output_tokens', 0)}, 推理: {reasoning_tokens})")
return result
else:
print(f"HTTP错误: {response.status_code}")
print(response.text)
return None
# 使用示例
if __name__ == "__main__":
print("使用gpt-5模型的原生思考模式")
response = create_thinking_response()
const axios = require('axios');
// 配置API密钥和基础URL
const API_KEY = '<API-KEY>';
const BASE_URL = 'https://api.tokenops.ai/v1';
async function createThinkingResponse() {
const url = `${BASE_URL}/responses`;
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`
};
const payload = {
model: 'gpt-5-2025-08-07',
input: '请解决这个数学问题:一个圆的面积是78.54平方厘米,求这个圆的周长。请详细说明解题思路。',
reasoning: {
effort: 'medium',
summary: 'detailed' // 必需参数:开启思考过程展示
}
};
try {
const response = await axios.post(url, payload, { headers });
// 获取推理过程(如果有)
if (response.data.output) {
response.data.output.forEach(outputItem => {
if (outputItem.type === 'reasoning' && outputItem.summary) {
console.log('=== 推理摘要 ===');
outputItem.summary.forEach(summaryItem => {
if (summaryItem.type === 'summary_text') {
console.log(summaryItem.text);
}
});
console.log();
}
});
}
// 获取最终回答
if (response.data.output) {
response.data.output.forEach(outputItem => {
if (outputItem.type === 'message' && outputItem.content) {
console.log('=== 最终回答 ===');
outputItem.content.forEach(contentItem => {
if (contentItem.type === 'output_text') {
console.log(contentItem.text);
}
});
}
});
}
// 显示使用统计
const usage = response.data.usage || {};
const reasoningTokens = usage.output_tokens_details?.reasoning_tokens || 0;
console.log(`\nToken使用: ${usage.total_tokens || 0} (输入: ${usage.input_tokens || 0}, 输出: ${usage.output_tokens || 0}, 推理: ${reasoningTokens})`);
return response.data;
} catch (error) {
console.error('请求出错:', error.response?.data || error.message);
return null;
}
}
// 使用示例
async function main() {
try {
console.log('使用gpt-5模型的原生思考模式');
await createThinkingResponse();
} catch (error) {
console.error('执行出错:', error);
}
}
main();
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
const (
API_KEY = "<API-KEY>"
BASE_URL = "https://api.tokenops.ai/v1"
)
type ThinkingRequest struct {
Model string `json:"model"`
Input string `json:"input"`
Reasoning *ReasoningOptions `json:"reasoning,omitempty"`
}
type ReasoningOptions struct {
Effort string `json:"effort,omitempty"`
Summary string `json:"summary,omitempty"`
}
type ThinkingResponse struct {
ID string `json:"id"`
Object string `json:"object"`
CreatedAt int64 `json:"created_at"`
Status string `json:"status"`
Model string `json:"model"`
Output []OutputItem `json:"output"`
Usage Usage `json:"usage"`
Reasoning *ReasoningInfo `json:"reasoning,omitempty"`
}
type OutputItem struct {
ID string `json:"id"`
Type string `json:"type"`
Summary []SummaryItem `json:"summary,omitempty"`
Content []ContentItem `json:"content,omitempty"`
Status string `json:"status,omitempty"`
Role string `json:"role,omitempty"`
}
type SummaryItem struct {
Type string `json:"type"`
Text string `json:"text"`
}
type ContentItem struct {
Type string `json:"type"`
Text string `json:"text"`
}
type Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
TotalTokens int `json:"total_tokens"`
}
type ReasoningInfo struct {
Effort string `json:"effort"`
Summary string `json:"summary"`
}
func createThinkingResponse() (*ThinkingResponse, error) {
url := BASE_URL + "/responses"
request := ThinkingRequest{
Model: "gpt-5-2025-08-07",
Input: "请解决这个数学问题:一个圆的面积是78.54平方厘米,求这个圆的周长。请详细说明解题思路。",
Reasoning: &ReasoningOptions{
Effort: "medium",
Summary: "detailed", // 必需参数:开启思考过程展示
},
}
jsonData, err := json.Marshal(request)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+API_KEY)
client := &http.Client{}
resp, err := client.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result ThinkingResponse
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
// 显示推理摘要和最终回答
for _, outputItem := range result.Output {
if outputItem.Type == "reasoning" && len(outputItem.Summary) > 0 {
fmt.Println("=== 推理摘要 ===")
for _, summaryItem := range outputItem.Summary {
if summaryItem.Type == "summary_text" {
fmt.Println(summaryItem.Text)
}
}
fmt.Println()
}
if outputItem.Type == "message" && len(outputItem.Content) > 0 {
fmt.Println("=== 最终回答 ===")
for _, contentItem := range outputItem.Content {
if contentItem.Type == "output_text" {
fmt.Println(contentItem.Text)
}
}
}
}
fmt.Printf("\nToken使用: %d (输入: %d, 输出: %d)\n",
result.Usage.TotalTokens,
result.Usage.InputTokens,
result.Usage.OutputTokens)
return &result, nil
}
func main() {
fmt.Println("使用gpt-5模型的原生思考模式")
response, err := createThinkingResponse()
if err != nil {
fmt.Printf("错误: %v\n", err)
}
if response != nil {
fmt.Printf("\n响应ID: %s\n", response.ID)
}
}
gpt-5 模型的思考模式实现
gpt-5 模型内置推理能力,通过reasoning 参数控制推理强度和思考过程展示:
{
"model": "gpt-5-2025-08-07",
"input": "your question here",
"reasoning": {
"effort": "medium", // low, medium, high
"summary": "detailed" // 必需参数:开启思考过程展示
}
}
reasoning 字段,展示模型的推理摘要。
响应结果示例
{
"id": "resp_01be82131ec287250068fdf7cd8d6c81969af11dab7e4218fe",
"object": "response",
"created_at": 1761474509,
"status": "completed",
"model": "gpt-5-2025-08-07",
"output": [
{
"id": "rs_01be82131ec287250068fdf7ce17a081968ba0933d6ff8baf0",
"type": "reasoning",
"summary": [
{
"type": "summary_text",
"text": "**Calculating circle properties**\n\nI'm laying out the steps for calculating the radius and circumference of a circle with an area of 78.54 cm². I'll use the formula A = πr², which gives me the radius, r = sqrt(A/π). So, with A plugged in, I find r to be 5 cm. \n\nNext, I calculate the circumference as C = 2πr, getting about 31.416 cm. I'll also explore an alternative formula for circumference, but it involves a lot of detailed computation that I just need to clarify and keep organized."
},
{
"type": "summary_text",
"text": "**Reviewing calculations for circumference**\n\nI'm comparing earlier calculations where I found the product Aπ to be 246.3, but the correct value should be 246.741264. Then, using 2√(Aπ), I calculate 2 * 15.712, which gives me about 31.424. This is slightly different from the exact value of 31.416 using r = 5 cm and π = 3.1416. \n\nI realize that the discrepancies in rounding might affect the results, and I'm clarifying that while A = 78.54 is technically equal to 25 * 3.1416, the algebraic relations hold true if I use the same π value consistently."
}
]
},
{
"id": "msg_01be82131ec287250068fdf7e0476c8196b7f3201011b0453d",
"type": "message",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "解题思路:\n- 用到两个公式:\n 1) 面积公式 A = πr²\n 2) 周长公式 C = 2πr\n- 由面积求半径:r = √(A/π)\n 代入 A = 78.54,取 π ≈ 3.1416,\n r = √(78.54 / 3.1416) = √25 = 5(厘米)\n- 求周长:C = 2πr = 2 × 3.1416 × 5 = 31.416(厘米)\n\n结论:该圆的周长约为31.416厘米(可记为31.42厘米)。此外,可写成 C = 10π 厘米。"
}
],
"role": "assistant"
}
],
"reasoning": {
"effort": "medium",
"summary": "detailed"
},
"usage": {
"input_tokens": 38,
"input_tokens_details": {
"cached_tokens": 0
},
"output_tokens": 2348,
"output_tokens_details": {
"reasoning_tokens": 2176
},
"total_tokens": 2386
}
}
参数说明
推理参数
reasoning.effort- 推理强度"low"- 快速推理"medium"- 平衡推理(推荐)"high"- 深度推理
reasoning.summary- 思考过程展示控制(必需参数)"detailed"- 展示详细的思考过程和推理步骤
注意:根据内容安全管理政策,必须设置 "summary": "detailed" 才能开启思考模式功能。
通用参数
max_tokens- 最大生成token数(建议2000以上用于思考模式)temperature- 控制输出的随机性
应用场景
- 数学问题解决 - 展示计算步骤和推理过程
- 逻辑推理 - 显示逻辑分析的每个环节
- 复杂决策 - 说明考虑的因素和权衡过程
- 代码调试 - 展示问题分析和解决思路
- 学术研究 - 提供详细的论证过程
注意事项
- 必需参数: 必须设置
"summary": "detailed"参数才能开启思考模式 - Token消耗: 思考模式通常消耗更多token,包括推理token
- 响应时间: 启用思考可能增加响应时间
- 内容质量: 思考过程有助于提高回答质量和可信度
- 隐私考虑: 思考内容可能包含敏感的推理过程
最佳实践
- 问题类型: 复杂问题更适合使用思考模式
- 参数设置: 根据问题复杂度选择合适的reasoning.effort
- 必需配置: 始终包含
"summary": "detailed"参数 - 结果解析: 合理处理思考内容和最终答案的分离
- Token管理: 考虑思考模式的额外token消耗