Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools

Sam Witteveen · Beginner ·📰 AI News & Updates ·3y ago

Key Takeaways

This video demonstrates the use of LangChain with various tools such as Wikipedia, DuckDuckGo, and Python REPL to build LLM-powered applications and agents. It showcases the capabilities of LangChain in chaining multiple tools together for complex tasks and customizing tools using Python code.

Full Transcript

okay in this video we're gonna look at some of the new tools that have come out recently and using them with Lang chain so specifically I'm going to look at three Tools in here the first one being Wikipedia second one being DuckDuckGo and the third one being the python read evaluate print Loop that we've got going on here as always I've just installed langchain I'm using open AI in this case which I'll talk about why when we go through and the first thing there are going to be other packages that we need to install for these tools so Wikipedia is one of those so we can just pip install that and then to set up the tools you're going to find for each of the tools to setting them up is actually pretty easy so here I can basically just bring in this Wikipedia API wrapper set it up and this is a chain in itself that when I run this and I pass in a query it will look for the Wikipedia page for that so for example Wikipedia run Lang chain it's going to go and find the Lang chain page and get the info about that if I was to put in someone's name it would go and look for a Wikipedia page about them the next one up actually I've got is the redevelum print Loop this is very simple this one you don't even need to pip install anything here we're just again we've got the idea of just bringing it in we've got the wrapper and then this makes a chain and then whatever we run in Python in here will evaluate and print out so you can see here I've just got 17 times 2 it's going to print out 34. if I wrote a whole function in here it would evaluate that and print that out as well so one of the key things I'm trying to go for here is that in future videos in the next video we're going to look at doing custom tools so one of the key things that you want to do a lot is doing custom tools but to have a just a good understanding of how these work I'm going through some of these basic ones and I'm going to show you how you can put them together as well okay we've got that for running python code and the last one we're going to look at in this video is DuckDuckGo so DuckDuckGo is a search engine the great thing with this one compared to the Google serp API is we don't have to pay for any API calls with this we can just bring this in we've got the wrapper and then we can just do a search on this and it will basically go and find out information and return it back to us so each of these things is powerful in their own right but what we want to do is by putting them together we're going to make an agent that can actually use all three of these tools and it's going to decide when it uses which tool by itself now this is the reason why I'm using the older open AI API in here if we were going to use the new chat one and we just ran it just like this you'll find that it actually will get things wrong so that one needs to be we need to sort of recalibrate The Prompt for the chat GPT API maybe that's something I'll look at doing in another video here though we're just going with the basic one to look at this and we're going to set up the tools so tools basically just set up as a list of tools and we're going to create a tool so this is just a class that creates a tool the tool has the name of the tool it has the function that's going to run it so just like up here when we did the python Ripple that was a wrapper function we did python Ripple dot run and we passed in whatever we wanted there the same is going to be here this is going to get the output of a language model passed into it to run now the last thing is we need to define a description for the tool so this is also really important is that you want to basically describe how your tool would be used in the agent so here I'm saying that okay this is useful for when you need to use Python to answer a question you should input python code in there so that's for the python one all right so I've got that tool set up I can set up tools outside the actual tools list so you can see I'm setting up the Wikipedia one so again we've got an name we've got the function for running it and here I've got the descriptions the description in this case is going to be useful for when you need to look up a topic country or person on Wikipedia and the last one is going to be DuckDuckGo so this is the name is DuckDuckGo search useful for when you need to do a search on the internet to find information that another tool can't find be specific with your input so the idea is that it's going to put keywords in to do a search on DuckDuckGo there okay we've defined these three tools I need to add the other tools to the list so that's just simply just simple python pending these to the list so then basically I've got all of these in a list of tools which I'm going to pass in to the agent so next up we've got to Define our agent so here we're basically going for a very simple agent where we're going to have a zero shot agent and we're going to initialize this agent with the zero shot react description so you'll see what that is when we look at the prompt we're going to pass in the tools we're going to pass in the llm we're going to pass in the verbose equals true and we're going to pass Max iterations so max iterations is the number of times it can kind of think to itself about is this the right tool and then if the tool doesn't work it might decide that no I actually want to use a different tool or something like that okay so in a second we'll look at the prompt and we'll see what's going on with this react description here but first let's just try it out so we've got our zero shot agent run when was Barack Obama born so this kicks off this agent executor chain and it's going to pass in our query and then that's going to sort of redefine it it's going to work out like what does it need to do so it says okay well I need to find out when Barack Obama was born so the action that it decides on this is it's going to use DuckDuckGo search and then the search that it's going to do is going to be action input Barack Obama birth date the observation is what it got back and so we can see that it got quite a lot of information back but from that it's gone through and it's decided that okay well the thought is Barack Obama was born on August 4th 1961 and then it gives us the final answer and it finishes Out The Chain so this used one tool of going to DuckDuckGo for that if I asked it something like what is 17 by 6 okay so in this case it's going to calculate what it is so it says that I need to do a calculation but rather than just sort of try to use the language module to calculate it it knows that okay this calculation could be run in Python so it basically decides that the action it's going to take is the python reple or read evaluate print Loop and its input is 17 times 6 that we've got there but written in the way that python would understand that it gets the answer back it says I now know the answer the final answer is a hundred and two so we can see that it's managed to do both of those really well so let's have a look at the prompt what actually is going on here so when we look at this prompt we can see that the prompt that it's sending in in this case let me just make sure that's the latest one up to date it's going to be answer the following questions as best you can you have access to the following tools and this is where you see our names the python Ripple and then the description useful for when you need to use Python to answer a question you should input python code duckduck go search useful for when you need to search the internet Etc like we looked at before Wikipedia are useful for when you need to look up a topic country or personal Wikipedia so it's got that and then it's got the use the following format and then it passes in the format for this so we're not giving it really a lot of examples in this so there are some other react agents that you can run where it passes in some examples as well and that's something that we could do if we wanted to try with the turbo API I mean we will look at doing that in the future the idea here is that it's going to basically use the format question the input question you must answer so sort of rephrase what the question is that you've got to answer thought you should always think about what to do action the action to take should be one of and then we pass in that list of the three tools and then the action input the input to the action observation the result of the action and it shows that this can be done for a number of times and then the final answer and then you would give the final answer so then we pass in the question and we pass in the scratch Pad so the scratch Pad is what's taking the information that we've received already from one of the tools so now you can see if I ask it okay tell me about Lang Chang and this time it didn't use the Wikipedia one so the problem with these three examples is that the Wikipedia is actually very similar to DuckDuckGo right they can they both have the ability to look things up so in this case it's decided to use DuckDuckGo for this so it's gone and done a search for Lang chain it's got some information back and it comes back with our final answer Lang chain is an open source python library that needs anyone who can write code to build llm-powered applications all right so I wanted to try and get it to do at least one where it shows you Wikipedia I knew that I had country in one of the things about Wikipedia in my description of Wikipedia so the base model when I said tell me about Singapore the base model obviously has some understanding is maybe a little bit of a stretch but it obviously has some representation of Singapore being a country so when it sees that it's able to then work out Singapore is a country so I should look it up on Wikipedia and sure enough it chooses the action Wikipedia the input Singapore it gets the Wikipedia page back and you can see that there's on that page there's stuff about Singapore dollar Singapore Army Singapore other things but I realize there's none of that we're focusing on the country so Singapore is an island and city-state in Southeast Asia and it gives goes on to give us a bunch of different information about this so this is an example of it using Wikipedia for this if we didn't have the DuckDuckGo it would have gone to Wikipedia for the Lang chain one because it doesn't have another tool for doing basic search all right what if I give it let's try some other things what is the current price of Bitcoin okay in this case it realizes best place to get this information go current price of BTC goes for it and just returns it then I was thinking okay well let's show one more with the python raffle Library so I tried is 11 a prime number and sure enough it decided that yes you know that the python repl was the way to go but it it kind of didn't do a great job so you can see what it did was it wrote out 11 divided by two what's left over if there's nothing it might be a prime number you can see that this is what it's got back is that oh it might be a prime number so in this case it decides well I've still got two possible iterations I can do I'm going to go and do another search I'm going to use duct and I'm going to do another tool and use DuckDuckGo search and its searches there is 11 a prime number and it obviously finds it in a list of prime numbers somewhere on the internet that it's able to return that so I wasn't that happy with that right because I wanted the python Ripple to do it so I thought okay well never mind we'll just try rephrasing a little bit and so we write write a function to check if 11 is a prime number and test it so here you can see it's right I need to write a function to check if a number is prime okay what do I need for that I need the python reader value print Loop so then it writes this function and then it obviously runs then I need to test this function so then it tests this function is it Prime prints back that it's true and then so then it finally comes back and says 11 is a prime number so you can see this is done multiple calls to get the answer in this case for both of these they've done multiple calls in there so I encourage you to play around with it you can you can certainly try with the GPT 3.5 turbo API you'll just find that it doesn't work as well for some of these react things where it has to choose the tool you can improve it by rewriting The Prompt a bit and also rather than using a zero shot agent giving it some examples for the agent but anyway this shows you some examples of running Tools in Lang chain in the next video we're going to look at how to make custom tools so that you could make your own tool if you wanted to do something just by writing some code in Python and then using that with Lang chain as you go along all right as always if you've got any questions please put them in the comments below uh if you found this useful please click like And subscribe I've got a bunch of videos coming up on Lang chain over the next week or so bye for now

