From the course: OpenAI API for Python Developers

Solution: Create a funny chatbot - part 2

From the course: OpenAI API for Python Developers

Solution: Create a funny chatbot - part 2

(upbeat music) - [Instructor] So let's continue with our funny chatbot challenge, and that's going to be part two. And you'll see that I have provided with an extra persona option if you'd like. And we're going to specify that you are a skilled standup comedian. Same below. You're going to say you are a skilled standup comedian with a knack for telling one to two sentence funny stories. And this is just to tell that we want you to act this way. And I am also providing with specific instructions as to how long we expect the completions to be. So we know that we can expect short but funny stories from the bot. So let's go here to complete our function here, which is to generate chat completions. And first we're going to create a message object. Remember that this is going to be a response with this format. We're going to access choices, then the first object, and then that's going to be message like this. And the next step will be to then add it to the list of messages, so we can add it and then send it as an input the next time around. And remember that the list of messages comprises the entire chat dialogue. And we're going to do the same here, line 31, when we add the user input, so whatever we type in the text input, it's going to be added as well as a new message object. The next step will be to obviously return the message. So that's going to be message content. So we can actually read the message, I mean the joke, in the user interface. So let's go here on the main.PY. As soon as you press send, which is the submit button right here, we're going to generate a new completion, or I should say a joke. Generate chat completion. And we pass as an argument, their user input, so whatever we type inside the text inputs, and the next step will be to then display the message or the joke. Let's go try this out. We go back to the browser and we're going to try a joke. Tell me a joke about something random like cars, for example. And we can see that this is running, although we don't see anything happening. "Why did the bicycle lose the race against the car? "Because it couldn't handle the cyclo-nuff speed." Alright, so I didn't really get it, but we can try something else. But before we try again, I'd like to also add something on the screen, so we don't only stare at a blank screen. So for that I'm going to add here a feature which is provided by Streamlit, which is a spinner. So we know that we can expect an answer as soon as this is finished, like here. So we know that this is thinking or loading, and I'm going to write something like, "Wait for it," so we know that we have to wait for it before we can actually read the joke for the funny chatbot. So let's try that again, and this time I'm going to ask a question about, tell me a joke about French people. I am French myself, so I'm not going to take any offense to that. I'm going to click send, and you see that we have the spinner starting. "Why don't French people like playing cards? "Because they always think the game is too dé-coupélt." Alright, let me try another one. French people, I'm just going to type. I didn't get it. Or maybe I'm too French? "Wait for it." Okay, so let's wait. "Why did the French chef only use one egg in his recipe? "Because one egg is un oeuf." Is enough. Okay, so that's a good one. "Is un oeuf." Oeuf is actually singular for eggs in French, and it's just saying that one egg is enough to make an omelet, so that's a good one. Alright, so let's try another one. So this time we're going to ask to tell a joke about chatbots. Alright, so we know that chatbots can easily act and reason like humans. However, I'm sure that they're not going to be too sensitive to that, so we're going to try a joke about chatbots. Alright, so "Why did the chatbot bring a ladder to the comedy club? "It wanted to reach new byte-sized heights!" Okay, good. So we can see that this is very thoughtful and funny at the same time. So that was a pretty interesting conversation and demonstration. Chatbots have never been more popular. Of course, chatbots powered by the GPT models can do much more than entertain us by cracking jokes. Large language models are equipped to handle a wide range of tasks. Their capabilities and potential applications continue to expand rapidly with the advancements in conversational AI and language models training, leading to an even greater integrations to more substantial and practical applications with the goal to enhance interactions between humans and computers, and transform several aspects of our society, like customer service, personal assistance, and even our work habits and daily lives. In a more advanced part of the course, we'll have a chance to explore options to extend the capabilities of large language models.

Contents