in this video you'll learn how to build an AI agent from scratch in Python in just a few minutes I'll walk you through everything step by step this will be very beginner friendly and you'll learn how to make something quite interesting in Python with some popular Frameworks like Lang chain I'll show you how to use various llms like Claude or GPT how you can give the agent access to various tools and how you can structure the output of the agent so you could use it in your code so let me show you a quick demo of the finished project and then we'll go through exactly how to build that so you can see that I've built what's known as a research assistant you can make this anything that you want this is just a quick demo for the tutorial and it asks me what it wants to research so I'm going to paste here tell me about Lang chain and its applications and I'm going to tell it to save it to a file so it can use the tool that I created to save the contents to a text file now this has access to some tools like Wikipedia and Google search so you can see here it says that it's actually searching on Wikipedia with this query gives us some kind of output here and then if we scroll through we can kind of see the entire thought process and we can disable that as well if we want anyways you see that we get some output here we have a topic we have a summary we have the sources that it used and then the various tools that it used as well and if we go here to the left hand side you can see that it actually generate a text file for me that contains the research output as well as a Tim stamp and all of the content that it gave us of course we can make this much more advanced we can give it access to many more tools and we can get it to do some really cool things with that tool access but this will really get us started and show you how you can write these types of Agents so I hope you're excited let's go ahead and get into the video so let's begin with a few prerequisites in order to follow along with this video first of all you will need python installed on your system and ideally python version 3.10 or above but the version won't be too important you also need some kind of code editor in this case I'll recommend using something like Visual Studio code which is what I currently have open on my computer you'll then need to open a new folder so you can go to file if you're in VSS code for example open folder and you can just make a new folder in this case I made one on my desktop by clicking new folder gave it a name and then I opened it that folder is called AI agent tutorial so I just opened it here and you can see this is where I'll be working from here we're then going to need to install some different Python dependencies and get access to some API keys so let's start with the python dependencies what I'm going to do is make a new file I'm doing that by pressing this button up here that says new file and I'm going to call this requirement .txt I suggest that you do the same here but there's other ways you can follow along with this now inside of this file I'm going to paste the following seven lines of code you can manually write these out or you can copy this code from the link in the description there'll be a GitHub link that has all of the code for this video and you can find this file in there now these are the packages that we'll need in our python environment in order to work with this agent so what we're going to do now is we're going to create something known as a virtual environment a virtual environment is an isolated place where we can install these dependencies and then we can work in this environment for the project if you're not familiar with virtual environments or you don't want to set one up then you can simply run the command pip install d r requirements.txt from your terminal ideally and vs code in the same directory where your file exists so in this case we're inside of the AI agent tutorial and then I'm running this command pip install dasr and then pointing it to requirements.txt which is again in this same directory which is why this will work otherwise you can do pip 3 install --r requirements.txt and that will install the dependencies globally however I'm going to suggest you create a virtual environment and to do that you're going to type a different command which is going to be python DM venv and then V EnV this is going to use VV to make a new virtual environment called VV in the current directory if you're on Mac or Linux you can adjust this to Python 3 on Windows it should Simply Be python when you run this command you should see that it takes a second and then you get a new directory here called VV now what we need to do is activate the virtual environment and then we can start using it and everything will be the same as it normally would so in order to activate the virtual environment if you're on Mac or Linux you're going to type Source do/ the name of the virtual environment which is venv and then this is going to be slash bin SL activate Okay let me just type this correctly for you guys and then you can hit enter this is if you're on Mac or Linux if if you're on Windows the command is different and it's simply /vv SL scripts SL activate Okay this is Windows the other one is Mac or Linux you hit enter and then you should see that you get this venv as a prefix in your terminal now we can install the dependencies like I said before so pip install - r requirements.txt or pip 3
install and they should install all of the dependencies into this virtual environment and will be good to start working so the dependencies have now been installed in this virtual environment M and I just want to make you aware that if you see that I have like this autocomplete popping up in my editor and you're wondering what that's from that's actually from GitHub co-pilot so if you want to use that you can do that for free by going to extensions and then type in GitHub and you can see co-pilot so pop up right here I've just installed this in my editor so that's why you're seeing the autocomplete and speaking of Microsoft's GitHub co-pilot yeah you know that insane AI tool that replaces 95% of your manual typing well that's just one example of how generative AI coding tools are transforming how us developers tackle software engineering now personally my workflow has changed massively over the past 2 years and to be honest with you I'd say it's for the better whether it's generating unit tests stubbing classes or even writing entire features I'm sure that you guys have come up with some pretty creative ways to use these AI tools now that's actually something that Microsoft is interested in empowering developers to learn new skills and showcase the creative and useful ways that they've used GitHub co-pilot to solve everyday problems now Microsoft is currently running # coding with copilot where they're highlighting and celebrating the standout ways that developers have used GitHub co-pilot to make their jobs easier now you can share your own GitHub co-pilot story for a chance to be featured on the Microsoft developer social channels if you have a cool story on how you've used GitHub co-pilot then you can share it anywhere including platforms like Instagram X YouTube or LinkedIn with the hash coding with copilot I'll personally be reviewing all of the sub for a shout out in a future video so make sure to tag me I'd love to check them out now we all know that AI is reshaping the industry and devs are able to leverage this tool to tackle and solve problems more easily and creatively than ever before now a massive shout out to GitHub co-pilot and Microsoft for sponsoring today's video and I look forward to reviewing some of your submissions in a future video so stay tuned okay so now that everything's set up it's time to start writing some code so what we're going to do is make a new file in this folder called main. py this will be the file where we're going to write most of our code we're also going to make another file called tools. py we're just going to separate things out so that we put the tools in this file and the main logic here so that things are a little bit easier to read and more organized we're also going to make one more file called Dot and then EnV this is an environment variable file where we're going to store some credentials for things like our GPT API key or our entropic API key because I'm going to show you two ways to use well two different llms within this agent okay so we're going to go to main.py and
we're going to start by setting up a very simple agent we're going to run the agent and then we'll start adding all of the tooling functionality so to begin we're going to start importing some things that we need so we're first going to say from. EnV import load. EnV we're then going to say from pantic import if we can spell this correctly the base model we're then going to say from Lang chain and this is going to be underscore open AI import and this is going to be chat open Ai and then additionally and this is optional I'm going to say from Lang chain entropic import and this is going to be chat entropic because I'm going to show you how you can choose between either open AI or CLA or GPT or Claud for this video beneath that I am going to type this line which says load. EnV what this is going to do is load the environment variable file that we created here which will fill in in just a minute so we have all of the credentials that we need to continue with the tutorial next what we want to do is we want to set up an llm so our agent will begin with some type of llm we can just use the llm normally but what we're going to do is give it to an agent and then give the agent some tools and some various other functionality like being able to generate output in a specific format so we're going to start by saying llm is equal to and here's where you're going to have a choice you can choose to use chat open aai which means you're going to be using something something like an open aai API key or you can use something like chat and Tropic you can see here it's giving me the auto complete for chat and Tropic now for both of these what you need to do is specify the model that you want to use so if you're using something like open AI then you can say model is equal to and then again something like GPT 5 turbo or you could use GPT 4 mini or 40 mini there's all kinds of different models you can just go with something like gtt4 I believe also 40 mini is another option as well and as for the API key don't I'll show you how you load that in 1 second now if you're using entropic then you want to load in probably the Claud model so I'm just going to paste in the one that I'm using here so Claude 3-5 Sonet and then this is the version that I picked but you can pick a different version and when this video is out there may be a more recent version that you can select here either way you're going to select a uh model and you're going to choose what type of llm you're using in my case I'm using chat entropic because I'm currently rate limited by open AI okay so now we've selected the llm however we need to provide an API key in order to be able to use this llm so regardless of if you're using open AI or entropic or really any other provider you're going to need to go into your environment variable file this EnV file and you're going to need to write one of the following variables the first is if you're using open aai you're going to have to say open aore API uncore key is equal to and then you're going to have to paste the API key here lastly if you're using entropic then how do you spell entropic uh let me make sure I do this correctly you're going to have to do anthropic if we can type this correctly _ API undor key is equal to an empty string so again if you're using open AI use this if you're using antrop or Claude you're going to use this I'm going to show you how to get both of the API keys so hang tight for one second okay so in order to get the API keys I believe you do need to have credit card information on file don't worry this will cost you like 1 cent at most if anything it should probably just be free uh regardless you can go to platform. open.com API keys I will leave this link
in the description and simply press generate new key give it a name create the secret key and copy it if you're working with open aai and for entropic or Claud you can go to this page right here console. entropic Doom settings Keys again Link in the description press create key same thing give it a name and then copy that key obviously do not share this with anyone so now what I'm going to do is I'm going to close that and I'm going to paste in my key and then I'm going to close this file so I don't leak the API key to you so my API key is now loaded in order to test if this is working we can invoke the llm and run it locally from our computer so in order to do that we can say response is equal to llm do invoke and then I believe we can simply just pass a query so something like what is the meaning of life great I love that that's giving me the auto complete from the AI and we can print the response it's possible that we need to pass uh something else but I think this is totally fine for right now so now that we have that we can simply run our python code make sure that when you run the python code you're running it from within your virtual environment and the easiest way to do that is again just to open up the terminal you created in VSS code and then type Python and then the name of your file which in this case is main.py or Python 3 main.py and then hit enter and you should see that we get a response it might just take one second so let's see and we get some content and kind of some other metadata information from this llm you can see it says that's one of Humanity's oldest most profound question blah blah blah blah blah and we get the response okay so that's how you use the llm very simply like we're just using the llm we haven't added any agent functionality but next what I want to do is obviously add some more content and make this a little bit more advanced so after we have our llm the next thing I'm going to set up is something known as a prompt template this is something that will kind of act as a template for any of the queries that we give to the llm so that we can give it more information on what we actually want it to do so for our prompt template we're going to do the following we're going to say from Lang chain and then this is going to be underscore and core and then this is going to be do prompts and we're going to import the chat prompt template okay while we're here we're also going to say from Lang chain core and then this is going to be do output unor parsers we're going to import the pantic output parser now basically what we're going to do is we're going to Define a simple python class which will specify the type of content that we want our llm to generate we're then going to give the llm a prompt and we're going to tell it hey answer the user's question and as a part of your response generate it using this schema or using this model so it will give us output in a format that we can then know and kind of use predictably you'll see what I mean in one second but just bear with me while I write some of this code so I'm going to create a class here and this is going to be my response or my research sorry response okay and this is going to inherit from my base model now you can make this class anything that you want I'm just giving you a simple example here with like a response that we're expecting from the llm so the response that I want is I want it to generate for me a topic and I want that topic to be of type string so I'm going to specify that right here I then want to have a summary and I want that summary to be of type string so I specify the type right then I want to have some sources and I want these sources to be a list of strings so I'm going to type list and then in square brackets string then I'm going to have some tools used and I'm going to type list and string now here is where you can just specify all of the fields that you want as output from your llm call you can make this as complicated as you want you can have nested objects so long as all of your classes inherit from the base model from pantic okay so you just need to make sure you inherit from base model and then you can specify all of the fields that you want to have in your response model and we can eventually pass that to the llm now that we have this what we're going to do is we're going to create a parser so we're going to say parser is equal to the pantic output parser and we're simply going to pass the research response but we're going to do this as the pantic object okay so we're going to say pantic object is equal to this there's other ways that you can set up this parser like using Json for example uh or using other types of kind of what do you call it schemas I guess but in this case we're using p antic which is very popular within python so this parser will now allow us to essentially take the output of the llm and parse it into this model and then we can use it like a normal python object inside of our code okay so next we need to set up a prompt now I'm just going to copy this in because I don't want to spend a ton of time writing it and I'll walk through it line by line so we're going to say our prompt is equal to our chat prompt template. from messages okay then inside of here the first thing we're going to specify is a system message the system message is information to the llm so it knows what it's supposed to be doing so we tell it you are a research assistant that will help generate a research paper answer the user query and use the necessary tools and then the important part is that I tell it to wrap the output in this format and provide no other text and I provide the format instructions okay that's very important make sure you have this part lastly there's a few things that you do need to add here so you need to add the chat history the query this is coming from the user and the play holder which is the agent scratch Pad don't worry too much about these three Fields right here the only important one is the query this is just necessary for the type of agent that we're going to create and you can find all this information from the Lang chain documentation okay then we say partial and what this means is we're partially going to fill in this prompt by passing The Format instructions so what this now does is it uses our parser that we created here and it just takes this pantic model and turns it into a string that we can then give to the prompt so we're pretty much just taking this model that we want to have our output in converting it to a string giving it to the llm as part of the system prompt so now it knows when it generates a response it's got to do it in this format so notice that format instructions here matches up with format instructions here they're the same variable that's important you could call this anything that you want so long as you adjust both of the values here okay and then these other values will be automatically filled in for us when we start actually running our agent okay so now we have our prompt we have our parser we have our llm and it's time to create a simple agent so we're going to say agent is equal to and then we're going to bring in a function called create tool calling agent so from the top of our code we're going to say from Lang chain and then this is going to be do agent import and then create underscore and notice there's all types of agents that you can create but we we want to sry create tool calling agent okay now this is the one we'll use again there's a bunch of ones you can use here so I'm going to use this function and inside of this function what we need to pass is the following our llm which will simply be equal to the llm that we've already defined up here and then we can pass it our prompt so we can say prompt is equal to prompt and then lastly we're going to say tools and for right now we're just going to make this equal to an empty list we'll specify some tools in a second but for now I just want to test the agent and make sure that the agent will work okay so we have our agent create two calling agent llm prompt tools and now we want to test the agent so in order to test the agent we need to import one more thing so from link chain. agents we're going to import
What's called the agent executor the agent executor is just a way to actually execute the agent right so that's why we need to bring that in and then down here we are going to say that the agent unor executor is equal to an agent executor for the agent executor we're going to say the agent is equal to our agent the tools is equal to again just an empty list for right now and we're going to say verbose if we specify this is equal to true so we can see the thought process of the agent if you don't care about the thought process and you don't want to see that then you can just mark this as false or not include it so now we have an agent executor and we can use the agent executor to generate some kind of response so we can say our rawcore response is equal to the agent executor do invoke and then when we invoke this we need to pass in this prompt variable which is query now you can have multiple prompt variables and notice that they're all specified inside of braces so we have the format instruction prompt variable that we filled in here the chat history and the agent scratch Pad will automatically be filled in by our agent executor and then we're left with one more prompt variable which is the query and if you wanted to add multiple variables here you could just do another one and you can say you know name or something and then here when you invoke this you're going to pass two things so you pass the query which is you know what is the capital France or something and the name of Alice so you can pass multiple prompt variables again in this case we just need query but I just wanted to show you that you can pass more if you want to okay so we're going to invoke the agent executor we're going to get a raw response and for now we can just print out the raw response and I believe that that should be it and this code should be working so let me zoom out a little bit you can see our Imports again you can find all of this code from the link in the description generate the llm make the parser create our prompt with the prompt template then we have our agent and we create the agent executor and generate the raw response so let's try this out and see if we get something give this a second you can see it's going into the agent executor chain and it should generate a response for me and you can see that we get that what is the capital of France and then it gives us kind of output text topic and it gives us the output in this format now if we just want to see the response that we're looking for so like this kind of model model what we need to do is use the parser to parse this content so what we're going to do is we're going to say our structured response is equal to parser dop parse and then rather than just parsing the raw response we're going to say raw response doget we're going to get the output key because it gives us kind of like a dictionary with multiple values like if you look here I know it's a little bit messed up you can see we have output and then we have an array and then we have another thing inside of here so we just need to go inside of that so we're going to say output at index zero and then text what this is going to do is it's going to get the output it's going to get the text from like the first value of output which is all we're going to have and it's going to parse that with our parser into a python object so then if I go here and I print the structured response let's run this again and I'll show you what we get and don't worry I'll slow down and go through all the code again okay so we're entering the agent executor chain and you can see that we get this right so query output our text and then when we print out the python object it gives us all of the fields that are in our pantic model so we have the topic if we keep going through here we should get like a summary yeah summary is right here we have the tools used and then we have the if we keep going here these sources right and it tells us where these sources came from cool okay so that is that uh and that allows us now to get it in the correct output and the interesting thing right is that now that this is in the python model what I can do is something like structured response Dot and then topic and I can just access this topic which is typed as a string and I can just use that so this is the really kind of neat part about using these structured output models is that now I can get specific components from the response and actually use them predictably in my code rather than just having this plain text which is usually what the models give you so we've already got that part we have our structured response uh and just one thing we want to do is we just want to add a simple try catch here or try accept block because it's possible the model can mess up and then this will give us an error so if it doesn't give us the correct response type uh which is possible we can get an error so what we're going to do is just say accept exception as e and we're going to say print and we're just going to go uh what do you call it here error parsing response then we're going to print out e and then we're going to say the raw response like this and we'll actually just do another one and we'll say raw response like that okay you can make this look better if you want but I'm just doing a simple error message so we can see kind of what's going wrong okay so there we go we have our try accept and now we need to do the cool part which is adding tools so of course uh you know this is interesting but we want to have the ability to call various tools tools are things that the llm can use or the agent can use that we can either write oursel or that we can bring in from things like the Lang chain Community Hub 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 different tools one for looking up Wikipedia one for going to duck ducko and searching something these are all free you don't need any API keys and then one Custom Tool that we write oursel which can be any python function so we're going to go to the top of our code and we're going to say from Lang chain and this is going to be underscore Community import and then the Wikipedia uh or sorry this is going to be tools and then we're going to import the Wikipedia run then we're going to say from Lang chain community. tools and actually this is going to be do utilities getting carried away with the co-pilot autocomplete here we're going to say import the Wikipedia API wrapper we're then going to say from langing chain and actually can do this in One Import up here we're going to bring in the doc Dogo search Run Okay so a tool that we can use and then we going to have two last Imports we're going to say from Lang chain. tools and we're going to import the tool this will allow us to kind of wrap or create our own custom tool and then we're going to say from date time import date time okay sorry I know that was uh me messing around I'm trying to remember what these Imports actually are so you can see we have some stuff related to acedia the Duck Duck Go uh search run again all these are free but you will get rate limited if you use them too much and then we'll be able to create our own custom tool so let's start by simply creating the tool that can access duck. go or kind of like search Google search is what I'm calling it even though it's duck. go search so to do that I'm simply going to say search is equal to duck. go search run
I'm just going to call that I'm then going to say my search tool is equal to a tool for my tool I need to give this a name so I'm just going to say that this tool is called search okay so we can say name is equal to this the function is going to be equal to search. run so this provides a function called run so that's what we're passing here and then what do we want to have here we need to have a description for the tool and this is just going to be search the web for information okay so this is our tool that's literally all you need to do we've now created a tool that we can pass to our agent the key thing is that you need to have some name this can name this names rray cannot have any spaces so just make sure if you want to have something like you know search web you do with an underscore or you do it like with camel case and then you just need to have a description so that the agent knows when it should be using this tool this is a basic description but you could give a more detailed description if you wanted it to only use the tool in a specific scenario okay so that's our first tool now in order to use that we're going to go back into our main.py file and we're going to import it so we're going to say from tools import and we're just going to import the Search tool okay we are then going to go to our agent and before here we're going to make a list called tools and this is going to be equal to our Search tool inside of a list we're then going to pass our tools now to our agent as well as to our agent executor okay so now we'll have access to this list of tools right now we just have one Tool The Search tool but if we gave it access to multiple tools then it can pick and use all of them or just the ones that are relevant last thing here rather than just having the query be manually typed in I'm going to just get it from the user so I'm going to say query is equal to input you know what can I help you research question mark and then we're just going to pass query here so now the user will just type in the query themselves okay and rather than printing the raw response I'm going to print the structured output okay so that we can use that as we see fit all right so let's come up here let's clear the screen and let's run and let's see what we get and there was issue here could not import dock. go search package okay so we just need to install dock. go search my bad guys so
I'm going to go to requirements.txt I'm going to put this inside of my requirement so that I don't forget and that you guys will have it when you look at the code in the video and I'm just going to run the command pip install and then Duck Duck Go search so that we're able to use that okay so give that a second to run all good and now we can run the code again and hopefully this will work what can to help you research I want to know about sharks okay and let's see what it gives us okay so you can see that it's invoking search so it's using this tool shark biology habit Behavior research okay that's an interesting uh search string and you can see that it gives us back this research paper and obviously if we improve the prompt we'd get a better response but you know this is what we're looking for perfect so that is the Search tool next I will show you how to set up the Wikipedia tool and then our own custom tool so the Wikipedia tool is pretty straightforward what we can do is below here we can say our API wrapper is equal to the Wikipedia API wrapper inside of here we can pass a few pieces of content or a few parameters so we can say top K results and in this case just make it equal to one but if we wanted to return say five different types of results from Wikipedia we could go with five right we can change this to be whatever we want then we can say the doc content characters Max I'll just make this equal to 100 because this is a quick demo but if you wanted to get a lot more content from the wik IIA page then you would put a th000 or 10,000 again it will take longer to run and you may get rate limited faster because you don't even need an API key for this but again just showing you a quick example so these are two parameters and I believe there's a few more that you can pass here like the language load all available M uh metadata Etc okay now that we have the API wrapper we need to convert this to a tool so we're going to say the wiki tool is equal to the Wikipedia query run and then all we're going to do is just pass our API wrapper equal to our API wrapper now that's actually all we need for the tool we don't need to wrap it in a custom tool we can just pass this as a tool directly to Lang chain so what I'm going to do now is import the wiki tool so say Wiki tool and then I'm going to go here to my tools list bring in the wiki tool and now we can run this and let's see what we get and if it's able to use Wikipedia so I'm going to say same thing uh you know sharks or let's go hammerhead sharks okay and let's see what we get so you can see it's using Wikipedia looking up the hammerhead shark and then it's using search uh yes search hammerhead shark uh research latest findings okay and then it gives us the response and it tells us that it used these two tools sweet okay last thing I'm going to show you how we make our own custom tool so we can save this to a file so in order to save this to a file we can actually just write our own python function so actually let's go up to the top here and this function or any function for that matter can be wrapped as a tool so we're just going to make a function called save and actually I'm going to save some time because I don't think you guys need to watch me write this out I'm just going to copy it in called save to txt we'll take in some data and we'll take in a file name now it's important that you give the parameters a type here so that the model knows how to call this function so make sure that you type them in this case I've typed it as a string if it was a more advanced type you'd want to include that as well so what I'm doing is I'm just writing at the top of the paper you know research output the timestamp and then I'm going to write the data and that data is going to be that pantic model which you'll see in just one second okay so that's my function now once we have the function we just need to wrap it as a tool so to do that we can say savecore tool is equal to Tool and then we just do the exact same thing that we had here so I'm just going to copy this paste it I'm going to change the function to be save to txt notice I didn't call the function I just wrote the name of it and then for the name we're just going to say save text to file again make sure we don't have any spaces and then I will just kind of bring in this description you know save structured research data to a text file so it knows what this is doing that's as easy as it is to make your own custom tool so if you want a tool that calls an API for example you can do that just write a python function wrap it as a tool and you can pass as many of these to your agent that you want and really get some Advanced functionality here so now we're going to bring in the tool so same thing we're going to bring in the save to txt that's what I called it right no sorry save tool okay and then we are going to put that in the list save tool and then we can start using the save tool now the only thing is we just need to instruct the model to save this to a file so I'm going to go python main up high I'm going to say let's research I don't know what do we want to research um you know South East Asia population or something and say save to a file okay and then it should use kind of all of the tools and save this to a file and let's see if it does that and just give this a second to run so you can see it just used Wikipedia it's using what else southeast Asia okay I think it's just my terminal is cutting off a little bit here and it used the save text to file and then finish the chain gave us the output and now you can see we have our research output txt file and inside of here we get our timestamp we get our topic and we get all of this information you can see the region has relatively young population blah blah blah blah blah and goes through um kind of all of the details so there you go we have just completed the project and built an agent from scratch in Python that has access to various tools this is super cool we are really just scratching the surface with what is possible here I just wanted to give you a video that kind of overviewed the main components the main topics that make up like 80% of the Asian applications and this really does get you quite far and allows you to build some really cool stuff so I will leave the code link here in the description in case you want to check out the GitHub which will have all of this content you can just copy it and do whatever you want with it if you enjoyed the video make sure to leave a like subscribe to the channel and I will see you in the next one [Music]
2025-03-18 13:00