-: Hey, welcome back.
So, in this video, we're gonna have a look
at the Responses API in a little bit more detail.
What I would recommend doing is
going into the GitHub repository,
going to the openai_features_and_functionality.
You'll be working through
the responses_api_and_messages notebook.
Now, I'm gonna actually recommend that if you want,
you can create a new notebook and you can follow along
and get that muscle memory from typing some of the commands.
We'll keep some of these commands in here,
like the pip installs, so feel free to run those.
We're installing tiktoken and openai.
And we are also then gonna import the OpenAI package
and we'll set the model name to gpt-4.1-mini.
Now, at this point, you will need to set your api_key.
So remember to do that.
Okay, so the first thing we're gonna do is
we're gonna have our message history.
We're gonna create a Python list
and in there we are going to have one specific entry,
which will be a dictionary.
And you're gonna have a "role:" of "user", "content":.
We're gonna put that to "tell me a joke!"
Then we're gonna call the API
with response = client.responses.create().
And then here we're gonna put the model=MODEL.
We're also going to put the input
is gonna be equal to our history
and we're gonna have this store=False.
Now, to tell you what the store=False means,
it means that we won't naturally save the message history.
So, now, we can print the response.output_text
and you'll see that you get back a response.
So, here we go.
Sure! Here's one for you:
Why did the scarecrow win an award?
Because he was outstanding in his field. Okay, great.
If we are doing the store=False, this means
that the message history won't be stored on OpenAI server.
Okay, so the first thing that we wanna do is
we want to update the message history
with the assistant output/message.
And we can do that by updating our history variable
with a += sign.
And we'll do a list comprehension
over all of the response.output.
So we're gonna go for el in response.output.
We are then going to create a certain number of dictionaries
for each of these, and we'll have the "role":
being equal to el.role.
And we will have the "content": being equal to el.content.
After this, then we need to ask for another joke.
So we'll do history.append
and we will put in here "role": "user", "content":
"tell me another".
We will then do a second response
by doing a new variable called
second_response = client.responses.create.
We're gonna put the model=MODEL.
We're gonna put the input=history
and we're also going to put store=False.
Then we're going to print "Second joke",
second_response.output_text.
So we've got that joke
and then we have our second joke here.
Cool, so you'll see here
that when you are using the store=False,
you need to make sure that you update the message history
which came out from the previous assistant message.
We also then added another user message
and then we did our second_response against OpenAI.
If you're doing the store=False,
you will have to handle the message history.
Okay, so let's say we're gonna store the results.
This changes things.
So now, we're gonna use the previous_response_id
because now we're gonna store everything on OpenAI servers.
So, for example, we'll do
responses = client.responses.create().
We're gonna do model=MODEL,
and then we'll say input='tell me a joke'.
After this, I want you to then print out
the response.output_text.
So, once you've done that, then you'll get your joke.
Now, instead of actually storing it in our own Python list,
we're gonna have the second_response =
client.responses.create().
And then in here, we're then gonna have
the model=MODEL, just like we've been doing before.
And that model variable is defined at the top.
We're gonna have a new thing called the previous_response_id
as a new parameter into this method.
And then what we're gonna do is provide the old response.id.
So, when you get a response back
from a client of responses.create, you have a .id on there,
which is a unique identifier for this response.
So, we're gonna have a new input, which is gonna be a list,
and we'll say the "role": is equal to "user",
and the "content": is "explain why this is funny".
And then we're then gonna print out the second response
here just below.
And we're gonna do the .output_text from that.
Cool. And then rerun this.
And what you'll see is we get a joke being made,
and then after that we end up with a explanation
of why this joke is funny, right?
So, the key point here is
if you are not using the store=False,
you can reference the old response.id
and that will give you a unique identifier
so that you can manually stitch conversations
between different responses calls
without having to have a Python list
and storing the state yourself.
In the next video, we'll have a look
at the chat completions API
and how that defers versus the Responses API.
You might still sometimes use the chat completions API,
because OpenAI have said
that they will support this endpoint indefinitely.
Cool, I'll see you in the next video.