- [Instructor] So we've seen our main.py file
and all of the other files that are used to make the agent,
but I want to build the main.py file from the ground up
so that you can get a really clear picture
of how the agent's logic will flow
and how each of the tools that we're using is going to be used
then in sequence, as well as how reflection is used
between them, as well as how reflection is going to be used
with the backend AI.
So we're going to start with this empty main.py file
or nearly empty main.py file.
All that we have is our imports
and the stubbed main function.
And we're going to use this as our starting point.
The first thing that our agent needs to do
is then figure out where we are.
So I'm going to start with this main function
and I'm going to add a call to our first tool,
and our first tool is to detect our location.
I'm not going to bore you by typing all the code,
I'm just going to paste it in,
and we'll see here that what it's going to do
is we're going to say we're detecting our location.
It's going to use the get current location tool
to return a latitude, a longitude, and a state.
If any of these is set, then latitude will be set.
So we want to check if latitude is not set,
then we don't have anything,
we're not detecting the location, restart, try again,
and then we'll exit the main function.
Then I'm just going to say we're detecting the state.
Simple enough, and we use the state later on
for the national parks API.
For the weather API, we actually use the latitude
and longitude and let's take a look at that.
So now that we have a location,
we want to use the weather tool for that location
to be able to get our forecast.
So I'll do the same thing, I'm just going to paste in the code.
So we'll see here, step two is getting our weather,
we're checking the weather near us, we're using the latitude
and longitude that we've just arrived from
get current location to be able to get the weather.
And as before, if there's no weather data, we'll say,
"Look, couldn't get any, please try again later"
and we'll exit out.
Otherwise, we'll also use another helper function
that we'll see later on.
Another tool, which is to actually get a summary
of the weather, so in plain English summary
instead of just like 23 degrees centigrade,
2% rain, that kind of thing.
This will actually give us a plain English summary
which even gives us details of what will happen
later in the day, for example,
and print out that plain English summary.
Now this might be a little bit counterintuitive
when programming, 'cause often when we're programming,
we want to have those definitive structured pieces of data
that we can make decisions on with if then rules.
But remembering that we're using a backend AI,
the AI can actually reason over the plain English,
and the more data it gets, the better it can do.
Okay, so we've gotten our location,
we've gotten the weather for our location.
These are the tool things.
So the first reflection that we want to do,
this first key moment is, "Is today a good day for a hike?"
Instead of just proceeding, let's have the agent reflect
by using a large language model,
in this case, the G-P-T-O-S-S, to have an opinion
and to make a decision for us.
And to do that, we're going to query the model,
and I'm going to need a new helper function
that I call query model.
And I'm going to just paste that in and then explain it
because there's a lot going on in here.
Okay, so query model, that's going to take a system prompt
and a user prompt,
and it's going to create this messages payload
with the role system containing the system prompt
and the role user containing the user prompt.
When programming against the GPT backend,
it expects messages to be formatted in this particular way.
You'll see one system prompt and then multiple user prompts
and then multiple answers
to those user prompts in a chat history.
But in this case, we're just doing one system, one user.
The system prompt is used to really kick the model
into acting in a particular way.
You've probably seen prompts like you are
an expert in dot, dot, dot, dot.
That's the system prompt.
The user prompt is then we're asking it for something.
So in this particular case, the system prompt,
we're going to say something along the lines of,
"Here's the weather, gimme an opinion, yes or no."
And then the user prompt is this particular weather is this.
So this helper function will do that.
It's creating the messages in the format that GPT expects,
and then it's using a LAMA to chat with GPT
passing in those messages in this format
and getting the response back.
So let's look at how we use that now
back into our main function.
So we've done our location, we've done our weather,
now we want to reason across that weather.
So I'm going to paste the code in so that we can see.
okay, so we had our weather summary, we printed that out,
and now we want to decide if it's good enough for a hike.
Here's the prompt that we're going to use.
Based on this forecast, is it a good day for a hike?
Remember I said it would be
the weather summary is plain English.
Instead of just saying 30 degrees,
2% rain or something like that,
it's a plain English summary that we get back
from the weather service.
That could include time-based thing, "Sunny now,
rainy later," that type of stuff.
So based on that forecast, we're going to ask,
is it a good day for a hike?
Now, one of the things when you ask an AI
something like this is that the answers
can be very, very unstructured.
And often with post-training on a lot of AIs,
they've been post trained to be more conversational
in nature, and as a result, it's very, very unstructured.
So it might give you answers like,
"Today is a good day for hike.
Today is a great day for hike.
Today might be a good day for hike" and stuff like that,
but we want to have a clear opinion that's just yes or no,
and that's what we use the system prompt for.
So I'm just saying, here's the weather system prompt,
and that's you're an assistant that determines
if the weather is good for hiking,
respond only with yes or no.
We are using the system role
to make the LLM be very opinionated
and very, very clear with its response.
So those are the system prompts, the user prompt,
and then the payload of course
that we pass in will be the weather summary data
and they'll get this decision back.
And if the decision that comes back has a no in it,
then we're saying the reasoning that the model is done
is to say, "Today is not a good day for hiking."
Okay, so now that we've done that one,
if we've passed this and the model has the opinion
that today is a good day for hiking,
well then the next part is of course to take a look
at the national parks and to take a look at the trails
in those particular national parks to see
if they are good days for it.
So I've pasted the code in, let's take a look at it.
So our answer is not no, means it's yes.
So the weather's looking good,
and let's start searching for nearby parks and trails.
First thing we're going to do is call get parks.
What get parks is going to do for our particular state
is find national parks that are close to us.
And if there aren't any, it's going to end
because hopefully there are national parks in our state.
Once we've done that, now it's going to go through that list
of parks and each park is going to have a number of trails.
So we're going to say for every park,
let's get the name of that park and let's get
all of the trails for that park.
And once we have done that, now we're going to create
this dictionary that I call parks and trails
so that every entry in the dictionary for a park
will have a number of entries for the trails for it.
So pretty complex interaction going on here,
but this tool is really just searching for parks near us.
Then for each of those parks, retrieving the details
of that park, getting the list of trails for that park,
and putting them into a big dictionary
so that we have them all in one place.
And now once we have that big blob of data,
here's what AI is really good at,
getting that big blob of data
and having the AI understand that on our behalf
and give us recommendations.
So now the final and most important reflection
will be we're going to ask the model to act as an expert guide
and give us opinionated recommendations
based on all of that data.
So what's that going to look like in code?
Okay, so I've pasted it in here
and I'll just step through it.
So remembering the system prompt that I spoke
about earlier on, we're going to use yet another system prompt,
but this time I'm going to be passing at this big blob
of data about national parks and about the trails.
And I'm going to say, "You're an expert hiking guide.
Your task is to analyze the following list of parks
and trails and recommend the top two to three options."
And then I had my dictionary of parks and trails,
and I'm going to go through that
and just create a big string where it's going
to be park colon, the park name, and then I'll indent
the trail a little bit with the trail name.
So it'll be park one trails, A, B, C, park two trails,
D, E, F, that kind of thing as a big data blob in a string
because when we converse with it,
we're not going to pass the dictionary,
we're just going to pass the string.
And then the system prompts that we just defined,
here's the available parks and trails,
and then that big string that we just generated
off of the list of parks and trails,
and then we'll get back the hiking recommendations.
So we formatted all of our data into that clean prompt.
We've given the model, this persona
that it's an expert hiking guide,
and we're going to ask it for its opinion on the final answer.
And that's it.
Okay, so let's take a look at what this will look like
when we actually run it.
So we can see, the first step is it's detecting my location.
It got that.
Now it's detecting the weather for me.
Here's the summary that it got in plain English,
overcast, average temperature,
maximum precipitation, probability is very low.
So we asked it for an opinion, should we hike today?
And the model said, "Yes, we should.
It's a good day for hiking."
So it goes to look for nearby parks and trails,
and then it analyzes them.
Once it's analyzed them, it's now going to ask the model
for its recommendations.
And now here are the recommendations that came out of it.
The first one is this, Muir Woods National Monument
to hike the main trail.
What I love about this one
is like it's understanding today's weather
and it's understanding the type of hiking
that's good for this type of weather.
Imagine if it was cold,
it would give me different recommendations,
or if it was hot,
it would give me different recommendations.
But given that today is quite a cool day,
it's like saying, "Hey look, maybe a classic easy stroll
through redwoods, perfect for relaxing,
one with unforgettable scenery."
This English language is being generated by the model
and then feeding my agents and giving me an agent
that's not just giving me rough recommendations,
but also opinionated recommendations
that are well-described.
So this is the whole idea of how the agent will work.
We've seen step by step to build it,
but the one thing that we haven't looked at in detail yet
are the tools.
So let's start looking at those next
beginning with the location tool.