Skip to content

Instantly share code, notes, and snippets.

@guillermoAlv
Created September 24, 2023 07:33
Show Gist options
  • Save guillermoAlv/25ada3f879f0cc57b368742422de9a4f to your computer and use it in GitHub Desktop.
Save guillermoAlv/25ada3f879f0cc57b368742422de9a4f to your computer and use it in GitHub Desktop.
Issue in async generator
import openai
import asyncio
os.environ['OPENAI_API_KEY'] = "XXXXXXXXXXXXXXXX"
openai.api_key = os.getenv('OPENAI_API_KEY')
def streaming_chat(history):
response = openai.ChatCompletion.acreate(
model="gpt-3.5-turbo",
messages=history,
stream=True
)
return response
history = \
[
{'role': 'user', 'content': "Can you give me three tips about cooking"}
]
response_async_generator = await streaming_chat(history=history)
print(type(response))
final_message = False
while True:
try:
next_element = await asyncio.wait_for(response_async_generator.__anext__(), timeout=0.01)
print(next_element)
except asyncio.TimeoutError as to_err:
print("Waiting...")
except asyncio.StopAsyncIteration as err:
print("Failed")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment