Created
July 21, 2024 11:31
-
-
Save mahemoff/35c6f4b45dc1305813aedd7b835f4681 to your computer and use it in GitHub Desktop.
OpenAI API streaming example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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