From the course: OpenAI API for Python Developers
Making requests and generating chat completions
- [Instructor] Now we are ready to generate a response from the language model, and for that we're going to complete this part, which is to print the response for now, we're going to do that. And what we're going to do, let's go back here to the actual interface that we used to build the chat. This is where we're going to call this message, which is generate_chat_completion. And we're going to pass, as an argument, the user input. So whatever we say, we want to be able to send as a new message sent to the chat bots. And we expect a response back from the chat bots. And so the next step will be to, of course, append, because for now, what we have inside the list of messages is the system message, which is used to instruct the language model how to behave. So here, we're going to remember also to do messages, and append any new message, which is our message, as a role, user. So we're going to pass the user input as the content, and then pass the list of messages as an input inside this prompt. And remember that the language model here, the purpose is that it remembers the last input, so that's going to be like a continuous conversation. And after that, we're going to print the response. So let's go back to the documentations to understand how this works, right here in Chat Completions API, and right below, you're going to find an example of a response. So, inside the object, which is returned as a response, we're going to access choices, which is an array with one object, and another object, line six, which is the message, which is the response from the bot with the role, assistant. So we're going to do the same. And actually, I'm going to save this to response. No, actually I'm going to name it message, because this is the message object. So that's going to be a response, and we're going to access choices, and then it's message like this. So we're going to print the results, the message object results. We're going to try that. Let's go back to here, I think this is working just fine. So we're going to try this first example. So we're going to start a new chat app. It's going to generate something as soon as we type a new message. So for example, I'm going to ask a math question, like what is two and five? And I think that I am missing something. Let me start over. We're going to start this again. I'm going to select Start Chat app, and right away, it's going to offer me to give assistance. And you see that what we are printing right now is the actual object with the content, and the role, assistant. And we're going to ask a question, a math question, which is, what is 2 and 10? And we're going to see what is the results. And after that, what we're going to do is to actually format the response to make it look like a real, actual conversation, so let's update this. So you got the answer here, the numbers 2 and 10 are both integers, so this is giving us a description of what it is. Okay, great. So let's format the response. For example, we're going to use Colorama, remember that we're using this Fore, like here, like for example, we can display the response in green. And I'm going to, here, display the message as "Bot: ", and then it's going to be message and content, so we're going to read just the content after that. So I think it's fine, we're going to try that. And the other step, remember that we need to also append, so the message, to keep this, as it comprises the entire dialogue. And that's going to be used as a list of messages the next time around. So let's pass this as a new message object, which is appended to the list of messages, so the next time around it's going to be added again as an input. So, let's try that. I'm going to cut it and start again. I'm going to start a new chat conversation. Okay, perfect. So now I'm going to ask another question, like what is the capital of, so, earlier, we had asked something easy, like France, but I'm going to try with Nepal. Let's try that. Okay, so the capital of Nepal is Kathmandu. Perfect. So now I'm going to ask another question, like, what is the weather like in Nepal? Now we have this lengthy and detailed answer back from the bot, and looks like this is telling us that the weather in Nepal can vary depending on the region and altitude. So the thing is here, is that I was expecting a response, like, actually the language models don't have access to current data, so sometime it's going to tell you that, "I don't have access to real-time data as an AI assistant." So this is something to keep in mind, which is that the language models have limitations, that they don't have access to current real-time data. And something that you can do as well, so while we are here, is you can always put here the settings, like the temperature, or the max_tokens here. And with the temperature, you can actually control how deterministic are the responses between the outputs, meaning that you can expect variations between outputs. But the thing to remember with language models is that they don't have access to real time data, so let's get to that. Also, we're going to go back to the models. We're going to find what we call the training data, which is the training data up to the date when the language models were trained, meaning that anything passed this date is not going to show in the results. For example for the model, gpt-3.5-turbo, the training data was up to September, 2021. So this is something to keep in mind, is that, with the language model, there are limitations like this. So asking a question about the weather, for example, will be actually a limitation for the language model. But lucky for us, OpenAI provides ways and possibilities to expand the capacities and the capabilities of the language models, and we'll have a chance to get to that. But for now, let's focus on how to set the behavior and the persona of a chat bot. You can train your chat bot to play any role you want, and that's going to be our next challenge.
Contents
-
-
-
-
-
Introduction to conversational AI2m 26s
-
Project setup and overview3m 11s
-
Authentication and configuration2m 38s
-
Define a system message with instructions2m 11s
-
Making requests and generating chat completions6m 6s
-
Challenge: Create a funny chatbot2m 11s
-
Solution: Create a funny chatbot - part 14m 26s
-
Solution: Create a funny chatbot - part 24m 51s
-
-
-
-
-