2026-06-16 14:34:09 +08:00
|
|
|
|
---
|
|
|
|
|
|
title: 流式调用
|
|
|
|
|
|
sidebarTitle: 流式调用
|
|
|
|
|
|
description: SSE 流式输出接入说明
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
# 流式调用
|
|
|
|
|
|
|
2026-07-02 12:58:18 +08:00
|
|
|
|
在请求体中设置 `"stream": true`,服务端以 SSE 形式返回增量内容。协议与 OpenAI 流式兼容;HTTP 基础见 [HTTP API](/docs/guides/http_api)。
|
2026-06-16 14:34:09 +08:00
|
|
|
|
|
|
|
|
|
|
## cURL
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
curl https://api.haiyushuke.com/v1/chat/completions \
|
|
|
|
|
|
-H "Authorization: Bearer YOUR_API_KEY" \
|
|
|
|
|
|
-H "Content-Type: application/json" \
|
2026-07-02 12:58:18 +08:00
|
|
|
|
-d '{"model":"deepseek-chat","stream":true,"messages":[{"role":"user","content":"写一首四句诗"}]}'
|
2026-06-16 14:34:09 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## OpenAI SDK(Node)
|
|
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
|
const stream = await client.chat.completions.create({
|
2026-07-02 12:58:18 +08:00
|
|
|
|
model: "deepseek-chat",
|
2026-06-16 14:34:09 +08:00
|
|
|
|
stream: true,
|
|
|
|
|
|
messages: [{ role: "user", content: "你好" }],
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
for await (const chunk of stream) {
|
|
|
|
|
|
const text = chunk.choices[0]?.delta?.content;
|
|
|
|
|
|
if (text) process.stdout.write(text);
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 注意
|
|
|
|
|
|
|
|
|
|
|
|
- 客户端需正确处理连接中断与超时重试
|
|
|
|
|
|
- 流式场景下计费通常按 **输出 token** 累计,以账单规则为准
|