back to indexLLM Chains using GPT 3.5 and other LLMs — LangChain #3
Chapters
0:0 What are Langchain chains?
1:1 LLMChain
5:14 Utility chains walkthrough
10:25 All generic chains in langchain
14:7 Langchain hub
15:10 Other chains in langchain
00:00:06.000 |
chains are a pretty big component of Langchain. 00:00:16.980 |
Now, today is going to be a little bit different. 00:00:19.440 |
We have Francisco, who is one of the contributors 00:00:35.000 |
I'll give you a quick overview before handing it over. 00:00:40.520 |
around different components in the Langchain library. 00:00:45.400 |
And more specifically, primitives within the library. 00:00:57.000 |
utilities within the library, and even other chains. 00:01:13.880 |
we'd actually use a prompt template object as well. 00:01:30.520 |
and putting it into first primitive of the chain, 00:01:36.080 |
And then the next step is this prompt template 00:01:42.880 |
will now be passed across to our large-language model, okay? 00:01:47.400 |
And the large-language model will, of course, 00:01:56.960 |
And yeah, I mean, you've seen it, it's super simple. 00:02:12.400 |
create our prompt template, as I just described, 00:02:17.200 |
So if we run that, we should be able to get an output. 00:02:20.240 |
So we're going to run this here, llmchain.run, 00:02:26.480 |
And it's going to return as a bullet point list 00:02:28.960 |
because in this prompt template that we use here, 00:02:36.320 |
So despite only running this single function, 00:02:43.320 |
Now, as I said, this is one of the simplest chains 00:02:48.680 |
And we can actually see that in the docs here. 00:02:50.800 |
So if we go over to the left, we have chains, 00:03:07.240 |
which is, as you might have guessed from the name, 00:03:10.960 |
They are more or less used to build other chains 00:03:14.360 |
You wouldn't necessarily use them by themselves 00:03:17.440 |
because they're not built for any particular purpose. 00:03:20.040 |
But nonetheless, you might find yourself using them 00:03:22.280 |
as I just demonstrated with the Lodged Image Model chain. 00:03:28.920 |
But typically, I'm using these as parts of other chains. 00:03:34.640 |
which is super useful when we are doing anything 00:03:42.840 |
summarization, or any retrieval augmented use case. 00:03:53.720 |
that consist of usually a Lodged Image Model chain 00:03:57.720 |
alongside another specific line chain utility. 00:04:16.720 |
which are the generic chains and the utility chains. 00:04:20.520 |
Now, what I'm going to do is leave you there, 00:04:22.320 |
and I'm going to let Francisco take you through these chains 00:04:31.880 |
and also have a look at what the generic chains are 00:04:35.640 |
and give a couple of cool examples on actually using them, 00:04:40.440 |
which I think rarely gets across what they are useful for. 00:04:45.000 |
So without any further ado, let me hand over to Francisco. 00:04:51.200 |
As James said, I am a contributor to Lankchain. 00:04:55.400 |
one of the most powerful libraries out there. 00:05:04.120 |
so we know how we can use them for our specific use case. 00:05:11.200 |
We will be using the OpenAI LLM as the LLM in this notebook. 00:05:16.040 |
So you will need to set your OpenAI API key right here. 00:05:19.920 |
I have already set it up so I can initialize my LLM there. 00:05:23.800 |
And we will be using this count tokens function, 00:05:27.080 |
which will really let us know how many tokens we are using 00:05:33.040 |
And this is important because OpenAI charges us 00:05:37.040 |
So we really want to keep track of these tokens 00:05:40.360 |
when we are making many experiments with Lankchain. 00:05:43.120 |
So we will be diving into one example for the utility chains 00:05:47.840 |
to really understand what a utility chain is under the hood. 00:05:51.160 |
And the example chain we'll be using is the LLM math chain. 00:06:07.600 |
And if we run this, we will be running the LLM math chain 00:06:20.560 |
because the LLM doesn't know how to do complex math. 00:06:24.080 |
So we need to help it to be able to return accurate results. 00:06:33.840 |
We are computing that question through the LLM 00:06:36.480 |
and we are getting some Python code in return. 00:06:42.760 |
And we can see that Python code is being generated 00:06:54.080 |
And prompts are really important for utility chains 00:06:56.640 |
because utility chains serve a very specific purpose. 00:07:02.560 |
will tell the chain how exactly it needs to perform 00:07:06.680 |
the utility purpose that we have for that chain. 00:07:15.640 |
because this prompt will give it very precise instructions. 00:07:23.840 |
As we can see here, we can always print a chain's prompt 00:07:29.240 |
and then the template attribute from the prompt. 00:07:31.800 |
So here we can see that we're telling the LLM 00:07:50.800 |
because it knows that it cannot do really complex math. 00:08:18.600 |
which is different and wrong from our right answer, 00:08:26.880 |
and we don't do this intermediate Python code, 00:08:46.320 |
we can force the LLM to avoid common pitfalls. 00:08:49.200 |
And this can be done by explicitly programming it 00:09:03.160 |
to see how this Python code is being used then. 00:09:08.040 |
we can see here that if the LLM returned Python code, 00:09:17.280 |
and then give that code as an answer right here. 00:09:21.040 |
And if not, we will just output that answer directly. 00:09:24.400 |
So here it's interesting that we can handle both cases. 00:09:38.400 |
As we can see, there are several other chains 00:09:43.760 |
There's the SQL chain, which computes SQL commands 00:09:47.560 |
and can build SQL commands for natural language queries. 00:09:53.600 |
which helps us make correct API calls to a specific API 00:09:58.240 |
by giving the LLM the documentation for that API. 00:10:04.280 |
which helps us create bash commands on the fly. 00:10:08.720 |
and we really encourage you to check them out on your own 00:10:17.400 |
And with that documentation and those notebooks, 00:10:20.900 |
you will be able to understand which utility chains 00:10:23.120 |
you might need for your specific application. 00:10:26.280 |
So now we are ready to go deep into generic chains, 00:10:30.060 |
and generic chains are a bit different than utility chains 00:10:37.000 |
but more as building blocks for building other chains. 00:10:43.200 |
and we will cover them all in the same example. 00:10:48.960 |
of combining these chains to build custom solutions 00:11:02.620 |
because extra spaces are being charged for us as tokens, 00:11:06.780 |
and we don't want to spend extra with dirty text. 00:11:22.500 |
So basically a transform chain just gets some inputs, 00:11:30.820 |
that the transform chain does not have an LLM. 00:11:36.300 |
So we here, we will be building our clean extra spaces chain, 00:11:41.300 |
We don't need the count tokens here function, 00:11:48.740 |
with a lot of irregular spacing will be cleaned, 00:11:52.380 |
and we will have our clean text as an output. 00:11:55.360 |
So let's say now that we want to use this chain 00:11:58.660 |
to clean some text and then send it to an LLM. 00:12:06.660 |
of what we have given as an input to another style. 00:12:10.560 |
So say to write it in the style of a poet or a policeman. 00:12:14.620 |
So to do that, we will need to build our prompt template 00:12:27.060 |
And here is our style paraphrase chain with this LLM chain, 00:12:31.100 |
which is the second generic chain that we will be seeing. 00:12:37.260 |
that computes an LLM call with a prompt template. 00:12:56.780 |
Well, we can do that with a sequential chain. 00:12:58.720 |
So the sequential chain just receives a few chains 00:13:02.260 |
And we want to tell it which are the input variables 00:13:06.780 |
and now which output variables we are expecting. 00:13:11.100 |
And now we can give our final chain an input, 00:13:22.940 |
So let's say we have this definition of chains 00:13:29.420 |
lots of extra spacing that we don't really want. 00:13:32.460 |
But we also want it to be written in the style of, 00:14:07.700 |
So finally, just a small note on LangChainHub. 00:14:37.580 |
set the path for the chain you want to import. 00:14:40.620 |
This path, you can find it in the LangChainHub repository. 00:14:48.220 |
And here you can overwrite any of the default parameters 00:14:53.460 |
So for example, if you want to have the chain 00:15:00.900 |
and then you can see here that it's not verbose anymore. 00:15:21.180 |
If there's one thing that I'd like to point out, 00:15:22.980 |
it's that just now we only saw a couple of chains. 00:15:39.040 |
And okay, we've covered generic chains relatively well. 00:15:42.840 |
Combined documents chains, we haven't covered whatsoever. 00:15:45.820 |
And utility chains, we've covered a little bit. 00:15:52.960 |
but we haven't had a look at any of the other ones. 00:16:04.160 |
But the point being that there is a lot more potential