> ## Documentation Index
> Fetch the complete documentation index at: https://docs-vip.apigo.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI 流式对话示例

> OpenAI 流式聊天输出示例。

## 推荐 endpoint

* [OpenAI /v1/chat/completions](/api-reference/endpoints/openai/chat-completions)
* [OpenAI /v1/responses](/api-reference/endpoints/openai/responses)

## 最小请求

```json theme={null}
{
  "model": "gpt-4.1",
  "messages": [
    { "role": "user", "content": "边生成边解释 SSE 流式输出。" }
  ],
  "stream": true
}
```

## cURL 示例

```bash theme={null}
curl https://api-vip.apigo.ai/v1/chat/completions \
  -H "Authorization: Bearer $YOUR API KEY" \
  -H "Content-Type: application/json" \
  -N \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      { "role": "user", "content": "边生成边解释 SSE 流式输出。" }
    ],
    "stream": true
  }'
```

## Python 示例

```python theme={null}
from openai import OpenAI

client = OpenAI(
    base_url="https://api-vip.apigo.ai/v1",
    api_key="<YOUR API KEY>",
)

stream = client.chat.completions.create(
    model="gpt-4.1",
    messages=[{"role": "user", "content": "边生成边解释 SSE 流式输出。"}],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content or ""
    print(delta, end="")
```

## Node.js 示例

```js theme={null}
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api-vip.apigo.ai/v1",
  apiKey: process.env.YOUR API KEY,
});

const stream = await client.chat.completions.create({
  model: "gpt-4.1",
  messages: [
    { role: "user", content: "边生成边解释 SSE 流式输出。" }
  ],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || "");
}
```

## 最佳实践

* 前端按 chunk 增量渲染，不要等完整 JSON 才显示
* 如果后面要接工具和结构化输出，优先评估 `responses` 的流式事件模型
* 服务端统一处理断流重试和最终结果拼接
