Skip to content

python 库流式输出

python
from openai import OpenAI


client = OpenAI(
    base_url="https://www.anyapi.vip/v1",
    api_key="sk-xxxx"
)
stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Say this is a test"}],
    stream=True,
)
for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="")

AnyAPI — 专业的 AI 接口聚合服务