Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Created July 21, 2024 11:31
Show Gist options
  • Save mahemoff/35c6f4b45dc1305813aedd7b835f4681 to your computer and use it in GitHub Desktop.
Save mahemoff/35c6f4b45dc1305813aedd7b835f4681 to your computer and use it in GitHub Desktop.
OpenAI API streaming example
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ.get("OPEN_AI_PYPLAY_KEY"))
messages=[{"role": "user", "content": "Write an essay about the historical significance of Canterbury Tales"}]
stream = client.chat.completions.create(model='gpt-4o-mini', messages=messages, stream=True)
for chunk in stream:
response=chunk.choices[0].delta.content
if response is not None:
print(response, end="")
if "\n" in response:
print('--')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment