Hello everyone and welcome back to the Text Generation with OpenAI demos. This follows lesson 3, Basic chat completion with gpt-4o. In this video, you will use streaming in Chat Completion.
Demo
Imagine you want to create a silly trivia game called “Who Wants to be a Memeionaire??” You want a moderator who writes the questions and gives you choices. You also want fast responses from the moderator, and you don’t want the user to wait too long.
You might have seen the stream parameter earlier from the chat completion API. This can be useful!
In order to set up streaming, take the code from the previous lesson for generating Python code and add the parameter stream=True.
You should have the code here:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": "Write hello world in python"}
],
stream=True
)
print(response.choices[0].message.content)
Now, run the cell. You should see an error:
AttributeError: 'Stream' object has no attribute 'choices'
This error occurs because when you set stream=True, it returns chunk instead of choices.
To handle this new format, replace the print statement with the following code:
def print_chunks(response):
for chunk in response:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print_chunks(response)
Then, run the cell again.
Asking Trivia Questions
Now, to fit your use case of asking trivia questions. You should change the prompt. Go ahead and replace the content of the first message in messages in your code. Use the prompt here or write your own.
"You are the host of 'who wants to be a Memeionaire'. First, entertain the audience. Then create tension with the contestant by doing some banter.
Then ask one question and provide 4 options. Give one trivia question at a time."
Run the cell and you should see a generated script streamed to you, like here:
**[Host's Voice with Enthusiasm]**
🎉 Ladies and Gentlemen, welcome to the most exhilarating game on the internet, "Who Wants to be a Memeionaire!" 🎉
Today, we’re not just testing knowledge; we’re diving into the ocean of memes, trends, and internet craziness! Buckle up, because you’re in for a wild ride!
**[Turning to the Contestant]**
Now, let's meet our contestant! What’s your name, and where are you from? Ah, I see a sparkle in your eye—ready to meme your way to victory! But let’s be real, your brain must be racing faster than a cat on a Roomba! Are you nervous? Or are you just thinking about that sweet, sweet cash prize?
💸 Remember, the pressure is on, and the memes are waiting! Let’s see if you can take this not-so-serious game seriously!
**[Pausing for Dramatic Effect]**
Alright, are you ready for the first question? Remember, you have your lifelines, and either way, you’ll leave with some funny stories! Here we go!
**[Question Appears on Screen]**
**Question 1:** Which of the following memes features a distracted boyfriend?
A) Drakeposting
B) Hide the Pain Harold
C) Distracted Boyfriend
D) Grumpy Cat
Take a moment to think it over... What’s your answer?
Amazing! Now you can even create games with your friends. :] Though, you still have to know the answer. In the next lesson, you’ll learn how to modify the prompt to give you the answer separately so you can hide it from the users.