-: Okay, so great.
So you've got billing enabled, you have an API key,
and you're ready to get started using OpenAI's API.
What I want you to do is make sure you've downloaded
and cloned to the GitHub repository.
Then I want you to go
to the openai_features_and_functionality folder.
Then click on core_features_walkthrough.
We're gonna go through and just walk through a couple
of the core features.
I'm not gonna write any code,
but what we're gonna do is just go through
each of the core services
and look at what's happening roughly,
and then in the future lessons,
we'll dive into each individual service
in a bit more detail.
So installing a bunch of packages,
we're then importing the os module and OpenAI module.
Then we are creating a global variable for the model.
In our case, we're gonna be using gpt-4.1-mini
for most of the core features.
Now, you will need to put your API key in here,
so I'll leave that up to you to do that.
The first thing we're gonna have a look at is,
how do you generate text directly from OpenAI's API
using the responses API?
You'll see we have a prompt.
So, "Write a one-sentence bedtime story about a unicorn."
We then create our client.responses.create,
we put in the model that we want to use and the input.
And what you'll get back from that is this .output_text.
You can also as well control the model's parameters
by adding things like a temperature.
So the lower will give you more deterministic outputs
or very similar outputs again and again
and a higher temperature will give you more random outputs.
And you can go and run this cell
and you'll see you get two responses here.
The prompt for each one and the response.
And all we're doing is using that response.output_text
and that will give you some text
specifically from the responses API.
If we're having a look at the second example,
we're using something called structured outputs.
So you'll see we have the same client.responses.create,
we have our model, the input is we have a system message,
which remember is an instructional developer message
that you can put into the start of a conversation
and we're telling it that it's a generator AI,
convert the user's input into a UI,
and we're saying make a user profile form.
Now, we've got here is something called JSON Schema.
JSON Schema is a way for us to tell the model
what we want the structure of that data to be
when it comes back from the model's response.
You'll see we have this type of JSON Schema,
the name, the description, and we have the schema itself,
which is the JSON schema
from this bit all the way down to here.
And we also have this strict is True.
Now, if we go and run this,
then what we get back comes from a string.
So this output text is a string,
and we're then loading that in
directly from the json.loads package.
And you'll now see that we have some structured data
that's come out from OpenAI's API.
This can be also done using
a data validation package called pydantic.
So we can import a base model,
we can set up various models such as the step
where we have an explanation
and an output which have data types of string.
We have some math reasoning which has a list of steps
and a final answer.
And then we can use, for example,
the chat completions endpoint
with the client.beta.chat.completions.parse.
We pass in the model, the messages, and the model.
And then we also put the response format as well.
And what you'll see if you run this
is you get a structured pydantic object.
If I do math reasoning,
what you can see is we can access this .parse
and then we have a maths reasoning object
where we can do the .steps
and we can see here are all the different steps
that the model decided to take
when reasoning over that type of math problem.
We can go in and have a look at the first one, for example.
It's also possible to get ChatGPT
to put an image in here.
And then we will use the responses API with this input.
And we're putting in a role of user
and we're saying the content is,
"What is in this image,"
and we have this type input_image and the image_url.
And then when you run that block of code,
what you'll see is ChatGPT will analyze in real time
what is inside of that image.
You also have text to speech.
So we have this
client.audio.speech.with_streaming_response.create.
We have a different model for this called gpt-4o-mini-tts.
We choose a voice that we're interested in
and what text we would like
to be converted into an MP3 file.
And then what we do is have some instructions
that help to standardize how that voice sounds.
Then we use this response.stream_to_file
and we provide a file path of speech.mp3.
You'll then see if you look on the right,
you then have a speech file which you can then play.
AI: It's a wonderful day to build something people love.
-: That's awesome.
And we also have the ability when we have an MP3
to convert that MP three directly back into transcription.
So you'll see that we're just using the file
that we just already made.
And then we can take that file
and you can see we can easily convert that back into text.
The next concept is this idea of function calling.
So what you can have is a function.
You can set up a bunch of tools.
So in this scenario we only have a Python list
with one tool.
We then create an input message, which is a role of user
and has the content of, "What's the weather
like in Paris today?"
And we can tell ChatGPT that it has access to these tools.
And what you'll see is ChatGPT,
if we look at the response.output,
you'll see that it decided it wants to call a tool,
and then you as the developer will get access
to the tool call,
the arguments that it wants to call it with,
which will be under the .arguments
under this output[0],
and you can see here these are the arguments
that it wants to use, the latitude and longitude.
And then we call the function.
And after calling the function,
we add onto the input messages
the original tool call that it wanted to make,
and also the result of that tool call.
Then we can call ChatGPT again.
After calling it again, it will then respond in text,
"The current weather in Paris is 16 degrees Celsius."
So this basically allows your AI models
to have access to functions
so that they can do work outside of textual environments.
The next thing we have is reasoning models.
So you can use a more powerful model
if you're working on a very complicated task.
The only difference here is
that we've just switched the model name
and we're using o4-mini here.
Now, I will say this will take a little bit longer
to come back, and you won't get immediate responses.
So depending upon how fast you need to respond to your users
and what cost you need to do,
you might want to have a look at the trade-offs
between using reasoning models
versus using standard chat models.
You can see this took around 15.5 seconds to come back.
We also have this idea called embeddings,
which is basically a numerical representation
of what text is.
You can see we have a bunch of words
and then we can have a client.embeddings.create,
choosing a embedding model that we want to pick,
and the words that we want to turn into numerical numbers.
We then extract the embeddings
for each individual piece of embedding.
We then have a function to calculate the .product,
and we basically then create a similarity matrix
for all of the .products between all of the numbers
that get created.
And if you run this, for example,
you will see exactly what's happening here,
where we have a similarity matrix using the .product,
which is the cosine similarity,
and each embedding has around 3,072 dimensions.
There are 10 embeddings,
and this is the similarity matrix between all of them.
You can see, for example,
so king and king is obviously completely the same,
king and man has a 0.41 similarity.
You'll see if we look at, for example, banana versus orange,
that has a 45 similarity.
It has quite a good one with apple as well,
orange and apple,
but it has less similarity against men and women,
and therefore you can use this to see what type
of similar words, you know, are similar to other phrases.
So you can see king and queen are similar with 0.55
versus apple and banana that has a lower similarity.
King and apple even, again, has a lower similarity.
So embeddings are useful
for when we're doing different types of work
where we need to go beyond just using the text.
This is where we will turn the text into a numerical number
and then when we use that for downstream tasks.
Cool, I know this is just a whistle-stop tour
and we're gonna go into more of the features in detail,
but I just thought I have one notebook
that kind of summarizes all of the core functionality
that I would recommend looking at
when using OpenAI's platform.