Streaming GPT chat completions

Have not seen this clearly defined on the forum. So I thought I’d share. This script allows for output from GPT to be written directly to terminal as it is being created(streamed). It also allows for very large text inputs so you can copy and paste gigantic pieces of code into the terminal without hitting terminal limits or having to upload any files. Conversation experience similar to Chat GPT on the website. Enjoy.

from openai import OpenAI
import time
client = OpenAI()

while True:
    print("You (type 'END' on a new line when finished):")
    user_input = ""
    while True:
        line = input()
        if line.strip().lower() == 'end':
        user_input += line + "\n"

    if user_input.lower().strip() in ['exit', 'quit']:
        print("Exiting the chat.")
    messages = [{"role": "user", "content": user_input}]
        stream =
        for chunk in stream:
            delta_content = chunk.choices[0].delta.content if chunk.choices[0].delta.content is not None else None
            if delta_content:
                print(delta_content, end="", flush=True,)
                time.sleep(0.1)  # Delay
                print("AI: End of stream")
    except Exception as e:
        print(f"An error occurred: {e}")
1 Like

You can start your own Internet Radio Station in minutes for FREE using the AZ-Streaming Servercom Platform.