What are chains?
And, uh, how do we use chains in large chain chains are sequences of operations.
We have two kinds of chains.
We have the predefined chains that long chain offers us, you know, to use.
And we have the customized chains we build ourselves.
So.
In this chapter.
I don't want to spend too much time because we are going to deal with a lot of things, and I do not
want to make it very long.
My purpose here is that you understand the main concepts, and that after this lesson you are able to
experiment, experiment and play around with the code and also with the land change documentation.
So normally a chain, a customised, a customised, a customised chain like the ones we are going to
build.
In this exercise a combines a language model, a prompt and usually another component, right?
So the concept of chain is instead of doing things separately one by one, we are going to build one
chain that includes all of the different steps.
So in this chapter we are going to build three types of chains.
And in every case we are going to do it in the traditional way and in the new way land chain is testing.
So in the in the last weeks, land chain is testing a new way of doing things with the framework, and
they call it the long chain expression language.
The main purpose of the long chain expression language, the new way of doing things in long chain.
The main purpose is to simplify things.
In my opinion, maybe this goal is not achieved yet.
So in my opinion, right now, long chain expression language is not more simple than the previous way
of doing things.
Long chain is accepting both ways.
So right now we can work one way and the other.
The important thing for me is to a communicate you that we have both ways and that you are able in this
and this exercise and in many others to compare both ways and to experiment with both ways.
So the most, uh, probable thing is that long chain is going to go further and further into the second
new way, right.
The chain expression language.
But this is still, you know, something that is not a 100% sure.
So, you know, it's important, in my opinion, right now to know both ways.
And then let's see what long chain is going to do next.
So the first thing we are going to do in the code here in the in the right side of the screen, as always,
is to connect with our dot m file where we have the credentials and the password.
So we can connect in this case with the API of OpenAI.
So we can use ChatGPT and the tools of OpenAI.
And we will start building a very simple chain, uh, using a module called LM chain from the chains
library of long chain.
Right.
So we are going to import chat OpenAI because we are going to work with a chat model, then chat prompt
template and then LM chain.
The first thing we do is we create an instance of the chat.
And we create a prompt template, in this case a chat prompt template.
What we are going to do in this exercise is to ask ChatGPT for a good name for a company that does something,
what is the company doing?
That is the user input we are going to get so the user can tell us, I want to do, I don't know whatever
product of service, right?
So depending on the product and service the user provide us, ChatGPT is going to invent a name for
the company, right?
So we have the chat model and we have the template.
Until now, we will combine the input of the user with the prompt template and we will create the final
prompt.
And with this final prompt we will, uh, execute, you know, the call to the chat model.
Right.
So remember we do elm parentheses prompt.
And we immediately have the response from the from ChatGPT.
Right.
In this case what we are doing is we are creating a chain.
So instead of calling step by step we are going to call the chain.
Right.
So with this uh function the function dot run, we are we just to, we just have to introduce the user
input.
And the chain uh, understands that this is the user input that has to be in the prompt template and
that has to use the chat model.
Right.
Because in the chain, as you can see here we are uh, defining the language model and the prompt template
we are going to use.
Okay.
So very easy very simple.
This chain.
How do we do this thing with the new, uh, long chain expression language.
We do it this way.
So instead of this way.
We are using this other way.
So this line is called pipe.
And what the pipes are doing are, uh, composing, you know, the chain.
So these pipes are telling, okay, use this prompt template with this model language.
And in this case we have added an output position.
Okay.
Instead of using dot run we are going to use dot invoke.
And you will see that the expression is a little bit different here, but very similar in the way of
doing things in the uh, old way and the new way, at least in concept.
Right.
What about the sequential chain?
So the important thing now is to understand the concept of what is a sequential chain.
So a sequential chain executes a number of chains one after the other.
Right.
So in this case we have designed a simple exercise where we get a text from the user and we translate
this text to Spanish.
And once we have this text translated then we summarize with the next chain.
We summarize this text.
And with the next chain we do another thing.
And with the next thing we do another thing.
And instead of calling each of these chains one after the other, we create one final sequential chain
that is going to do all the chains one by one.
Okay, so as you can see, this requires a new module that we have, uh, imported here, which is the
sequential chain module.
And it has a little bit different, uh, way of expressing things than the ways we are familiar.
So we, we see here this, uh, item chains we see here this item output variable.
So the input variable is just the variable of the first chain in order etc..
Right.
But I think the concept is very is very easy to get.
And the code is just a question of practice experiment and go to launching documentation if you have
any problem or doubt.
So since we have included variables true we can see what, uh, this uh, model language is doing in
the, in the background.
And as you can see here, what you see, you know, the results.
So this is the, the text that we include.
And here we have the Spanish translation.
Here we have the summary, the follow up message etc. etc..
So the new way of doing of doing this thing with the long chain expression language, we have included
here a couple of options.
The first one is with the simpler syntax, uh, we can, uh, use in this way.
We just have the output and then we have a second option, a little bit more sophisticated that, uh,
gets that gets us input and output.
Okay.
So as you can see, we make use of the pipe a couple of different ways, you know, to include the variables
the and the prompts using invoke instead of review.
Uh, look at this way of entering.
You know, the the input, the user input instead of the, the one we, we are used to and see also
here they are a little bit more advanced way of doing things with runnable pass through and see how
it is used in the example.
Okay.
So I'm going to leave you with the code which is a working code for you to start experimenting, start
trying, and also to go to the launching documentation where you will find more documentation about
this sequential chain.
If you are one day interested.
In my experience, the sequential chain and also the router chain are.
Seldom used, so they are not very frequently used.
A but if you find a case where they can be useful for you, you know where to find it.
Right.
So the router chain is a different concept.
So here I think is going to be very easy for you to understand with this example.
What we are trying to do is we are trying to tell ChatGPT that.
It has four hats he can use so he can be a rock, rock and roll expert.
He can be a politics expert, he can be a history expert, or he can be a sports expert.
So we are defining here how to behave in each of these scenarios.
And what we are trying to do is to get ChatGPT to decide which of these hats he has to use in order
to respond to our question.
So if we ask this LLM application about a rock and roll topic, he's going to use the rock and roll
hat in order to respond.
If we make a history question, then ChatGPT will, uh, decide to use this other roll in order to answer
this question.
Okay, so I think the concept is the most important thing to get.
That's the purpose of the outer chain.
So the router or I think is the is the key here.
And here you have how to do it in code.
So the I think the most uh complex thing you will see in the code is this for probably it is going to
be easier to understand if you replace p info with the word item.
So what this is doing is this is looping.
Through.
This, uh, prompt enforce uh, variable.
Right.
And this p info is in fact.
One item.
Okay, so for each of these items, you have to do this loop in order to create the destination chains
okay.
Little bit cumbersome, but I think having the code and the possibility to experiment will be the best
thing for you to master this other way.
And here you have also the a way to do it with long chain expression language.
Okay.
So remember in this case we are going to make a lot of pipes okay.
We are going to use a lot of pipes in the different operations we do okay.
So for me the most important thing in this lessons is to instead of going, you know, detail by detail
is that you understand the concept that, you know, here you have the working code and that you can
refer to the long chain documentation whenever you want to experiment, no more, etc., etc..
Okay.
So this is the important goal for this chapter.