You need to enable JavaScript to run this app.
文档中心
火山方舟

火山方舟

复制全文
下载 pdf
使用 Responses API
文本生成
复制全文
下载 pdf
文本生成
文本生成模型能够基于输入的提示词生成连贯且有意义的文本内容。​
Responses API 支持更简洁的输入输出格式,能够更加高效地进行上下文管理,推荐在项目中使用。​
说明​
方舟平台的新用户?获取 API Key 及 开通模型等准备工作,请参见 快速入门。​
支持模型​
250615 及之后版本的大语言模型,如无特殊说明,默认支持 Responses API,方舟平台大语言模型列表,请参见:文本生成能力。​
快速开始​
​
Curl
Python
Go
Java
OpenAI SDK
​
​
# Get API Key: https://ark.volcengine.com/region:cn-beijing/apikey​
curl https://ark.cn-beijing.volces.com/api/v3/responses \​
-H "Authorization: Bearer $ARK_API_KEY" \​
-H "Content-Type: application/json" \​
-d '{​
"model": "doubao-seed-2-1-pro-260915",​
"input": "hello"​
}'​
​
​
API 文档​
上下文管理​
上下文状态管理简单,默认情况下每个请求的输入和回复(不包括思维链内容) 都会持久化存储,后续请求只需传入 id 就可以引入对应请求的输入和回复,无需像 Chat API 每次请求,传入历史对话。详细信息,参见工作原理及使用说明。​
多轮对话​
在多轮调用模式下,系统能够自动管理上下文,持续追踪和记忆之前的对话内容,使对话更加连贯自然,提升智能交互体验。​
​
Curl
Python
Go
Java
OpenAI SDK
​
​
curl https://ark.cn-beijing.volces.com/api/v3/responses \​
-H "Authorization: Bearer $ARK_API_KEY" \​
-H "Content-Type: application/json" \​
-d '{​
"model": "doubao-seed-2-1-pro-260915",​
"input": "Hi,讲个笑话。"​
}'​
​
​
curl https://ark.cn-beijing.volces.com/api/v3/responses \​
-H "Authorization: Bearer $ARK_API_KEY" \​
-H "Content-Type: application/json" \​
-d '{​
"model": "doubao-seed-2-1-pro-260915",​
"previous_response_id":"<id>",​
"input": "这个笑话的笑点在哪?"​
}'​
​
第二次请求需将 curl 命令中的 <id> 替换为上一次请求返回的 Response id。​
​
重新生成对话​
这段代码展示了 Responses API 中通过灵活调用 previous_response_id 重新生成对话的树状分叉结构,并展示了如何在不同的分支中进行不同的操作。在重新生成对话的场景中,系统能够根据不同的分支条件,灵活地执行多种的操作,从而实现更复杂的对话逻辑。​
​
Python
Go
Java
OpenAI SDK
​
​
import os​
from arkruntime import Ark​
​
# Get API Key: https://ark.volcengine.com/region:cn-beijing/apikey​
api_key = os.getenv('ARK_API_KEY')​
​
client = Ark(​
base_url='https://ark.cn-beijing.volces.com/api/v3',​
api_key=api_key,​
)​
​
# Create the first-round conversation request​
response = client.responses.create(​
model="doubao-seed-2-1-pro-260915",​
input=[{"role": "user", "content": "你知道余弦相似度的原理吗?"}],​
store=True, # store the conversation​
)​
print(response)​
​
# Create the second-round conversation request​
second_response = client.responses.create(​
model="doubao-seed-2-1-pro-260915",​
previous_response_id=response.id,​
input=[{"role": "user", "content": "我希望你可以用小学生都能听懂的方式来解释这个问题"}],​
store=True,​
)​
print(second_response)​
​
# Recreate the second-round conversation request​
re_second_response = client.responses.create(​
model="doubao-seed-2-1-pro-260915",​
previous_response_id=response.id,​
input=[{"role": "user", "content": "我希望你可以用教授的思考逻辑来解释这个问题"}],​
store=True,​
)​
print(re_second_response)​
​
​
窗口截断​
这段代码展示了如何利用 delete 接口实现 Responses API 的窗口截断的功能,使得程序可以在 response 粒度上管理历史记忆,便于后续进行更复杂的对话。​
​
Python
Go
Java
OpenAI SDK
​
​
import os​
from arkruntime import Ark​
​
# Get API Key: https://ark.volcengine.com/region:cn-beijing/apikey​
api_key = os.getenv('ARK_API_KEY')​
​
client = Ark(​
base_url='https://ark.cn-beijing.volces.com/api/v3',​
api_key=api_key,​
)​
​
response = client.responses.create(​
model="doubao-seed-2-1-pro-260915",​
input="讲个谐音梗笑话",​
)​
print(response)​
​
second_response = client.responses.create(​
model="doubao-seed-2-1-pro-260915",​
previous_response_id=response.id,​
input=[{"role": "user", "content": "讲个有哲理的笑话"}],​
)​
print(second_response)​
​
third_response = client.responses.create(​
model="doubao-seed-2-1-pro-260915",​
previous_response_id=second_response.id,​
input=[{"role": "user", "content": "讲个冷笑话"}],​
)​
print(third_response)​
​
deleting_response = client.responses.delete(second_response.id)​
print(deleting_response)​
​
fourth_response = client.responses.create(​
model="doubao-seed-2-1-pro-260915",​
previous_response_id=third_response.id,​
input=[{"role": "user", "content": "你刚刚讲了几个笑话?都是关于什么主题的?"}],​
)​
print(fourth_response)​
​
​
流式输出​
流式输出支持内容动态实时呈现,既能够缓解用户等待焦虑,又可以规避复杂任务因长时间推理引发的客户端超时失败问题,保障请求流程顺畅。​
​
Curl
Python
Go
Java
OpenAI SDK
​
​
curl https://ark.cn-beijing.volces.com/api/v3/responses \​
-H "Authorization: Bearer $ARK_API_KEY" \​
-H "Content-Type: application/json" \​
-d '{​
"model": "doubao-seed-2-1-pro-260915",​
"input": "常见的十字花科植物有哪些?",​
"thinking":{"type": "enabled"},​
"stream": true​
}'​
​
输出示例:​
​
event: response.created​
data: {"type":"response.created","response":{"created_at":1764229579,"id":"resp_021764229578658fe9a0f6cb2cc6c828e7a59adbdb971872aee70","max_output_tokens":32768,"model":"doubao-seed-2-1-pro-260628","object":"response","thinking":{"type":"enabled"},"service_tier":"default","caching":{"type":"disabled"},"store":true,"expire_at":1764488778},"sequence_number":0}​
​
event: response.in_progress​
data: {"type":"response.in_progress","response":{"created_at":1764229579,"id":"resp_021764229578658fe9a0f6cb2cc6c828e7a59adbdb971872aee70","max_output_tokens":32768,"model":"doubao-seed-2-1-pro-260628","object":"response","thinking":{"type":"enabled"},"service_tier":"default","caching":{"type":"disabled"},"store":true,"expire_at":1764488778},"sequence_number":1}​
​
event: response.output_item.added​
data: {"type":"response.output_item.added","output_index":0,"item":{"id":"rs_02176422957963700000000000000000000ffffac15dd335c9c43","type":"reasoning","status":"in_progress"},"sequence_number":2}​
​
event: response.reasoning_summary_part.added​
data: {"type":"response.reasoning_summary_part.added","item_id":"rs_02176422957963700000000000000000000ffffac15dd335c9c43","output_index":0,"summary_index":0,"part":{"type":"summary_text"},"sequence_number":3}​
​
event: response.reasoning_summary_text.delta​
data: {"type":"response.reasoning_summary_text.delta","summary_index":0,"delta":"\n","item_id":"rs_02176422957963700000000000000000000ffffac15dd335c9c43","output_index":0,"sequence_number":4}​
​
event: response.reasoning_summary_text.delta​
data: {"type":"response.reasoning_summary_text.delta","summary_index":0,"delta":"用户","item_id":"rs_02176422957963700000000000000000000ffffac15dd335c9c43","output_index":0,"sequence_number":5}​
​
event: response.reasoning_summary_text.delta​
data: {"type":"response.reasoning_summary_text.delta","summary_index":0,"delta":"问","item_id":"rs_02176422957963700000000000000000000ffffac15dd335c9c43","output_index":0,"sequence_number":6}​
...​
​
​
补充系统提示词​
在 Responses API 中,instructions 字段用于在特定轮次中补充系统提示词,其核心机制是在对话上下文头部新增一条系统提示信息,实现更灵活的输出。​
配置 instructions 字段后无法写入缓存以及使用缓存,即无法配置 caching 字段为{"type":"enabled"},同时无法命中缓存。​
​
Python
Go
Java
OpenAI SDK
​
​
import os​
from arkruntime import Ark​
​
# Get API Key: https://ark.volcengine.com/region:cn-beijing/apikey​
api_key = os.getenv('ARK_API_KEY')​
​
client = Ark(​
base_url='https://ark.cn-beijing.volces.com/api/v3',​
api_key=api_key,​
)​
​
response = client.responses.create(​
model="doubao-seed-2-1-pro-260915",​
input=[​
{​
"role": "system",​
"content": "你是一位数学老师,能够讲清楚相应的数学问题。"​
},​
]​
)​
print(response)​
​
second_response = client.responses.create(​
model="doubao-seed-2-1-pro-260915",​
previous_response_id=response.id,​
input=[{"role": "user", "content": "请解释一下余弦相似度原理"}],​
)​
print(second_response)​
​
third_response = client.responses.create(​
model="doubao-seed-2-1-pro-260915",​
instructions="增加一个要求:我希望你可以用小学生能听懂的方式解释这个问题。",​
previous_response_id=response.id,​
input=[{"role": "user", "content": "请解释一下余弦相似度原理"}],​
)​
print(third_response)​
​
​
续写模式​
通过预填(Prefill)部分 assistant 角色的内容,引导和控制模型从已有的文本片段继续输出,以及控制模型在角色扮演场景中保持一致性。​
支持模型​
  • doubao-seed-evolving​
  • doubao-seed-2-1-pro-260915​
  • doubao-seed-2-1-lite-260915​
  • doubao-seed-2-1-pro-260628​
  • doubao-seed-2-1-turbo-260628​
  • doubao-seed-2-0-lite-260428​
  • doubao-seed-2-0-mini-260428​
  • doubao-seed-2-0-pro-260215​
  • doubao-seed-2-0-lite-260215​
  • doubao-seed-2-0-mini-260215​
  • doubao-seed-2-0-code-preview-260215​
  • doubao-seed-character-260628​
  • doubao-seed-character-260628​
  • glm-5-3-flash-260828​
  • deepseek-v4-1-flash-261017​
  • deepseek-v4-1-flash-260910​
  • deepseek-v4-1-flash-economy-261017​
  • kimi-k3-1-pro-261024​
