1 00:00:00,05 --> 00:00:01,09 - [Instructor] Our agent is fully functional, 2 00:00:01,09 --> 00:00:03,06 and that's fantastic. 3 00:00:03,06 --> 00:00:06,04 But what happens if an external API is down, 4 00:00:06,04 --> 00:00:09,08 or our network connection fails, or other issues like that? 5 00:00:09,08 --> 00:00:12,09 Right now, there's a risk that our agent would crash. 6 00:00:12,09 --> 00:00:14,04 So one of the things I'd like to do 7 00:00:14,04 --> 00:00:17,00 is to look at how to make the agent more robust 8 00:00:17,00 --> 00:00:19,01 by adding error handling. 9 00:00:19,01 --> 00:00:22,00 And we can do this manually or we can use an AI assistant 10 00:00:22,00 --> 00:00:26,00 like the Gemini CLI to do most of that heavy lifting for us. 11 00:00:26,00 --> 00:00:28,01 The goal is to take all of the code 12 00:00:28,01 --> 00:00:30,03 in our hiking_agent directory 13 00:00:30,03 --> 00:00:33,02 and make it more resilient to errors. 14 00:00:33,02 --> 00:00:35,02 If we can wrap the external calls 15 00:00:35,02 --> 00:00:37,08 to the location, weather, and parks APIs, 16 00:00:37,08 --> 00:00:39,06 and try...except blocks, 17 00:00:39,06 --> 00:00:42,02 then this would allow our agent to fail gracefully 18 00:00:42,02 --> 00:00:44,06 with a clear message instead of crashing. 19 00:00:44,06 --> 00:00:46,04 Indeed, we saw some of the code for this 20 00:00:46,04 --> 00:00:47,08 a little bit earlier on, 21 00:00:47,08 --> 00:00:50,08 but I want to show what it would be like with the initial code 22 00:00:50,08 --> 00:00:52,00 that many of us might build 23 00:00:52,00 --> 00:00:54,03 that might be a little bit more naive. 24 00:00:54,03 --> 00:00:57,04 For example, let's start with the parks.py. 25 00:00:57,04 --> 00:00:59,06 And it's making these URL requests 26 00:00:59,06 --> 00:01:02,01 that are common areas of failure. 27 00:01:02,01 --> 00:01:03,09 So for example, something like this one, 28 00:01:03,09 --> 00:01:06,06 the common pattern is to define the URL, 29 00:01:06,06 --> 00:01:08,05 to call requests to get the response, 30 00:01:08,05 --> 00:01:11,01 and then to return the response.json. 31 00:01:11,01 --> 00:01:13,08 Now, there are a number of areas where this could go wrong, 32 00:01:13,08 --> 00:01:15,05 and naively, we could say, to fix it, 33 00:01:15,05 --> 00:01:18,07 we could manually add a try...except block, 34 00:01:18,07 --> 00:01:22,00 but there may be corner cases that we don't see. 35 00:01:22,00 --> 00:01:29,00 So what if we ask an AI assistant to do this work for us? 36 00:01:29,00 --> 00:01:31,07 So I'm going to do that by starting Gemini. 37 00:01:31,07 --> 00:01:34,00 You can use any AI assistant you like of course, 38 00:01:34,00 --> 00:01:36,03 but I like using the Gemini CLI 39 00:01:36,03 --> 00:01:39,00 because I'm in the same directory as the code 40 00:01:39,00 --> 00:01:42,00 and I can work directly within the code itself. 41 00:01:42,00 --> 00:01:44,04 So I can specifically say something like, 42 00:01:44,04 --> 00:01:48,08 in my parks.py file, 43 00:01:48,08 --> 00:01:54,00 please update the get_parks function 44 00:01:54,00 --> 00:01:59,02 to handle potential network errors. 45 00:01:59,02 --> 00:02:05,06 It should use a try...except block. 46 00:02:05,06 --> 00:02:11,06 Check for bad HTTP responses, 47 00:02:11,06 --> 00:02:14,01 network failures, 48 00:02:14,01 --> 00:02:18,08 or any other issues you can think of 49 00:02:18,08 --> 00:02:22,00 and return None 50 00:02:22,00 --> 00:02:24,07 when an error occurs. 51 00:02:24,07 --> 00:02:27,05 This is one of the ways that your skills as a developer 52 00:02:27,05 --> 00:02:29,05 are actually more useful, I think, 53 00:02:29,05 --> 00:02:32,04 in the age of AI because you can be much more specific 54 00:02:32,04 --> 00:02:34,07 with your prompt here by the kind of things 55 00:02:34,07 --> 00:02:37,03 that you're experienced, like HTTP errors, 56 00:02:37,03 --> 00:02:39,05 bad H responses, network failures, 57 00:02:39,05 --> 00:02:41,03 all of those kind of things. 58 00:02:41,03 --> 00:02:43,00 And I just did get something like this 59 00:02:43,00 --> 00:02:46,01 and have it kind of generate the code for me. 60 00:02:46,01 --> 00:02:47,05 It's the first time I'm running it, 61 00:02:47,05 --> 00:02:49,06 so sometimes it's going to ask me for permission 62 00:02:49,06 --> 00:02:51,06 to be able to write and stuff like that. 63 00:02:51,06 --> 00:02:53,00 And we can see the code 64 00:02:53,00 --> 00:02:55,07 that Gemini came up with here for me. 65 00:02:55,07 --> 00:02:57,06 It's using this raise_for_status, 66 00:02:57,06 --> 00:03:00,07 which can check for HTTP errors 67 00:03:00,07 --> 00:03:02,06 and do other goodies like this as well. 68 00:03:02,06 --> 00:03:06,07 So it actually also spotted this one where I could have had, 69 00:03:06,07 --> 00:03:09,02 if I look at it here briefly before I continue, 70 00:03:09,02 --> 00:03:11,01 that there are corner cases 71 00:03:11,01 --> 00:03:14,09 where if there's a null in the data, that it would crash. 72 00:03:14,09 --> 00:03:18,03 And the if data in parks_data would actually crash 73 00:03:18,03 --> 00:03:20,01 rather than failing 74 00:03:20,01 --> 00:03:22,09 and gracefully allowing the agent to continue, 75 00:03:22,09 --> 00:03:24,01 there'll be an actual crash here. 76 00:03:24,01 --> 00:03:27,02 So we're saying like, you know, it's going to update that. 77 00:03:27,02 --> 00:03:30,00 And the agent being the agent, the Gemini one, 78 00:03:30,00 --> 00:03:31,04 is it's going to actually run it 79 00:03:31,04 --> 00:03:33,00 and just check if it's working 80 00:03:33,00 --> 00:03:36,00 and do other things and fixes for us like that. 81 00:03:36,00 --> 00:03:37,08 I find that's really, really useful, 82 00:03:37,08 --> 00:03:39,03 and I think it's one of those things 83 00:03:39,03 --> 00:03:41,03 that would be very important for you 84 00:03:41,03 --> 00:03:45,01 to be able to check errors in your files like this. 85 00:03:45,01 --> 00:03:47,06 In this case, actually, I didn't run it 86 00:03:47,06 --> 00:03:48,08 in a virtual environment, 87 00:03:48,08 --> 00:03:50,07 so it found that that package was missing. 88 00:03:50,07 --> 00:03:53,05 And with that package missing, it wants to install that, 89 00:03:53,05 --> 00:03:56,01 and then retest the file with the package there. 90 00:03:56,01 --> 00:03:58,07 And we can see it actually ran parks.py 91 00:03:58,07 --> 00:04:00,06 and was able to find some trails for us, 92 00:04:00,06 --> 00:04:02,06 so we know that it's working now. 93 00:04:02,06 --> 00:04:05,04 The changes that it did to add error handling 94 00:04:05,04 --> 00:04:08,00 have not changed anything. 95 00:04:08,00 --> 00:04:11,09 And I'm just allowing it to remove any files that it added. 96 00:04:11,09 --> 00:04:13,01 While it was working through this, 97 00:04:13,01 --> 00:04:15,02 sometimes it writes temporary files and stuff like that. 98 00:04:15,02 --> 00:04:17,01 I'm allowing it to remove them. 99 00:04:17,01 --> 00:04:21,01 So this is a good example of being able to use 100 00:04:21,01 --> 00:04:24,02 a AI agent or an AI model. 101 00:04:24,02 --> 00:04:27,01 I love to do it as a command-line interface, personally, 102 00:04:27,01 --> 00:04:29,04 so it save a lot of cutting and pasting when I'm working 103 00:04:29,04 --> 00:04:32,02 and stuff like this to update and improve your code. 104 00:04:32,02 --> 00:04:35,01 And we can see the updated and improved code here. 105 00:04:35,01 --> 00:04:36,09 So as you can see, 106 00:04:36,09 --> 00:04:40,06 the idea here is you can write the code yourself, 107 00:04:40,06 --> 00:04:44,00 you can create code for building an agent like this one 108 00:04:44,00 --> 00:04:45,04 using your own understanding, 109 00:04:45,04 --> 00:04:48,07 but there are always corner cases that you may not see, 110 00:04:48,07 --> 00:04:50,07 and I find that using an AI assistant 111 00:04:50,07 --> 00:04:53,03 can dramatically speed up the process 112 00:04:53,03 --> 00:04:56,03 of that polishing your code and hardening your code 113 00:04:56,03 --> 00:04:58,05 and finding those corner cases. 114 00:04:58,05 --> 00:05:01,07 I gave a quick example of a clear semantic prompt, 115 00:05:01,07 --> 00:05:04,02 and I would encourage you to think about that, 116 00:05:04,02 --> 00:05:06,02 think about the code that you're doing, 117 00:05:06,02 --> 00:05:08,06 and provide those prompts that will allow you 118 00:05:08,06 --> 00:05:12,01 to delegate the important and repetitive tasks 119 00:05:12,01 --> 00:05:14,05 of doing things like adding error handling. 120 00:05:14,05 --> 00:05:17,00 I've provided in the download finished versions of the code 121 00:05:17,00 --> 00:05:19,07 that I worked on that I find is more robust 122 00:05:19,07 --> 00:05:21,05 than I built it in that way. 123 00:05:21,05 --> 00:05:24,01 And in the next video, I'm going to use the same technique 124 00:05:24,01 --> 00:05:27,00 to add documentation to the code.