1 00:00:00,05 --> 00:00:01,04 - [Instructor] Okay, 2 00:00:01,04 --> 00:00:03,06 the next and probably the most important API tool 3 00:00:03,06 --> 00:00:05,04 that we're going to be using in this agent 4 00:00:05,04 --> 00:00:07,02 is really the one for the national parks. 5 00:00:07,02 --> 00:00:10,04 So we can understand the trails in the national parks, 6 00:00:10,04 --> 00:00:11,06 and select one of them, 7 00:00:11,06 --> 00:00:14,08 and have the agent give us an opinionated selection for ones 8 00:00:14,08 --> 00:00:17,01 that would be good for hiking in today. 9 00:00:17,01 --> 00:00:17,09 The pattern for this 10 00:00:17,09 --> 00:00:19,09 is very similar to the weather one that we saw, 11 00:00:19,09 --> 00:00:22,00 where we're going to be calling a URL, 12 00:00:22,00 --> 00:00:24,08 a backend URL with the API in it. 13 00:00:24,08 --> 00:00:26,02 And then the response from that 14 00:00:26,02 --> 00:00:28,00 will be something that we process. 15 00:00:28,00 --> 00:00:30,02 One difference with The National Park Service one 16 00:00:30,02 --> 00:00:33,00 is that it's not completely free in OpenAPI, 17 00:00:33,00 --> 00:00:34,07 that it needs an API key. 18 00:00:34,07 --> 00:00:35,07 It's free of charge, 19 00:00:35,07 --> 00:00:38,05 but ya just do need to subscribe to the API key 20 00:00:38,05 --> 00:00:40,02 in order to be able to use the service. 21 00:00:40,02 --> 00:00:41,09 And note that it takes a state code, 22 00:00:41,09 --> 00:00:43,03 instead of a state name. 23 00:00:43,03 --> 00:00:46,01 And as we saw earlier on, we wrote helper functions 24 00:00:46,01 --> 00:00:50,01 that would turn the state names from the locations API 25 00:00:50,01 --> 00:00:52,08 into a state code that this will understand. 26 00:00:52,08 --> 00:00:53,07 But other than that, 27 00:00:53,07 --> 00:00:55,05 the pattern is exactly the same as we saw, 28 00:00:55,05 --> 00:00:57,02 where we're using the requests library 29 00:00:57,02 --> 00:00:59,05 to call this and get a response. 30 00:00:59,05 --> 00:01:02,02 So the magic happens in this function, 31 00:01:02,02 --> 00:01:04,08 the very simple function of get_trails. 32 00:01:04,08 --> 00:01:11,02 So once we have a park, we have a park code for that park. 33 00:01:11,02 --> 00:01:13,05 So if I were to use an example here, 34 00:01:13,05 --> 00:01:16,06 say my state code, I've hard-coded to California, 35 00:01:16,06 --> 00:01:17,09 my parks_data, 36 00:01:17,09 --> 00:01:20,09 the function that I've just shown how to call here 37 00:01:20,09 --> 00:01:22,06 is loaded by get_parks. 38 00:01:22,06 --> 00:01:25,03 I pass it the API key and the state code. 39 00:01:25,03 --> 00:01:27,05 So now, for that parks data, 40 00:01:27,05 --> 00:01:29,05 all of the data that's in that park, 41 00:01:29,05 --> 00:01:32,07 I want to get the trails for that data. 42 00:01:32,07 --> 00:01:35,01 So I'm going to, park by park, 43 00:01:35,01 --> 00:01:38,07 get a list of the trails for that park. 44 00:01:38,07 --> 00:01:41,01 And then for each of those here, 45 00:01:41,01 --> 00:01:44,00 ya see, I'm going to step through the trails 46 00:01:44,00 --> 00:01:46,02 and get details about that data, 47 00:01:46,02 --> 00:01:48,08 the full name, the title, and stuff like that. 48 00:01:48,08 --> 00:01:52,02 So I'm getting all of the data for all of the trails 49 00:01:52,02 --> 00:01:56,00 in all of the parks that are within my state. 50 00:01:56,00 --> 00:01:59,00 And the code to do that is this get_trails. 51 00:01:59,00 --> 00:02:00,01 How does it do it? 52 00:02:00,01 --> 00:02:02,04 Well, again, it's using the API. 53 00:02:02,04 --> 00:02:05,07 So the API here for The National Park Service. 54 00:02:05,07 --> 00:02:09,04 In this case, I give it a park code and an API key, 55 00:02:09,04 --> 00:02:12,06 and that's going to return the set of trails 56 00:02:12,06 --> 00:02:14,04 for that particular park. 57 00:02:14,04 --> 00:02:15,02 And that's it. 58 00:02:15,02 --> 00:02:16,09 You can see, again, the pattern is very similar, 59 00:02:16,09 --> 00:02:18,03 where we specify the URL, 60 00:02:18,03 --> 00:02:22,00 we do a request or get of that URL, we check the response, 61 00:02:22,00 --> 00:02:25,03 we turn it into JSON so that we can process it. 62 00:02:25,03 --> 00:02:28,00 And similar here, with get_trails. 63 00:02:28,00 --> 00:02:30,05 Basically, it's the same thing across both. 64 00:02:30,05 --> 00:02:32,01 So, that's pretty much it. 65 00:02:32,01 --> 00:02:33,05 That's all we have to do 66 00:02:33,05 --> 00:02:37,03 in order to be able to get the details of the parks. 67 00:02:37,03 --> 00:02:40,00 And remembering that we're not making any decisions here, 68 00:02:40,00 --> 00:02:42,08 we're just downloading those details of the parks 69 00:02:42,08 --> 00:02:44,09 and of the trails within the parks. 70 00:02:44,09 --> 00:02:49,05 So that later, within the main.py, as we saw earlier on, 71 00:02:49,05 --> 00:02:51,00 that we're going to take that data 72 00:02:51,00 --> 00:02:52,06 and we're going to give it a system prompt 73 00:02:52,06 --> 00:02:55,01 that says, "Here's a bunch of data about parks. 74 00:02:55,01 --> 00:02:56,07 Here's today's weather. 75 00:02:56,07 --> 00:02:58,05 Find ones that ya think are suitable, 76 00:02:58,05 --> 00:02:59,08 and give me an opinion 77 00:02:59,08 --> 00:03:02,01 for ones that ya think are suitable for hiking in." 78 00:03:02,01 --> 00:03:05,01 And that's part of the magic about agentic API programming 79 00:03:05,01 --> 00:03:09,00 is that all that my traditional code like this is doing 80 00:03:09,00 --> 00:03:12,01 is gathering data, and then once I've gathered that data, 81 00:03:12,01 --> 00:03:15,03 is to turn that into a payload that goes to an LLM, 82 00:03:15,03 --> 00:03:17,06 have the LLM understand that data, 83 00:03:17,06 --> 00:03:19,07 figure out what it is that we're asking from that 84 00:03:19,07 --> 00:03:21,03 and give us a response, 85 00:03:21,03 --> 00:03:24,02 and then we can continue to act on those responses 86 00:03:24,02 --> 00:03:26,04 until we get our final answer. 87 00:03:26,04 --> 00:03:28,00 So that's it for the tools, 88 00:03:28,00 --> 00:03:30,09 and that's it for building the basic agent. 89 00:03:30,09 --> 00:03:32,01 Next up, what I want to do 90 00:03:32,01 --> 00:03:35,01 is now to start making that agent more robust 91 00:03:35,01 --> 00:03:36,07 by looking at things that we can do 92 00:03:36,07 --> 00:03:39,00 for error handling in the agent.