Hi, so far you ve learned about how to invoke different LLMs and how to create
and use prompt templates.
Now let s move on and talk about chains.
LLMs can be used for a variety of tasks, but they are often limited in what they
can do on their own.
For simple tasks or applications, you can
use LLMs in isolation.
For more complex tasks, it is often
necessary to chain or combine different components.
Chains in LinkedIn are like a series of
steps or actions.
Imagine it as a sequence of calls where
each step can involve talking to a language model using a tool or preparing data.
Chains allow us to combine multiple components together to create a single
coherent application.
For example, imagine an answering
questions app with retrieval and knowledge base.
This app is made of chains and these chains efficiently answer natural
language questions by retrieving relevant documents from a knowledge base and
leveraging an LLM to generate comprehensive and informative responses.
LinkedIn provides a standard interface for chains as well as some common
implementations of chains to make it easy to create and use chains.
There are simple, sequential and custom chains.
In this video, I ll show you how to
create and use a simple chain.
Let s import the necessary libraries.
Even though these libraries might have been imported in previous cells, I ll
reimport all of them here.
This ensures that the entire code within
this cell is self -contained and can be executed independently.
From LinkedIn Chains, I am importing LLMChain.
Now let s create the LLM object.
LLM equals shutOpenA.
Next, we ll set up the prompt.
I ll use the same prompt as before, no need to reinvent the wheel.
I ve already explained it in a previous video.
With the LLM and the prompt template in
place, let s create the chain.
Chain equals and I m calling the LLMChain constructor.
The LLM constructor sets up a simple chain and takes the prompt template,
fills it with user input and returns the response from the LLM.
The arguments are LLM equals LLM and prompt equals promptTemplate.
Finally, we ll run the chain, specifying the input variables output equals chain
.invoke of and the dictionary.
The input variables are virus and
language, so virus colon hsv and the language colon Spanish.
I am printing the output.
I m running the code, very well.
I ve got the response in Spanish.
If you d like to run the chain in verbose
mode to display intermediate logs, simply add verbose equals true to the chain constructor.
I am running it again.
Take a look here, this way we can observe
the prompt and other elements that might be helpful for debugging the application.
In this example, the prompt template requires two variables, but if it
requires only one, you can call chain .invoke with a string variable as an
argument instead of a example.
Template equals what is the capital of
country in curly braces and list the top three places to visit in that city.
Use bullet points.
And I m creating the prompt template.
I m also copying and pasting the code that creates the chain.
Country equals input of enterCountry and output equals chain .invoke of country
and print output of text.
Output is a dictionary and text is a key
of the dictionary.
The value of the text key contains the response.
I am running it.
And I am entering India.
It is running in the verbose mode.
Now, I am entering Italy.
Great, we are done.
In this video, we discussed simple
chains, which are the most basic type of chain.
They consist of a single LLM that is used
to perform a single task.
For example, a simple chain could be used
to generate text by passing a prompt to an LLM.
Simple chains are easy to create and use and also relatively efficient, however,
they are limited in what they can do.
For more complex tasks, it is often
necessary to use sequential chains.
We ll take a break and in the next video,
we ll talk about sequential chains.