Skip to Content
接口文档流式输出接口

流式输出接口(Streaming)

POST https://ai.amaxsmp.com/v1/chat/completions

流式输出复用对话补全接口,只需将 stream 参数设为 true。响应采用 SSE(Server-Sent Events)格式。

请求示例

cURL

curl https://ai.amaxsmp.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \ -d '{ "model": "gpt-4o", "messages": [ {"role": "user", "content": "写一首关于春天的诗"} ], "stream": true }'

Python(OpenAI SDK)

from openai import OpenAI client = OpenAI( base_url="https://ai.amaxsmp.com/v1", api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ) stream = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "user", "content": "写一首关于春天的诗"} ], stream=True ) for chunk in stream: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="")

JavaScript

import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://ai.amaxsmp.com/v1", apiKey: "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", }); const stream = await client.chat.completions.create({ model: "gpt-4o", messages: [ { role: "user", content: "写一首关于春天的诗" }, ], stream: true, }); for await (const chunk of stream) { if (chunk.choices[0]?.delta?.content) { process.stdout.write(chunk.choices[0].delta.content); } }

SSE 数据格式

流式响应中每条数据以 data: 开头:

data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1710000000,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]} data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1710000000,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"春"},"finish_reason":null}]} data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1710000000,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"天"},"finish_reason":null}]} ... data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1710000000,"model":"gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]} data: [DONE]
字段说明
choices[].delta.role仅第一个 chunk 包含,值为 assistant
choices[].delta.content流式输出的文本片段
choices[].finish_reason最后一个 chunk 为 stop / length,其余为 null
[DONE]流结束标记

更多代码示例