Text Generation with OpenAI

Nov 14 2024 · Python 3.12, openAI 1.52, JupyterLab

Lesson 03: Basic Chat Completion with GPT-4o

Streaming in Chat Completion - Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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.

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)
AttributeError: 'Stream' object has no attribute 'choices'
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)

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."
**[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?
See forum comments
Cinema mode Download course materials from Github
Previous: OpenAI's Chat Completion API - Instruction Next: Basic Chat Completion with GPT-4o - Conclusion