Original Description

Colab: https://colab.research.google.com/drive/16gWpUMOfRsvXDVPGtNwwDLV49SyxkJaD?usp=sharing In this video, I go through using some of the recent tools released on LangChain to show how chains work and how they can be combined in an Agent. For more tutorials on using LLMs and building Agents, check out my Patreon: Patreon: https://www.patreon.com/SamWitteveen Twitter: https://twitter.com/Sam_Witteveen My Links: Linkedin: https://www.linkedin.com/in/samwitteveen/ Github: https://github.com/samwit/langchain-tutorials https://github.com/samwit/llm-tutorials
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Sam Witteveen · Sam Witteveen · 46 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
13 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
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 with various tools to build LLM-powered applications and agents. It covers the basics of LangChain, tool chaining, and prompt engineering, and provides examples of how to customize tools using Python code. By watching this video, viewers can learn how to use LangChain to build complex applications and improve tool performance.

Key Takeaways
  1. Install Wikipedia API wrapper and set it up as a chain
  2. Create a tool class with name, function, and description
  3. Define a description for the tool to describe how it would be used in the agent
  4. Set up tools outside the tools list
  5. Add tools to the list
  6. Define agent with zero shot react description
  7. Pass in tools and LLM to the agent
  8. Run the agent executor chain
  9. Ask question
  10. Specify action and input
💡 LangChain provides a flexible and customizable way to build LLM-powered applications and agents, allowing users to chain multiple tools together and improve tool performance through prompt engineering.

Related AI Lessons

When AI Asks for More Electricity Than a Country Can Imagine
AI's increasing power consumption is causing concerns, learn why it matters for data centers and energy supply
Medium · AI
You Are Not Behind. The World Is.
You're not behind, the world is still adapting to AI, and it's okay to take your time to learn and grow
Medium · AI
Career choice with the advent of AI - pure Computer Science or learn software with a background of core engineering area
Learn how to choose between a Computer Science and Engineering career path or combining programming with a core engineering background in the age of AI
Dev.to AI
The AI Hype Cycle: Calm Before the Next Breakthrough?
Understand the AI hype cycle to anticipate the next breakthrough and make informed decisions
Medium · Programming
Up next
Motorist saved by human chain | 9 News Australia
9 News Australia
Watch →