1 00:00:00,05 --> 00:00:02,05 - [Instructor] So we've seen our main.py file 2 00:00:02,05 --> 00:00:05,04 and all of the other files that are used to make the agent, 3 00:00:05,04 --> 00:00:08,07 but I want to build the main.py file from the ground up 4 00:00:08,07 --> 00:00:10,08 so that you can get a really clear picture 5 00:00:10,08 --> 00:00:13,01 of how the agent's logic will flow 6 00:00:13,01 --> 00:00:16,02 and how each of the tools that we're using is going to be used 7 00:00:16,02 --> 00:00:18,06 then in sequence, as well as how reflection is used 8 00:00:18,06 --> 00:00:21,07 between them, as well as how reflection is going to be used 9 00:00:21,07 --> 00:00:23,03 with the backend AI. 10 00:00:23,03 --> 00:00:25,08 So we're going to start with this empty main.py file 11 00:00:25,08 --> 00:00:27,09 or nearly empty main.py file. 12 00:00:27,09 --> 00:00:29,06 All that we have is our imports 13 00:00:29,06 --> 00:00:31,08 and the stubbed main function. 14 00:00:31,08 --> 00:00:33,09 And we're going to use this as our starting point. 15 00:00:33,09 --> 00:00:35,07 The first thing that our agent needs to do 16 00:00:35,07 --> 00:00:38,00 is then figure out where we are. 17 00:00:38,00 --> 00:00:40,04 So I'm going to start with this main function 18 00:00:40,04 --> 00:00:43,00 and I'm going to add a call to our first tool, 19 00:00:43,00 --> 00:00:46,08 and our first tool is to detect our location. 20 00:00:46,08 --> 00:00:48,06 I'm not going to bore you by typing all the code, 21 00:00:48,06 --> 00:00:50,05 I'm just going to paste it in, 22 00:00:50,05 --> 00:00:52,01 and we'll see here that what it's going to do 23 00:00:52,01 --> 00:00:54,02 is we're going to say we're detecting our location. 24 00:00:54,02 --> 00:00:56,07 It's going to use the get current location tool 25 00:00:56,07 --> 00:00:59,06 to return a latitude, a longitude, and a state. 26 00:00:59,06 --> 00:01:02,05 If any of these is set, then latitude will be set. 27 00:01:02,05 --> 00:01:04,08 So we want to check if latitude is not set, 28 00:01:04,08 --> 00:01:06,03 then we don't have anything, 29 00:01:06,03 --> 00:01:09,04 we're not detecting the location, restart, try again, 30 00:01:09,04 --> 00:01:11,03 and then we'll exit the main function. 31 00:01:11,03 --> 00:01:13,08 Then I'm just going to say we're detecting the state. 32 00:01:13,08 --> 00:01:16,02 Simple enough, and we use the state later on 33 00:01:16,02 --> 00:01:18,06 for the national parks API. 34 00:01:18,06 --> 00:01:20,08 For the weather API, we actually use the latitude 35 00:01:20,08 --> 00:01:23,02 and longitude and let's take a look at that. 36 00:01:23,02 --> 00:01:25,00 So now that we have a location, 37 00:01:25,00 --> 00:01:28,00 we want to use the weather tool for that location 38 00:01:28,00 --> 00:01:30,00 to be able to get our forecast. 39 00:01:30,00 --> 00:01:33,04 So I'll do the same thing, I'm just going to paste in the code. 40 00:01:33,04 --> 00:01:36,00 So we'll see here, step two is getting our weather, 41 00:01:36,00 --> 00:01:39,02 we're checking the weather near us, we're using the latitude 42 00:01:39,02 --> 00:01:41,07 and longitude that we've just arrived from 43 00:01:41,07 --> 00:01:45,04 get current location to be able to get the weather. 44 00:01:45,04 --> 00:01:48,02 And as before, if there's no weather data, we'll say, 45 00:01:48,02 --> 00:01:50,07 "Look, couldn't get any, please try again later" 46 00:01:50,07 --> 00:01:52,01 and we'll exit out. 47 00:01:52,01 --> 00:01:55,00 Otherwise, we'll also use another helper function 48 00:01:55,00 --> 00:01:56,01 that we'll see later on. 49 00:01:56,01 --> 00:01:59,00 Another tool, which is to actually get a summary 50 00:01:59,00 --> 00:02:01,04 of the weather, so in plain English summary 51 00:02:01,04 --> 00:02:04,05 instead of just like 23 degrees centigrade, 52 00:02:04,05 --> 00:02:06,05 2% rain, that kind of thing. 53 00:02:06,05 --> 00:02:08,05 This will actually give us a plain English summary 54 00:02:08,05 --> 00:02:10,05 which even gives us details of what will happen 55 00:02:10,05 --> 00:02:12,02 later in the day, for example, 56 00:02:12,02 --> 00:02:14,03 and print out that plain English summary. 57 00:02:14,03 --> 00:02:16,05 Now this might be a little bit counterintuitive 58 00:02:16,05 --> 00:02:19,00 when programming, 'cause often when we're programming, 59 00:02:19,00 --> 00:02:22,05 we want to have those definitive structured pieces of data 60 00:02:22,05 --> 00:02:25,02 that we can make decisions on with if then rules. 61 00:02:25,02 --> 00:02:27,08 But remembering that we're using a backend AI, 62 00:02:27,08 --> 00:02:30,02 the AI can actually reason over the plain English, 63 00:02:30,02 --> 00:02:33,00 and the more data it gets, the better it can do. 64 00:02:33,00 --> 00:02:34,08 Okay, so we've gotten our location, 65 00:02:34,08 --> 00:02:36,07 we've gotten the weather for our location. 66 00:02:36,07 --> 00:02:37,07 These are the tool things. 67 00:02:37,07 --> 00:02:39,09 So the first reflection that we want to do, 68 00:02:39,09 --> 00:02:44,03 this first key moment is, "Is today a good day for a hike?" 69 00:02:44,03 --> 00:02:47,08 Instead of just proceeding, let's have the agent reflect 70 00:02:47,08 --> 00:02:49,04 by using a large language model, 71 00:02:49,04 --> 00:02:52,07 in this case, the G-P-T-O-S-S, to have an opinion 72 00:02:52,07 --> 00:02:54,08 and to make a decision for us. 73 00:02:54,08 --> 00:02:57,03 And to do that, we're going to query the model, 74 00:02:57,03 --> 00:02:58,08 and I'm going to need a new helper function 75 00:02:58,08 --> 00:03:00,07 that I call query model. 76 00:03:00,07 --> 00:03:03,04 And I'm going to just paste that in and then explain it 77 00:03:03,04 --> 00:03:05,05 because there's a lot going on in here. 78 00:03:05,05 --> 00:03:08,02 Okay, so query model, that's going to take a system prompt 79 00:03:08,02 --> 00:03:09,09 and a user prompt, 80 00:03:09,09 --> 00:03:12,06 and it's going to create this messages payload 81 00:03:12,06 --> 00:03:15,04 with the role system containing the system prompt 82 00:03:15,04 --> 00:03:18,03 and the role user containing the user prompt. 83 00:03:18,03 --> 00:03:21,04 When programming against the GPT backend, 84 00:03:21,04 --> 00:03:26,01 it expects messages to be formatted in this particular way. 85 00:03:26,01 --> 00:03:30,00 You'll see one system prompt and then multiple user prompts 86 00:03:30,00 --> 00:03:31,02 and then multiple answers 87 00:03:31,02 --> 00:03:33,07 to those user prompts in a chat history. 88 00:03:33,07 --> 00:03:36,08 But in this case, we're just doing one system, one user. 89 00:03:36,08 --> 00:03:40,01 The system prompt is used to really kick the model 90 00:03:40,01 --> 00:03:42,06 into acting in a particular way. 91 00:03:42,06 --> 00:03:44,05 You've probably seen prompts like you are 92 00:03:44,05 --> 00:03:47,00 an expert in dot, dot, dot, dot. 93 00:03:47,00 --> 00:03:48,02 That's the system prompt. 94 00:03:48,02 --> 00:03:51,04 The user prompt is then we're asking it for something. 95 00:03:51,04 --> 00:03:53,06 So in this particular case, the system prompt, 96 00:03:53,06 --> 00:03:56,00 we're going to say something along the lines of, 97 00:03:56,00 --> 00:03:58,08 "Here's the weather, gimme an opinion, yes or no." 98 00:03:58,08 --> 00:04:02,03 And then the user prompt is this particular weather is this. 99 00:04:02,03 --> 00:04:04,02 So this helper function will do that. 100 00:04:04,02 --> 00:04:08,01 It's creating the messages in the format that GPT expects, 101 00:04:08,01 --> 00:04:12,02 and then it's using a LAMA to chat with GPT 102 00:04:12,02 --> 00:04:14,07 passing in those messages in this format 103 00:04:14,07 --> 00:04:16,09 and getting the response back. 104 00:04:16,09 --> 00:04:18,06 So let's look at how we use that now 105 00:04:18,06 --> 00:04:21,00 back into our main function. 106 00:04:21,00 --> 00:04:24,00 So we've done our location, we've done our weather, 107 00:04:24,00 --> 00:04:26,03 now we want to reason across that weather. 108 00:04:26,03 --> 00:04:30,06 So I'm going to paste the code in so that we can see. 109 00:04:30,06 --> 00:04:34,01 okay, so we had our weather summary, we printed that out, 110 00:04:34,01 --> 00:04:37,00 and now we want to decide if it's good enough for a hike. 111 00:04:37,00 --> 00:04:39,06 Here's the prompt that we're going to use. 112 00:04:39,06 --> 00:04:42,04 Based on this forecast, is it a good day for a hike? 113 00:04:42,04 --> 00:04:43,08 Remember I said it would be 114 00:04:43,08 --> 00:04:45,06 the weather summary is plain English. 115 00:04:45,06 --> 00:04:47,02 Instead of just saying 30 degrees, 116 00:04:47,02 --> 00:04:49,04 2% rain or something like that, 117 00:04:49,04 --> 00:04:51,04 it's a plain English summary that we get back 118 00:04:51,04 --> 00:04:52,04 from the weather service. 119 00:04:52,04 --> 00:04:54,09 That could include time-based thing, "Sunny now, 120 00:04:54,09 --> 00:04:57,01 rainy later," that type of stuff. 121 00:04:57,01 --> 00:04:59,03 So based on that forecast, we're going to ask, 122 00:04:59,03 --> 00:05:01,01 is it a good day for a hike? 123 00:05:01,01 --> 00:05:03,02 Now, one of the things when you ask an AI 124 00:05:03,02 --> 00:05:05,01 something like this is that the answers 125 00:05:05,01 --> 00:05:07,08 can be very, very unstructured. 126 00:05:07,08 --> 00:05:10,02 And often with post-training on a lot of AIs, 127 00:05:10,02 --> 00:05:13,02 they've been post trained to be more conversational 128 00:05:13,02 --> 00:05:16,06 in nature, and as a result, it's very, very unstructured. 129 00:05:16,06 --> 00:05:18,03 So it might give you answers like, 130 00:05:18,03 --> 00:05:19,08 "Today is a good day for hike. 131 00:05:19,08 --> 00:05:21,06 Today is a great day for hike. 132 00:05:21,06 --> 00:05:24,04 Today might be a good day for hike" and stuff like that, 133 00:05:24,04 --> 00:05:28,02 but we want to have a clear opinion that's just yes or no, 134 00:05:28,02 --> 00:05:30,09 and that's what we use the system prompt for. 135 00:05:30,09 --> 00:05:33,02 So I'm just saying, here's the weather system prompt, 136 00:05:33,02 --> 00:05:35,02 and that's you're an assistant that determines 137 00:05:35,02 --> 00:05:36,09 if the weather is good for hiking, 138 00:05:36,09 --> 00:05:38,09 respond only with yes or no. 139 00:05:38,09 --> 00:05:40,02 We are using the system role 140 00:05:40,02 --> 00:05:43,02 to make the LLM be very opinionated 141 00:05:43,02 --> 00:05:46,02 and very, very clear with its response. 142 00:05:46,02 --> 00:05:49,06 So those are the system prompts, the user prompt, 143 00:05:49,06 --> 00:05:51,04 and then the payload of course 144 00:05:51,04 --> 00:05:54,08 that we pass in will be the weather summary data 145 00:05:54,08 --> 00:05:56,09 and they'll get this decision back. 146 00:05:56,09 --> 00:06:00,00 And if the decision that comes back has a no in it, 147 00:06:00,00 --> 00:06:02,05 then we're saying the reasoning that the model is done 148 00:06:02,05 --> 00:06:05,01 is to say, "Today is not a good day for hiking." 149 00:06:05,01 --> 00:06:08,04 Okay, so now that we've done that one, 150 00:06:08,04 --> 00:06:12,00 if we've passed this and the model has the opinion 151 00:06:12,00 --> 00:06:14,02 that today is a good day for hiking, 152 00:06:14,02 --> 00:06:17,04 well then the next part is of course to take a look 153 00:06:17,04 --> 00:06:20,07 at the national parks and to take a look at the trails 154 00:06:20,07 --> 00:06:23,04 in those particular national parks to see 155 00:06:23,04 --> 00:06:25,03 if they are good days for it. 156 00:06:25,03 --> 00:06:28,06 So I've pasted the code in, let's take a look at it. 157 00:06:28,06 --> 00:06:31,05 So our answer is not no, means it's yes. 158 00:06:31,05 --> 00:06:32,09 So the weather's looking good, 159 00:06:32,09 --> 00:06:36,01 and let's start searching for nearby parks and trails. 160 00:06:36,01 --> 00:06:38,09 First thing we're going to do is call get parks. 161 00:06:38,09 --> 00:06:42,00 What get parks is going to do for our particular state 162 00:06:42,00 --> 00:06:44,07 is find national parks that are close to us. 163 00:06:44,07 --> 00:06:46,05 And if there aren't any, it's going to end 164 00:06:46,05 --> 00:06:49,09 because hopefully there are national parks in our state. 165 00:06:49,09 --> 00:06:53,04 Once we've done that, now it's going to go through that list 166 00:06:53,04 --> 00:06:58,00 of parks and each park is going to have a number of trails. 167 00:06:58,00 --> 00:07:00,06 So we're going to say for every park, 168 00:07:00,06 --> 00:07:03,03 let's get the name of that park and let's get 169 00:07:03,03 --> 00:07:06,02 all of the trails for that park. 170 00:07:06,02 --> 00:07:10,08 And once we have done that, now we're going to create 171 00:07:10,08 --> 00:07:14,03 this dictionary that I call parks and trails 172 00:07:14,03 --> 00:07:18,01 so that every entry in the dictionary for a park 173 00:07:18,01 --> 00:07:21,08 will have a number of entries for the trails for it. 174 00:07:21,08 --> 00:07:24,07 So pretty complex interaction going on here, 175 00:07:24,07 --> 00:07:29,06 but this tool is really just searching for parks near us. 176 00:07:29,06 --> 00:07:32,05 Then for each of those parks, retrieving the details 177 00:07:32,05 --> 00:07:35,06 of that park, getting the list of trails for that park, 178 00:07:35,06 --> 00:07:37,06 and putting them into a big dictionary 179 00:07:37,06 --> 00:07:39,08 so that we have them all in one place. 180 00:07:39,08 --> 00:07:42,00 And now once we have that big blob of data, 181 00:07:42,00 --> 00:07:43,07 here's what AI is really good at, 182 00:07:43,07 --> 00:07:45,04 getting that big blob of data 183 00:07:45,04 --> 00:07:48,01 and having the AI understand that on our behalf 184 00:07:48,01 --> 00:07:50,01 and give us recommendations. 185 00:07:50,01 --> 00:07:52,08 So now the final and most important reflection 186 00:07:52,08 --> 00:07:57,00 will be we're going to ask the model to act as an expert guide 187 00:07:57,00 --> 00:07:59,00 and give us opinionated recommendations 188 00:07:59,00 --> 00:08:01,03 based on all of that data. 189 00:08:01,03 --> 00:08:08,03 So what's that going to look like in code? 190 00:08:08,03 --> 00:08:11,08 Okay, so I've pasted it in here 191 00:08:11,08 --> 00:08:13,03 and I'll just step through it. 192 00:08:13,03 --> 00:08:15,08 So remembering the system prompt that I spoke 193 00:08:15,08 --> 00:08:18,08 about earlier on, we're going to use yet another system prompt, 194 00:08:18,08 --> 00:08:21,09 but this time I'm going to be passing at this big blob 195 00:08:21,09 --> 00:08:25,04 of data about national parks and about the trails. 196 00:08:25,04 --> 00:08:28,04 And I'm going to say, "You're an expert hiking guide. 197 00:08:28,04 --> 00:08:31,09 Your task is to analyze the following list of parks 198 00:08:31,09 --> 00:08:35,09 and trails and recommend the top two to three options." 199 00:08:35,09 --> 00:08:39,02 And then I had my dictionary of parks and trails, 200 00:08:39,02 --> 00:08:40,07 and I'm going to go through that 201 00:08:40,07 --> 00:08:42,09 and just create a big string where it's going 202 00:08:42,09 --> 00:08:46,05 to be park colon, the park name, and then I'll indent 203 00:08:46,05 --> 00:08:48,06 the trail a little bit with the trail name. 204 00:08:48,06 --> 00:08:52,06 So it'll be park one trails, A, B, C, park two trails, 205 00:08:52,06 --> 00:08:56,04 D, E, F, that kind of thing as a big data blob in a string 206 00:08:56,04 --> 00:08:57,07 because when we converse with it, 207 00:08:57,07 --> 00:08:58,09 we're not going to pass the dictionary, 208 00:08:58,09 --> 00:09:00,06 we're just going to pass the string. 209 00:09:00,06 --> 00:09:04,01 And then the system prompts that we just defined, 210 00:09:04,01 --> 00:09:06,03 here's the available parks and trails, 211 00:09:06,03 --> 00:09:08,05 and then that big string that we just generated 212 00:09:08,05 --> 00:09:10,08 off of the list of parks and trails, 213 00:09:10,08 --> 00:09:13,04 and then we'll get back the hiking recommendations. 214 00:09:13,04 --> 00:09:16,04 So we formatted all of our data into that clean prompt. 215 00:09:16,04 --> 00:09:18,04 We've given the model, this persona 216 00:09:18,04 --> 00:09:20,04 that it's an expert hiking guide, 217 00:09:20,04 --> 00:09:23,02 and we're going to ask it for its opinion on the final answer. 218 00:09:23,02 --> 00:09:24,06 And that's it. 219 00:09:24,06 --> 00:09:27,02 Okay, so let's take a look at what this will look like 220 00:09:27,02 --> 00:09:32,04 when we actually run it. 221 00:09:32,04 --> 00:09:35,06 So we can see, the first step is it's detecting my location. 222 00:09:35,06 --> 00:09:36,04 It got that. 223 00:09:36,04 --> 00:09:38,03 Now it's detecting the weather for me. 224 00:09:38,03 --> 00:09:40,07 Here's the summary that it got in plain English, 225 00:09:40,07 --> 00:09:42,08 overcast, average temperature, 226 00:09:42,08 --> 00:09:46,00 maximum precipitation, probability is very low. 227 00:09:46,00 --> 00:09:48,05 So we asked it for an opinion, should we hike today? 228 00:09:48,05 --> 00:09:50,02 And the model said, "Yes, we should. 229 00:09:50,02 --> 00:09:51,09 It's a good day for hiking." 230 00:09:51,09 --> 00:09:54,08 So it goes to look for nearby parks and trails, 231 00:09:54,08 --> 00:09:56,05 and then it analyzes them. 232 00:09:56,05 --> 00:09:59,01 Once it's analyzed them, it's now going to ask the model 233 00:09:59,01 --> 00:10:00,08 for its recommendations. 234 00:10:00,08 --> 00:10:04,02 And now here are the recommendations that came out of it. 235 00:10:04,02 --> 00:10:07,03 The first one is this, Muir Woods National Monument 236 00:10:07,03 --> 00:10:08,07 to hike the main trail. 237 00:10:08,07 --> 00:10:10,00 What I love about this one 238 00:10:10,00 --> 00:10:13,00 is like it's understanding today's weather 239 00:10:13,00 --> 00:10:15,03 and it's understanding the type of hiking 240 00:10:15,03 --> 00:10:17,03 that's good for this type of weather. 241 00:10:17,03 --> 00:10:18,06 Imagine if it was cold, 242 00:10:18,06 --> 00:10:20,01 it would give me different recommendations, 243 00:10:20,01 --> 00:10:21,00 or if it was hot, 244 00:10:21,00 --> 00:10:22,09 it would give me different recommendations. 245 00:10:22,09 --> 00:10:25,04 But given that today is quite a cool day, 246 00:10:25,04 --> 00:10:28,02 it's like saying, "Hey look, maybe a classic easy stroll 247 00:10:28,02 --> 00:10:31,02 through redwoods, perfect for relaxing, 248 00:10:31,02 --> 00:10:33,03 one with unforgettable scenery." 249 00:10:33,03 --> 00:10:37,04 This English language is being generated by the model 250 00:10:37,04 --> 00:10:40,08 and then feeding my agents and giving me an agent 251 00:10:40,08 --> 00:10:44,00 that's not just giving me rough recommendations, 252 00:10:44,00 --> 00:10:46,03 but also opinionated recommendations 253 00:10:46,03 --> 00:10:48,00 that are well-described. 254 00:10:48,00 --> 00:10:50,07 So this is the whole idea of how the agent will work. 255 00:10:50,07 --> 00:10:52,08 We've seen step by step to build it, 256 00:10:52,08 --> 00:10:54,09 but the one thing that we haven't looked at in detail yet 257 00:10:54,09 --> 00:10:56,03 are the tools. 258 00:10:56,03 --> 00:10:58,02 So let's start looking at those next 259 00:10:58,02 --> 00:11:00,00 beginning with the location tool.