1
00:00:03,330 --> 00:00:06,570
What are output parsers and.

2
00:00:07,370 --> 00:00:11,750
How do we use output parsers in Lanchang?

3
00:00:12,710 --> 00:00:22,790
So we use output parsers to format a language models response into a different format from text, like

4
00:00:22,790 --> 00:00:24,380
for example, a Json dictionary.

5
00:00:24,380 --> 00:00:34,610
So if you remember, language models in the previous applications always give us responses in a unique

6
00:00:34,610 --> 00:00:35,510
format.

7
00:00:35,510 --> 00:00:39,560
They are providing, in most cases a string of text.

8
00:00:39,560 --> 00:00:40,280
Right?

9
00:00:40,580 --> 00:00:49,040
But sometimes we want our LM application to provide a response in a different format.

10
00:00:49,040 --> 00:00:58,640
So instead of having a string of text, we may want to have a Json dictionary or a Python list or whatever.

11
00:00:58,640 --> 00:00:59,150
Right.

12
00:00:59,150 --> 00:01:04,610
So this is what a output parsers are for.

13
00:01:05,269 --> 00:01:13,790
So in this exercise, we are going to create a very simple LM application without output parser and

14
00:01:13,790 --> 00:01:17,180
a very simple LM application with output parser.

15
00:01:17,180 --> 00:01:24,230
I don't want you to be, you know, more into the details in this case, because we are going to have

16
00:01:24,230 --> 00:01:33,530
additional exercises in, in, in, in which we are going to, uh, go deeper into the output parsers

17
00:01:33,530 --> 00:01:34,280
details.

18
00:01:34,280 --> 00:01:38,300
So right now I just want you to stay with the concept, okay.

19
00:01:38,300 --> 00:01:41,540
Just remember the, the concept of the output parser.

20
00:01:41,840 --> 00:01:43,910
Uh, and that's it.

21
00:01:43,910 --> 00:01:49,760
So let's start with the exercise with the code on the right.

22
00:01:49,760 --> 00:01:55,280
Initially we are going to create an application without an output parser.

23
00:01:55,490 --> 00:02:06,080
So we are going to try with our template to get the response from the language model in a particular

24
00:02:06,080 --> 00:02:07,100
format.

25
00:02:07,100 --> 00:02:19,700
So this application is going to uh, take a book review and give us three, uh, things one.

26
00:02:20,750 --> 00:02:25,550
Response we are looking for is if this review is positive or negative.

27
00:02:25,580 --> 00:02:33,860
The second response we are going to expect is if this review has any positive elements.

28
00:02:33,860 --> 00:02:41,720
And the third response we are going to expect is if this review, if this book review has any negative

29
00:02:41,720 --> 00:02:42,800
elements on it.

30
00:02:42,800 --> 00:02:43,310
Right.

31
00:02:43,310 --> 00:02:53,750
And what we are telling the LM model here is I want you to output three things in a particular format.

32
00:02:53,750 --> 00:03:01,790
So we are asking for a Json dictionary with these three labels and these three responses here.

33
00:03:02,240 --> 00:03:04,670
So we create a template.

34
00:03:05,420 --> 00:03:09,170
We add the user input.

35
00:03:09,170 --> 00:03:19,010
And when we combine this template with this user input and we apply it to the LM uh, instance we have

36
00:03:19,010 --> 00:03:23,090
created, we have a very nice response.

37
00:03:23,090 --> 00:03:23,690
Right.

38
00:03:23,690 --> 00:03:27,230
So this seems like a Json dictionary.

39
00:03:27,230 --> 00:03:35,270
But if we print the type of this response, we see that it is in fact a string of text.

40
00:03:36,050 --> 00:03:44,690
So if we want to deal with this, uh, as if it were a Json dictionary, we are going to have problems.

41
00:03:44,960 --> 00:03:51,530
So in order to solve this situation we use the output parsers.

42
00:03:52,480 --> 00:03:59,440
In this second exercise, we are going to do the same thing, but we are going to add an output parser.

