In the previous video, we talked about simple chains.
They are easy to create and use, but they are limited in what they can do.
For more complex tasks, it is often necessary to use sequential chains.
In this video, we will discuss sequential chains.
With sequential chains, you can make a
series of calls to one or more LLMs.
You can take the output from one chain
and use it as input to another chain.
There are two types of sequential chains,
simple sequential chains and the more general form of sequential chains.
The simple sequential chain represents a series of chains where each individual
chain has a single input and a single output, and the output of one step is
used as input to the next.
For example, you could create a simple
sequential chain that first asks a question to an LLM, then takes the answer
from the first LLM and uses it as input to a second LLM to get a more detailed answer.
Let's see how to create and use sequential chains.
I am importing all the required libraries and from lengthchain .chains, I am also
importing the SimpleSequentialChain class.
I am defining the first LLM which will
use GPT 3 .5 turbo.
Shut open AI, model name equals GPT 3 .5
turbo and the temperature equals 1 .2.
And the prompt template1 equals
promptTemplate .fromTemplate and the argument template equals, you are an
experienced scientist and Python programmer, write a function that implements the
concept of and concept in curly braces, concept is the dynamic part of the prompt.
I am creating the first chain, chain1 equals LLMchain of LLM equals LLM1 and
prompt equals promptTemplate1.
I am defining a second LLM which will use
the GPT 4 turbo model, GPT 4 turbo preview.
Note that at this moment, GPT 4 turbo is
available only to paid plans.
Use another model if you use the free plan.
I will use a lower temperature for a more precise response.
I am defining the second prompt template, promptTemplate .fromTemplate and the
argument will be template equals and the prompt.
Given the Python function and function in
curly braces, describe it as detailed as possible.
Maybe it is a good idea to switch the
temperatures around.
I will use 0 .5 for the first LLM and 1
.2 for the second one.
I want a precise and exact function, but
a creative description of it.
And the second chain, chain2 equals
LLMchain of LLM equals LLM2 and prompt equals promptTemplate2.
Now, I am defining a sequential chain using the two chains above.
The second chain takes the output which is a function from the first chain as input.
Overall chain equals simple sequential chain of chains equals and the list of chains.
And I am also adding verbose equals to.
I have mistyped two keywords, exprompt
and verbose.
Sorry, I am running the overall chain
specifying only the input variable for the first chain, output equals
overallchain .invoke and a data science concept, linear regression.
Take a look at the debug information that is being displayed.
You can see each prompt template and how the chains are invoked.
Now you know what chains are and what they can do.
You also learned that there are two types of chains, simple chains and sequential chains.
The type of chain you use will depend on the task you are trying to accomplish.
If you are trying to do a simple task, a simple chain might be enough, but if you
are trying to do a complex task, a sequential chain is what you need.