[0:00] In this video, you'll learn how to build an AI agent from scratch in Python in just a few minutes. [0:06] I'll walk you through everything step by step. [0:08] This will be very beginner friendly, and you'll learn how to make something [0:11] quite interesting in Python with some popular frameworks like Lang Chain. [0:15] I'll show you how to use various LMS like Claude or GPT, [0:19] how you can give the agent access to various tools, [0:22] and how you can structure the output of the agent so you can use it in your code. [0:26] So let me show you [0:27] a quick demo of the finished project, and then we'll go through exactly how to build that. [0:30] So you can see that I built what's known as a research assistant. [0:33] You can make this anything that you want. [0:35] This is just a quick demo for the tutorial. [0:37] And it asks me what it wants to research. [0:39] So I'm going to paste here. [0:41] Tell me about Lang Chain and its applications. [0:43] And I'm going to tell it to save it to a file. [0:45] So it can use the tool that I created to save the contents to a text file. [0:49] Now this has access to some tools like Wikipedia and Google Search. [0:53] So you can see here it says that it's actually searching on Wikipedia [0:56] with this query gives us some kind of output here. [0:59] And then if we scroll through we can kind of see the entire thought process. [1:02] And we can disable that as well if we want. [1:04] Anyways, you see that we get some output here. [1:06] We have a topic. [1:07] We have a summary. [1:08] We have the sources that it used and then the various tools that are used as well. [1:12] And if we go here to the left hand side you can see that actually generate a text file for me [1:17] that contains the research output as well as a timestamp and all of the content that it gave us. [1:22] Of course, we can make this much more advanced, we can give it access to many more tools, [1:26] and we can get it to do some really cool things with that tool access. [1:30] But this will really get us started and show you how you can write these types of agents. [1:34] So I hope you're excited. [1:35] Let's go ahead and get into the video. [1:38] So let's begin with a few prerequisites in order to follow along with this video. [1:42] First of all, you will need Python installed on your system [1:45] and ideally Python version 3.10 or above, but the version won't be too important. [1:50] You'll also need some kind of code editor. [1:52] In this case, I'll recommend using something [1:54] like Visual Studio Code, which is what I currently have open on my computer. [1:58] You'll then need to open a new folder so you can go to file. [2:01] If you're in VS code, for example, open folder and you can just make a new folder. [2:05] In this case, I made one on my desktop by clicking new folder, gave it a name and then I opened it. [2:10] That folder is called AI Agent Tutorial. [2:12] So I just opened it here and you can see this is where I'll be working from here. [2:16] We're then going to need to install some different Python dependencies and get access to some API keys. [2:21] So let's start with the Python dependencies. [2:23] What I'm going to do is make a new file. [2:25] I'm doing that by pressing this button up here that says new file. [2:28] And I'm going to call this requirements.txt. [2:31] I suggest that you do the same here. [2:32] But there's other ways that you can follow along with this. [2:35] Now inside of this file I'm going to paste the following seven lines of code. [2:39] You can manually write these out, or you can copy this code from the link in the description. [2:43] There'll be a GitHub link that has all of the code for this video. [2:46] And you can find this file in there. [2:48] Now these are the packages that we'll need in our Python environment in order to work with this agent. [2:53] So what we're going to do now is we're going to create something known as a virtual environment. [2:57] A virtual environment is an isolated place where we can install these [3:01] dependencies, and then we can work in this environment for the project. [3:04] If you're not familiar with virtual environments or you don't want to set one up, [3:08] then you can simply run the command pip install dash [3:11] r requirements.txt from your terminal, [3:14] ideally in VS code in the same directory where your file exists. [3:18] So in this case, we're inside of the AI agent tutorial. [3:21] And then I'm running this command pip install dash r and then pointing it [3:25] to requirements.txt which is again in this same directory. [3:28] Which is why this will work. [3:30] Otherwise you can do pip three, install dash r [3:32] requirements.txt and that will install the dependencies globally. [3:36] However, I'm going to suggest you create a virtual environment. [3:39] And to do that you're going to type a different command which is going to be Python [3:42] dash m v env and then v env. [3:46] This is going to use venv to make a new virtual environment called venv. [3:50] In the current directory, if you're on Mac or Linux, you can adjust this to Python three. [3:55] On windows it should simply be Python. [3:57] When you run this command, you should see that it takes a second [3:59] and then you get a new directory here called V env. [4:03] Now what we need to do is activate the virtual environment. [4:05] And then we can start using it. [4:07] And everything will be the same as it normally would. [4:09] So in order to activate the virtual environment, if you're on Mac or Linux, you're going to type [4:13] source dot slash the name of the virtual environment which is V and v. [4:17] And then this is going to be slash bin slash activate. [4:22] Okay let me just type this correctly for you guys. [4:26] And then you can hit enter. [4:27] This is if you're on Mac or Linux. [4:29] If you're on windows the command is different and it's simply dot slash Venv [4:34] slash scripts slash activate okay this is windows. [4:37] The other one is Mac or Linux. [4:39] You hit enter and then you should see that you get this venv as a prefix in your terminal. [4:44] Now we can install the dependencies like I said before. [4:47] So pip install dash requirements.txt or pip three install. [4:51] And this should install [4:52] all of the dependencies into this virtual environment and we'll be good to start working. [4:56] So the dependencies have now been installed in this virtual environment. [5:00] And I just want to make you aware that if you see that I have like this autocomplete [5:03] popping up in my editor, and you're wondering what that's from, that's actually from GitHub Copilot. [5:08] So if you want to use that, you can do that for free by going to extensions. [5:11] And then type in GitHub and you can see copilot. [5:15] So pop up right here. [5:16] I've just installed this in my editor. [5:18] So that's why you're seeing the autocomplete and speaking of Microsoft's GitHub copilot. [5:21] Yeah. [5:22] You know that insane AI tool that replaces 95% of your manual typing? [5:26] Well, that's just one example of how generative AI coding tools [5:30] are transforming how US developers tackle software engineering. [5:33] Now, personally, my workflow has changed massively over the past [5:36] two years and to be honest with you, I'd say it's for the better. [5:39] Whether it's generating unit tests, stubbing classes, or even writing entire features. [5:44] I'm sure that you guys have come up with some pretty creative ways to use these AI tools. [5:48] Now that's actually something that Microsoft is interested in empowering developers [5:52] to learn new skills and showcase the creative and useful ways [5:56] that they've used GitHub Copilot to solve everyday problems. [5:59] Now, Microsoft is currently running hashtag coding with Copilot, where they're highlighting [6:04] and celebrating the standout ways that developers have used GitHub Copilot to make their jobs easier. [6:10] Now, you can share your own GitHub Copilot story for a chance [6:13] to be featured on the Microsoft Developer social channels. [6:16] If you have a cool story on how you've used GitHub, Copilot, then you can share it anywhere, including [6:21] platforms like Instagram X, YouTube, or LinkedIn [6:24] with the hashtag coding with Copilot all personally be reviewing [6:28] all of the submissions for a shoutout in a future video, so make sure to tag me. [6:32] I'd love to check them out. [6:34] Now we all know the AI is reshaping the industry, and devs are able to leverage this tool [6:39] to tackle and solve problems more easily and creatively than ever before. [6:43] Now, a massive shoutout to GitHub Copilot and Microsoft for sponsoring today's video, [6:48] and I look forward to reviewing some of your submissions in a future video, so stay tuned. [6:52] Okay, so now that everything is set up, it's time to start writing some code. [6:55] So what we're going to do is make a new file in this folder called Main.py. [7:00] This will be the file where we're going to write most of our code. [7:02] We're also going to make another file called tools.py. [7:05] We're just going to separate things out so that we put the tools in this file. [7:08] And the main logic here so that things are a little bit easier to read and more organized. [7:13] We're also going to make one more file called dot and then env. [7:16] This is an environment variable file where we're going to store some credentials [7:20] for things like our GPT API key or our entropic API key. [7:25] Because I'm going to show you two ways to use while two different lines within this agent. [7:31] Okay. So we're going to go to Main.py. [7:34] And we're going to start by setting up a very simple agent. [7:37] We're going to run the agent. [7:38] And then we'll start adding all of the tooling functionality. [7:41] So to begin we're going to start importing some things that we need. [7:43] So we're first going to say from dot env import local env. [7:49] We're then going to say from pedantic import. [7:53] If we can spell this correctly the base model [7:57] we're then going to say from Lang train. [8:00] And this is going to be underscore open AI import. [8:04] And this is going to be chat open AI. [8:06] And then additionally and this is optional, I'm going to say from Lang Train and Tropic Imports. [8:12] And this is going to be Chat and Tropic because I'm going to show you how you can [8:16] choose between either open AI or cloud or GPT or Claude for this video. [8:21] Beneath that I am going to type this line which says load env. [8:25] What this is going to do is load the environment variable file that we created here, [8:29] which we'll fill in in just a minute. [8:30] So we have all of the credentials that we need to continue with the tutorial. [8:34] Next what we want to do is we want to set up in Lem. [8:37] So our agent will begin with some type of Lem. [8:40] We can just use the yellow Lem normally. [8:42] But what we're going to do is give it to an agent and then give the agent some tools [8:46] and some various other functional, like being able to generate output in a specific format. [8:51] So we're going to start by saying Lem is equal to. [8:54] And here's where you're going to have a choice. [8:55] You can choose to use chat OpenAI, which means you're going to be using something like an OpenAI API key. [9:01] Or you can use something like Chat and Tropic. [9:04] So you can see here it's giving me the autocomplete for chat and Tropic. [9:08] Now for both of these, what you need to do is specify the model that you want to use. [9:11] So if you're using something like OpenAI then you can say model is equal to. [9:16] And then again something like GPT five turbo. [9:18] Or you could use GPT for mini or for mini. [9:21] There's all kinds of different models. [9:22] You can just go with something like GPT for, I believe also [9:25] for mini is another option as well. [9:27] And as for the API key, don't worry, I'll show you how you load that in one second. [9:31] Now if you're using entropic then you want to load in probably the Claude model. [9:35] So I'm just going to paste in the one that I'm using here. [9:38] So Claude 3-5 sonnet. [9:39] And then this is the version that I picked. [9:41] But you can pick a different version. [9:43] And when this video is out, there may be a more recent version that you can select here. [9:47] Either way you're going to select a model and you're going to choose what type of LM you're using. [9:52] In my case, I'm using Chat Tropic because I'm currently rate limited by OpenAI. [9:57] Okay. So now we've selected the yellow line. [9:59] However, we need to provide an API key in order to be able to use this element. [10:03] So regardless of if you're using OpenAI or in Tropic or really any other provider, [10:07] you're going to need to go into your environment variable file this dot env file, [10:12] and you're going to need to write one of the following variables. [10:15] The first is if you're using OpenAI, you're going to have to say OpenAI [10:18] underscore API underscore key is equal to. [10:21] And then you're going to have to paste the API key here. [10:24] Lastly, if you're using entropic then how do you spell entropic. [10:28] Let me make sure I do this correctly. [10:30] You're going to have to do anthropic [10:33] if we can type this correctly. [10:35] Underscore API underscore key is equal to an empty string. [10:40] So again if you're using OpenAI I use this. [10:42] If you're using anthropic or Claude you're going to use this. [10:45] I'm going to show you how to get both of the API keys. [10:47] So hang tight for one second. [10:48] Okay. [10:48] So in order to get the API keys, I believe you do need to have credit card information on file. [10:53] Don't worry, this will cost you like $0.01 at most. [10:55] If anything, it should probably just be free. [10:58] Regardless, you can go to platform.openai.com/api keys. [11:02] I will leave this link in the description and simply press Generate new key. [11:05] Give it a name, create the secret key and copy it. [11:08] If you're working with OpenAI and for Entropic or Claude, you can go to this page right here. [11:13] Console dot dot entropic.com/settings/keys. [11:17] Again link in the description. [11:18] Press create key. [11:19] Same thing. [11:20] Give it a name and then copy that key. [11:22] Obviously do not share this with anyone. [11:24] So now what I'm going to do is I'm going to close that and I'm going to paste in my key. [11:28] And then I'm going to close this file so that I don't leak the API key to you. [11:31] So my API key is now loaded in order to test if this is working [11:35] we can invoke the lemon run it locally from our computer. [11:38] So in order to do that we can say response is equal to LM dot invoke. [11:43] And then I believe we can simply just pass a query. [11:45] So something like what is the meaning of life? Great I love that. [11:48] That's giving me the autocomplete from the AI and we can print the response. [11:52] It's possible that we need to pass, something else, but I think this is totally fine for right now. [11:57] So now that we have that, we can simply run our Python code. [12:00] Make sure that when you run the Python code, you're running it from within your virtual environment. [12:04] And the easiest way to do that is, again, just to open up the terminal you created in VS code [12:09] and then type Python and then the name of your file, which in this case [12:12] is main.py or Python three main.py. [12:16] And then hit enter and you should see that we get a response. [12:18] It might just take one second. So let's see. [12:20] And we get some content and kind of some other metadata information from this lab. [12:26] You can see it says that's one of humanity's oldest most profound questions, blah blah blah, blah, blah. [12:30] And we get the response. [12:32] Okay, so that's how you use the LM very simply. [12:34] Like we're just using L alone. [12:35] We haven't added any agent functionality. [12:37] But next what I want to do is obviously add some more content [12:41] and make this a little bit more advanced. [12:43] So after we have our LM, the next thing I'm going to set up is something known as a prompt template. [12:48] This is something that will kind of act as a template for any of the queries that we give to the LM, [12:53] so that we can give it more information on what we actually want it to do. [12:57] So for our prompt template, we're going to do the following. [13:00] We're going to say from line chain. [13:03] And then this is going to be underscore underscore. [13:08] And then this is going to be dot prompts. [13:11] And we're going to import the chat prompt template. [13:15] Okay. [13:15] While we're here we're also going to say from length chain core. [13:19] And then this is going to be dot output underscore parsers. [13:23] We're going to import the pedantic output parser. [13:28] Now basically what we're going to do is we're going to define a simple Python class [13:32] which will specify the type of content that we want our LM to generate. [13:36] We're then going to give the LM a prompt and we're going to tell it hey answer the user's question. [13:41] And as a part of your response generate it using this schema or using this model. [13:47] So it will give us output in a format that we can then know and kind of use predictably. [13:52] You'll see what I mean in one second, but just bear with me while I write some of this code. [13:55] So I'm going to create a class here. [13:57] And this is going to be my response or my research. [14:00] Sorry response. [14:02] Okay. And this is going to inherit from my base model. [14:05] Now you can make this class anything that you want. [14:07] I'm just giving you a simple example here with like a response that we're expecting from the lab. [14:12] So the response that I want is I want it to generate for me a topic. [14:15] And I want that topic to be of type string. [14:17] So I'm going to specify that right here. [14:19] I then want to have a summary. [14:21] And I want that summary to be of type string. [14:23] So I specify the type right. [14:25] Then I want to have some sources. [14:27] And I want these sources to be a list of strings. [14:30] So I'm going to type list and then in square brackets string. [14:33] Then I'm going to have some tools used. [14:35] And I'm going to type list and string. [14:38] Now here is where you can just specify all of the fields [14:41] that you want as output from your LM call. [14:44] You can make this as complicated as you want. [14:46] You can have nested objects, so long as all of your classes inherit from the base model from pedantic. [14:52] Okay. [14:53] So you just need to make sure you inherit from base model, and then you can specify [14:56] all of the fields that you want to have in your response model. [15:00] And we can eventually pass that to the LM. [15:03] Now that we have this, what we're going to do is we're going to create a parser. [15:06] So we're going to say parser is equal to the pedantic output parser. [15:11] And we're simply going to parse the research response. [15:14] But we're going to do this as the pedantic object. [15:17] Okay. So we're gonna say pedantic object is equal to this. [15:19] There's other ways that you can set up this parser like using Json for example, [15:24] or using other types of kind of what do you call it, schemas, I guess. [15:27] But in this case we're using pedantic, which is very popular within Python. [15:31] So this parser will now allow us to essentially take the output of the LM and parse it into this model. [15:37] And then we can use it like a normal Python object inside of our code. [15:41] Okay. So next we need to set up a prompt. [15:43] Now I'm just going to copy this in because I don't wanna spend a ton of time writing it. [15:46] And I'll walk through it line by line. [15:48] So we're going to say our prompt is equal to our chat prompt template [15:52] dot from messages okay. [15:55] Then inside of here the first thing we're going to specify is a system message. [15:59] The system message is information to the LM. [16:01] So it knows what it's supposed to be doing. [16:03] So we tell it you are a research assistant that will help generate a research paper, [16:07] answer the user query and use the necessary tools. [16:10] And then the important part is that I tell it to wrap the output in this format [16:14] and provide no other text, and I provide the format instructions. [16:18] Okay. That's very important. [16:19] Make sure you have this part. [16:21] Lastly, there's a few things that you do need to add here. [16:24] So you need to add the chat history. [16:26] The query this is coming from the user. [16:28] And the placeholder which is the agent scratchpad. [16:31] Don't worry too much about these three fields right here. [16:33] The only important one is the query. [16:35] This is just necessary for the type of agent that we're going to create. [16:38] And you can find all this information from the link chain documentation. [16:41] Okay. Then we say partial. [16:44] And what this means is we're partially going to fill in this prompt by passing the format instructions. [16:50] So what this now does is it uses our parser that we created here. [16:54] And it just takes this pedantic model and turns it into a string [16:59] that we can then give to the prompt. [17:02] So we're pretty much just taking this model that we want to have our output [17:04] and converting it to a string, giving it to the other as part of the system prompt. [17:09] So now it knows when it generates a response, it's got to do it in this format. [17:13] So notice that format instructions here matches up with format instructions here. [17:17] They're the same variable. That's important. [17:19] You could call this anything that you want so long as you adjust both of the values here. [17:23] Okay. [17:23] And then these other values will be [17:24] automatically filled in for us when we start actually running our agent. [17:29] Okay. So now we have our prompt. [17:31] We have our parser. [17:32] We have our LM. And it's time to create a simple agent. [17:35] So we're going to say agent is equal to. [17:38] And then we're going to bring in a function called create tool calling agent. [17:41] So from the top of our code we're going to say from link chain. [17:46] And then this is going to be dot agent imports. [17:50] And then create underscore. [17:51] And notice there's all types of agents that you can create. [17:54] But we we want to create tool calling agent okay. [17:59] Now this is one we'll use again there's a bunch of ones that you can use here. [18:02] So I'm going to use this function. [18:03] And inside of this function what we need to pass is the following are alarm [18:08] which will simply be equal to the alarm that we've already defined up here. [18:12] And then we can pass it our prompt. [18:14] So we can say prompt is equal to prompt. [18:17] And then lastly we're going to say tools. [18:19] And for right now we're just going to make this equal to an empty list. [18:22] We'll specify some tools in a second. [18:23] But for now I just want to test the agent and make sure that the agent will work. [18:27] Okay. [18:27] So we have our agent create two calling agent alarm prompt tools. [18:31] And now we want to test the agent. [18:33] So in order to test the agent we need to import one more thing. [18:36] So from link change agents we're going to import what's called the agent Executer. [18:41] The agent Executer is just a way to actually execute the agent. [18:44] Right. So that's why we need to bring that in. [18:46] And then down here we're going to say that the agent underscore [18:51] executer is equal to an agent Executer. [18:55] For the agent Executer, we're going to say the agent is equal to our agent. [18:59] The tools is equal to again, just an empty list for right now. [19:02] And we're going to say verbose if we specify this is equal to true. [19:07] So we can see the thought process of the agent. [19:10] If you don't care about the thought process and you don't want to see that, [19:12] then you can just mark this as false or not include it. [19:15] So now we have an agent Executer, and we can use the agent executer to generate some kind of response. [19:21] So we can say our raw underscore response is equal [19:24] to the agent Executer dot invoke. [19:28] And then when we invoke this we need to pass in this prompt variable which is query. [19:32] Now you can have multiple prompt variables. [19:34] And notice that they're all specified inside of braces. [19:37] So we have the format instruction prompt variable that we filled in here. [19:41] The chat history and the agent scratchpad will automatically be filled in by our agent Executer. [19:46] And then we're left with one more prompt variable which is the query. [19:49] And if you wanted to add multiple variables here, you could just do another one. [19:52] And you can say you know, name or something. [19:54] And then here when you invoke this you're going to pass two things. [19:58] So you'd pass the query which is you know, what is the capital France or something. [20:02] And the name of Alice. [20:03] So you can pass multiple prompt variables. [20:05] Again in this case we just need query. [20:08] But I just wanted to show you that you can pass more if you want to. [20:11] Okay. [20:12] So we're going to invoke the agent Executer. [20:14] We're going to get a raw response. [20:16] And for now we can just print out the raw response. [20:18] And I believe that that should be it in this code should be working. [20:21] So let me zoom out a little bit. [20:23] You can see our imports again. [20:24] You can find all of this code from the link in the description. [20:27] Generate the Elm. [20:28] Make the parser create our prompt with the prompt template. [20:31] Then we have our agent and we create the agent executer and generate the raw response. [20:36] So let's try this out and see if we get something. [20:40] Give this a second. [20:41] You can see it's going into the agent executor chain. [20:44] And it should generate a response for me. [20:46] And you can see that we get that. What is the capital of France. [20:48] And then it gives us kind of output text topic. [20:51] And it gives us the output in this format. [20:53] Now if we just want to see the response that we're looking for. [20:57] So like this kind of model, what we need to do [21:00] is use the parser to parse this content. [21:03] So what we're going to do is we're going to say [21:06] our structured response is equal to parser dot parse. [21:09] And then rather than just parsing the raw response we're going to say raw response dot get [21:15] we're going to get the output key because it gives us kind of like a dictionary with multiple values. [21:19] Like if you look here, I know it's a little bit messed up. [21:21] You can see we have output and then we have an array and then we have another thing inside of here. [21:26] So we just need to go inside of that. [21:27] So we're going to say output at index zero and then text. [21:32] What this is going to do is it's going to get the output. [21:34] It's going to get the text from like the first value of output, which is all we're going to have. [21:39] And it's going to parse that with our parser into a Python object. [21:43] So then if I go here and I print the structured [21:46] response, let's run this again and I'll show you what we get. [21:49] And don't worry, I'll slow down and go through all of the code again. [21:52] Okay. [21:52] So we're entering the agent executer chain and you can see that we get this right. [21:58] So query output our text. [21:59] And then when we print out the Python object it gives us all of the fields [22:03] that are in our pedantic bottle. So we have the topic. [22:06] If we keep going through here we should get like a summary a summary is right here [22:09] we have the tools used and then we have the. [22:13] If we keep going here, the sources. Right. [22:15] And it tells us where these sources came from. [22:17] Cool. Okay. So that is that. [22:20] And that allows us now to get it in the correct output. [22:22] And the interesting thing, right, is that now that this is in the Python model, [22:26] what I can do is something like structured response dot and then topic. [22:31] And I can just access this topic which is typed as a string. [22:35] And I can just use that. [22:36] So this is the really kind of neat part about using these structured output models [22:40] is that now I can get specific components from their response and actually use them predictably [22:45] in my code, rather than just having this plain text, which is usually what the models give you. [22:51] So we've already got that part. [22:52] We have our structured response, and just one thing we want to do is we just want to [22:56] add a simple try catch here or try accept block because it's possible the model can mess up. [23:01] And then this will give us an error. [23:03] So if it doesn't give us the correct response type, which is possible, we can get an error. [23:07] So what we're going to do is just say accept exception as E and we're going to say print, [23:11] and we're just going to go, what do you call it here? [23:15] Error parsing response. [23:19] Then we're going to print out E. [23:21] And then we're going to say the raw response like this. [23:24] And we'll actually just do another one and we'll say wrong response like that okay. [23:29] Can make this look better if you want, [23:30] but I'm just doing a simple error message so we can see kind of what's going wrong. [23:34] Okay. So there we go. We have our try accept. [23:37] And now we need to do the cool part which is adding tools. [23:41] So of course, you know this is interesting, but we want to have the ability to call various tools. [23:46] Tools are things that the LM can use or the agent can use [23:50] that we can either write ourself or we can bring in from things like the Lang Chain Community Hub. [23:55] So what I'm going to do is go to my tools.py file, and I'm going to show you how to add three [23:59] different tools one for looking up Wikipedia, [24:02] one for going to DuckDuckGo and searching something. [24:05] These are all free. You don't need any API keys. [24:08] And then one custom tool that we write ourself which can be any Python function. [24:12] So we're going to go to the top of our code. [24:13] We're going to say from Lang Chain. [24:15] And this is going to be underscore community import. [24:19] And then the Wikipedia or sorry this is going to be dot tools. [24:25] And then we're going to import the Wikipedia query run. [24:29] Then we're going to say from Lang chain community dot tools. [24:32] And actually this is going to be dot utilities [24:36] getting carried away with the copilot autocomplete. [24:39] Here we're going to say import the Wikipedia API wrapper. [24:43] We're then going to say from Lang Chain and actually can do this and want to import up here [24:48] we're going to bring in the DuckDuckGo search run okay. [24:53] So a tool that we can use. [24:55] And then we are going to have two last imports. [24:57] So we're going to say from [25:01] Lang chain dot tools. [25:04] And we're going to import the tool. [25:07] This will allow us to kind of wrap or create our own custom tool. [25:10] And then we're going to say from date time [25:13] imports date time okay. [25:16] Sorry I know that was me messing around and trying to remember what these imports actually are. [25:20] So you can see we have some stuff related to Wikipedia, the DuckDuckGo, search run. [25:24] Again, all of these are free, but you will get rate limited if you use them too much. [25:28] And then we'll be able to create our own custom tool. [25:30] So let's start by simply creating the tool that can access DuckDuckGo or kind of like search. [25:35] Google search is what I'm calling it, even though it's DuckDuckGo search. [25:38] So to do that, I'm simply going to say search is equal to DuckDuckGo search run. [25:42] I'm just going to call that. [25:43] I'm then going to say my search underscore tool is equal to a tool. [25:48] For my tool, I need to give this a name. [25:50] So I'm just going to say that this tool is called search okay. [25:53] So we can say name is equal to this. [25:56] The function is going to be equal to search dot run. [26:00] So this provides a function called run. [26:02] So that's what we're passing here. And then [26:05] what do we want to have here. [26:06] We need to have a description for the tool. [26:09] And this is just going to be search the web for information okay. [26:13] So this is our tool. [26:15] That's literally all you need to do. [26:17] We've now created a tool that we can pass to our agent. [26:20] The key thing is that you need to have some name. [26:22] This can name this names right. Can not have any spaces. [26:24] So just make sure if you want to have something like you know [26:26] search web you do with an underscore or you do it like with camel case. [26:30] And then you just need to have a description so that the agent knows when it should be using this tool. [26:34] This is a basic description, but you could give a more detailed description [26:37] if you wanted it to only use the tool in a specific scenario. [26:40] Okay, so that's our first tool. [26:42] Now in order to use that, we're going to go back into our Main.py file and we're going to import it. [26:47] So we're going to say from tools imports. [26:50] And we're just going to import the search tool okay. [26:54] We are then going to go to our agent. [26:57] And before here we're going to make a list called tools. [27:00] And this is going to be equal to our search tool inside of a list. [27:04] We're then going to pass our tools now to our agent [27:07] as well as to our agent Executer okay. [27:11] So now we'll have access to this list of tools. [27:13] Right now we just have one tool the search tool. [27:15] But if we gave it access to multiple tools and it can pick and use all of them [27:19] or just the ones that are relevant. [27:21] Last thing here, rather than just having the query [27:24] be manually typed in, I'm going to just get it from the user. [27:27] So I'm going to say query is equal to input. [27:30] You know what can I help you research question mark. [27:36] And then we're just going to pass query here. [27:38] So now the user will just type in the query themselves okay. [27:42] And rather than printing the raw response I'm going to print [27:45] the structured output okay. [27:48] So that we can use that as we see fit. [27:50] All right. So let's come up here. [27:52] Let's clear the screen and let's run and let's see what we get. [27:57] And there was an issue here. [27:58] Could not import DuckDuckGo search package okay. [28:01] So we just need to install DuckDuckGo search my bad guys. [28:04] So I'm going to go to requirements.txt. [28:07] And I put this inside of my requirements so that I don't forget in that you guys will have it [28:10] when you look at the code in the video. [28:12] And I'm just going to run the command pip install and then DuckDuckGo search. [28:16] So we're able to use that okay. [28:18] So give that a second to run. All good. [28:21] And now we can run the code again. [28:23] And hopefully this will work. [28:24] What can I help you research I want to know about sharks okay. [28:31] And let's see what it gives us okay. [28:34] So you can see that it's invoking search. [28:36] So it's using this tool shark biology habit behavior research okay. [28:39] That's an interesting search string. [28:41] And you can see that it gives us back this research paper. [28:44] And obviously if we improve the prompt, we'd get a better response. [28:46] But you know this is what we're looking for. Perfect. [28:49] So that is the search tool. [28:51] Next I will show you how do you set up the Wikipedia tool. [28:53] And then our own custom tool. [28:55] So the Wikipedia tool is pretty straightforward. [28:57] What we can do is below here we can say our API wrapper [29:01] is equal to the Wikipedia API wrapper. [29:04] Inside of here we can pass a few pieces of content or few parameters. [29:07] So we can say top k results. [29:09] In this case, just make it equal to one. [29:11] But if we want it to return, say five different types of results from Wikipedia, [29:15] we could go with five, right? [29:16] We can change this to be whatever we want. [29:18] Then we can say the doc content character is Max. [29:22] I'll just make this equal to 100 because this is a quick demo, but if you want it [29:26] to get a lot more content from the Wikipedia page, then you would put 1000 or 10,000. [29:30] Again, it will take longer to run, [29:32] and you may get rate limited faster because you don't even need an API key for this. [29:36] But again, just showing you a quick example. [29:38] So these are two parameters. [29:39] And I believe there's a few more that you can pass here [29:41] like the language load all available mate metadata etc.. [29:45] Okay. [29:45] Now that we have the API wrapper, we need to convert this to a tool. [29:48] So we're going to say the wiki tool is equal to the Wikipedia query run. [29:53] And then all we're going to do is just pass our API wrapper equal to our API wrapper. [29:58] Now that's actually all we need for the tool. [30:00] We don't need to wrap it in a custom tool. [30:02] We can just pass this as a tool directly to link chain. [30:05] So what I'm going to do now is import the wiki tool. [30:08] So say wiki underscore tool. [30:10] And then I'm going to go here to my tools list. [30:12] Bring in the wiki tool. [30:15] And now we can run this and let's see what we get. [30:18] And if it's able to use Wikipedia. [30:20] So I'm gonna say same thing. [30:22] Yeah. [30:23] Shark. [30:23] So let's go hammerhead sharks [30:29] okay. [30:29] Let's see what we get. [30:30] So you can see it's using Wikipedia looking up the hammerhead shark. [30:34] And then it's using search. [30:36] Yes. Search. Hammerhead shark. [30:38] Research latest findings. Okay. [30:40] And then it gives us the response, and it tells us that it used these two tools. [30:45] Suite. Okay. Last thing. [30:47] I'm going to show you how we make our own custom tool so we can save this to a file. [30:51] So in order to save this to a file we can actually just write our own Python function. [30:55] So actually let's go up to the top here. [30:57] And this function or any function for that matter can be wrapped as a tool. [31:01] So we're just going to make a function called save. [31:03] And actually I'm going to save some time because I don't think you guys need to watch me write this out. [31:06] I'm just going to copy it in called save to txt. [31:09] We'll take in some data and we'll take in a file name. [31:12] Now it's important that you give the parameters a type here [31:16] so that the model knows how to call this function. [31:19] So make sure that you type them. [31:20] In this case I've typed it as a string. [31:22] If it was a more advanced type you'd want to include that as well. [31:25] So what I'm doing is I'm just writing at the top of the paper in a research output, [31:28] the timestamp, and then I'm going to write the data. [31:30] And that data is going to be that pedantic model which you'll see in just one second. [31:35] Okay, so that's my function. [31:36] Now once we have the function we just need to wrap it as a tool. [31:39] So to do that we can say save underscore tool is equal to tool. [31:43] And then we just do the exact same thing that we had here. [31:46] So I'm just going to copy this paste it I'm going to change the function to be save to txt. [31:51] Notice I didn't call the function, I just wrote the name of it. [31:54] And then for the name we're just going to say save text to file. [32:00] Again make sure we don't have any spaces. [32:01] And then I will just kind of bring in this description. [32:04] You know, save structured research data to a text file so it knows what this is doing. [32:08] That's as easy as it is to make your own custom tool. [32:10] So if you want a tool that calls an API for example, you can do that. [32:14] Just write a Python function, wrap it as a tool, and you can pass as many of these [32:18] to your agent that you want and really get some advanced functionality here. [32:22] So now we're going to bring in the tool. [32:23] So same thing got bring in. We save to TXT. [32:26] That's what I called it right. [32:27] No sorry. Save tool okay. [32:31] And then we are going to put that in the list save tool. [32:36] And then we can start using the save tool. [32:38] Now the only thing is we just need to instruct the model to save this to a file. [32:42] So we need a Python Main.py I'm going to say [32:45] let's research I don't know what do we want to research. [32:49] You know self [32:52] East Asia population or something. [32:57] And so save to a file okay. [33:00] And then it should use kind of all of the tools and save this to a file. [33:03] And let's see if it does that. [33:05] And just give this a second to run. [33:06] So you can see it just used Wikipedia. [33:09] It's using what else. [33:10] Southeast Asia. [33:12] Okay. [33:12] I think it's just my terminals cutting off a little bit here. [33:16] And it used the saved text file and then finished the chain gave us the output. [33:21] And now you can see we have our research output. TXT file. [33:24] And inside of here we get our time stamp. [33:26] We get our topic and we get all of this information. [33:29] You can see the region has relatively young population blah blah blah [33:32] blah blah and goes through kind of all of the details. [33:35] So there you go. [33:35] We have just completed the project and built an agent from scratch [33:39] in Python that has access to various tools. [33:42] This is super cool. [33:43] We are really just scratching the surface with what is possible here. [33:47] I just wanted to give you a video that kind of overview, the main components [33:51] and main topics that make up like 80% of the Asian applications. [33:54] And this really does get you quite far and allows you to build some really cool stuff. [33:59] So I will leave the code link here in the description in case you want to check out the GitHub [34:02] which will have all of this content you can just copy it and do whatever you want with it. [34:06] If you enjoyed the video, make sure leave a like. [34:08] Subscribe to the channel and I will see you in the next one.