43
00:03:59,440 --> 00:04:03,760
In this case, we are going to use the pedantic output parser.

44
00:04:03,760 --> 00:04:08,020
This is one of the most frequent parses we use with long chain.

45
00:04:08,590 --> 00:04:14,830
In order to use it, we have to import a few additional modules.

46
00:04:14,830 --> 00:04:25,090
And what we are going to do is we are going to tell long chain what format we want to have.

47
00:04:25,450 --> 00:04:33,010
So in this initial class, we are defining the target format we want to have.

48
00:04:33,460 --> 00:04:40,270
And we follow the same procedure of the first exercise.

49
00:04:40,270 --> 00:04:47,410
Except when we define our template we are including and we are including an additional variable.

50
00:04:47,410 --> 00:04:55,450
This format instructions variable is an additional variable that we define as a partial variable in

51
00:04:55,450 --> 00:04:57,130
the prompt template definition.

52
00:04:57,130 --> 00:05:05,500
And you will see that what this variable is doing is it's saying okay, this response is going to be

53
00:05:05,500 --> 00:05:09,970
formatted as the output parser says.

54
00:05:09,970 --> 00:05:16,600
The output parser is the name of the variable that we have decided to call.

55
00:05:16,600 --> 00:05:17,860
Output parser.

56
00:05:17,860 --> 00:05:21,700
The pedantic output parser we have created.

57
00:05:21,700 --> 00:05:22,300
Okay.

58
00:05:22,300 --> 00:05:26,650
And in this definition what we are saying, we are just saying this is okay.

59
00:05:26,650 --> 00:05:33,280
I want you to create a pedantic output parser with this class definition.

60
00:05:33,700 --> 00:05:34,360
Okay.

61
00:05:34,450 --> 00:05:40,690
So let's let's see the result and you will understand better this class definition.

62
00:05:42,000 --> 00:05:47,640
So what we see here is a the question.

63
00:05:48,060 --> 00:05:51,030
The final problem we call it question.

64
00:05:51,030 --> 00:05:58,050
And what we do as usual is okay, we are combining the template with the user input.

65
00:05:58,050 --> 00:06:02,160
And then we are sending this question to our LM.

66
00:06:02,970 --> 00:06:10,860
In this case this response is going to be a.

67
00:06:11,250 --> 00:06:16,290
We are going to use the output parser to change this response.

68
00:06:16,530 --> 00:06:24,330
And this original response when we apply the output parser is not going to be a string of text.

69
00:06:24,330 --> 00:06:26,910
Now it's going to be a class.

70
00:06:27,210 --> 00:06:32,100
And this is a class that we can convert into a Json object.

71
00:06:32,100 --> 00:06:39,150
So instead of convert this into a Json dictionary excuse me, we are going to follow a couple of steps.

72
00:06:39,150 --> 00:06:48,810
First we apply the model dump Json function and then we apply the Json loads.

73
00:06:48,810 --> 00:06:50,280
So it's a couple of steps.

74
00:06:50,280 --> 00:06:57,360
But once we complete these two steps we have in fact a Python dictionary.

75
00:06:57,870 --> 00:06:58,350
Right.

76
00:06:58,350 --> 00:07:03,750
So we can start working with this as a Json dictionary itself.

77
00:07:04,810 --> 00:07:06,850
So I don't want you to go.

78
00:07:08,020 --> 00:07:10,870
Alerting to the details of these exercises.

79
00:07:10,870 --> 00:07:18,850
I just want to I just want you to, uh, to end up with the concept of output parser.

80
00:07:18,850 --> 00:07:29,800
So output parser is a tool we use in order to transform the format of the initial response provided

81
00:07:29,800 --> 00:07:31,630
by the language model.

82
00:07:31,630 --> 00:07:32,650
That's it.

83
00:07:32,650 --> 00:07:39,160
And then you will see in next exercises how to deal with output parsers in detail.