核心配置​
在 input 列表里设置最后一条消息的 role 为assistant,并设置 partial 为true开启续写模式,模型会基于 content 内容进行续写。​
​
"input": [​
{"role": "user", "content": "Please write bubble sort code without any additional content."},​
{"role": "assistant", "content": "def bubble_sort(arr):", "partial": true}​
]​
​
注意​
使用续写模式,需注意:​
  • 使用 deepseek-v4-1-flash-261017、deepseek-v4-1-flash-260910、deepseek-v4-1-flash-economy-261017、kimi-k3-1-pro-261024 模型时,不设置 partial 不会报错,只是不开启续写。​
  • 新一轮输入支持将assistant角色的 content 设置为空进行 Prefill,实现模型多轮连续回答。​
  • 支持使用前缀缓存和 Session 缓存。其中 Session 缓存场景下,缓存存储、下一轮的命中缓存,都按本轮输入 tokens 计算。​
  • 不支持结构化输出(json_object 模式和 json_schema 模式)。​
  • 建议不要与内置工具搭配使用。因涉及大模型调用和工具调用的循环逻辑,续写模式下的 content 无法作为最终输出的起点内容,导致续写模式失效。​
使用示例​
代码补全场景​
下面是对冒泡排序算法的代码进行续写的示例。开启续写模式后,模型会基于 "def bubble_sort(arr):"进行续写。​
​
Curl
Python
Go
Java
OpenAI SDK
​
​
curl https://ark.cn-beijing.volces.com/api/v3/responses \​
-H "Authorization: Bearer $ARK_API_KEY" \​
-H "Content-Type: application/json" \​
-d '{​
"model": "doubao-seed-2-1-pro-260915",​
"input": [​
{"role": "user", "content": "Please write bubble sort code without any additional content."},​
{"role": "assistant", "content": "def bubble_sort(arr):", "partial": true}​
],​
"caching": {"type": "disabled"}​
}'​
​
​
返回示例:​
模型根据信息 "def bubble_sort(arr):" 续写,保持对应的格式和行文风格。​
​
n = len(arr)​
for i in range(n):​
swapped = False​
for j in range(0, n-i-1):​
if arr[j] > arr[j+1]:​
arr[j], arr[j+1] = arr[j+1], arr[j]​
swapped = True​
if not swapped:​
break​
return arr​
​
上下文缓存场景​
以 Curl 方式为例介绍在续写模式下使用上下文缓存,其他 SDK 示例参见上下文缓存。​
  • 创建前缀缓存,同时使用续写模式。​
