核心 API/流式输出

开启流式输出以获得更低延迟和渐进式 UI 渲染体验。

流式输出(Streaming)

流式输出通过 SSE 按增量返回内容。

  • 方法:POST
  • 路径:/v1/chat/completions
  • 请求体设置 "stream": true

cURL 示例

curl https://api.unigateway.ai/v1/chat/completions \
  -H "Authorization: Bearer $UNIGATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -N \
  -d '{
    "model": "gemini-3-pro-preview",
    "stream": true,
    "messages": [
      {"role": "user", "content": "给我一个上线流程清单。"}
    ]
  }'

事件格式

通常为标准 SSE:

  • data: { ...chunk... }
  • data: [DONE]

Python 示例

from openai import OpenAI

client = OpenAI(
    api_key="<YOUR_UNIGATEWAY_API_KEY>",
    base_url="https://api.unigateway.ai/v1",
)

stream = client.chat.completions.create(
    model="claude-sonnet-4-6",
    stream=True,
    messages=[{"role": "user", "content": "用一段话解释 SSE。"}],
)

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

TypeScript 示例

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.UNIGATEWAY_API_KEY,
  baseURL: "https://api.unigateway.ai/v1",
});

const stream = await client.chat.completions.create({
  model: "gpt-5.2",
  stream: true,
  messages: [{ role: "user", content: "说明流式输出的前端处理要点。" }],
});

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

Example request

Run it in your stack

Pick the SDK style that matches your app and copy the snippet directly into your project.

from openai import OpenAI

client = OpenAI(
    api_key="<YOUR_UNIGATEWAY_API_KEY>",
    base_url="https://api.unigateway.ai/v1",
)

stream = client.chat.completions.create(
    model="gpt-5.2",
    stream=True,
    messages=[{"role": "user", "content": "Explain SSE in one paragraph."}],
)

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