流式输出与参数支持
Routic 支持流式输出和大量 OpenAI 兼容参数。本文档说明哪些参数可用、哪些暂不支持。
流式输出
在请求中设置 stream: true,即可通过 Server-Sent Events (SSE) 接收增量响应。
from openai import OpenAI
client = OpenAI(
base_url="https://api.routic.ai/v1",
api_key="sk-xxxxxxxx",
)
stream = client.chat.completions.create(
model="deepseek-r1",
messages=[{"role": "user", "content": "你好"}],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
流式输出格式
- Content-Type:
text/event-stream - 每个分块遵循 OpenAI
chat.completion.chunk格式 - 最后一个事件为
data: [DONE]
推理模型流式输出
推理模型(如 deepseek-r1)在流式输出时,推理内容通过 delta.reasoning_content 字段返回:
data: {"object":"chat.completion.chunk","delta":{"reasoning_content":"让我想想..."},"index":0}
data: {"object":"chat.completion.chunk","delta":{"content":"答案是..."},"index":0}
data: [DONE]
Stream options
使用 stream_options 参数控制流式行为:
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
include_usage | boolean | false | 在最后一个分块中包含 token 用量 |
{
"model": "deepseek-r1",
"messages": [{"role": "user", "content": "你好"}],
"stream": true,
"stream_options": {"include_usage": true}
}
当 include_usage 为 true 时,[DONE] 之前的最后一个分块会包含 usage 对象。
参数参考
支持的参数
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
model | string | — | 模型名,支持标准名和智能路由名。 |
messages | array | — | 对话消息,每条需含 role 和 content。 |
stream | boolean | false | 启用 SSE 流式输出。 |
temperature | number | 1 | 采样随机性,范围 0–2。 |
max_tokens | integer | — | 最大生成 token 数。 |
top_p | number | 1 | 核采样阈值。 |
frequency_penalty | number | 0 | 重复惩罚,范围 -2 到 2。 |
presence_penalty | number | 0 | 话题多样性,范围 -2 到 2。 |
stop | array / string | null | 最多 4 个停止序列。 |
tools | array | — | Function calling 定义。 |
tool_choice | string / object | — | "none"、"auto" 或指定工具。 |
response_format | object | — | 强制 JSON 模式或 schema。 |
logprobs | boolean | false | 返回对数概率。 |
top_logprobs | integer | — | 返回前 N 个对数概率,范围 0–20。 |
stream_options | object | — | 流式选项(仅 stream: true 时)。 |
thinking | object | — | Routic 扩展——启用推理模式。 |
暂不支持
| 参数 | 类型 | 说明 |
|---|---|---|
n | integer | 始终返回 1 个 choice。 |
seed | integer | 暂不支持。 |
logit_bias | object | 暂不支持。 |
网关级控制
以下控制在网关层面配置,不能通过请求参数设置:
| 控制 | 范围 | 说明 |
|---|---|---|
| RPM 限制 | 按密钥 | 每分钟请求数,默认 100,上限 1,000。 |
| TPM 限制 | 按密钥 | 每分钟 token 数,默认 10,000,上限 100,000。 |
| 最大预算 | 按密钥 | 消费上限,默认 $100,上限 $1,000。 |
| 自动重试 | 按路由 | 模型提供商错误时网关自动重试,默认 3 次。 |
| 超时 | 按路由 | 请求超时,默认 60 秒。 |
如需调整密钥级限制,联系支持或在控制台中修改。详见速率限制。