In this basic application, we are going to extract structured data from a conversation.
So, uh, let's say that we have the text of our chat conversation in which a person talks about, uh,
his favorite song.
And we want our application, our LM application, to extract the names of the song and the singer and
to archive them, store them in a Json dictionary.
Right.
So in order to do that, we are going to use a couple of new things in landscape.
One is going to be response schema and the other is going to be an output parser.
So what are we going to do.
And you will see here in the code we are going to follow these five steps.
In the first step we are going to determine which data we want to extract.
We are going to tell and chain.
We want to extract the name of the singer and the name of the song.
In the second step, we are going to archive the the extracted data into a Json dictionary.
Well, in fact, what we are going to tell the long chain is the kind of structure a output we want
to have.
Then we are going to create the the prompt template, in this case a chat prompt template.
And we are going to ask the user for a the input.
Right.
So the user is going to tell us or we are going to have a text of a conversation where the user is talking
about his favorite singer and so on.
Right.
Then we are going to apply.
A the all this, uh, configuration into our model, and we are going to extract the data and archive
it in a Json format.
So these are the steps in code.
The first step as, as usual is to get the OpenAI API key from our m uh file.
Once we have that solved the first thing is to define our extraction goal.
This is what Lang chain calls, uh, to define their response schema.
Right.
And what we are going to do using the response schema component is just tell lang chain the kind of
information we want to extract from our text.
So we are telling lang chain, okay.
We want to extract the the name of the singer and we want to extract the name of the song.
Once we have that, once we won once launching.
Understand?
What are we looking for?
We are going to configure what is called the output parser.
So the output parser is the way to tell lang chain a.
What do we want to do once we have this information.
So once we have this data how do we want to organize it.
How do we want to store it.
And in this case we are telling Lang Chain that we want to store this information in a Json dictionary.
How do we say that to Nancy?
We are saying this importing the component structure output parser.
This is a predefined output parser from lang chain.
And if we check what kind of format has this uh output parser associated, you will see that we we have
a this answer, the output should be blah blah blah blah blah.
In short, the output is going to be a structure in a Json vocabulary.
Right.
So these initial steps are going to tell lang chain okay.
This is the data we are looking to extract.
And this is the way we want to store this data after we have them.
Once we have that we are going to a manage, uh, you know, the conversation.
In order to manage the conversation we are going to create a chat prompt template.
Right.
So we are going to tell, uh, our chat model, we are going to tell our chat model a given a command
or a text or a conversation from the user.
Extract the artist and the song names.
So and we are associating here, you know, the, the the.
Output parser.
Uh, we are going to use and also the user prompt that we are going to receive.
So uh, once we have defined the chat prompt template with this information we can enter the user message.
This is going to be the input from the user.
And as you can see here we are a very simple one.
This could be a super complex conversation or a super complex piece of conversation.
Let's say, I don't know, a chat of 10,000, uh, messages, people, uh, social network, whatever
you want.
And in this case, it's a very simple sentence that says, I like the song New York, New York by Frank
Sinatra.
So once we use this, uh, user message in our, uh, chat prompt template.
We can start extracting the singer and the song.
So the only thing we need to do is to apply this, uh.
Chat, a prompt template we have created to the output parser we defined previously, and we are going
to store the result of this extraction in the variable with the same name.
If we print this variable, we will see that.
Now we have a Json dictionary that is storing two variables singer and song with the name of Frank Sinatra
and the name of the song.
So as you can see here in this very basic application, we can use Lang Chain to, uh, have a very
sophisticated, uh, task performed for us, which is extract for from a document, uh, only the kind
of information we are interested in.
And not just that, once we have that information, store it in a particular way.
In this case, it's going to be a structured way in a Json dictionary.