输入内容需要大于等于256个tokens,否则无法创建前缀缓存。​
​
curl https://ark.cn-beijing.volces.com/api/v3/responses \​
-H "Authorization: Bearer $ARK_API_KEY" \​
-H "Content-Type: application/json" \​
-d '{​
"model": "doubao-seed-2-1-pro-260915",​
"input":[​
{​
"role": "system",​
"content": "你是豆包,<prompt_content>"​
},​
{​
"role": "user",​
"content": "你喜欢吃什么?"​
},​
{​
"role": "assistant",​
"content": "西红柿"​
},​
{​
"role": "user",​
"content": "还有呢?"​
},​
{​
"role": "assistant",​
"content": "豆腐",​
"partial":true​
}​
],​
"caching":{​
"type":"enabled",​
"prefix": true​
},​
"thinking": {​
"type": "disabled"​
}​
}'​
​
  • 创建 Session 缓存,同时使用续写模式。通过上一轮请求返回 id 读取并使用前缀缓存。​
  • ​
    curl https://ark.cn-beijing.volces.com/api/v3/responses \​
    -H "Authorization: Bearer $ARK_API_KEY" \​
    -H "Content-Type: application/json" \​
    -d '{​
    "model": "doubao-seed-2-1-pro-260915",​
    "input":[​
    {​
    "role": "assistant",​
    "content": "包子"​
    },​
    {​
    "role": "assistant",​
    "content": "",​
    "partial":true​
    }​
    ],​
    "previous_response_id": "<id>",​
    "caching":{​
    "type":"enabled"​
    },​
    "thinking": {​
    "type": "disabled"​
    }​
    }'​
    ​
