1 00:00:00,05 --> 00:00:02,08 - [Instructor] Okay, so our agent is now very robust 2 00:00:02,08 --> 00:00:04,09 and can handle errors gracefully. 3 00:00:04,09 --> 00:00:07,00 Our next step is to polish our code 4 00:00:07,00 --> 00:00:09,05 to do things like adding documentation. 5 00:00:09,05 --> 00:00:12,03 Good documentation will make your code much easier 6 00:00:12,03 --> 00:00:15,09 to understand, to maintain and to share with others. 7 00:00:15,09 --> 00:00:17,02 And just like with error handling, 8 00:00:17,02 --> 00:00:18,08 we can use an AI assistant 9 00:00:18,08 --> 00:00:22,07 to generate high-quality docstrings and comments for us. 10 00:00:22,07 --> 00:00:24,00 Now, I've used the word docstring 11 00:00:24,00 --> 00:00:27,09 and the goal here in Python is to use a docstring, 12 00:00:27,09 --> 00:00:30,03 which is a Python function that allows you, 13 00:00:30,03 --> 00:00:32,07 in a particularly clear way, 14 00:00:32,07 --> 00:00:34,09 to explain what your function does. 15 00:00:34,09 --> 00:00:38,01 A good docstring explains it using, for example, 16 00:00:38,01 --> 00:00:42,03 the arguments that it takes and the things that it returns. 17 00:00:42,03 --> 00:00:44,05 And it makes it possible for a human reading it 18 00:00:44,05 --> 00:00:47,04 to understand the function's purpose without needing 19 00:00:47,04 --> 00:00:49,05 to read the entire implementation. 20 00:00:49,05 --> 00:00:51,03 Now, I've explained the get_parks API 21 00:00:51,03 --> 00:00:52,09 that you can see here a few times, 22 00:00:52,09 --> 00:00:54,06 but you have the advantage 23 00:00:54,06 --> 00:00:56,04 of watching the video to have it explained. 24 00:00:56,04 --> 00:00:58,02 But what if you're sharing this type 25 00:00:58,02 --> 00:01:00,00 of code with somebody else? 26 00:01:00,00 --> 00:01:02,01 So let's take a look at what would happen 27 00:01:02,01 --> 00:01:03,05 if we wanted to change this. 28 00:01:03,05 --> 00:01:05,08 And when we look at it, it's kind of cool, 29 00:01:05,08 --> 00:01:08,09 but it's not immediately clear what it does 30 00:01:08,09 --> 00:01:12,00 beyond probably the words get_parks. 31 00:01:12,00 --> 00:01:15,01 So I'm going to use an AI to help me to do this 32 00:01:15,01 --> 00:01:17,09 and use the AI assistant to write this docstring 33 00:01:17,09 --> 00:01:20,01 for me with a simple prompt. 34 00:01:20,01 --> 00:01:22,08 So I'm going to start with gemini again. 35 00:01:22,08 --> 00:01:24,09 I like using a CLI, and in this case, 36 00:01:24,09 --> 00:01:28,03 I'm using the Gemini CLI command line interface 37 00:01:28,03 --> 00:01:30,05 and I have the get_parks function. 38 00:01:30,05 --> 00:01:32,00 So I'm going to say, 39 00:01:32,00 --> 00:01:37,06 please add a standard Python docstring 40 00:01:37,06 --> 00:01:42,06 to my 'get_parks' function. 41 00:01:42,06 --> 00:01:44,03 No, I'm not going to actually say 42 00:01:44,03 --> 00:01:45,08 where the get_parks function is. 43 00:01:45,08 --> 00:01:47,09 Let's see if it's smart enough to do that. 44 00:01:47,09 --> 00:01:49,09 It should explain 45 00:01:49,09 --> 00:01:54,06 that the function fetches 46 00:01:54,06 --> 00:01:59,08 park data using the NPS API. 47 00:01:59,08 --> 00:02:06,01 Make sure to document the 'api_key' 48 00:02:06,01 --> 00:02:10,06 and state code arguments, 49 00:02:10,06 --> 00:02:14,05 as well as describing 50 00:02:14,05 --> 00:02:19,03 the dictionary or other data that it returns. 51 00:02:19,03 --> 00:02:20,08 Something like that. 52 00:02:20,08 --> 00:02:24,02 Again, I always find if you know development 53 00:02:24,02 --> 00:02:26,01 and if you are a developer, then the prompts 54 00:02:26,01 --> 00:02:29,03 that you can do here are usually more specific 55 00:02:29,03 --> 00:02:31,05 and usually more straight to the point. 56 00:02:31,05 --> 00:02:34,06 And then as a result, you'll end up getting better results. 57 00:02:34,06 --> 00:02:35,09 Okay, it's done it here. 58 00:02:35,09 --> 00:02:37,09 It's showing me the details that it's done, 59 00:02:37,09 --> 00:02:41,04 the args of the api_key, which is a string, the state_code, 60 00:02:41,04 --> 00:02:43,09 the two-letter state code, for example, CA. 61 00:02:43,09 --> 00:02:46,03 And it's going to return a dictionary containing 62 00:02:46,03 --> 00:02:48,01 the API response with park data. 63 00:02:48,01 --> 00:02:50,01 And if I go back here and take a look, 64 00:02:50,01 --> 00:02:52,08 we can see we have this much nicer docstring 65 00:02:52,08 --> 00:02:54,03 that has been done for us. 66 00:02:54,03 --> 00:02:56,07 Now, of course, I just picked out one specific function 67 00:02:56,07 --> 00:02:59,00 to do it for it here as an example. 68 00:02:59,00 --> 00:03:00,05 But this is the type of thing that I think 69 00:03:00,05 --> 00:03:03,09 is really, really useful if you really want to make sure 70 00:03:03,09 --> 00:03:06,06 that people can understand your code. 71 00:03:06,06 --> 00:03:09,08 We could also maybe do one more test of this 72 00:03:09,08 --> 00:03:11,08 to see how it's working. 73 00:03:11,08 --> 00:03:14,02 And I will say something along the lines 74 00:03:14,02 --> 00:03:17,08 of please add docstrings 75 00:03:17,08 --> 00:03:22,05 to the main function in main.py 76 00:03:22,05 --> 00:03:28,02 that summarize the agent's main steps 77 00:03:28,02 --> 00:03:33,00 and add comments throughout the function 78 00:03:33,00 --> 00:03:37,08 to clearly label these steps 79 00:03:37,08 --> 00:03:40,08 and what they do. 80 00:03:40,08 --> 00:03:42,09 Now, I had done some of that earlier on 81 00:03:42,09 --> 00:03:44,08 when were looking through the main.py. 82 00:03:44,08 --> 00:03:47,04 You were looking at code that I had created earlier on 83 00:03:47,04 --> 00:03:49,07 that had a lot of these comments in it. 84 00:03:49,07 --> 00:03:51,02 But again, the old adage 85 00:03:51,02 --> 00:03:52,07 of you can never have too many comments 86 00:03:52,07 --> 00:03:55,05 and you can't have too many documentation holds. 87 00:03:55,05 --> 00:03:58,06 And being able to use an AI agent like this one to be able 88 00:03:58,06 --> 00:04:02,01 to create the data for me is really, really good. 89 00:04:02,01 --> 00:04:04,06 So we can see here it's added some of the docstrings, 90 00:04:04,06 --> 00:04:09,02 such as the reflections for me to determine the response. 91 00:04:09,02 --> 00:04:11,04 There was actually a little bit of a bug sometimes 92 00:04:11,04 --> 00:04:14,03 with follow-up prompts in the system prompt here. 93 00:04:14,03 --> 00:04:15,09 So it's actually fixed them for me. 94 00:04:15,09 --> 00:04:18,03 There was a little bug that I had put in 95 00:04:18,03 --> 00:04:20,01 and here we can see now, 96 00:04:20,01 --> 00:04:22,08 if I go back and take a look at my main.py, 97 00:04:22,08 --> 00:04:25,08 we've got a lot of stuff has been added to it 98 00:04:25,08 --> 00:04:28,05 to help me understand what each of the models do, 99 00:04:28,05 --> 00:04:30,06 use the model to reflect on the conversation 100 00:04:30,06 --> 00:04:33,07 to determine has it given me a final answer? 101 00:04:33,07 --> 00:04:35,08 But the one that I specifically asked for here, 102 00:04:35,08 --> 00:04:38,07 that I mentioned for the agent itself is like, you know, 103 00:04:38,07 --> 00:04:42,05 take a look at and document the actual steps that it does, 104 00:04:42,05 --> 00:04:43,08 detect the user location, 105 00:04:43,08 --> 00:04:46,03 fetch and analyze the weather conditions, 106 00:04:46,03 --> 00:04:48,05 use a model to determine if the weather's suitable 107 00:04:48,05 --> 00:04:50,00 for hiking, if it is, 108 00:04:50,00 --> 00:04:53,01 search for nearby national parks, et cetera, et cetera. 109 00:04:53,01 --> 00:04:56,04 Good code is not just about making the computer understand, 110 00:04:56,04 --> 00:05:00,01 it's also about making other humans understand your code. 111 00:05:00,01 --> 00:05:03,06 So by using an AI assistant like this, I find this great 112 00:05:03,06 --> 00:05:06,08 to quickly generate high-quality documentation 113 00:05:06,08 --> 00:05:08,05 and to make your project more professional 114 00:05:08,05 --> 00:05:10,02 and easier to work with. 115 00:05:10,02 --> 00:05:11,07 And one of the last things that I want to do 116 00:05:11,07 --> 00:05:14,01 with the agent is to give it some memory 117 00:05:14,01 --> 00:05:15,06 to handle follow-up questions. 118 00:05:15,06 --> 00:05:18,08 So maybe I want to inquire about some of the parks 119 00:05:18,08 --> 00:05:19,09 and stuff like that. 120 00:05:19,09 --> 00:05:22,00 We'll take a look at how that's done next.