Files
haiyushuke-website/content/guides/streaming.mdx
2026-07-02 12:58:18 +08:00

39 lines
1016 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 流式调用
sidebarTitle: 流式调用
description: SSE 流式输出接入说明
---
# 流式调用
在请求体中设置 `"stream": true`,服务端以 SSE 形式返回增量内容。协议与 OpenAI 流式兼容HTTP 基础见 [HTTP API](/docs/guides/http_api)。
## cURL
```bash
curl https://api.haiyushuke.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"deepseek-chat","stream":true,"messages":[{"role":"user","content":"写一首四句诗"}]}'
```
## OpenAI SDKNode
```ts
const stream = await client.chat.completions.create({
model: "deepseek-chat",
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** 累计,以账单规则为准