1 00:00:00,05 --> 00:00:02,03 - [Laurence] It's time to write some code. 2 00:00:02,03 --> 00:00:04,00 So, in this video, we're going to build 3 00:00:04,00 --> 00:00:06,00 a very simple Hello World agent, 4 00:00:06,00 --> 00:00:08,01 and this will help us see the concepts 5 00:00:08,01 --> 00:00:12,01 of intent, planning, tools, and reflection in action. 6 00:00:12,01 --> 00:00:13,09 Our agent's goal is going to be very simple, 7 00:00:13,09 --> 00:00:15,05 it's to greet the user, 8 00:00:15,05 --> 00:00:17,05 but instead of just printing a static message, 9 00:00:17,05 --> 00:00:20,07 it's going to use a tool to generate the greeting. 10 00:00:20,07 --> 00:00:22,04 So, let's get started with this one, 11 00:00:22,04 --> 00:00:23,09 and I've started by just creating 12 00:00:23,09 --> 00:00:26,04 this file called hello_agent.py, 13 00:00:26,04 --> 00:00:28,08 and I'm going to start coding in it right away. 14 00:00:28,08 --> 00:00:31,00 Okay, first of all is to define the tool, 15 00:00:31,00 --> 00:00:34,02 so I'm going to call the tool create_greeting. 16 00:00:34,02 --> 00:00:36,07 It's going to take the name as a string. 17 00:00:36,07 --> 00:00:38,09 I'll just have that return a string. 18 00:00:38,09 --> 00:00:43,04 And then here I'm going to return, "Hello," 19 00:00:43,04 --> 00:00:46,07 to the name that was passed in. 20 00:00:46,07 --> 00:00:49,01 And maybe just something simple like, 21 00:00:49,01 --> 00:00:52,04 "Welcome to the world 22 00:00:52,04 --> 00:00:55,05 of AI agents." 23 00:00:55,05 --> 00:00:58,05 Okay, so that's my very simple tool. 24 00:00:58,05 --> 00:01:00,06 So, now let's take a look at the main logic 25 00:01:00,06 --> 00:01:02,01 of the actual agent itself, 26 00:01:02,01 --> 00:01:06,08 so I'm just going to call that run_agent. 27 00:01:06,08 --> 00:01:10,05 So, step one is going to be to understand the intent. 28 00:01:10,05 --> 00:01:13,02 Now in this case, understanding the intent is very simple. 29 00:01:13,02 --> 00:01:15,04 We're just getting the user's name. 30 00:01:15,04 --> 00:01:16,08 Typically with an agent, 31 00:01:16,08 --> 00:01:19,01 you're going to have like a vague thing 32 00:01:19,01 --> 00:01:22,04 that you want the agent to do that's an unstructured text, 33 00:01:22,04 --> 00:01:25,01 like, "Find me a place where I could hike today," 34 00:01:25,01 --> 00:01:27,06 or, "Find me a restaurant that serves my favorite dish 35 00:01:27,06 --> 00:01:30,07 that's open after nine o'clock on Mondays 36 00:01:30,07 --> 00:01:33,05 and is within 25 miles of me." 37 00:01:33,05 --> 00:01:35,03 And the whole idea of intent is being able 38 00:01:35,03 --> 00:01:37,02 to take that big block of text 39 00:01:37,02 --> 00:01:39,04 and really understand what the user wants. 40 00:01:39,04 --> 00:01:41,08 It's one of the magical things of large language models 41 00:01:41,08 --> 00:01:45,01 compared to old-style natural language processing, 42 00:01:45,01 --> 00:01:47,01 where you can throw that block of text and say, 43 00:01:47,01 --> 00:01:48,03 "Help me figure out the intent. 44 00:01:48,03 --> 00:01:49,08 What does the person want to do? 45 00:01:49,08 --> 00:01:52,00 Do they want to hike, do they want to go to a restaurant? 46 00:01:52,00 --> 00:01:53,00 Tell me those details." 47 00:01:53,00 --> 00:01:54,05 And then to turn that unstructured data 48 00:01:54,05 --> 00:01:55,09 into structured data. 49 00:01:55,09 --> 00:01:58,08 But in this case, the agent is much, much simpler than that. 50 00:01:58,08 --> 00:02:01,09 The intent is to just figure out the person's name 51 00:02:01,09 --> 00:02:03,03 so that you can say hello to them. 52 00:02:03,03 --> 00:02:05,07 And in this case, we're not going to bother with an LLM, 53 00:02:05,07 --> 00:02:07,05 we're just going to ask them their name. 54 00:02:07,05 --> 00:02:10,07 So, then the second part is when your agent understands 55 00:02:10,07 --> 00:02:12,06 what the tools are available to it, 56 00:02:12,06 --> 00:02:15,00 it's got to plan to be able to use those. 57 00:02:15,00 --> 00:02:16,06 So, I'm going to put the plan together, 58 00:02:16,06 --> 00:02:18,03 but of course, here my plan 59 00:02:18,03 --> 00:02:19,09 is just to use the greeting tool. 60 00:02:19,09 --> 00:02:22,02 We're just going to say hello to that person. 61 00:02:22,02 --> 00:02:25,02 So, my plan is I'll just put out something, 62 00:02:25,02 --> 00:02:28,06 I'll print that the agent is going to say, 63 00:02:28,06 --> 00:02:32,09 "I should use the create greeting tool." 64 00:02:32,09 --> 00:02:34,02 I know I'm cheating a little bit, 65 00:02:34,02 --> 00:02:37,08 but the whole idea is just to show that level of 66 00:02:37,08 --> 00:02:40,09 breaking it down into the steps that an agent would do. 67 00:02:40,09 --> 00:02:43,08 And again, typically, if you're thinking about 68 00:02:43,08 --> 00:02:45,08 the vague intent that I mentioned earlier on, 69 00:02:45,08 --> 00:02:48,01 I would like to have dinner of a particular cuisine, 70 00:02:48,01 --> 00:02:50,04 or I'd like to hike in a particular place. 71 00:02:50,04 --> 00:02:53,00 Then once you've established the intent from that, 72 00:02:53,00 --> 00:02:56,00 then you could also call a large language model to say, 73 00:02:56,00 --> 00:02:57,07 okay, my user wants to do this. 74 00:02:57,07 --> 00:02:59,06 These are the tools that are available to me. 75 00:02:59,06 --> 00:03:02,02 Help me break it down into a plan to use those tools, 76 00:03:02,02 --> 00:03:03,03 that type of thing. 77 00:03:03,03 --> 00:03:05,09 But in this case, I just have a single tool 78 00:03:05,09 --> 00:03:09,08 and I'm going to bypass all of that and just create a greeting. 79 00:03:09,08 --> 00:03:13,05 So, create_greeting, and the user has entered their name. 80 00:03:13,05 --> 00:03:16,05 So, now I'll call that create greeting tool to do that. 81 00:03:16,05 --> 00:03:17,05 And then finally, of course, 82 00:03:17,05 --> 00:03:20,05 the final step was to use the tools. 83 00:03:20,05 --> 00:03:21,09 I should put that here. 84 00:03:21,09 --> 00:03:25,02 This we're actually using the tools was step three. 85 00:03:25,02 --> 00:03:28,05 And then finally step four is to reflect on what we've done. 86 00:03:28,05 --> 00:03:31,09 And in this case, reflecting would be, did it work? 87 00:03:31,09 --> 00:03:33,00 Did we create a greeting? 88 00:03:33,00 --> 00:03:34,06 Did something go wrong? 89 00:03:34,06 --> 00:03:37,04 And because I'm returning my greeting, I could just say, 90 00:03:37,04 --> 00:03:39,08 if greeting, 91 00:03:39,08 --> 00:03:42,09 print, 92 00:03:42,09 --> 00:03:44,04 agent 93 00:03:44,04 --> 00:03:46,01 says 94 00:03:46,01 --> 00:03:48,07 the greeting that was returned. 95 00:03:48,07 --> 00:03:51,09 Otherwise it failed on the greeting, 96 00:03:51,09 --> 00:03:54,01 so I'm just going to say, print, 97 00:03:54,01 --> 00:03:55,08 agent says 98 00:03:55,08 --> 00:03:57,05 I failed 99 00:03:57,05 --> 00:04:01,00 to create a greeting. 100 00:04:01,00 --> 00:04:01,09 If you wanted to make it 101 00:04:01,09 --> 00:04:03,05 a little bit more sophisticated there, 102 00:04:03,05 --> 00:04:06,00 you could have it do something like, 103 00:04:06,00 --> 00:04:07,03 if it failed to create the greeting, 104 00:04:07,03 --> 00:04:10,04 maybe it could go back and ask or tell the user to be 105 00:04:10,04 --> 00:04:12,08 more specific about the data that they put in, 106 00:04:12,08 --> 00:04:14,04 or something along those lines. 107 00:04:14,04 --> 00:04:16,07 But again, this is just a very simple 108 00:04:16,07 --> 00:04:19,04 Hello World-type thing. 109 00:04:19,04 --> 00:04:21,08 And we just want to show all of the steps 110 00:04:21,08 --> 00:04:24,03 that would be in creating an agent. 111 00:04:24,03 --> 00:04:26,05 So, if you're a Python developer, you'll be like, 112 00:04:26,05 --> 00:04:28,05 why on earth is he doing it this way? 113 00:04:28,05 --> 00:04:30,02 There's a much simpler way of doing that. 114 00:04:30,02 --> 00:04:32,03 All you have to do is input and print. 115 00:04:32,03 --> 00:04:33,04 But the whole idea here 116 00:04:33,04 --> 00:04:35,07 is to start getting into that mindset 117 00:04:35,07 --> 00:04:37,09 when you're building an agentic AI 118 00:04:37,09 --> 00:04:40,06 to think about breaking it into those steps 119 00:04:40,06 --> 00:04:43,05 of understanding intent, making a plan, 120 00:04:43,05 --> 00:04:45,03 using tools to achieve that plan, 121 00:04:45,03 --> 00:04:47,01 and then reflecting on them. 122 00:04:47,01 --> 00:04:49,04 So, this is our app, let's see what happens if we run it. 123 00:04:49,04 --> 00:04:52,08 Hopefully I don't have any bugs in there, but... 124 00:04:52,08 --> 00:04:54,07 So, now to run it, I'm just going to do 125 00:04:54,07 --> 00:04:57,03 python hello_agent.py. 126 00:04:57,03 --> 00:05:00,08 It's going to ask me my name, I'll tell it the truth, Laurence. 127 00:05:00,08 --> 00:05:01,07 And you can see here, 128 00:05:01,07 --> 00:05:03,06 the agent decided to use the greeting tool, 129 00:05:03,06 --> 00:05:05,03 the create greeting tool created the string, 130 00:05:05,03 --> 00:05:07,06 "Hello, Laurence, welcome to the world of AI agents." 131 00:05:07,06 --> 00:05:09,09 And then the agent outputted that to me. 132 00:05:09,09 --> 00:05:14,01 So, let's quickly break that down into our agentic loop. 133 00:05:14,01 --> 00:05:15,05 And you'll hear me saying this one a lot, 134 00:05:15,05 --> 00:05:17,04 but it is important. 135 00:05:17,04 --> 00:05:19,04 Breaking it down your applications 136 00:05:19,04 --> 00:05:21,05 when you want to build AI agents 137 00:05:21,05 --> 00:05:22,08 is to really think about 138 00:05:22,08 --> 00:05:25,07 how you're going to meet those four steps, 139 00:05:25,07 --> 00:05:27,04 understanding the user's intent. 140 00:05:27,04 --> 00:05:29,08 Now, you can do that with AI or not. 141 00:05:29,08 --> 00:05:32,08 Making a plan, you can do that with AI or not. 142 00:05:32,08 --> 00:05:34,09 Using the tools, here's the important part 143 00:05:34,09 --> 00:05:37,01 where the agent executes the plan 144 00:05:37,01 --> 00:05:39,06 by calling whatever tools are available to it. 145 00:05:39,06 --> 00:05:42,02 In this case, it was just that create greeting function, 146 00:05:42,02 --> 00:05:43,05 and then reflect. 147 00:05:43,05 --> 00:05:45,00 And in this case, the agent was able 148 00:05:45,00 --> 00:05:46,04 to check the output of the tool. 149 00:05:46,04 --> 00:05:48,05 It got a valid greeting, so it printed it. 150 00:05:48,05 --> 00:05:50,07 Otherwise it would acknowledge that it failed. 151 00:05:50,07 --> 00:05:51,05 And there you have it. 152 00:05:51,05 --> 00:05:55,02 This is the very first, very, very basic AI agent. 153 00:05:55,02 --> 00:05:57,01 The next module, we're going to start building out 154 00:05:57,01 --> 00:06:00,00 our much more advanced hiking assistant agent.