1
00:00:03,570 --> 00:00:05,160
What are callbacks?

2
00:00:05,160 --> 00:00:11,280
And, uh, how do we use callbacks with, uh, long chain applications?

3
00:00:11,640 --> 00:00:18,690
So executing a callback is like running a function in the middle of a longer process.

4
00:00:19,530 --> 00:00:25,800
Like chain offers several standard callbacks that allow us to interact with different phases of the

5
00:00:25,800 --> 00:00:27,900
communication process with the LM.

6
00:00:28,410 --> 00:00:33,150
And long chain also allows us to create our own callbacks.

7
00:00:33,150 --> 00:00:39,330
So a callback is like a way to, uh, look under the hood, right?

8
00:00:39,330 --> 00:00:46,800
So you have a process, a working process that is not functioning as you expect.

9
00:00:46,800 --> 00:00:51,120
And you need to understand what is happening under the hood.

10
00:00:51,300 --> 00:00:58,380
So a the callback is the way you have to look under the hood.

11
00:00:58,380 --> 00:01:05,519
Callbacks are small functions that will allow you, you know, to take a look at what is happening in

12
00:01:05,519 --> 00:01:08,070
the middle of the process that is not working well.

13
00:01:08,100 --> 00:01:08,670
Right.

14
00:01:08,670 --> 00:01:16,020
So in this exercise, we are not going to go, you know, very deep into details, but we are going

15
00:01:16,020 --> 00:01:24,420
to see a few examples of callbacks, uh, basic callbacks, customized callbacks and also standard callbacks

16
00:01:24,420 --> 00:01:31,470
for you to understand the concept and to see how to continue experimenting and studying in the long

17
00:01:31,470 --> 00:01:34,380
chain documentation, if you have the necessity to do that.

18
00:01:34,380 --> 00:01:42,360
So if you look at the code in the right side of the screen, first we as usual connect with dot m file.

19
00:01:42,360 --> 00:01:49,050
We get the credentials in order to, uh, be able to communicate with the OpenAI API.

20
00:01:49,050 --> 00:02:01,470
And then we are going to, uh, first of all, we are going to do a very, uh, normal operation LM

21
00:02:01,470 --> 00:02:09,449
instance from template simple chain with the LM model and the prompt template and a user input.

22
00:02:09,539 --> 00:02:13,890
In this case, these are small application that tells jokes about a topic.

23
00:02:14,100 --> 00:02:16,620
In this case we are entering the topic bear.

24
00:02:16,620 --> 00:02:20,220
And we have here the uh the the joke.

25
00:02:20,220 --> 00:02:29,190
But in this case, if you, uh, notice, we are uh, we are importing the standard out callback handler.

26
00:02:29,490 --> 00:02:35,580
So in the second, uh, section we are going to perform the same operation.

27
00:02:35,580 --> 00:02:43,080
But in this case we are defining a callback even it is going to be like an empty callback.

28
00:02:43,080 --> 00:02:45,600
So it's not going to do anything.

29
00:02:45,600 --> 00:02:49,350
But we just creating we are just creating a callback.

30
00:02:49,350 --> 00:02:58,980
And you will see that just by a introducing this callback in the in the chain execution, automatically

31
00:02:58,980 --> 00:03:07,590
the LM is going to reply not just with the previous response, but also showing us what is happening

32
00:03:07,590 --> 00:03:08,790
under the hood.

33
00:03:08,820 --> 00:03:16,350
Okay, so if you remember, we can achieve this when we enter this this configuration verbose true.

34
00:03:16,380 --> 00:03:18,030
You know in the chain definition.

35
00:03:18,030 --> 00:03:19,770
So this is doing the same.

36
00:03:19,770 --> 00:03:28,110
So just by uh telling the LM, hey I am using a callback here, even an empty callback, the LM knows

37
00:03:28,110 --> 00:03:31,950
that it has to open the hood to show us what is going on.

38
00:03:31,950 --> 00:03:32,610
Right.

39
00:03:33,210 --> 00:03:37,020
So a little bit more advanced example.

40
00:03:37,620 --> 00:03:42,090
Uh, in this example we are going to create a customized callback.

41
00:03:42,090 --> 00:03:46,890
I'm not going to uh, go into the details of this callback definition.

42
00:03:47,070 --> 00:03:51,150
Uh, you will, you will see a lot of different callback definitions.

43
00:03:51,150 --> 00:03:58,680
In short, what this a callback is doing is telling the LM, I want you to give me all the data that

44
00:03:58,680 --> 00:04:02,160
is, uh, happening, you know, during this process.

45
00:04:02,160 --> 00:04:02,580
Right?

46
00:04:02,580 --> 00:04:10,980
So we are using a different module, the base callback handler, and we use it when we define this custom,

47
00:04:10,980 --> 00:04:12,420
uh, callback.

48
00:04:12,420 --> 00:04:20,579
So as you see now before getting the response we are introducing a different input with the same chain.

49
00:04:20,579 --> 00:04:22,860
And now we have a joke about whales.

50
00:04:22,860 --> 00:04:29,670
But before that, the LM is giving us a lot of information about what is going on in the process.

51
00:04:29,670 --> 00:04:36,150
Okay, so what is happening, the number of prompts being used, the model we are using, etc., etc..

52
00:04:36,150 --> 00:04:46,290
Okay, so now you understand that the callback is getting information a that was previously hidden from

53
00:04:46,290 --> 00:04:46,920
us.

54
00:04:47,130 --> 00:04:54,810
Uh, so the callback has, has has opened the hood for us and is allowing us to see what is going on

55
00:04:54,810 --> 00:04:58,020
and to understand if something is not going well.

56
00:04:58,020 --> 00:05:03,180
So this is useful whenever we want to solve a problem or to fix a bug or.

57
00:05:03,300 --> 00:05:05,490
Or an issue with our application, right?

58
00:05:06,390 --> 00:05:16,740
A very popular callback is the one we can use to check the open AI cost that one particular operation

59
00:05:16,740 --> 00:05:17,940
is having for us.

60
00:05:17,940 --> 00:05:29,880
So if we, for example, want to, uh, see how much, uh, what is the cost in dollars and tokens of

61
00:05:29,880 --> 00:05:32,730
this operation executing this chain?

62
00:05:32,730 --> 00:05:43,590
We can, uh, wrap it in this with expression and use this module, the get OpenAI callback a.

63
00:05:44,370 --> 00:05:46,140
During the execution.

64
00:05:46,140 --> 00:05:54,570
So if we do that and print the CV, we are going to get the information you are seeing in the screen.

65
00:05:54,570 --> 00:06:01,290
So the number of tokens used and the total cost in dollars, this is not $4.

66
00:06:01,290 --> 00:06:07,080
As you know, this is like a fraction of a dollar is zero comma 0.0004 whatever.

67
00:06:07,080 --> 00:06:07,530
Right.

68
00:06:07,530 --> 00:06:08,070
Okay.

69
00:06:08,070 --> 00:06:17,400
So the main goal of this exercise is for you to understand the concept of callbacks and for you to understand

70
00:06:17,400 --> 00:06:25,620
that in the launching documentation, you can continue, uh, to study and learn and practice callbacks

71
00:06:25,620 --> 00:06:27,240
if you are in that position.

72
00:06:27,240 --> 00:06:35,820
This is something that you are not going to use unless you are focused on, uh, debugging or solving

73
00:06:35,820 --> 00:06:39,870
a errors in your LM application.

