- [Instructor] Okay, so our agent is now very robust
and can handle errors gracefully.
Our next step is to polish our code
to do things like adding documentation.
Good documentation will make your code much easier
to understand, to maintain and to share with others.
And just like with error handling,
we can use an AI assistant
to generate high-quality docstrings and comments for us.
Now, I've used the word docstring
and the goal here in Python is to use a docstring,
which is a Python function that allows you,
in a particularly clear way,
to explain what your function does.
A good docstring explains it using, for example,
the arguments that it takes and the things that it returns.
And it makes it possible for a human reading it
to understand the function's purpose without needing
to read the entire implementation.
Now, I've explained the get_parks API
that you can see here a few times,
but you have the advantage
of watching the video to have it explained.
But what if you're sharing this type
of code with somebody else?
So let's take a look at what would happen
if we wanted to change this.
And when we look at it, it's kind of cool,
but it's not immediately clear what it does
beyond probably the words get_parks.
So I'm going to use an AI to help me to do this
and use the AI assistant to write this docstring
for me with a simple prompt.
So I'm going to start with gemini again.
I like using a CLI, and in this case,
I'm using the Gemini CLI command line interface
and I have the get_parks function.
So I'm going to say,
please add a standard Python docstring
to my 'get_parks' function.
No, I'm not going to actually say
where the get_parks function is.
Let's see if it's smart enough to do that.
It should explain
that the function fetches
park data using the NPS API.
Make sure to document the 'api_key'
and state code arguments,
as well as describing
the dictionary or other data that it returns.
Something like that.
Again, I always find if you know development
and if you are a developer, then the prompts
that you can do here are usually more specific
and usually more straight to the point.
And then as a result, you'll end up getting better results.
Okay, it's done it here.
It's showing me the details that it's done,
the args of the api_key, which is a string, the state_code,
the two-letter state code, for example, CA.
And it's going to return a dictionary containing
the API response with park data.
And if I go back here and take a look,
we can see we have this much nicer docstring
that has been done for us.
Now, of course, I just picked out one specific function
to do it for it here as an example.
But this is the type of thing that I think
is really, really useful if you really want to make sure
that people can understand your code.
We could also maybe do one more test of this
to see how it's working.
And I will say something along the lines
of please add docstrings
to the main function in main.py
that summarize the agent's main steps
and add comments throughout the function
to clearly label these steps
and what they do.
Now, I had done some of that earlier on
when were looking through the main.py.
You were looking at code that I had created earlier on
that had a lot of these comments in it.
But again, the old adage
of you can never have too many comments
and you can't have too many documentation holds.
And being able to use an AI agent like this one to be able
to create the data for me is really, really good.
So we can see here it's added some of the docstrings,
such as the reflections for me to determine the response.
There was actually a little bit of a bug sometimes
with follow-up prompts in the system prompt here.
So it's actually fixed them for me.
There was a little bug that I had put in
and here we can see now,
if I go back and take a look at my main.py,
we've got a lot of stuff has been added to it
to help me understand what each of the models do,
use the model to reflect on the conversation
to determine has it given me a final answer?
But the one that I specifically asked for here,
that I mentioned for the agent itself is like, you know,
take a look at and document the actual steps that it does,
detect the user location,
fetch and analyze the weather conditions,
use a model to determine if the weather's suitable
for hiking, if it is,
search for nearby national parks, et cetera, et cetera.
Good code is not just about making the computer understand,
it's also about making other humans understand your code.
So by using an AI assistant like this, I find this great
to quickly generate high-quality documentation
and to make your project more professional
and easier to work with.
And one of the last things that I want to do
with the agent is to give it some memory
to handle follow-up questions.
So maybe I want to inquire about some of the parks
and stuff like that.
We'll take a look at how that's done next.