LangChain Basics Tutorial #2 Tools and Chains
Key Takeaways
This video tutorial covers the basics of Tools and Chains in LangChain, including PAL, LLMChains, and API tools, and demonstrates how to use them to develop applications in LangChain.
Full Transcript
alright welcome back to the second video in the Lang Chain video series in this video we're going to be looking at tools and chains so these are the key building blocks in langchi that you're going to be using for building a lot of the apps and a lot of the things that you're going to be doing so these are different things but you'll see that they interrelate quite a lot as well so first up what are tools in Lang chain tools are the individual components that Lang chain uses as links in the chain so these are a variety of sort of little sub modules that you can use when combined with a language model to do a task and that's what makes up a chain overall so that some of the example tools would be like the python read evaluate print Loop that's going on this allows you to basically export something out as code and run it as code and take the output the printed output from that and feed it back into a model with a chain like that you've got things like the search search engine API you've got wolf from alpha which allows you to query the Wolfram Alpha engine for facts and data and then bring that back in and use that in a a large model or you know in one of the other tools that you're using just like your query wall from alpha can also query things like Google search and the News API so these allow you to basically get external data and external results and feed them you know back into your app that you that you're using so what is a chain in Lang chain so a chain is really just you know made up of links of tools so you can kind of think of like each of the tools as being a link in an overall chain these can be as simple as linking a prompt template and a large language model and then you've got a llm chain which is one of the most common chains that you will see in Lang chain it also can be much more complicated so you can get things with multiple links going through multiple steps of a whole process which might include multiple large language models in there as well as a variety of different utility tools Etc the main thing to understand is that in a chain the output of one link is what's going to become the input of the next Link in there so when these chains are running you might take something as an output from a large language model feed that into a query of Wolfram Alpha bring that back run that again through a large model to formulate a response that you're going to give back to the user that's an example of a chain so there are three categories of chains currently we have generic change which are basically used for building other chains at utility chains this is where a lot of the utilities and tools actually are then you also have asynchronous chains for doing async functions and tasks like that so looking at the generic chains by far the most common chain that you will see is a large language model chain and this is basically you know going to take some sort of input format that with a prompt template run that through a large language model and return and this is the kind of chain that we looked at very briefly in the first video second kind of chain that you'll see in generic chains uh transformation chains so this allows you to basically take something and run some code in a function on the actual inputs or outputs of a of another chain or another Link in another chain so that could be simply taking the response back from a large language model and then running it through a regex to check if there's something in it or you're doing a variety of things like that and that can be you know both on the input or on the output for this and then the other generic chain is sequential chains so this is just basically multiple chains and allowing you to join them together so you'll find that while chains and are made up of individual tools they can also be made up of other chains that get joined together so utility chains is where a lot of the magic happens and there are a variety of these so this is not all of them there's there's a lot more than this and I expect that there'll be a lot more added over time as well the pound chain is a very interesting one and maybe I'll do a video just about this because this is actually based on an interesting paper of using a language model that that converts the reasoning of a question to python code and then allows you to run that and return that response back another interesting one is like the SQL database chain so this allows you to take natural language convert it to the SQL query and then return that information which you might then feed back in to another large language model chain with a different prompt and then take the output of that and give that back to your user so you see that this is a quite common thing to basically go through an llm chain go out to something else come back to another llm chain and then go back to the user The Bash chain is another sort of thing where it allows you to run bash commands the requests chain allows you to go out and request a particular HTML page and get the data from that and bring that and pass that back to the next Link in the chain and then you've got a variety of API chains which let you do things like query different apis that are already set up inside of Lang chain all right so let's jump into the code and look at how we can you know build some of these things with code okay in this notebook I'm going to go through the tools and then linking those tools to basically make chains I'll show you a few common chains and we'll look at some of the others as we go through as well so you want to make sure that you've got link chain installed you've got your libraries for the various large language models installed set up your API Keys Etc the first one is just going to be a really simple chain and this is by far the most common chain that you will see and this is basically just linking a large language model with a prompt and then feeding something with a prompt template and feeding something into this so here you can see I'm setting up the open AI DaVinci 3 Model we're going to set temperature to zero we're going to just set the max tokens a lot of these will be set by default if you know that this is a standard model that it defaults to I've got a little article in here so what I'm going to be doing is actually fact extraction so here I've basically taken an article all about coinbase and so this is quite a long article if we look at it it's 3 500 characters and what we want to do is basically extract down the key facts from this and then we're going to play around with that and try rewriting those facts into kind of new piece of content so first off we need our prompt template so our prompt template is going to basically take in the input that's what we've got up here and then we need the actual prompt so the prompt here is going to be extract the key facts out of this text don't include opinions give each fact a number and keep them for short sentences and then I'm going to basically just the input is going to be this text input that we've got here all right so making the chain is actually pretty simple we basically just say we're going to be using the large language model chain we pass in the large language model and then we pass in the prompt template we're going to be using so here I've got the fact extraction prompt and we're passing that in all right and then we can basically run it and you can see that after we run it sure enough this has gone through and it's done a pretty good job of getting out the facts from our article all right so you could play around with getting better prompts here there's certainly this is like the whole world of prompt engineering it's worth testing out different prompts for the kind of content that you want to use I but here enough it's done a pretty good job we've got 10 facts out of this that have each come from the article that we've got above there so now what we want to do is chain we're going to make a new one and then we're going to chain some of these things together so the next one I'm going to do is also a large language model chain and this is going to be taking those facts and rewriting them but we're going to rewrite them as if it was like an investor report here so you see here we're going to say okay you're a Goldman Sachs analyst take the following list of facts and use them to write a short paragraph for investors don't leave out key info we could also put probably should put in something there don't make up info as well but here's the facts that we're going to pass in so again this is a large language model chain we pass in the language model we're still using the original one we defined above we pass in the prompt template and then we can run this and you can see sure enough it's come back and it's written a pretty coherent nice little article there and it's way shorter than what we had before right so the idea here is that we're trying of doing a little bit of a kind of summary but we also want to maybe take those facts and put them into something and this is where you could even play with this just to give you an idea I've made another one of these that takes these facts and turns them into triples for a Knowledge Graph so here we're saying okay take the following list of facts and turn them into triples for a Knowledge Graph and you can I'm passing in the facts just like I passed in the facts that we had up there again Define your chain it's going to be a large language model it's going to have a prompt template and then you can see sure enough this has gone and made triples now I could have got it to do in a particular style like if I wanted to show these in d3js or something like that or it's some sort of format for neo4j or something like that I would be able to do that in that style to get the triples but just here without asking it for a specific style it's got the sense of that like coinbase released fourth quarter earnings coinbase generated and it's got revenue and maybe we would want to guide it a little bit more with this but just to show you some of the ideas that you could do with this thing I next up we want to chain these together so now we're actually taking a sort of small chain and chains can be made out of either tools or they can be made out of other chains so we're taking a small chain here that we've got or the couple of small chains that we've got up there the fact extractor and then the sort of investment analysis rewriter kind of thing and we're putting them together and we're just going to do that with our simple sequential chain all right so the simple sequential chain is just like a standard sort of sequential model in something like Keras or pie torch where you're basically just going from A to B to C you're not really doing anything fancy in there we've also got this idea of that the inputs from one will become the output sorry the outputs from one of them will become the inputs for the next chain so you can see here we've basically set the full chain we're going to have our Factory extraction chain and we're going to have our investor update chain here and now when I take the original article and run it through it's going to do both those things so you can see here now it's basically done the fact extraction and now it's done the rewriting and then it's finished the chain here so I this is one of the ways that I that we're able to link these things together rather than having to rewrite code to do to do one thing and then do the other thing Etc and we sure enough we can see okay if we take this the response out that we got we've got this our response back of what the investor update did even though we passed in the original article now so you could try this for testing the idea of using this versus like just a one summary chain or something if you wanted to incorporate the triples somewhere that's something that you could do with this kind of thing as well so by far the most common change that you will see are made up of just a large language model and a prompt template there are a bunch of other cool chains and tools though in Lang chain and the next one I'm going to show you is the pound math chain so I may make a video about this whole paper and the concepts in this but just let me quickly show you to show it here I so what this basically does is it uses a different large language model we're using one of the code models here and what we're going to basically do is this is going to be used for when we've got some kind of number problem here so this is an example from the Lang chain documentation so Jan has three times the number of pets you can see there's basically like a math equation and what the what we're going to be what we're basically prompting the model to do here and we're using an inbuilt prompt that's there but we're prompting it to basically take this written statement and turn it into a small python function which will then calculate the math rather than just guess it with a language model and then return the output for them so the idea here this one's from uh one of the flan papers the recent flan paper and I think this one is actually an interesting one so this is pretty simple math right the cafeteria had 23 apples if they used 20 for lunch and bought six more how many apples do they have the the issue with this is that if you've used the big language models yes they will probably get this right but if you're using something smaller if you're using even just like the T5 models the majority of the G5 models will get something like this wrong rather than just rely on one of those sort of to do it here we can basically use this pal system where we're just getting it to basically take this data and rewrite it and you can see here it's writing a python function it uses the doc string to talk to put in what we actually had up there we start off with Apple's initial so it's making just the variables and assigning them Apple's used apples bought and then apples left equals Apple's initial minus Apple's use plus Apple's bought sure enough then it gives us our result and it gives us the nice accurate result here so rather than rely on the language model to do this what we're doing is just getting the right language model to rewrite it in something that we can basically run in the python read evaluate print Loop and then we can take that output now we could take this output and then feed it back into another large language model and have it rephrase it conversationally so that it could tell you ah the amount of apples left is or we could just take the output straight from this module another example of a tool chain which is can be really useful are the API tool chains so here I'm just showing you the example of the one for for weather information so this basically we're setting up the API that we're going to be using for this and what this is going to do is it's going to take in our queries so we're basically just setting up large language mod by default this is going to be the DaVinci zero zero three we're setting up the docs for it so even the docs and then it's able to then write an API call based on those docs and that's what the this is going to output and then this is going to then query that API with that call and see the result we get back so here you can see I'm asking it what is the temperature in bedok Singapore in degrees Celsius and it writes this it writes this whole URL for basically querying this here let's work out that okay temperature unit should be Celsius and sure enough it basically gives us back the temperature right now in gives its location and the answer back so what it actually returned was right and then that has then gone into a larger in back into the language model to rewrite it in something rather than just a adjacent format into something that a user would understand here I can see we can also ask it a little bit more vague questions and obviously it can only answer what the API can give you back here it's it's basically giving this back and it's telling us yeah that okay the a number of things in this Json response indicate that it's raining the danger with this one that you need to be careful is that often the docs plus the uh the URL plus the jsons will go beyond the number of tokens that your large language model can handle so you can get errors if you're going above 4000 tokens on The DaVinci the the other thing too is to think about is that this is pretty expensive right if we're paying two cents per thousand tokens and we've just put in 4 000 tokens just to get back the weather or something this is not always the most efficient way to do it but it does show you that link Ching can do these things and you could write some of this to make your own API calls for something that you want all right in the next video we're going to be looking at the whole sort of chains around conversation and incorporating memory into those so I'll see you there don't forget if you find this information useful click subscribe so that you get told when the new videos are coming out thanks for watching
Original Description
Colab Code Notebook - https://rli.to/5eoj4
In this video, we jump into the Tools and Chains in LangChain. We look at what they are and specifically what tools like PAL, LLMChains, API tools do and how they can be used to get started developing applications in LangChain. If there are particular topics or parts of LangChain you would like me to cover then leave a note in the comments.
My Links:
Twitter - https://twitter.com/Sam_Witteveen
Linkedin - https://www.linkedin.com/in/samwitteveen/
Github:
https://github.com/samwit/langchain-tutorials
https://github.com/samwit/llm-tutorials
00:00 Intro
00:30 What are Tools in LangChain?
02:45 3 Categories of Chains
04:11 Tools - Utility Chains
05:45 - Code - Basic Chains
10:30 - Chaining Chains together
12:26 - PAL Math Chain
15:02 - API Tool Chains
17:23 - Conclusion
#LangChain #BuildingAppswithLLMs
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Sam Witteveen · Sam Witteveen · 2 of 60
1
▶
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
LangChain Basics Tutorial #1 - LLMs & PromptTemplates with Colab
Sam Witteveen
LangChain Basics Tutorial #2 Tools and Chains
Sam Witteveen
ChatGPT API Announcement & Code Walkthrough with LangChain
Sam Witteveen
Trying Out Flan 20B with UL2 - Working in Colab with 8Bit Inference
Sam Witteveen
LangChain - Conversations with Memory (explanation & code walkthrough)
Sam Witteveen
LangChain Chat with Flan20B
Sam Witteveen
LangChain - Using Hugging Face Models locally (code walkthrough)
Sam Witteveen
PAL : Program-aided Language Models with LangChain code
Sam Witteveen
Building a Summarization System with LangChain and GPT-3 - Part 1
Sam Witteveen
Building a Summarization System with LangChain and GPT-3 - Part 2
Sam Witteveen
Microsoft's Visual ChatGPT using LangChain
Sam Witteveen
Building a Summarization System with LangChain - Part 3 Using ChatGPT Turbo
Sam Witteveen
LangChain Agents - Joining Tools and Chains with Decisions
Sam Witteveen
Investigating Alpaca 7B - Finetuned LLaMa LLM
Sam Witteveen
Comparing LLMs with LangChain
Sam Witteveen
Running Alpaca7B in Colab
Sam Witteveen
How to finetune your own Alpaca 7B
Sam Witteveen
How to make a custom dataset like Alpaca7B
Sam Witteveen
Understanding Constitutional AI - the paper and key concepts
Sam Witteveen
Using Constitutional AI in LangChain
Sam Witteveen
Talking to Alpaca with LangChain - Creating an Alpaca Chatbot
Sam Witteveen
Text-to-video-synthesis with Diffusers and Colab
Sam Witteveen
Meet Dolly the new Alpaca model
Sam Witteveen
Checking out the Cerebras-GPT family of models
Sam Witteveen
A Step-by-Step Guide to Fine-Tuning Your Dolly Model (tutorial)
Sam Witteveen
Is GPT4All your new personal ChatGPT?
Sam Witteveen
Raven - RWKV-7B RNN's LLM Strikes Back
Sam Witteveen
Talk to your CSV & Excel with LangChain
Sam Witteveen
Vicuna - 90% of ChatGPT quality by using a new dataset?
Sam Witteveen
Koala Revealed: The ChatGPT Alternative You Need to Know! 🔍
Sam Witteveen
Running Koala for free in Colab. Your own personal ChatGPT? (tutorial)
Sam Witteveen
BabyAGI: Discover the Power of Task-Driven Autonomous Agents!
Sam Witteveen
Auto-GPT - How to Automate a Task Based AI with GPT-4
Sam Witteveen
Improve your BabyAGI with LangChain
Sam Witteveen
Generative Agents - Deep Dive and GPT-4 Recreation
Sam Witteveen
GPT4ALLv2: The Improvements and Drawbacks You Need to Know!
Sam Witteveen
Dolly 2.0 by Databricks: Open for Business but is it Ready to Impress!
Sam Witteveen
Red Pajama - Operation: Freeing LLaMA
Sam Witteveen
Investigating Open Assistant - Models, Datasets and Addons
Sam Witteveen
Investigating MiniGPT-4 - The Secret behind GPT-V?
Sam Witteveen
Stable LM 3B - The new tiny kid on the block.
Sam Witteveen
Bard can now code and put that code in Colab for you.
Sam Witteveen
Checking out Bark: a Text to Speech system by Suno AI
Sam Witteveen
Fine-tuning LLMs with PEFT and LoRA
Sam Witteveen
Master PDF Chat with LangChain - Your essential guide to queries on documents
Sam Witteveen
Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools
Sam Witteveen
Building Custom Tools and Agents with LangChain (gpt-3.5-turbo)
Sam Witteveen
StableVicuna: The New King of Open ChatGPTs?
Sam Witteveen
WizardLM: Evolving Instruction Datasets to Create a Better Model
Sam Witteveen
LaMini-LM - Mini Models Maxi Data!
Sam Witteveen
Finding the Best Free ChatGPT
Sam Witteveen
MPT-7B - The First Commercially Usable Fully Trained LLaMA Style Model
Sam Witteveen
LangChain Retrieval QA Over Multiple Files with ChromaDB
Sam Witteveen
LangChain Retrieval QA with Instructor Embeddings & ChromaDB for PDFs
Sam Witteveen
LangChain + Retrieval Local LLMs for Retrieval QA - No OpenAI!!!
Sam Witteveen
Transformers Agent - Is this Hugging Face's LangChain Competitor?
Sam Witteveen
StarCoder - The LLM to make you a coding star?
Sam Witteveen
Testing Starcoder for Reasoning with PAL
Sam Witteveen
The New Wizards - Unfiltered & Unaligned
Sam Witteveen
Camel + LangChain for Synthetic Data & Market Research
Sam Witteveen
More on: LLM Foundations
View skill →Related AI Lessons
Chapters (9)
Intro
0:30
What are Tools in LangChain?
2:45
3 Categories of Chains
4:11
Tools - Utility Chains
5:45
Code - Basic Chains
10:30
Chaining Chains together
12:26
PAL Math Chain
15:02
API Tool Chains
17:23
Conclusion
🎓
Tutor Explanation
DeepCamp AI