流式输出
Chat Completions API
- 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="gpt-5.4",
messages=[{"role": "user", "content": "写一首关于春天的诗"}],
stream=True,
)
for chunk in stream:
if 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: "gpt-5.4",
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": "gpt-5.4",
"messages": [{"role": "user", "content": "写一首关于春天的诗"}],
"stream": true
}'
Responses API
- Python
- JavaScript
- curl
from openai import OpenAI
client = OpenAI(api_key="your-api-key", base_url="https://www.cheapertoken.work/v1")
stream = client.responses.create(
model="gpt-5.4",
input="写一首关于夏天的诗",
stream=True,
)
for event in stream:
if event.type == "response.output_text.delta":
print(event.delta, 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.responses.create({
model: "gpt-5.4",
input: "写一首关于夏天的诗",
stream: true,
});
for await (const event of stream) {
if (event.type === "response.output_text.delta") {
process.stdout.write(event.delta);
}
}
curl https://www.cheapertoken.work/v1/responses \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.4",
"input": "写一首关于夏天的诗",
"stream": true
}'