[0:00] Today I'm going to show you how to build [0:01] an AI agent in Python in less than 10 [0:05] minutes. So let's get started. To begin [0:07] our project, we need to open up a folder [0:09] in some type of code editor. In my case, [0:11] I'm going to be using PyCharm, but you [0:12] can use anything that you want. I do [0:14] typically recommend PyCharm for large [0:16] Python projects because it is well [0:18] designed for Python and I have a [0:20] long-term partnership with them. So if [0:21] you want to try it out for free, you can [0:23] do that by clicking the link in the [0:24] description. Now I've made a project [0:26] here or made a folder called 10minute [0:28] agent. So open up some kind of folder. [0:30] We're then going to go to our terminal [0:31] and we're going to install the [0:32] dependencies that we need. Now to [0:34] install these, I recommend using uv and [0:36] by starting to do uv init and then dot. [0:39] Do this in the directory where you want [0:41] to create this project. So we're going [0:43] to initialize our project. If you don't [0:45] already have uv installed, I'll leave a [0:46] video on screen that shows you how it [0:48] works. From here, we're going to open up [0:50] main.py, delete everything inside of [0:52] there, and then start installing our [0:54] dependencies. So from our terminal, [0:55] we're going to type uvad. We're going to [0:57] add lang chain. We're going to add lang [1:00] graph. We're going to add python- [1:03] env. And we're going to add langchain- [1:07] open aai. Now, these are all the [1:09] dependencies that we need for making an [1:10] agent. There's multiple ways to do this. [1:12] We're going to use lang chain and [1:14] langraph, which is a really modern [1:15] approach that makes it very easy for us [1:17] to design agents in Python. So, go ahead [1:19] and press enter. Add all of those [1:21] packages and then we're good to start [1:22] writing some code. So, for this project, [1:24] I'm going to use GPT4 as my LLM, but you [1:27] can use any LLM that you want. Since I'm [1:29] using an LLM from OpenAI, I need to get [1:31] an OpenAI API key. So, I'm going to make [1:34] a new file here. I'm going to call this [1:36] env. And then inside of this file, I'm [1:39] going to make a variable that says [1:40] OpenAI_API_key. [1:42] And I'm going to fill this in with my [1:44] OpenAI key, which I'll get right now. [1:46] So, in order to get a key, you will need [1:47] an account with OpenAI. You can go to [1:49] this website right here, [1:50] platform.opai.com/api. openai.com/api [1:53] keys. You will need a credit card on [1:55] file, but this will cost you a fraction [1:56] of a few cents if you're just using it [1:58] for a demo example. So, here we're going [2:00] to go here, go ahead and press on create [2:02] secret key. We're then going to copy the [2:04] key, which we're not going to share with [2:05] anybody else, and we're going to paste [2:06] this inside of our environment variable [2:08] file. From here, we'll save, close, and [2:10] then we're ready to start building our [2:12] agent. Now, speaking of agents, today's [2:15] sponsor, Notion, just released theirs, [2:18] which is a complete gamecher. Now, [2:20] Notion's new AI agent doesn't just help. [2:22] It finishes the job for you. And as a [2:25] Notion power user, myself, this has been [2:27] absolutely amazing. Now, this thing [2:29] isn't a chatbot. It's an actual AI [2:31] teammate that lives inside of your [2:33] Notion workspace and can complete tasks [2:35] end to end. You give it a goal like [2:37] summarize last week's meeting notes into [2:40] a project update, and it figures out the [2:42] plan, pulls the info, and updates your [2:44] pages. It even notifies your teammates [2:46] as well. Now, it's mastered every Notion [2:49] building block. So, it can edit [2:50] databases, rewrite documents, audit [2:52] knowledge bases, or even draft an entire [2:55] launch plan, all exactly the way that [2:57] you would. And here's what makes it [2:59] pretty cool. Your agent actually learns [3:01] your style. You can tell it where to [3:03] file things, how to write, even what [3:05] tone to use. And over time, it builds [3:07] memories so it starts anticipating what [3:09] you need next. So, instead of you [3:11] spending your day buried in admin work, [3:13] your agent just handles it all for you. [3:16] Now, if you want to try it out, Notion [3:17] agents are available right now. Click [3:19] the link in the description and let [3:21] notion agent do your work for you. [3:23] Thanks to Notion. Now, let's get back [3:25] into it. So, because I'm trying to do [3:27] this in 10 minutes, I'm not going to [3:28] write every line of code out [3:30] individually. I'm going to copy in some [3:31] different chunks. And if you want all of [3:33] the code, I'll leave a link to it in the [3:35] description where you can download it [3:36] and mess with it from there. So, first [3:37] things first, we're going to bring in [3:39] our imports. Now, we're going to need to [3:41] import a few things from the standard [3:42] library for doing our typing. We're then [3:44] going to bring chat openai. We're going [3:46] to bring in a bunch of different [3:47] messages that we need. We're going to [3:49] import the tool and then we're going to [3:50] import the create react agent which [3:52] comes from langraph. Now we're also [3:54] going to do another import where we say [3:56] from.env [3:57] import and this is going to be load.env. [4:00] We're then going to call the load.env [4:02] function. And what this is going to do [4:03] is load our environment variables from [4:05] our environment variable file. We're [4:07] using a combination of langraph and lang [4:09] chain here in order to initialize our [4:12] agent and to get it to be able to call [4:14] tools. The thing that makes an agent an [4:16] agent is that it has access to something [4:19] outside of just a chat interface. So in [4:21] our case, we're going to provide to this [4:23] agent a series of tools that allows it [4:25] to generate kind of mock user data and [4:28] then save that data into JSON files. [4:30] Once you understand how to add one tool, [4:32] you can add as many of them as you want. [4:34] So let me copy in a few tools that our [4:36] agent is going to have access to. Then [4:38] we'll start initializing the agent and [4:40] start writing everything so we can [4:41] interact with it. So I've just added [4:43] three tools which really are just Python [4:45] functions to my file. Let's go through [4:47] them one by one so you can understand [4:49] the kind of composition of a tool. Now a [4:52] tool can simply be a Python function. So [4:54] in this case we have a tool called write [4:56] JSON. We take in some file path which is [4:58] the file that we want to write to and [5:00] then some data that we want to write and [5:02] this returns a string. Now we denote [5:04] that this is a tool by decorating it [5:06] with the at@ tool decorator which we [5:08] imported right here from langchain. Now [5:10] inside of this tool we can do anything [5:12] that we want. But we should make sure [5:14] that we return some kind of AI readable [5:17] information so that the AI understands [5:19] what this tool call actually achieved. [5:21] Now the thing that makes this a tool is [5:23] this tool decorator. But for any tool, [5:25] you need to make sure that you denote [5:27] the types of your parameters. So in this [5:29] case, we've denoted this is a string and [5:31] this is a dictionary and then the return [5:33] type of the function. This is [5:34] information that will be passed to our [5:36] LLM or our agent. So it knows which tool [5:39] to call and how to call that tool. It's [5:42] also important that you write a dock [5:43] string. That's something inside of these [5:45] three uh quotes right here that [5:47] describes what the function does or what [5:50] the tool does so the agent understands [5:52] which tools to call. So in this case we [5:54] have write a Python dictionary as JSON [5:56] to a file with pretty formatting. So now [5:58] the agent knows this is the tool that I [6:00] can use when I need to do that. Moving [6:02] on, we have our next tool which can read [6:03] a JSON file. Same thing we denote the [6:06] parameters and their types as well as [6:07] the return type of the function. And [6:09] then we have a dock string that [6:10] describes when to use this tool or this [6:12] function. Okay. And we return good error [6:14] messages explaining what's going on. [6:17] Lastly, we have another tool. This can [6:18] generate sample users. We take in a list [6:20] of first names, a list of last names, a [6:22] list of domains, and a minimum and [6:24] maximum age. And then we generate some [6:26] sample user data with all of this error [6:28] and validation checking and pass that [6:30] back to the LLM. You can have any tools [6:33] that you want. I'm just giving you three [6:34] here so you can see a bit of diversity [6:36] in terms of how they work. Okay, so now [6:38] that we have our tools, we need to [6:39] create an LLM and an agent and [6:41] essentially give these tools to our LLM. [6:44] So let's start by defining a list of [6:45] tools, which we can do with this [6:47] variable right here. Then let's define [6:49] our model. So the model that we're going [6:51] to use or the LLM is going to be GPT4 [6:54] mini. You can use any LLM that you want [6:56] here, but this is the one that I'll use [6:57] for this video. Then we're going to [6:59] define what I call our system message or [7:02] our system prompt. Now that's not just [7:04] what I call it, that's what it's called. [7:06] But essentially, this tells the system [7:08] what it's supposed to be doing so that [7:10] it has some more context in terms of how [7:12] it should behave. You can read through [7:13] this if you want, but essentially I'm [7:15] telling it that this is a data generator [7:17] agent that's able to generate sample [7:19] data for an application and then save [7:21] that in a JSON file. Now, once we have a [7:24] list of tools, an LLM or a model, and [7:26] some system message, we can create our [7:29] agent. And that's because an agent is [7:30] made up of those three components. It [7:32] can also be made up of more, but in this [7:34] case, that's what we'll use. So, we can [7:35] say agent is equal to create react [7:37] agent. We pass the LLM, we pass the list [7:40] of tools, and then we pass the system [7:42] message. Now, in order to use our agent, [7:44] we need some kind of driver code. So, [7:46] we're going to bring in a function that [7:48] looks like this, where in order to run [7:50] the agent, we take in some user input, [7:52] we can take in a history, which is all [7:54] of the previous messages. So, we can [7:55] store that and have some previous [7:57] context, and then we return an AI [8:00] message. What we're able to do is invoke [8:02] the agent. So, we say agent.invoke. [8:04] We can pass all of the previous messages [8:06] plus the new message. And then what we [8:09] can say is that we have some limit in [8:10] terms of how many tool calls this can do [8:12] which is 50. And then we can return [8:14] whatever the last response is from this [8:17] agent. If there's an error then we can [8:19] return something like this. Now what [8:21] this is going to do is automatically [8:23] respond to the most recent message for [8:25] us. So we'll get the most recent [8:27] response and it will do any tool calls [8:29] as it needs to in this step. So when we [8:32] agent on.invoke invoke and we have [8:33] access to various tools. If the agent [8:35] needs to call a tool, it will go ahead [8:36] and just call it, use the tool and then [8:39] return to us whatever the response is [8:41] that it thinks that we want. That's how [8:43] we run the agent. Now, let's bring in a [8:45] little bit more driver code just to make [8:46] this application look a bit prettier and [8:48] we can test it out. So, what I'm doing [8:50] is I'm just putting some formatting. I'm [8:52] saying, okay, let's create a list of all [8:54] of the messages we've sent so far. Let's [8:56] ask the user for some user input. If [8:57] they want to quit, we can quit. [8:59] Otherwise, we can just run the agent and [9:01] then we can print out whatever the [9:03] response was, update the history and [9:05] keep going. Now, in order to run our [9:07] agent, what we can do is open up our [9:09] terminal. So, let's go here, type uv [9:11] run, and then the name of our file, [9:13] which is main. py. This will take a [9:15] second, and then what we're able to do [9:17] is start asking the agent questions. So, [9:18] we can say something like generate five [9:21] random users. And let's see what the [9:23] result is that we get. Okay. Okay. And [9:25] if we just bring this up a little bit [9:26] larger, you can see that we have five [9:28] random users generated. Now, let's take [9:30] one of the examples here. So, maybe make [9:32] users like this. Okay. So, make users [9:35] age 25 to 35 with company.com emails and [9:38] save three of them to users.json. [9:43] And let's see what we get. Okay. And if [9:45] we have a look here, we can see a [9:46] users.json file was just created. We [9:49] have three users inside of here. And [9:51] then lastly to test this we can say what [9:54] is the oldest user in users.json [9:59] and let's see if it's able to read that [10:00] and give us the result. Okay we found [10:02] the oldest user which is Bob Johnson [10:04] here at age 25. So that is the AI agent [10:08] in Python in approximately 10 minutes. [10:11] You can add any tools that you want. You [10:13] can change the LLM. You can change the [10:14] system prompt and quite quickly you can [10:16] make something very interesting. Again, [10:18] if you want the code, it will be [10:19] available from the link in the [10:20] description. And I look forward to [10:21] seeing you in another video. [10:24] [Music]