#Stream=True python example

1 messages · Page 1 of 1 (latest)

manic juniper
#

Would it be possible for someone to give an example of using the stream parameter to print a completion in real time kind of like how ChatGPT would print a response?

I have the code:

import openai

# Set the API key for OpenAI
openai.api_key = "YOUR_API_KEY"

def generate_response(prompt):
    completions = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=256,
        n=1,
        stop=None,
        temperature=0.9,
    )

    message = completions.choices[0].text
    return message

# Example usage
while True:
    user_input = input("You: ")
    if user_input == "exit":
        break
    response = generate_response(user_input)
    print(f"ChatGPT: {response}")

But I want to modify this to use streaming and print the response as it comes in.