1 00:00:00,05 --> 00:00:02,05 - [Instructor] So earlier we saw the main.py 2 00:00:02,05 --> 00:00:04,08 that really held the entire agent together 3 00:00:04,08 --> 00:00:07,07 and allowed us to plan for our hiking trip. 4 00:00:07,07 --> 00:00:10,07 The first tool that it used was a location tool 5 00:00:10,07 --> 00:00:12,00 where it was able to take a look 6 00:00:12,00 --> 00:00:14,07 at the location of the user's machine 7 00:00:14,07 --> 00:00:16,00 and determine the state 8 00:00:16,00 --> 00:00:18,03 and the latitude and longitude from that. 9 00:00:18,03 --> 00:00:21,03 So let's take a look at that tool and how it all works. 10 00:00:21,03 --> 00:00:23,06 So in that one, I have a couple of helper functions, 11 00:00:23,06 --> 00:00:25,09 get_current_location 12 00:00:25,09 --> 00:00:28,05 and state to abbreviation. 13 00:00:28,05 --> 00:00:30,07 So we're going to begin by looking at this 14 00:00:30,07 --> 00:00:32,00 get_current_location 15 00:00:32,00 --> 00:00:33,05 And the important piece of code 16 00:00:33,05 --> 00:00:36,01 that we have here is this one. 17 00:00:36,01 --> 00:00:38,01 The idea is that we want to know 18 00:00:38,01 --> 00:00:40,04 the latitude, longitude, and state, 19 00:00:40,04 --> 00:00:44,00 and this geocoder will help us to do that. 20 00:00:44,00 --> 00:00:47,03 And the magic is, of this is in this library here, 21 00:00:47,03 --> 00:00:49,02 import geocoder. 22 00:00:49,02 --> 00:00:52,03 And it's brilliant for all sorts of location lookups. 23 00:00:52,03 --> 00:00:54,08 So we've imported it at the top of our file 24 00:00:54,08 --> 00:00:56,02 and now we're going to use the code 25 00:00:56,02 --> 00:00:57,08 to actually be able to use it. 26 00:00:57,08 --> 00:01:01,04 So geocoder has this function called .ip 27 00:01:01,04 --> 00:01:03,02 that when I pass it me, 28 00:01:03,02 --> 00:01:05,05 it's going to say give me the geocode 29 00:01:05,05 --> 00:01:08,01 for the location that I'm at now. 30 00:01:08,01 --> 00:01:10,09 It has this property called, ok 31 00:01:10,09 --> 00:01:14,00 and this will tell us if the lookup is successful 32 00:01:14,00 --> 00:01:15,07 and if it was successful, 33 00:01:15,07 --> 00:01:17,05 then we can start getting our data. 34 00:01:17,05 --> 00:01:21,01 So for example, we can get the state out of this thing 35 00:01:21,01 --> 00:01:23,07 and we can get the latitude and longitude. 36 00:01:23,07 --> 00:01:27,05 So g dot get latlong zero will be the latitude, 37 00:01:27,05 --> 00:01:31,00 g dot latlong one will be the longitude. 38 00:01:31,00 --> 00:01:33,03 And then the state abbreviation will be, 39 00:01:33,03 --> 00:01:36,09 g dot state will be the name of the state. 40 00:01:36,09 --> 00:01:39,00 And the state abbreviation will be 41 00:01:39,00 --> 00:01:41,08 to turn that from a name like California 42 00:01:41,08 --> 00:01:44,00 into an abbreviation like CA, 43 00:01:44,00 --> 00:01:45,04 using this helper up here 44 00:01:45,04 --> 00:01:47,08 where we're just mapping California to CA, 45 00:01:47,08 --> 00:01:49,04 that type of thing. 46 00:01:49,04 --> 00:01:51,05 So yeah, the important thing that I would say here 47 00:01:51,05 --> 00:01:53,09 is that getting the latitude and longitude, 48 00:01:53,09 --> 00:01:55,08 it's a little bit unintuitive. 49 00:01:55,08 --> 00:01:58,01 You're going to have this array called latlong. 50 00:01:58,01 --> 00:02:01,01 Zero will be latitude, one will be the longitude, 51 00:02:01,01 --> 00:02:02,08 and if you want to check this kind of thing, 52 00:02:02,08 --> 00:02:04,02 if any of them fail, 53 00:02:04,02 --> 00:02:07,00 we'll return none for all three values. 54 00:02:07,00 --> 00:02:10,08 The one small problem that this application has 55 00:02:10,08 --> 00:02:12,04 is that later on things like the 56 00:02:12,04 --> 00:02:14,03 National Park Service API 57 00:02:14,03 --> 00:02:16,06 work much better with the abbreviations 58 00:02:16,06 --> 00:02:18,06 than they do with the state names. 59 00:02:18,06 --> 00:02:21,03 So even though the geocoder returns the state name, 60 00:02:21,03 --> 00:02:24,01 I've added this code here to turn that into an abbreviation 61 00:02:24,01 --> 00:02:25,05 and then override that. 62 00:02:25,05 --> 00:02:28,05 And we can see here that it's a very simple dictionary 63 00:02:28,05 --> 00:02:32,07 where Alabama is, AL, Alaska is AK, and so on. 64 00:02:32,07 --> 00:02:33,09 This all looks pretty good, 65 00:02:33,09 --> 00:02:35,02 but what if, for example, 66 00:02:35,02 --> 00:02:37,04 something like the geocoder's service is down 67 00:02:37,04 --> 00:02:39,02 and then our program will crash. 68 00:02:39,02 --> 00:02:40,05 I've just added some code here 69 00:02:40,05 --> 00:02:42,00 to make it a little bit more robust 70 00:02:42,00 --> 00:02:43,06 so that we can wrap our code in this. 71 00:02:43,06 --> 00:02:45,03 Try accept. 72 00:02:45,03 --> 00:02:47,08 And this is a key principle of good tool design 73 00:02:47,08 --> 00:02:50,00 because you don't want your tool failing 74 00:02:50,00 --> 00:02:52,04 to bring down your entire agent. 75 00:02:52,04 --> 00:02:53,06 So when the tool fails, 76 00:02:53,06 --> 00:02:55,03 if it gives an error like this one, 77 00:02:55,03 --> 00:02:57,03 then as part of the reflection, 78 00:02:57,03 --> 00:02:59,04 you could take a look to see the tool failed. 79 00:02:59,04 --> 00:03:00,08 I got an error. 80 00:03:00,08 --> 00:03:01,08 Maybe do something else. 81 00:03:01,08 --> 00:03:03,00 Use an alternative tool, 82 00:03:03,00 --> 00:03:05,01 try again, that type of thing. 83 00:03:05,01 --> 00:03:06,01 And that's pretty much it. 84 00:03:06,01 --> 00:03:07,02 It's pretty straightforward. 85 00:03:07,02 --> 00:03:08,04 It's pretty simple. 86 00:03:08,04 --> 00:03:11,01 So this is the location tool from the ground up. 87 00:03:11,01 --> 00:03:13,05 You can actually run Python location py 88 00:03:13,05 --> 00:03:15,01 in your terminal. 89 00:03:15,01 --> 00:03:16,05 I could simply do something like 90 00:03:16,05 --> 00:03:20,04 python location. py, 91 00:03:20,04 --> 00:03:21,06 and we'll see here 92 00:03:21,06 --> 00:03:23,05 that we got the response directly from it. 93 00:03:23,05 --> 00:03:24,06 So I've created the tool 94 00:03:24,06 --> 00:03:26,09 so it could be used independently of the agent 95 00:03:26,09 --> 00:03:28,05 and we can see my latitude and longitude 96 00:03:28,05 --> 00:03:29,07 in the state that I'm presently in. 97 00:03:29,07 --> 00:03:32,03 In the next video, we're going to do the same thing 98 00:03:32,03 --> 00:03:34,02 and take a little bit more of a detailed look 99 00:03:34,02 --> 00:03:36,00 at the weather tool.