- [Instructor] So earlier we saw the main.py
that really held the entire agent together
and allowed us to plan for our hiking trip.
The first tool that it used was a location tool
where it was able to take a look
at the location of the user's machine
and determine the state
and the latitude and longitude from that.
So let's take a look at that tool and how it all works.
So in that one, I have a couple of helper functions,
get_current_location
and state to abbreviation.
So we're going to begin by looking at this
get_current_location
And the important piece of code
that we have here is this one.
The idea is that we want to know
the latitude, longitude, and state,
and this geocoder will help us to do that.
And the magic is, of this is in this library here,
import geocoder.
And it's brilliant for all sorts of location lookups.
So we've imported it at the top of our file
and now we're going to use the code
to actually be able to use it.
So geocoder has this function called .ip
that when I pass it me,
it's going to say give me the geocode
for the location that I'm at now.
It has this property called, ok
and this will tell us if the lookup is successful
and if it was successful,
then we can start getting our data.
So for example, we can get the state out of this thing
and we can get the latitude and longitude.
So g dot get latlong zero will be the latitude,
g dot latlong one will be the longitude.
And then the state abbreviation will be,
g dot state will be the name of the state.
And the state abbreviation will be
to turn that from a name like California
into an abbreviation like CA,
using this helper up here
where we're just mapping California to CA,
that type of thing.
So yeah, the important thing that I would say here
is that getting the latitude and longitude,
it's a little bit unintuitive.
You're going to have this array called latlong.
Zero will be latitude, one will be the longitude,
and if you want to check this kind of thing,
if any of them fail,
we'll return none for all three values.
The one small problem that this application has
is that later on things like the
National Park Service API
work much better with the abbreviations
than they do with the state names.
So even though the geocoder returns the state name,
I've added this code here to turn that into an abbreviation
and then override that.
And we can see here that it's a very simple dictionary
where Alabama is, AL, Alaska is AK, and so on.
This all looks pretty good,
but what if, for example,
something like the geocoder's service is down
and then our program will crash.
I've just added some code here
to make it a little bit more robust
so that we can wrap our code in this.
Try accept.
And this is a key principle of good tool design
because you don't want your tool failing
to bring down your entire agent.
So when the tool fails,
if it gives an error like this one,
then as part of the reflection,
you could take a look to see the tool failed.
I got an error.
Maybe do something else.
Use an alternative tool,
try again, that type of thing.
And that's pretty much it.
It's pretty straightforward.
It's pretty simple.
So this is the location tool from the ground up.
You can actually run Python location py
in your terminal.
I could simply do something like
python location. py,
and we'll see here
that we got the response directly from it.
So I've created the tool
so it could be used independently of the agent
and we can see my latitude and longitude
in the state that I'm presently in.
In the next video, we're going to do the same thing
and take a little bit more of a detailed look
at the weather tool.