Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with OpenAI Streaming code #86

Open
tyler-suard-parker opened this issue Jan 9, 2024 · 1 comment
Open

Issues with OpenAI Streaming code #86

tyler-suard-parker opened this issue Jan 9, 2024 · 1 comment

Comments

@tyler-suard-parker
Copy link

When I run your async code, I get:
Error: await can't be used outside of a function.
When I enclose the async code within an async function, I get:
 TypeError: object Stream can't be used in 'await' expression

@simonw
Copy link
Owner

simonw commented Jan 9, 2024

You mean the code from https://til.simonwillison.net/gpt3/python-chatgpt-streaming-api

Does this work?

import openai
import asyncio

async def generate_cheesecake_names():
    async for chunk in await openai.ChatCompletion.acreate(
        model="gpt-3.5-turbo",
        messages=[{
            "role": "user",
            "content": "Generate a list of 20 great names for sentient cheesecakes that teach SQL"
        }],
        stream=True,
    ):
        content = chunk["choices"][0].get("delta", {}).get("content")
        if content is not None:
            print(content, end='')

asyncio.run(generate_cheesecake_names())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants