Hey, welcome.
In this video, you're going to cover how you can get structured data out directly from OpenAI using
their SDK package.
We'll look at how you can do this with tool cooling.
Also how you can do it using the response format parameters inside of OpenAI.
Firstly, structured outputs give almost 100% accuracy.
This is a massive improvement.
Rather than relying on prompting to give a structured data back, we're going to look at two different
ways that you can get structured outputs, both using function calling, which is using tools, and
also then as well as that using a new option called the response format parameter.
Firstly, we're going to import a couple of libraries and show you how to do it using the response format.
So we will import Pydantic base model the field.
We'll also import the OS and we'll also import the OpenAI package.
Now once you've done this you will need to set your environment key so you can do that directly here.
Or you can use an environment variable file or do it on your terminal.
Also we're going to create a Pydantic model that will be used to extract some structured data.
So we'll have a class of key points, which is a base model that will take key points, which is a list
of strings.
And each of these will be a field with a description equal to a list of key points extracted from the
text.
Okay.
So we've given the description of what that field should do.
And then we're going to set up an open AI client.
And then once we've done that we're then going to create a completion request.
Now to do the response format.
At the moment at this current time of filming, you will need to do the client beta chat completion
pass.
And then you will then provide a model that will allow for structured outputs via the API.
So we'll use GPT for a mini.
We will also do a Messages Python list.
And we will have one message in here, which is a role of user.
And the content of that message will be.
You know, I will say James is awesome and likes to learn code and new things.
And then notice here we're going to add something new that you might not have seen before in OpenAI,
which is this response format.
And we're providing a format here.
When we get our result back, we get a past chat completion.
So let's go and have a look at what this is.
You can see we've got this dot key points.
So if we go in here we have a look.
Notice how we've got the structured data that would allow us to form that Pydantic model.
So I could do something like this.
Key points dot model dot validate JSON.
And you can see here we've then created a Key Points object.
And we could store that directly inside of a variable.
So we could put like key points.
And then we can then print that out.
And now we've got our structured data that we've got back which is returning a key point Pydantic model.
So this is really great if you want to easily extract structured data from text, and you'll use it
with this response format as a parameter that goes into the parse function.
Be aware that you can inherit from other models.
So we could have key points and summary which could inherit from the key points.
And the key points could also then have a summary which will then have a description of.
We'll put a summary of this text.
And this can be quite useful because then we can change the response format to have key points and summary.
Go in here into the response format.
And then you'll see.
Now what we would do is we would validate that model with the key points and summary.
And you can see we've got both the key points.
And we also have a summary of that specific piece of text.
This is the quickest way to extract structured outputs using OpenAI's package.
We'll have a look at a secondary way that you can do this by using OpenAI's tools, which allow you
to do this whilst allowing you to answer original questions alongside structured outputs.
We're going to build a database query analyzer, which will extract structured data.
In order to get back that we can operate on our database.
We'll give OpenAI some information about the different types of tables that we have.
And also then we're going to build some dynamic pydantic models and enums so that we can encapsulate
all of this with tool use.
To start with, we need to import the enum.
We also need to import typing modules for union.
And then you'll also then going to need to import Pydantic and import OpenAI.
And from OpenAI we're also going to import OpenAI.
Now we're going to set up a client if you don't already.
And I'm just going to put up here.
Import OS If you don't already have an environment variable of your API key, you'll need to set this
here.
So remember to set your API key.
I already have one set, so I'm going to leave this cell and I'm not going to execute it.
The next thing that we're going to do is set up the specific type of table that we want OpenAI to query.
The first thing that we're going to do is we're going to build our table and column enums, so that
OpenAI knows easily what table and column values it's working with.
Firstly, we'll create a new class called table which is a string of enum.
And in there we're just going to put fruits is equal to fruits.
Then we'll create a second class called column which is also going to be a string enum.
And inside of that we're going to have a range of different column names such as the ID column name
color.
We're also going to have ripeness.
We're going to have a picture.
We're also going to have sold art.
Stored art.
And discarded art.
Next you're going to set up a different type of enum, which is called an operator.
And this is going to give the ability for you to extract what types of operations might need to be done
to filter the data from the SQL database.
So we're going to have a new class called operator which is a string of enum.
And then we're going to list some of the normal edge cases that you would find when using a SQL database,
such as equal to greater than is equal to greater than, we'll have less than is equal to less than.
We'll also have less.
Less than or equal to is equal to this.
And we'll also have greater than or equal to is equal to this string operator.
And we'll also have a not equal to operator as well.
Now that we have our operator we're also going to create a order by string enum.
And in that we're going to have two different types of keys.
For our enum we're going to have the ask which stands for ascending.
And we're also going to have the desk which stands for descending.
Okay cool.
So now that we've got the order done, the next thing that we're going to do is we're going to allow
for a dynamic value.
And that is basically just going to be a base model.
So a Pydantic based model, and they're going to have a column name which will be equal to a type of
string.
Okay.
So we've set up a dynamic value.
The final two pydantic models that we need is a condition which will be a base model.
And this is where we start integrating things which we've written before.
So we have a column which will be a string.
But we then also use the operator.
So the operator here will be equal to a type of operator.
And then as well as that we're going to have a value for this condition which will be either a string
int or dynamic value.
Okay.
Now we can write the final thing which is the query.
So we're going to have a query.
And in our query what we're going to take is the table name which is the type of table.
We are also going to take the columns which is a list of column.
And then we also then have the conditions which is a list of conditions.
And then we have a order by which is equal to an enum of order by.
Once we've written all these Pydantic models, we can then query this directly.
By using an open AI client.
We'll create an open AI client here.
Then the next thing that we're going to need to do is we're going to need to set up a completion.
So I'm going to type completion is equal to client dot chat dot completions dot create.
And then inside of here we're going to use the model GPT four zero.
And then we're going to have messages.
And then inside that we're going to have two Python dictionaries.
The first one which is the role system setting the system message.
And we have a content which is you are a helpful assistant that extracts information from a user request.
We're going to have a second one which says Role User content.
I want to query the fruits table to get all of the apples that are ripe and sold.
After 2024 0101.
We're then just going to print this message and see what comes out.
And you can see we've got this information here.
You'll notice is we haven't got the structured data back.
And that's because we also have to add the tools to load the tools properly.
We need to make a new tools.
And then we'll do OpenAI dot Pydantic function tool.
And then we put the query inside that.
And then what you'll see is OpenAI is wrapping that Pydantic model and it's making it into a tool call.
And then what you can see is if we look at the arguments, these are the arguments of the function.
Also if we have a look at the tool call in general, you can see we've got things like the id, the
function, the args that the function is going to be called.
And also we can look at that as well in greater detail.
So again you can see here these are the arguments.
These are the bits of structured data.
And returning that in a JSON format.
Once we load the import JSON we can get these out by wrapping this with Json.loads.
And you can see this is the structured data.
So it's decided it needs to query the table name.
It's going to use these columns and it's going to have these conditions.
So the column which is a type of operator is equal to and the value is going to be an apple.
It's also going to have another condition which is the ripeness operator is going to be equal to ripe.
And then we've also got a column which is sold at.
And the operator that we're going to use for that is greater than this value is 2024 0101.
So you can quite easily see how we've made our own domain specific language using this Pydantic query,
so that we're able to extract structured information from user requests about data in your back end.
Now, the reason why you might want to use this way of extracting structured data specifically using
Pydantic models is potentially you might use the same code to respond in a natural language way.
So for example, if you copy the code above.
Delete the client line.
Then after that, all I want you to do is change the user to.
Hi, how are you?
So we're going to print the completion dot choices zero dot message.
And what you'll see here is we get a chat completion message with the content.
So I can then for example do the content and you'll see that we're getting a message back from OpenAI.
So the fact is that you won't always call the tools, but these are able to be called if a specific
query comes in that is useful for that.
So that's why you would use the tools in this video.
You've learned about both the response format and also how to do tool use, which can either respond
to the user or it can extract structured data via tools.