流式输出
- Python
- JavaScript
- curl
from openai import OpenAI
client = OpenAI(api_key="your-api-key", base_url="https://www.cheapertoken.work/v1")
stream = client.chat.completions.create(
model="gemini-2.5-flash",
messages=[{"role": "user", "content": "写一首关于秋天的诗"}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="", flush=True)
import OpenAI from "openai";
const client = new OpenAI({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work/v1" });
const stream = await client.chat.completions.create({
model: "gemini-2.5-flash",
messages: [{ role: "user", content: "写一首关于秋天的诗" }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) process.stdout.write(content);
}
curl https://www.cheapertoken.work/v1/chat/completions \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-2.5-flash",
"messages": [{"role": "user", "content": "写一首关于秋天的诗"}],
"stream": true
}'