-: Now that we have everything we need
to have our first conversations with the API,
let's see the step-by-step process.
Here, we have the main command lines
that will generate our interactions with OpenAI's models.
First, we instantiate the OpenAI client with this line.
After that, we have the main command, completion,
that will hold all the most important parameters,
such as model that the preset is GPT-3.5 Turbo.
Then we have the messages payload
that will hold our prompts, such as "Hello, chat,"
with the user role as we were in ChatGPT.
And finally, to print the final message,
we use this print line.
So now, let's jump to the code to see this in action.
So here, we instantiate the OpenAI client,
and then we create a completion request.
Here we have the model parameter
and the messages payload.
Here, we have three roles
that we are going to talk about later,
but here in the user role, that it's us,
we are going to say, "Hello, chat! Tell me a joke."
And we have two more parameters that are really important.
We have the temperature that controls the randomness
in the response generation.
Something closer to zero will be more straightforward
and something closer to one will be more creative.
And finally, we have the max tokens parameter
that will set a maximal length to our generated responses.
So let's run this.
Now, let's print our response.
First, we are going to print completion just like this
and see what we get.
We get our answer,
but with lots of metadata that we don't need,
so let's try printing completion.choices.
This has a little bit less metadata,
but still is more than we need,
so now, we are going to print completion like this,
and now we are going to get what we need.
"Sure, here's a classic one for you.
Why don't scientists trust atoms?
Because they make up everything."
So now, let's turn this into a function
to be a bit more easy to work with.
So here, we have the get response function
with the prompt and model parameters.
We have here the messages payload
and the completions command.
Here, we set model as a variable that we input above.
Messages also, that's we have here.
Temperature, we are going to set this to zero,
and max tokens to 400, and this function
will return the result, just as we want it.
So now we can run this,
and we can execute this little cell with the prompt we want,
and once we execute the function,
we are going to print the response
and have the response we need.
So now you may say, hey, this looks great,
but it doesn't have a memory,
and I don't have a little pretty chatbox
where I can input my prompts,
so let's see something a little bit more complex.
Make sure you have the IPyWidgets library for this,
so we are going to run this cell.
Don't worry if you don't follow this code.
Remember, we can have ChatGPT generate code for us
and explain it line by line, but here,
we have our pretty chatbox.
So now let's say "Hello, chat," and we have a response.
and say, "Hi! My name is Augusto."
Whoops. There we go.
Great, and we are going to ask him, "What's my name?"
to see if it has a memory.
It knows my name, it has a memory.
Now let's do another example.
"What is the sum of two plus two?"
It says it's four.
Okay, and let's ask, "And that minus three?"
All right, and the result is one, so it does have a memory.
Now that we had our first conversations with the API,
we are going to dive deeper into the roles
and see some examples.