LangChain Agents - Joining Tools and Chains with Decisions

Sam Witteveen · Beginner ·🧠 Large Language Models ·3y ago

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 LangChain Basics Tutorial #1 - LLMs & PromptTemplates with Colab
LangChain Basics Tutorial #1 - LLMs & PromptTemplates with Colab
Sam Witteveen
2 LangChain Basics Tutorial #2 Tools and Chains
LangChain Basics Tutorial #2 Tools and Chains
Sam Witteveen
3 ChatGPT API Announcement & Code Walkthrough with LangChain
ChatGPT API Announcement & Code Walkthrough with LangChain
Sam Witteveen
4 Trying Out Flan 20B with UL2 - Working in Colab with 8Bit Inference
Trying Out Flan 20B with UL2 - Working in Colab with 8Bit Inference
Sam Witteveen
5 LangChain - Conversations with Memory (explanation & code walkthrough)
LangChain - Conversations with Memory (explanation & code walkthrough)
Sam Witteveen
6 LangChain Chat with Flan20B
LangChain Chat with Flan20B
Sam Witteveen
7 LangChain - Using Hugging Face Models locally (code walkthrough)
LangChain - Using Hugging Face Models locally (code walkthrough)
Sam Witteveen
8 PAL : Program-aided Language Models with LangChain code
PAL : Program-aided Language Models with LangChain code
Sam Witteveen
9 Building a Summarization System with LangChain and GPT-3 - Part 1
Building a Summarization System with LangChain and GPT-3 - Part 1
Sam Witteveen
10 Building a Summarization System with LangChain and GPT-3 - Part 2
Building a Summarization System with LangChain and GPT-3 - Part 2
Sam Witteveen
11 Microsoft's Visual ChatGPT using LangChain
Microsoft's Visual ChatGPT using LangChain
Sam Witteveen
12 Building a Summarization System with LangChain - Part 3 Using ChatGPT Turbo
Building a Summarization System with LangChain - Part 3 Using ChatGPT Turbo
Sam Witteveen
LangChain Agents - Joining Tools and Chains with Decisions
LangChain Agents - Joining Tools and Chains with Decisions
Sam Witteveen
14 Investigating Alpaca 7B - Finetuned LLaMa LLM
Investigating Alpaca 7B - Finetuned LLaMa LLM
Sam Witteveen
15 Comparing LLMs with LangChain
Comparing LLMs with LangChain
Sam Witteveen
16 Running Alpaca7B in Colab
Running Alpaca7B in Colab
Sam Witteveen
17 How to finetune your own Alpaca 7B
How to finetune your own Alpaca 7B
Sam Witteveen
18 How to make a custom dataset like Alpaca7B
How to make a custom dataset like Alpaca7B
Sam Witteveen
19 Understanding Constitutional AI - the paper and key concepts
Understanding Constitutional AI - the paper and key concepts
Sam Witteveen
20 Using Constitutional AI in LangChain
Using Constitutional AI in LangChain
Sam Witteveen
21 Talking to Alpaca with LangChain - Creating an Alpaca Chatbot
Talking to Alpaca with LangChain - Creating an Alpaca Chatbot
Sam Witteveen
22 Text-to-video-synthesis with Diffusers and Colab
Text-to-video-synthesis with Diffusers and Colab
Sam Witteveen
23 Meet Dolly the new Alpaca model
Meet Dolly the new Alpaca model
Sam Witteveen
24 Checking out the Cerebras-GPT family of models
Checking out the Cerebras-GPT family of models
Sam Witteveen
25 A Step-by-Step Guide to Fine-Tuning Your Dolly Model (tutorial)
A Step-by-Step Guide to Fine-Tuning Your Dolly Model (tutorial)
Sam Witteveen
26 Is GPT4All your new personal ChatGPT?
Is GPT4All your new personal ChatGPT?
Sam Witteveen
27 Raven - RWKV-7B RNN's LLM Strikes Back
Raven - RWKV-7B RNN's LLM Strikes Back
Sam Witteveen
28 Talk to your CSV & Excel with LangChain
Talk to your CSV & Excel with LangChain
Sam Witteveen
29 Vicuna - 90% of ChatGPT quality by using a new dataset?
Vicuna - 90% of ChatGPT quality by using a new dataset?
Sam Witteveen
30 Koala Revealed: The ChatGPT Alternative You Need to Know! 🔍
Koala Revealed: The ChatGPT Alternative You Need to Know! 🔍
Sam Witteveen
31 Running Koala for free in Colab. Your own personal ChatGPT? (tutorial)
Running Koala for free in Colab. Your own personal ChatGPT? (tutorial)
Sam Witteveen
32 BabyAGI: Discover the Power of Task-Driven Autonomous Agents!
BabyAGI: Discover the Power of Task-Driven Autonomous Agents!
Sam Witteveen
33 Auto-GPT - How to Automate a Task Based AI with GPT-4
Auto-GPT - How to Automate a Task Based AI with GPT-4
Sam Witteveen
34 Improve your BabyAGI with LangChain
Improve your BabyAGI with LangChain
Sam Witteveen
35 Generative Agents - Deep Dive and GPT-4 Recreation
Generative Agents - Deep Dive and GPT-4 Recreation
Sam Witteveen
36 GPT4ALLv2: The Improvements and Drawbacks You Need to Know!
GPT4ALLv2: The Improvements and Drawbacks You Need to Know!
Sam Witteveen
37 Dolly 2.0 by Databricks: Open for Business but is it  Ready to Impress!
Dolly 2.0 by Databricks: Open for Business but is it Ready to Impress!
Sam Witteveen
38 Red Pajama - Operation: Freeing LLaMA
Red Pajama - Operation: Freeing LLaMA
Sam Witteveen
39 Investigating Open Assistant - Models, Datasets and Addons
Investigating Open Assistant - Models, Datasets and Addons
Sam Witteveen
40 Investigating MiniGPT-4 - The Secret behind GPT-V?
Investigating MiniGPT-4 - The Secret behind GPT-V?
Sam Witteveen
41 Stable LM 3B - The new tiny kid on the block.
Stable LM 3B - The new tiny kid on the block.
Sam Witteveen
42 Bard can now code and put that code in Colab for you.
Bard can now code and put that code in Colab for you.
Sam Witteveen
43 Checking out Bark: a Text to Speech system by Suno AI
Checking out Bark: a Text to Speech system by Suno AI
Sam Witteveen
44 Fine-tuning LLMs with PEFT and LoRA
Fine-tuning LLMs with PEFT and LoRA
Sam Witteveen
45 Master PDF Chat with LangChain - Your essential guide to queries on documents
Master PDF Chat with LangChain - Your essential guide to queries on documents
Sam Witteveen
46 Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools
Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools
Sam Witteveen
47 Building Custom Tools and Agents with LangChain (gpt-3.5-turbo)
Building Custom Tools and Agents with LangChain (gpt-3.5-turbo)
Sam Witteveen
48 StableVicuna: The New King of Open ChatGPTs?
StableVicuna: The New King of Open ChatGPTs?
Sam Witteveen
49 WizardLM: Evolving Instruction Datasets to Create a Better Model
WizardLM: Evolving Instruction Datasets to Create a Better Model
Sam Witteveen
50 LaMini-LM - Mini Models Maxi Data!
LaMini-LM - Mini Models Maxi Data!
Sam Witteveen
51 Finding the Best Free ChatGPT
Finding the Best Free ChatGPT
Sam Witteveen
52 MPT-7B - The First Commercially Usable Fully Trained LLaMA Style Model
MPT-7B - The First Commercially Usable Fully Trained LLaMA Style Model
Sam Witteveen
53 LangChain Retrieval QA Over Multiple Files with ChromaDB
LangChain Retrieval QA Over Multiple Files with ChromaDB
Sam Witteveen
54 LangChain Retrieval QA with Instructor Embeddings & ChromaDB for PDFs
LangChain Retrieval QA with Instructor Embeddings & ChromaDB for PDFs
Sam Witteveen
55 LangChain + Retrieval Local LLMs for Retrieval QA - No OpenAI!!!
LangChain + Retrieval Local LLMs for Retrieval QA - No OpenAI!!!
Sam Witteveen
56 Transformers Agent - Is this Hugging Face's LangChain Competitor?
Transformers Agent - Is this Hugging Face's LangChain Competitor?
Sam Witteveen
57 StarCoder - The LLM to make you a coding star?
StarCoder - The LLM to make you a coding star?
Sam Witteveen
58 Testing Starcoder for Reasoning with PAL
Testing Starcoder for Reasoning with PAL
Sam Witteveen
59 The New Wizards - Unfiltered & Unaligned
The New Wizards - Unfiltered & Unaligned
Sam Witteveen
60 Camel + LangChain for Synthetic Data & Market Research
Camel + LangChain for Synthetic Data & Market Research
Sam Witteveen

