ff
This commit is contained in:
38
content/guides/streaming.mdx
Normal file
38
content/guides/streaming.mdx
Normal file
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: 流式调用
|
||||
sidebarTitle: 流式调用
|
||||
description: SSE 流式输出接入说明
|
||||
---
|
||||
|
||||
# 流式调用
|
||||
|
||||
在请求体中设置 `"stream": true`,服务端以 SSE 形式返回增量内容。
|
||||
|
||||
## cURL
|
||||
|
||||
```bash
|
||||
curl https://api.haiyushuke.com/v1/chat/completions \
|
||||
-H "Authorization: Bearer YOUR_API_KEY" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"model":"gpt-4o-mini","stream":true,"messages":[{"role":"user","content":"写一首四句诗"}]}'
|
||||
```
|
||||
|
||||
## OpenAI SDK(Node)
|
||||
|
||||
```ts
|
||||
const stream = await client.chat.completions.create({
|
||||
model: "gpt-4o-mini",
|
||||
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** 累计,以账单规则为准
|
||||
Reference in New Issue
Block a user