调用模型,如果希望控制和引导模型的输出,您可以通过续写模式实现:预填assistant角色的部分内容,模型将基于该内容延续输出,以此达成输出控制目标。您可以将该输出控制机制应用于以下场景:强制输出 JSON/XML 等特定格式、绕过模型最大输出限制生成超长内容、确保大模型在角色扮演场景中保持角色一致性。
模型及 API#
支持的模型如下:
-
文本生成模型中无深度思考能力的模型:具体模型可在文本生成能力筛选。
-
部分具有深度思考能力的模型:
- 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
- glm-5-3-flash-260828
- deepseek-v4-1-flash-260910
API 支持:
核心配置#
方式一#
支持模型:除 deepseek-v4-1-flash-260910 之外的其他模型。
请将消息列表的最后一条消息的 role 设置为assistant,模型会遵循 assistant 消息的已有格式和内容来续写。
messages = [
{"role": "user", "content": "You are a calculator, please calculate: 1 + 1"},
# 最后消息为模型消息,role 为 assistant
{"role": "assistant", "content": "="}
]
方式二#
支持模型:deepseek-v4-1-flash-260910
请将消息列表的最后一条消息的 role 设置为assistant,并设置最后一条消息的 prefix 参数为true。模型会遵循 assistant 消息的已有格式和内容来续写。
messages = [
{"role": "user", "content": "You are a calculator, please calculate: 1 + 1"},
{"role": "assistant", "content": "=", "prefix": True}
]
模式对比#
| 普通模式 | 续写模式 |
|---|---|
|
请求示例 |
请求示例 |
|
返回示例 输出会按照模型根据问题的完整自然语言回复。 |
返回示例 模型根据信息“=”续写,保持对应的格式和行文风格。 |
接下来,对于几种典型使用场景进行举例说明。
场景:改善输出格式#
模型可能会基于自身理解输出内容,导致结果无法直接被其他程序解析。可以通过预填充 { 符号,引导模型跳过一些场景回复,直接输出 JSON 对象,会让回答更加简洁和工整,可以被其他程序更好地解析。
| 普通模式 | 续写模式 |
|---|---|
|
请求示例 |
请求示例 |
|
返回示例 Note: Some details about Doubao might not be publicly available, as it's a relatively new model from ByteDance. The information here represents common capabilities of similar large language models.
|
返回示例 |
注意事项#
-
方舟已提供结构化输出能力,可让模型输出严格符合格式要求的 JSON 内容,请参见结构化输出(beta)。
-
由于模型具有一定的随机性,无法 100% 保证回复为标准 JSON 对象。建议您对模型的回复进行校验,下面示例使用 json_repair 库校验返回的是否为合法的 JSON 对象。
import json # Install json-repair: pip install json-repair import json_repair import os # Install SDK: python -m pip install --upgrade arkruntime from arkruntime import Ark PREFILL_PREFIX = "{" client = Ark( # The base URL for model invocation base_url="https://ark.cn-beijing.volces.com/api/v3", # Get API Key: https://ark.volcengine.com/region:cn-beijing/apikey api_key=os.getenv('ARK_API_KEY'), ) completion = client.chat.completions.create( # Replace with Model ID model = "doubao-seed-2-1-pro-260915", messages=[ {"role": "user", "content": "Use JSON to describe the name and function of the Doubao model"}, {"role": "assistant", "content": PREFILL_PREFIX}, ] ) # 拼接 PREFILL_PREFIX 和模型输出 json_string = PREFILL_PREFIX + completion.choices[0].message.content # 解析 JSON Object obj = {} try: obj = json.loads(json_string) except json.JSONDecodeError as e: obj = json_repair.loads(json_string) print(obj)
场景:输出超过模型输出上限的内容#
使用续写模式让大模型根据前轮请求续写内容,并拼接多次输出的内容,组合成超长输出。可以避免因为 max_tokens(最大输出长度)限制,导致无法获取完整内容。
这种方式会将模型输出多次返回给模型,会有更多 token 用量。
注意增加循环次数限制等兜底,防止死循环输出等特殊情况,导致意外花费。
import os
# Install SDK: python -m pip install --upgrade arkruntime
from arkruntime import Ark
messages = [
{"role": "user", "content": '''Please translate the following text into English: <TEXT_TO_BE_TRANSLATED> '''}, # Replace <TEXT_TO_BE_TRANSLATED> with your content
{"role": "assistant", "content": ""}
]
# Replace with Model ID
ark_model="doubao-seed-2-1-pro-260915"
client = Ark(
# The base URL for model invocation
base_url="https://ark.cn-beijing.volces.com/api/v3",
# Get API Key: https://ark.volcengine.com/region:cn-beijing/apikey
api_key=os.getenv('ARK_API_KEY'),
)
completion = client.chat.completions.create(
model=ark_model,
messages=messages
)
loop_count = 0 # 初始化循环计数器
max_loops = 20 # 设置最大循环次数为20
# 触发因为输出长度限制,循环生成 assistant 回复
while completion.choices[0].finish_reason == "length":
messages[-1]["content"] += completion.choices[0].message.content
completion = client.chat.completions.create(
model=ark_model,
messages=messages
)
# 增加计数器
loop_count += 1
# 检查是否超过最大循环次数
if loop_count >= max_loops:
print(f"Warning: Reached maximum loop count {max_loops}, stopping content generation.")
break
# 返回最后一条回复
messages[-1]["content"] += completion.choices[0].message.content
print(messages[-1]["content"])
场景:增强角色扮演一致性#
我们通常使用系统消息来设置角色的一些信息,但是随着对话轮次的增多,模型可能会跳脱出角色的约束,通过续写模式,提醒模型本次输出扮演的角色,保证对话符合预期。
import os
# Install SDK: python -m pip install --upgrade arkruntime
from arkruntime import Ark
client = Ark(
# The base URL for model invocation
base_url="https://ark.cn-beijing.volces.com/api/v3",
# Get API Key: https://ark.volcengine.com/region:cn-beijing/apikey
api_key=os.getenv('ARK_API_KEY'),
)
# Replace with Model ID
ark_model="doubao-seed-2-1-pro-260915"
messages = [
{"role": "system", "content": "The following is a scene from Journey to the West. Please converse according to the specified role. For each round of dialogue, only reply with the content of the current role; do not add any additional content.\n\nAfter leaving Wuzhuang Guan, Tang Seng and his disciples traveled for more than a month and arrived at the foot of a great mountain. Wukong looked around and saw lofty mountains, steep ridges, overgrown weeds, and extremely dangerous terrain."},
{"role": "assistant", "content": "Wukong said:"}
]
completion = client.chat.completions.create(
model=ark_model,
messages=messages
)
print(messages[-1]['content']+completion.choices[0].message.content)
模型输出示例:
Wukong said, "Master, this mountain looks very dangerous. I will go explore the way first. You wait here."
您也可以通过变更messages最后消息,实现角色切换。
...
messages[-1] = {"role": "assistant", "content": "Tang Seng said:"}
completion = client.chat.completions.create(
model=ark_model,
messages=messages
)
print(messages[-1]['content']+completion.choices[0].message.content)
模型输出示例:
Tang Seng said: "Wukong, this mountain looks very dangerous, you must be careful to check it out, don't fall into the trap of the monster."