simonw / til

Today I Learned

Home Page:https://til.simonwillison.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issues with OpenAI Streaming code

tyler-suard-parker opened this issue · comments

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

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())