跳到主要内容

流式输出

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)