代码示例 / Python

python 库流式输出


from openai import OpenAI



client = OpenAI(

    base_url="https://www.cokeapi.com/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="")