What are output parsers and.
How do we use output parsers in Lanchang?
So we use output parsers to format a language models response into a different format from text, like
for example, a Json dictionary.
So if you remember, language models in the previous applications always give us responses in a unique
format.
They are providing, in most cases a string of text.
Right?
But sometimes we want our LM application to provide a response in a different format.
So instead of having a string of text, we may want to have a Json dictionary or a Python list or whatever.
Right.
So this is what a output parsers are for.
So in this exercise, we are going to create a very simple LM application without output parser and
a very simple LM application with output parser.
I don't want you to be, you know, more into the details in this case, because we are going to have
additional exercises in, in, in, in which we are going to, uh, go deeper into the output parsers
details.
So right now I just want you to stay with the concept, okay.
Just remember the, the concept of the output parser.
Uh, and that's it.
So let's start with the exercise with the code on the right.
Initially we are going to create an application without an output parser.
So we are going to try with our template to get the response from the language model in a particular
format.
So this application is going to uh, take a book review and give us three, uh, things one.
Response we are looking for is if this review is positive or negative.
The second response we are going to expect is if this review has any positive elements.
And the third response we are going to expect is if this review, if this book review has any negative
elements on it.
Right.
And what we are telling the LM model here is I want you to output three things in a particular format.
So we are asking for a Json dictionary with these three labels and these three responses here.
So we create a template.
We add the user input.
And when we combine this template with this user input and we apply it to the LM uh, instance we have
created, we have a very nice response.
Right.
So this seems like a Json dictionary.
But if we print the type of this response, we see that it is in fact a string of text.
So if we want to deal with this, uh, as if it were a Json dictionary, we are going to have problems.
So in order to solve this situation we use the output parsers.
In this second exercise, we are going to do the same thing, but we are going to add an output parser.
In this case, we are going to use the pedantic output parser.
This is one of the most frequent parses we use with long chain.
In order to use it, we have to import a few additional modules.
And what we are going to do is we are going to tell long chain what format we want to have.
So in this initial class, we are defining the target format we want to have.
And we follow the same procedure of the first exercise.
Except when we define our template we are including and we are including an additional variable.
This format instructions variable is an additional variable that we define as a partial variable in
the prompt template definition.
And you will see that what this variable is doing is it's saying okay, this response is going to be
formatted as the output parser says.
The output parser is the name of the variable that we have decided to call.
Output parser.
The pedantic output parser we have created.
Okay.
And in this definition what we are saying, we are just saying this is okay.
I want you to create a pedantic output parser with this class definition.
Okay.
So let's let's see the result and you will understand better this class definition.
So what we see here is a the question.
The final problem we call it question.
And what we do as usual is okay, we are combining the template with the user input.
And then we are sending this question to our LM.
In this case this response is going to be a.
We are going to use the output parser to change this response.
And this original response when we apply the output parser is not going to be a string of text.
Now it's going to be a class.
And this is a class that we can convert into a Json object.
So instead of convert this into a Json dictionary excuse me, we are going to follow a couple of steps.
First we apply the model dump Json function and then we apply the Json loads.
So it's a couple of steps.
But once we complete these two steps we have in fact a Python dictionary.
Right.
So we can start working with this as a Json dictionary itself.
So I don't want you to go.
Alerting to the details of these exercises.
I just want to I just want you to, uh, to end up with the concept of output parser.
So output parser is a tool we use in order to transform the format of the initial response provided
by the language model.
That's it.
And then you will see in next exercises how to deal with output parsers in detail.