- [Instructor] Okay,
the next and probably the most important API tool
that we're going to be using in this agent
is really the one for the national parks.
So we can understand the trails in the national parks,
and select one of them,
and have the agent give us an opinionated selection for ones
that would be good for hiking in today.
The pattern for this
is very similar to the weather one that we saw,
where we're going to be calling a URL,
a backend URL with the API in it.
And then the response from that
will be something that we process.
One difference with The National Park Service one
is that it's not completely free in OpenAPI,
that it needs an API key.
It's free of charge,
but ya just do need to subscribe to the API key
in order to be able to use the service.
And note that it takes a state code,
instead of a state name.
And as we saw earlier on, we wrote helper functions
that would turn the state names from the locations API
into a state code that this will understand.
But other than that,
the pattern is exactly the same as we saw,
where we're using the requests library
to call this and get a response.
So the magic happens in this function,
the very simple function of get_trails.
So once we have a park, we have a park code for that park.
So if I were to use an example here,
say my state code, I've hard-coded to California,
my parks_data,
the function that I've just shown how to call here
is loaded by get_parks.
I pass it the API key and the state code.
So now, for that parks data,
all of the data that's in that park,
I want to get the trails for that data.
So I'm going to, park by park,
get a list of the trails for that park.
And then for each of those here,
ya see, I'm going to step through the trails
and get details about that data,
the full name, the title, and stuff like that.
So I'm getting all of the data for all of the trails
in all of the parks that are within my state.
And the code to do that is this get_trails.
How does it do it?
Well, again, it's using the API.
So the API here for The National Park Service.
In this case, I give it a park code and an API key,
and that's going to return the set of trails
for that particular park.
And that's it.
You can see, again, the pattern is very similar,
where we specify the URL,
we do a request or get of that URL, we check the response,
we turn it into JSON so that we can process it.
And similar here, with get_trails.
Basically, it's the same thing across both.
So, that's pretty much it.
That's all we have to do
in order to be able to get the details of the parks.
And remembering that we're not making any decisions here,
we're just downloading those details of the parks
and of the trails within the parks.
So that later, within the main.py, as we saw earlier on,
that we're going to take that data
and we're going to give it a system prompt
that says, "Here's a bunch of data about parks.
Here's today's weather.
Find ones that ya think are suitable,
and give me an opinion
for ones that ya think are suitable for hiking in."
And that's part of the magic about agentic API programming
is that all that my traditional code like this is doing
is gathering data, and then once I've gathered that data,
is to turn that into a payload that goes to an LLM,
have the LLM understand that data,
figure out what it is that we're asking from that
and give us a response,
and then we can continue to act on those responses
until we get our final answer.
So that's it for the tools,
and that's it for building the basic agent.
Next up, what I want to do
is now to start making that agent more robust
by looking at things that we can do
for error handling in the agent.