stream: true:
curl https://api.eztokens.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $EZTOKENS_API_KEY" \
-d '{
"model": "gpt-4.1",
"messages": [{"role": "user", "content": "Tell me a story."}],
"stream": true
}'
流式响应格式
每个 chunk 是一个以data: 为前缀的 JSON 对象:
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
data: [DONE]
SDK 用法
- Python
- TypeScript
from openai import OpenAI
client = OpenAI(
api_key="EZTOKENS_API_KEY",
base_url="https://api.eztokens.ai/v1"
)
stream = client.chat.completions.create(
model="gpt-4.1",
messages=[{"role": "user", "content": "Tell me a story."}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "EZTOKENS_API_KEY",
baseURL: "https://api.eztokens.ai/v1",
});
const stream = await client.chat.completions.create({
model: "gpt-4.1",
messages: [{ role: "user", content: "Tell me a story." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}
流式行为取决于所选模型的能力。当服务器发送
data: [DONE] 或连接关闭时,流结束。