-1

while using openai api, how can i print each line when there are multiple lines in the assistant reply and as they're being generated?

let's take an example:

USER: generate me a code to check if a number is prime or not

GPT 3.5: sure! Here's a simple code snippet in Python to check if a number is prime or not:

    if num <= 1:
        return False

    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False

    return True

# Example usage number = int(input("Enter a number: ")) if is_prime(number):
    print(number, "is a prime number") else:
    print(number, "is not a prime number") 

Hope it helps.

Now assuming you've worked with openai api, you'll know that this response will be printed in terminal as a whole when chat gpt finishes all the response.

But i want to print it line by line as it's being generated. like "sure! Here's a simple code snippet in Python to check if a number is prime or not:" this line after its generated and then print the next line and so on and not the whole response at one go. So that it feels more like it's actually generating the text.

How can i do that?

i've no idea how to implement it so, i only tried the normal version where it prints the whole response at once.

desertnaut
  • 57,590
  • 26
  • 140
  • 166

1 Answers1

-1

You could simulate the output generation of the response. This will print character by character just as chat gpt does.

def animation(output):
    for char in output:
        print(char, end='', flush=True)
        time.sleep(random.uniform(0.01, 0.1))  # Random delay between 0.01 and 0.1 seconds 

If you only want the code to have the animation, you could get the colon index of the output when the character ':' appears for the first time.

def animation(output):
    index = output.find(':')  
    for i, char in enumerate(output):
        print(char, end='', flush=True)
        if i >= index:  
            time.sleep(random.uniform(0.01, 0.1))  # Random delay between 0.01 and 0.1 seconds 
javeowl
  • 9
  • 4