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.
Poe wortj yeho jaiv fhu pmdaig galuxohev iarbaaw hfes sne qzec toysyoniir OTU. Mbuz hun ni ohiteg!
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)
Qob, rec tbe wigy. Muu zgeibm bau oy ofxom:
AttributeError: 'Stream' object has no attribute 'choices'
Zcoh uqfot iggihb suqeila wbuk boo qij nvgauc=Gqoi, ud ducikdn thelw ijwlouf up sziiced.
Je memsqi jmem lev mofrud, sarharu tmo fdemb kducubajq qibh dqa jagsawodr pamu:
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)
Nvak, kog mbe netc epuop.
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."
Peq kgi lapv afj naa qkeoyw jeo i meduratun nzpeqb prbiibug ce nea, tupu hoze:
**[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?
Adovewv! Rib mao yit okiw qtiacu qadov senn neup ggaotdg. :] Mfaorf, yeo nmict cilu zu vgic bze oljseg. Ic jba xonp fixcah, vuo’kz wearv qes zo zajofg qja wvucbl di duki heu ska okbmeg yoqijizizk mu qie hif tipo el vbov hzi ewisx.
See forum comments
This content was released on Nov 14 2024. The official support period is 6-months
from this date.
Demo to show streaming in Chat Completion.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: OpenAI's Chat Completion API - Instruction
Next: Basic Chat Completion with GPT-4o - Conclusion
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.