So in this chapter we are going to build another basic rug application.
But in this case with an advanced output parser.
So until now we have used in the previous exercises are very simple output parser called structured
output parsers.
If you remember, that parser was used to format the output into a Json dictionary.
The structured output parser is a very simple parser that can only support strings and do not provide
options for other data types, such as lists or integers.
So in this case, we are going to uh, to we want to test an output parser that is capable of using
other types of inputs besides strings and validating the output format format.
So we are going to use the most frequent, the most popular parser in the long chain community, which
is Pydantic output parser, which is an advanced parser that admits many data types and other features
like validators.
We will see.
So.
The process we are going to follow is this one you have in the left side of the screen, and here in
the code we are going to go step by step.
So first we are going to load the necessary modules.
So the pedantic output parser.
So a few modules from pedantic library and one module from the Typekit library.
The first thing we are going to do is to define the desired output data structure.
So with this class definition, what we are saying is what kind of answer do we want?
So as you remember, the LM is going to provide the initial answer in one simple format.
And we use the output parsers to transform this initial format in the format we want.
So in this case we are defining a class with the format we want and the validators we want.
So in this case what we are saying is okay, I want that your output is in two lists.
The first list is a list of substitute words, and the second list is a list of reasons why you think
these words fit.
You will see why we say this.
You will see the purpose of the application.
We are, uh, we are building.
So the first thing here, the first part of this class is okay, I want these two things in this format.
I want words as a list and I want reasons as a list.
And then I'm telling I'm giving you two validation criterias.
For the first item, I want you to give me a I am going to give you this, uh, validator criteria.
If this validator validation criteria is not met, you are going to throw an error.
The first criteria is that the words cannot start with a number.
And the second validation criteria is that the reasons you have in the second list have to end with
a dot.
If any of these criteria is not met, you are going to throw a error message.
So this is the way we define the output format format we want and the validation criteria we want.
Then we are ready to create the parser.
We create the parser applying the pedantic output parser module we downloaded okay.
So once we have our parser created we can determine the input.
So the purpose of this application is uh giving the the the LM application, uh sentence and ask the
LM application to replace one word in that sentence with a good substitute word.
So in order to determine the input, we are going to, uh, follow a process that we have been, uh,
following in previous applications.
So we are going to define a template.
A we are going to build a prompt template.
And once we have that, we are going to load the user input.
Here we are entering the user input manually, but in your application the user may enter this a input
you know, through a form or whatever.
Right.
So if you look at the user input you will immediately understand the purpose of this LM application
we are building.
So the user is giving us one sentence.
The sentence is the loyalty of the soldier was so great that even under severe torture, he refused
to betray his comrades.
So he's talking about a soldier in a particular situation.
And what he's telling is I want to substitute the word loyalty in this sentence.
So what we are asking the application is find.
Wars that can substitute loyalty and.
Keep the meaning of this sentence.
Okay.
So in order to do that.
We are creating this prompt template.
So in the prompt template the instructions we are giving the LM are offered a list of suggestions to
substitute the specified target word based on the present context and the reasoning for each word.
So in this template we are saying okay, this is the target word the user wants to replace.
This is the context or the sentence the user has provided, and this is what you have to do.
These format instructions is the new thing you will see here in this template.
And this is associated with the output parser we are using.
So you will see here that when we create the prompt template we are telling the the application okay
you are going to use this template.
And in this template you see that you have three variables.
These two variables are the classical input variables we use.
But this one here is what we call a partial variable that is associated with the output partial we are
using.
So in short, what we are telling here in our application is I want you to provide this answer in a
particular format.
And in order to understand what format you need to look in this variable.
So this variable called format instructions is telling the LM application that we are using a parser,
which is my parser.
And this parser is using this definition.
And this definition suggestions output as output structure is having all these conditions.
So we want two uh outputs words and reasons in this particular format as as list.
And we want this validation criteria.
And if these validation criterias are not met we are going to throw an error message okay.
So this is the a prompt template definition that we are using.
Once we have that ready we can create the LM instance as we have done many times before.
And we can start uh asking our application.
Okay.
So here we are saying okay use this user input and apply that to the LM instance you have created in
order to give me a response.
Okay.
So what we are using here is once you have this response in your regular format, now apply the parser
to the response in order to give me the response in the proper format.
And as you see here, a the application is giving us two lists.
One list is called words, the other list is called reasons.
If you look at the.
For my definition, we have one list called words and other lists called reasons.
In the first list, we have four words that the application thinks that can be good replacement for
loyalty, devotion, alliance, fidelity, and dedication.
So the application is telling us that he thinks she thinks that we can use devotion, fidelity, alliance
instead of loyalty in this sentence.
So we can say the devotion of the soldier was so great that even under severe, severe torture, he
refused Baroda or the allegiance of the soldier.
Okay, so the important thing here is not the response, but the format that the application is using
to give us the response.
And as you can see here, the application is giving us two lists.
If you remember, with a previous output parser, we were using a structured output parsers.
We were only able to uh provide an output into a Json dictionary.
So structured output parser is not able to provide two lists as as as a response.
He is only able to format the output into a Json dictionary, but if we use an advanced output parser
like pedantic output parser, we can do a more sophisticated things like a many output data types and
also other features like validators.
Okay, so this was an example of a little bit more advanced output parser we can use in launching.