What are callbacks?
And, uh, how do we use callbacks with, uh, long chain applications?
So executing a callback is like running a function in the middle of a longer process.
Like chain offers several standard callbacks that allow us to interact with different phases of the
communication process with the LM.
And long chain also allows us to create our own callbacks.
So a callback is like a way to, uh, look under the hood, right?
So you have a process, a working process that is not functioning as you expect.
And you need to understand what is happening under the hood.
So a the callback is the way you have to look under the hood.
Callbacks are small functions that will allow you, you know, to take a look at what is happening in
the middle of the process that is not working well.
Right.
So in this exercise, we are not going to go, you know, very deep into details, but we are going
to see a few examples of callbacks, uh, basic callbacks, customized callbacks and also standard callbacks
for you to understand the concept and to see how to continue experimenting and studying in the long
chain documentation, if you have the necessity to do that.
So if you look at the code in the right side of the screen, first we as usual connect with dot m file.
We get the credentials in order to, uh, be able to communicate with the OpenAI API.
And then we are going to, uh, first of all, we are going to do a very, uh, normal operation LM
instance from template simple chain with the LM model and the prompt template and a user input.
In this case, these are small application that tells jokes about a topic.
In this case we are entering the topic bear.
And we have here the uh the the joke.
But in this case, if you, uh, notice, we are uh, we are importing the standard out callback handler.
So in the second, uh, section we are going to perform the same operation.
But in this case we are defining a callback even it is going to be like an empty callback.
So it's not going to do anything.
But we just creating we are just creating a callback.
And you will see that just by a introducing this callback in the in the chain execution, automatically
the LM is going to reply not just with the previous response, but also showing us what is happening
under the hood.
Okay, so if you remember, we can achieve this when we enter this this configuration verbose true.
You know in the chain definition.
So this is doing the same.
So just by uh telling the LM, hey I am using a callback here, even an empty callback, the LM knows
that it has to open the hood to show us what is going on.
Right.
So a little bit more advanced example.
Uh, in this example we are going to create a customized callback.
I'm not going to uh, go into the details of this callback definition.
Uh, you will, you will see a lot of different callback definitions.
In short, what this a callback is doing is telling the LM, I want you to give me all the data that
is, uh, happening, you know, during this process.
Right?
So we are using a different module, the base callback handler, and we use it when we define this custom,
uh, callback.
So as you see now before getting the response we are introducing a different input with the same chain.
And now we have a joke about whales.
But before that, the LM is giving us a lot of information about what is going on in the process.
Okay, so what is happening, the number of prompts being used, the model we are using, etc., etc..
Okay, so now you understand that the callback is getting information a that was previously hidden from
us.
Uh, so the callback has, has has opened the hood for us and is allowing us to see what is going on
and to understand if something is not going well.
So this is useful whenever we want to solve a problem or to fix a bug or.
Or an issue with our application, right?
A very popular callback is the one we can use to check the open AI cost that one particular operation
is having for us.
So if we, for example, want to, uh, see how much, uh, what is the cost in dollars and tokens of
this operation executing this chain?
We can, uh, wrap it in this with expression and use this module, the get OpenAI callback a.
During the execution.
So if we do that and print the CV, we are going to get the information you are seeing in the screen.
So the number of tokens used and the total cost in dollars, this is not $4.
As you know, this is like a fraction of a dollar is zero comma 0.0004 whatever.
Right.
Okay.
So the main goal of this exercise is for you to understand the concept of callbacks and for you to understand
that in the launching documentation, you can continue, uh, to study and learn and practice callbacks
if you are in that position.
This is something that you are not going to use unless you are focused on, uh, debugging or solving
a errors in your LM application.