- [Instructor] So we've come a long way
and our agent is robust.
It can use tools
and we've recently added memory to remember conversations.
But one other thing is that truly powerful agents
can also reflect on the results of their actions
and adapt their own plan.
One of the things you've probably noticed
when we were running this agent was
it could actually be quite slow
when it was going through the national parks, part of it.
So something that I have learned over time
from building agents and building code like this
is that the reflection step,
if you can add it in more places
so that you can really try to understand
the data that you have,
when you're flowing from step to step in the agent,
you can really speed up
and you can make the agent
smarter and more focused.
And right now, one of the things was,
and the reason why it's slow is that the agent
finds all of the parks in the state,
lists all of the trails in those parks,
and then dumps that into the recommendation prompt.
The couple of demos I was running earlier on
and presently in California
and it's finding lots and lots of parks in California.
So the prompt gets very cluttered
and the recommendation may not be very focused
or worse, if you're running locally on a small model,
you might blow up the context window to begin with
and the model will fail or act very, very slowly.
So one of the places here is where
we can add that reflection step
where the agent analyzes the park data it receives
and then decides if it needs to
refine that data before proceeding.
Let me give a quick demo of this.
So I'm going to run the reflecting version of this one
where I've added a new reflection to this one
and it's like I'm searching for nearby parks and trails
and I found 34 parks in California.
So that's a lot of them.
So I'm working with the model to pick a diverse few,
and I picked these few.
Yosemite, Death Valley, Sequoia,
Joshua Tree, Channel Islands,
Redwood, Lassen Volcanic,
and Mojave National Preserve.
So now that it's focusing on those three,
it's actually getting a much faster response.
Now, I did this one quite naively
to pick the top ones.
I could have done it with a little bit more detail,
maybe based on distance.
But in this case, let me show you
how I did this actual reflection.
So starting with this one,
the code that we had back in main.py,
once the weather was good,
was of course that we did a call to
the get_parks
with the API key in the state
in order to get data back.
And then we just proceeded straight
onto processing that data.
There was no filtering
or no reflection here on this one
other than null or zero.
So we thought, okay, the nice logic here would be
what if there's too many?
So in the reflection step for that one,
then I've changed that.
So again, I'm getting my parks
and if there's not none I can continue.
But here what I'm going to do is this simple analysis of parks
that I've actually found.
So I'm going to do original parks,
is the parks data for them,
do the count of them.
That's what we saw.
We found 34 of them.
So what I'm going to do here now is,
instead pick a diverse few.
So what I've done
to pick the diverse few is to use an LLM
to do that for me.
And using the LLM, I created this prompt,
You're an expert travel guide.
From this following list of national parks,
select a diverse and interesting subset
of up to eight of them
Consider geographical diversity and variety
in park types of possible.
Return only a comma separated list
of the park names you select.
So this time you can see that I decided to say
pick a diverse few geographically and all that kind of stuff.
Another thing that I could have done would be distance
to kind of find those parks
and maybe use another API
to calculate how far they are
from my current latitude and longitude.
It's really up to you how you would do it,
but this is how I'd put that reflection in there
to whittle it down.
Here's the full list of parks,
and then I just use query model
with that system prompt
and the whittle down set
to get my new park names.
And then I would just work with those parks.
If there were too few of them,
like say it only found like three or less,
then just use the original full list,
no matter how many there were in it.
And it's a really nice example of
how I was able to insert a new reflection
so that the agent could be more intelligent
about the data that it's using.
And I would thoroughly recommend
to start thinking about those points
where you can integrate new points of reflection,
particularly if you're using small local models
that generally have a small context window.
This was a big step.
The agent is no longer just a blind follower
of a pre-written script.
It can now assess the information that it gathers
and make intelligent decisions
to refine its own strategy.
Here I chose to refine the strategy based on
diverse geographic locations,
but you could choose however way you want to do it.
And this concept of reflection
is key to building those agents
to handle complex multi-step tasks in the real world.
And as you're building out agents,
always look for those possibilities
and those opportunities where you can inject reflection
to improve performance and to improve intelligence.
So congratulations on building such a sophisticated agent.