From the course: OpenAI API for Python Developers

Solution: Create a funny chatbot - part 1

From the course: OpenAI API for Python Developers

Solution: Create a funny chatbot - part 1

(ambient music) - [Instructor] Now let's begin with our funny chat bot challenge. And the first thing that we would like to do is maybe to change this title to make it more personal and also funny. And my recommendation is also to check out the documentation for the library Streamlit, which is very simple to get started with and user friendly. There is even one section for generative AI with one code example to build a chatbot driven by AI. And here, for example, it's going to set this title. So we're going to do the same, actually, we're going to replace the one that we have for now. Let's go back to the project and I'm going to go ahead and replace this one. So that's going to be st, which is short for Streamlit, then title. And here I'm going to write Funny Chatbot App. And I'm going to go look for this imagery with tears of joy. Okay, so I'm going to look for this one, copy it, then go back to my project. And I'm going to replace this with this new emoji. Let's go back to the browser and then refresh. And we're going to be able to then read the new title. So that is a good beginning. The next step will be to then generate a completion. So every time that you press Send, you're going to send a prompt, a text input to the language model. And so remember the syntax for that. So here we're going to generate the completions by calling this method completions.create. We're going to specify a model, a language model, and also give a list of messages as an input. So let's copy all of that. We're going to add this code to our project, and this is where we're going to do that. You see that for now, I just put path, but we're going to complete by allowing to call this method which we have defined. So I put it here inside handles. We're going to look at it in a moment. So that's going to be generate_chat_completion. And we're going to pass the user inputs. So let's go check this one out. So for now, we don't have anything. And the goal is to, here we go, generate a new completion. I'm going to replace this with completion, and we're going to have a list of messages. And if you check these lines right here at the top of the module, you're going to find constants and also variables that are defined. With this first variable, which is messages that includes a system message. And this is where we're going to define our persona. But for now, let's just replace this with messages. And optional, you can also define set up settings for this completion by providing, so here, it is suggested by Copilot to define the max tokens. But I'm not going to put it, I'm going to leave room for the response to accept as many tokens as necessary. And after that, what we're going to do is to simply print the completion. And what we're going to do, remember that we need to define a system message. We're going to do that right away. And what we want, what we expect for this chatbot is to be funny, to act funny. So let's do that. Instead of just being a helpful assistant, we're going to be very specific. And I'm going to say that you are a skilled standup comedian with a quick, oh, that's not bad, with a knack for telling funny stories. I'm going to keep this one, this is a good suggestion. But you can be very creative with the type of persona and instructions that you give to the language model in order to act the way that you want. So we're going to keep this one, I think it's interesting and we're going to see the outputs from it. So for now, what we're going to do is to allow to pass here the user input. And we're just going to print the completion. So let's try that. Let's go back to our chatbot. I'm going to say, tell me a joke. I'm going to say something simple like bees, honeybees. Tell a joke about bees. Okay, so now we see that this is running. We're going to go back to our project because we should see something printing if everything is running properly. Here we go, so that's a long one, actually. We have a long joke that was generated by AI. Here we can expand, and I'm going to let you read through this, but for the next part, what we're going to do is to finish, because instead of just reading this in the common line interface, we would like to be able to enjoy this joke here from the browser. So we continue in part two.

Contents