Hey, in the previous video, we built this guy, which has three widgets.
This one here, this one here and this one here.
So this was the front end, basically.
Now we're ready to build the back end.
So the front end was built by this class class chat bot to window write.
We wrote this class in the previous video.
Let me fold it.
I'm not deleting it, just folding it.
So I have a better view here in the script.
Now the back end will be in this class.
And before we begin, I think it makes sense that we refactor this class.
Or right click here.
I'll select the class, right click, go to refactor and then go to move.
So I want to move this class to Bec and that PE basically.
Add to it.
So what I've just done is I deleted the class from here.
You could do that manually as well, if you like.
So if you do it manually, you would have to delete the class from here or cut it and paste it in this
new file back.
And that's P right.
Well, that's what I did.
So I use the refactor function in part to do this.
This makes more sense because this is the code that builds the gooey.
And this does the processing.
So it makes sense to have them separate.
So in this code now, we want to interact with the Open I API.
The interaction happens with the Open I library, which is something you need to install.
So install open eye.
Once this is installed, then you need an API key.
So for that you need to go to google, search for open ie API and you should go to open i dot com slash
API.
So this is the official website of Open AI, an organization that develops AI tools.
So, Chad, Djibouti is one of those tools.
And we will use The Da Vinci three model, which is a very advanced algorithm, and it's very similar
to the Chad Djibouti algorithm.
To be able to use the API, you need to sign up first for an account.
It's free, and then go to log in to log in to that account.
And so once you log in, you should see this page.
So this is a platform that open A.I. dot com.
So that's the overview page.
And now you need to get the API for the chat bots.
So you want to go to this personal and then you're going to go to view API keys.
Here you'll see a list of API keys you have created and to create a new one, you go to create a new
secret key and copy that.
So that's your new key.
Go to Python and then create a define init methods and to open AI dot.
API underscore key is equal to to double quotes and paste the API key there.
So open AI is the module we imported and API key is an attribute of that module.
So we declare the API key in here.
That's all we have to do in the init function.
Then we want to create a GetResponse method which will be responsible for getting a response from the
API.
So let's create a response variable.
To get a response, we'll do open AI, dot completion, dot create and inside this parentheses.
Now this will be a long list of parameters that this create method gets.
The first argument is the engine.
This comes as a string.
And this is the AI model we will use.
Text Da Vinci.
003.
So text minus the Vinci, -0 zero three, a comma.
Prompt is the second argument.
This will be the user input.
Right.
And let's have this here as a parameter user input.
So our other class, this class will call this chat bot class and the user will enter something in the
text input field here in this class.
Right?
So the user will enter something in here and that string is going to be sent to this method.
So similar in this code here in our guy, we will call this get response method method by providing
the user string as the value of the user input variable.
And so that value will be sent in the prompt.
Then we need to.
Provide a max tokens arguments set it to 4000.
What is this?
Well, if you want the bot to provide longer answers, you can set a maximum of 4080 tokens or tokens
are words.
So this means that the boat cannot provide more than 40 tokens.
If you want to provide shorter answers, perhaps you can set it to 2000.
So I would go for 3000.
That would be a good value comma.
And lastly, the temperature, I'll suggest a value of 0.5.
You can set the value from 0 to 1 if you are closer to zero.
So if you are giving a low temperature, that will produce more accurate answers, but they will be
less diverse.
So more rigid, if you provide a higher temperature, you will sacrifice a bit on accuracy, but the
answers will be more diverse, if that makes sense.
So 0.5 is a good value to have.
So that's the create methods.
It opens in here.
It closes in here.
And then after this, you need to extract the choices.
This will give you a list.
You get the first item of that list, and you extract the text out of that the text property and then
return the response.
And that's it.
That's the complete class.
So let's try it now.
I'll go and do.
If name is equal to main, instantiate the chat bot.
It doesn't take any arguments, so just parentheses there.
The response will be equal to chat bots.
Dot get re spawns and get response will get an argument that is the user inputs.
So for example, write a joke about birds and I'll print out the response here.
Of course later on it is the chat bot window which will call this chat bot class and it will call that
gets response methods.
But for now, let's keep it here so we make sure that our chat bot class is working fine.
So I just ran this backends py file.
I've got an attribute error.
Attribute error choice.
So choice is not an attribute of its object.
So actually it's choices.
So let's run again.
There we go.
That's the response of the chat board.
So the chat bot gave a joke in the form of a Q&A.
So there's a question, Oh, what did the bird say when it flew into the world?
Oh, I shouldn't have flown straight.
That's the joke.
So it seems to be working fine.
That's how you create a chat bot with open AI.
In the next video, we'll go ahead and connect this backend file with the main dot p file, So we connect
the chat bot with the chat bot window so that our chat bot window will function like a real chat bot.
Thanks.
I'll see you in the next video.