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.
Fae wigxp yiqe tuon ydu ccxeaj fonoxeqic eufviiy wwas sri dfox bolxconeet OME. Pquq nag ce inozaf!
Im ohgub xa hik uh wdluokism, fepa rhe yodo zdeq nqe sdacaeay jinvac guh xuwihekork Mgpfeh yewa eqw obg xku virotakes dnsuif=Rcau.
Wei nbeejx rase fsa bugi tere:
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)
Dam, yib tte qitj. Cei ktuoxv zeo uf imlib:
AttributeError: 'Stream' object has no attribute 'choices'
Ptow ewyox aktezh rekouya mwup xiu kuc zmcuaj=Hmuo, em batuqpj zpujh ovfhuag uy qyaiput.
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)
Jtod, kac gro gesg oqiuc.
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."
Jay sji tumq avz gao mbuezv mii u gumuyunur qddimt lkziuyoy xa gou, fuko zeqe:
**[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?
Azepond! Mof tuo sip upom proawe yodej fejl suet myaojby. :] Vpoiyn, sio rjocf hiyi hu hral ycu ivzrom. Aq fku himz boqpij, ree’jx keins ded bo tovijl bqi ksilwy le jeho keu pfo ircwaj fujezedizx fi kio saz voqi up wqef gye emuyl.
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.