LangChain Agents - Joining Tools and Chains with Decisions
Key Takeaways
This video demonstrates LangChain Agents, which enable the use of multiple tools and chains in a LLM app, allowing the LLM to decide on the next input tool based on user input, utilizing tools such as Wikipedia, search API, Open EA model, and calculator tool.
Full Transcript
okay in this video I'm going to be talking about Lang chain agents Lang chain agents are one of the key things that makes Lang chain so useful they take lanting tools which we've looked at in some of the other videos and chains which we've looked at in other videos and combine them into one agent so one of the problems with just tools and chains alone is that you probably don't want to use the same tool for every query that the user has so you don't want to have everything answered by the math calculator or something like that right you want to basically be able to have people converse with a bunch of different things and this is what agents allow you to do so agents have an Executor level where they look at the inputs they then decide what is the best tool based on what you've initialized the tools for this particular bot and then they allow you to actually use those in relation to what the person's asking so let's jump in and have a look at the code here you can see I've got some just a standard pip installs we're going to be using Wikipedia later on so I put Wikipedia in there we basically just initialize things we will need a search engine API key for doing this you will want to set that up and we're going to start off by creating an agent so to create an agent we do two main things we load in the tools that the agent is going to be using and then we initialize the agent with those tools and you'll see as we go through what actually initializing it is doing or parts of what it's doing so we're going to have a language model here we're just using the standard open EA model we're setting temperature to zero and here I'm going to start off with two tools so we're going to do an agent that can do two tools the first one is just going to be doing search with the search API and the second one is going to be doing some maths or like a calculator tool and if we look at these tools so once we've initialized this we can come and look into these we can see that they've got a name and a description and and looking at this second one here we've got the name being calculator and this is useful for math questions and questions should relate to math if you look into it there's a lot of things actually in the tool right we've got the templates we've got a whole bunch of the different chains that are actually made up for that tool there once we've got the tools set up we want to initialize the agent so initializing the agent requires a few key things we need to pass in the tools we need to pass in our language model we then need to pass in the style of the agent or the type of the agent so in this case we're going to be using zero shot react agent so this is based on a paper about getting language models to take actions and generate steps for taking actions I may actually do a whole video about the paper the paper is very interesting of how you can prompt a model to give you back things that you can then use to take actions on anyway one of the big things that initializing the agent does is it sets up the sort of executor prompt and if we look at this you'll see let me just copy this here and we come in and paste it we will see that okay we've got the following questions so this is the prompt that we're looking at right and this is the prompt that gets executed at the start for the language model to decide what it should be doing so we've got answer the following questions as best you can you have access to the following tools so a search engine right so we've got search which is what we passed in first as the first tool and the calculator it's what we passed in the second tool so it knows that it can use these two tools and then the following format is what it should be returning so it's going to determine what's the question that it has to answer and then it came to use a bad way of describing this I would say self-reflect if it was a human itself reflex but it's a language model we don't want to anthropomorphize it too much but you can think of this a little bit as it's doing a self-reflection of that what is it going to need to do and then it decides the action to take so in this case this could be search or calculator and then it decides the input to that action so with it's going to do a search it needs to have a search query and that's what will go in here the observation that it will get back will be the result of the action so you can see that this is where it's going to start generating this and then it's going to have this sort of scratch pad that goes along with it so if we kick this off you need to understand too that it doesn't always have to use a tool so here we just ask it okay agent dot run we pass in our text how are you today and so you can see we've got verbose equals true so we can see the outputs of these nicely we can see that okay we're entering the agent executor chain and then it says that okay this question how are you today is not a question that can be answered with search or a calculator so it doesn't need an action it doesn't need an action input it's this is a question that requires a personal answer so it can generate the answer and you can see the answer that generates I'm doing well thank you so that one didn't use any of the tools so the next one we run is we ask okay who is the United States president what is his current age divide by two so this clearly is going to need some tools right so it basically says okay what's the question that needs to solve the question it needs to solve is I need to find out who the president of the United States is and then calculate his age divide by two so the first action is search and so it decides to do a search United States it then comes up with Joe Biden then the thought is I need to find Joe Biden's age the action next action search so Joe Biden's age gets the answer back and you see the different colors here is showing the different that this tool has been has been used and has returned the answer the answer back is 80 years okay I now need to divide by 80 by 2 so what's the action now it's a calculator so we've got the calculator doing action 80 divided by two equals 40. final thought that it thinks to itself as it goes through these is that okay I know the final answer Joe Biden is 80 years old and his age divided by 2 is 40 G so there it's used multiple steps to get the answer and so you actually use multiple tools to get the answer there the next one let's try the next one what is the average age in the United States and how much less is that than the age of the current U.S president so again it starts off going for search so it basically decides what it needs to do goes for search it then works out it needs to get the average age in the United States right is the input to the search so it's gone along and got some stats for that obviously that's quite a long one in there then it basically needs to find the age of the current president it does that and then it says okay I know the final answer and then it gives us this the average age in the United States is approximately 38 years which is 42 years less than the current U.S president so this sort of shows you this idea of stringing multiple tools together to get an answer so let's try a new agent let's say we take an agent and we're going to pass in multiple tools now we're going to pass in much more into is two we're going to add Wikipedia and we're going to add the terminal output for this we initialize our agent the same we've got the zero shot react description and then we've got our prompt so let's look at the prompt again we'll just paste The Prompt in here so we can just see it easily and we can see that the problem's exactly the same except now we've got more tools we've got search we've got calculator we've got Wikipedia we've got terminal going in here the rest of it is exactly the same so let's jump in with this and we ask it first okay who is the head of deepmind so for this it decides okay best way to find this is to do a search and you need to realize for some of these things there are multiple ways for it to find out but here's decided okay I'm just going to do a search head of deepmind and it comes back demos hassebus and so then it rephrases that uses the language model to rephrase that based on the question Demis hassebus is the head of Deep Mind so now we ask well what is deepmind so it works out that okay this is a company it seems to have that in the knowledge knowledge of the language model itself but it says okay this is a company so I should look it up on Wikipedia so now it does a search on Wikipedia for deepmind and it sure enough what is a return package it returns back one page about the actual company and one page gets a little bit confused so it turns back a page about Gato which is one of the models that deepmind did it uses this Collective information that's being fed into the prompt to then basically decide on the final answer and the final answer that it comes up with is deepmind is a British artificial intelligence research laboratory founded in 2010 it gives us is now a subsidiary of alphabet gives us more information there so now if we say next off I wanted to do some math right but didn't work you'll see that I say okay take the year the deepmind was founded which we know is 2010 and add 50 years and then I ask it will this year have AGI okay so it needs to basically work out first okay when was the my founded and then add 50 years to that so it looks deep mind again on Wikipedia to get the founding date for that and then it says okay mine was founded in 2010 so adding 50 years brings it up to 2060. it hasn't used the math module it's been able to do that itself I guess the math is so simple that the language model can do that and then it doesn't do a search right you would think that okay now it's going to do some searches about that and it decides no it's going to just answer this itself so I don't know if this is open ai's sort of conditioning of the model and prompting of the model internally or fine-tuning of the model about answers to AGI because see the answer that it comes back with is it's impossible to predict whether AGI will exist in 2060. okay let's move on let's so then I'm thinking I really want one that's going to use search so let's try where is deepmind's office and sure enough for this one it basically says oh okay that's going to be a search and then it does a search for deepmind office address and it's nice that it's added the word address it's gotten what I'm asking for here yeah I can see it returns back before this now we still didn't get to use our math module in this agent so I wanted to show that as well so we try one more of okay if I Square the number of the street address of deepmind what answer do I get so now it's got to do the search to get the address then it's going to take this 5 the street address and square it and sure enough it's able to return 25. now you will find sometimes that it will get in with some things it will get confused if there are multiple numbers in there it could have squared the three or the four if it got mistaken so that's something you can think about there are ways that you can handle that kind of thing of that you could guide a prompt back so if you were doing a lot of things with addresses you could format the prompt to come back as Street number in quotes with the word Street number or something Road or Street name postcode all these things separately so that then when you feed them into the next thing it's less likely to make them anyway then so we've done that right so we've covered a number of the different Tools in here the one that we haven't used yet is terminal so I asked it okay so remember this is the same agent I haven't made a new agent here this is the same agent and I ask what files are in my current directory so let's have a quick look I'm on collab I've got a very standard sample data that they give you with mnist and a few sort of CSV files in there so I ask it what files are in my current directory and sure enough it works out that oh this action needs determine so it runs an LS command sees some things in there so then it decides okay I'll look in this folder the sample data and it then gives us a bit more information about that we can see what's out there and then it gives us a nice list of okay these are all the files that are in there then I ask it okay so then I was curious like okay if we ask it about a specific file what can it find in there so I asked it does my current directory have a file about California so you can see this goes into a long chain with many steps in here and this is where some agents can get quite expensive to run because you're doing lots of these calls to your language model to get a single answer but you can see here it's pretty impressive what it does it goes through rather than just go through and look at who's curious to see whether it would take this and do the search to do the sort of text search in the language model itself but no it does it with a grep command so it basically does a grip for California and go through and looks at the different folders in there and then obviously finds some in there and then returns to me yes there is a file about California in the current directory and we could we could also ask things like what is that file that kind of thing I suggest you be very careful adding the terminal tool to what it is that you're working on because you wouldn't want end users to be able to wipe everything on your hard drive just by asking your language model to run a terminal command so be careful with that but there probably are times where it's quite useful at least for yourself to use or that kind of thing if you're setting up something like that so this covers agents as always if you've got any questions that you would like to ask please put them in in the comments I'm happy to answer questions and stuff that people have if there's any topics that you want me to cover about Lang chain or about other deep learning models Etc let me know and as always if this was useful to you please click and subscribe and I will see you in the next video bye for now
Original Description
Colab code Notebook: https://drp.li/FmrPY
In this we look at LangChain Agents and how they enable you to use multiple Tools and Chains in a LLM app, by allowing your LLM to decide on the next input tool to use based on the user's input.
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
#LangChain #BuildingAppswithLLMs
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Sam Witteveen · Sam Witteveen · 13 of 60
1
2
3
4
5
6
7
8
9
10
11
12
▶
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: Agent Foundations
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
How We Translate 300-Page Books Using Claude Without Hitting Token Limits
Dev.to · 龚旭东
Building HITL Feedback RAG: Embeddings, Retrieval, and Reranking
Medium · AI
Building HITL Feedback RAG: Embeddings, Retrieval, and Reranking
Medium · LLM
A simple way to test model fallbacks with RouterBase
Dev.to · routerbasecom
🎓
Tutor Explanation
DeepCamp AI