工作原理及使用说明​
上下文管理工作原理​
上下文管理流程如下:​
​​​
  • item:任意角色的一条信息,包括系统信息、用户信息、模型信息、思维链信息、工具信息等。​
  • response_id:代表本轮请求输入 item + 回答 item。​
  • 输入 item:previous_response_id 指代的 item + 本轮新输入 item。通过链表的形式串联之前轮次的对话,形成新的输入。​
使用说明​
默认开启存储功能,支持通过设置"store": true 启用 / "store": false 关闭。​
  • 存储条件:本次请求回复完毕且成功(返回 status 字段为completed),或者回复触发了长度限制导致截断(status 字段为 incomplete)都会将本轮记录存储。当思维链内容触发了长度限制导致截断,在查询模型响应时返回 output 为空。​
  • 输入限制:​
  • store: true(开启存储)时,输入限制同时受模型上下文窗口/最大输入长度、存储 items 数约束,实际可用输入上限以先达到的限制为准。​
  • 模型长度限制:在模型列表查看对应模型的长度限制。​
  • items 上限:最多支持 1000 个 item(任意角色的一条信息为 1 个 item,如一问一答为 2 个 item)。达到此限制,无法继续对话,可通过删除 Response手动清理记录。​
  • store: false(关闭存储)时,输入限制仅受模型上下文窗口/最大输入长度约束。​
  • 存储时长:默认存储 3 天,可通过 expire_at 字段自定义设置,最长支持 7 天。​
  • 存储内容:存储 input 和 output 字段的信息,不存储思维链内容。​
  • 其他说明:store 功能当前不收取费用;数据会采用加密方式,确保信息安全和隐私合规。​
QPS 限流​
Responses API QPS 限流如下。如果需要提升限流值,请提交 工单。​
​
接口名称​
账号维度的 QPS 限流​
创建模型响应​
无​
查询模型响应​
20​
列出输入项​
20​
删除模型响应​
20​
​
​
最近更新时间:2026.10.24 19:43:58
这个页面对您有帮助吗?
有用
有用
无用
无用