Does anyone have a working code snippet for how to make streaming work in python? All the discussion I’ve seen is about doing this in JavaScript.
Basically, I want the counterpart of the following where stream=True:
r = openai.Completion.create(
model=“code-davinci-002”,
prompt= prompt",
temperature=0,
max_tokens=4096,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=[“I:”, “O:”]
)
I tried using sseclient and urllib3, but can’t get this to work.
Here’s a very quick example that streams tokens and prints out each token as it comes in:
for resp in openai.Completion.create(model='code-davinci-002', prompt='def hello():', max_tokens=512, stream=True):
sys.stdout.write(resp.choices[0].text)
sys.stdout.flush()
I know this is old (providing samples for the first question, not returning token cost), but I came across the post while trying to figure this out myself with the ChatCompletion api. I got it working today, it’s the one labeled oai-text-gen-with-secrets-and-streaming.py on GitHub - trackzero/openai: Experiments with the OpenAI API. You'll need your own API keys..
That particular example is authing using AWS Secrets Manager, but you can just delete the get_secret function and pull an environment variable with openai.api_key = os.getenv("OPENAI_API_KEY")
I’ll see about adding token cost on the exit function in the next day or two.