This video teaches how to use LangChain Agents to enable multiple tools and chains in a LLM app, allowing the LLM to decide on the next input tool based on user input. It demonstrates how to initialize an agent, set up an executor prompt, and use multiple tools to answer questions.

Key Takeaways
  1. Load tools
  2. Initialize the agent with tools
  3. Set up the executor prompt
  4. Decide the action to take
  5. Decide the input to that action
  6. Use a search query
  7. Use a calculator
  8. Find the age of the current president
  9. Ask the agent a question
  10. Decide which tool to use based on the question and available information
💡 LangChain Agents can be used to enable multiple tools and chains in a LLM app, allowing the LLM to decide on the next input tool based on user input, improving the accuracy and effectiveness of the app.

Related AI Lessons

Your LLM Doesn’t Pick Stocks — It Remembers Them
Discover how LLMs remember stock picks rather than making actual predictions, and why this matters for AI-driven investment strategies
Medium · Machine Learning
Word Representation
Learn how word representation works in NLP and its importance in understanding human language, enabling applications like text classification and language translation
Medium · NLP
When Cosine Similarity Approaching Singularity in Google Search AI Mode
Learn how cosine similarity approaching singularity affects Google Search AI and unified knowledge graphs, and why it matters for AI engineers and data scientists
Medium · AI
When Cosine Similarity Approaching Singularity in Google Search AI Mode
Learn how cosine similarity approaching singularity affects Google Search AI and unified knowledge graphs, and why it matters for data science and AI development
Medium · Data Science
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →