0:00Hi, this is Jeremy Howard from fast.ai. And I'm joined here by Johno. Hey Johno.
0:08We're going to be showing you today how to build an agent from scratch. An AI agent, not a PR agent or a sports agent. We'll be showing exactly what an agent is. We'll be showing where and why they can be useful, as opposed to harmful. They can certainly also be harmful. And hopefully that in
0:33itself will be very interesting and useful. But also in the process, perhaps more importantly, we're going to be showing you how you can use a particular approach to learning anything.
0:48It doesn't have to be how to build an agent or what is an agent, but an approach to tackle any you know, complex topic that you'd like to learn more about. This is lesson two of 'how to solve it with code'. This approach we're talking about is called the 'solve it' approach. Uh, you don't need to have watched lesson one. Might... some things might perhaps have more useful context but it's
1:12fine to just keep going here. But before I start, I think Johno, I'd be interested to hear your thoughts on this, but I think I'd summarize it as saying we're not going to show you today how to get an AI to do everything for you, because that would not achieve the goal that we've set out, which is teaching you what is an agent, teaching you how to learn things. The whole goal of the
1:39solveit approach is to create a positive feedback loop for you personally, where you become more and more competent over time. You get deeper and deeper understanding of your craft, deeper and deeper understanding of what you're building and how you're building it. We leverage AI to help learn, but we... and sometimes we get the AI to even build little bits for us, very small bits, but that's less common. Johno, what would be your summary of yeah, what we're trying to show here?
2:12Yeah, I think, I think I think that's pretty good. It's not that the thing we're going to build won't be able to do lots of stuff together. You know, we'll show it doing some of the problems that we're also going to do potentially. But we'll have some good discussion as to why you might want to choose one or the other and where the limitations will be. But also, yeah, I think the, this lesson two being on agents isn't because you need to know about agents.
2:38Like the main focus is this is a fun thing to like explore how one does something like this in code.
2:43And I think you'll find it's way easier than you expected. Uh, and it's going to hopefully totally demystify it. Now, I will add in the video resources links to a couple of videos I strongly recommend you read about why you should care a whole lot about your own personal development and building of your craft at this particular point in history. And that if you how if you fail to do so,
3:09you may be kind of throwing away your future in some ways and so hopefully that will convince you that this is worth doing. So let's dive into it. Um... So, the stuff we're going to be showing today is in Python. And I'm using a our software platform here, which is unsurprisingly called Solveit. The vast majority of the stuff we show today, you can use any other platform to do the
3:39same thing. We think Solveit's a more convenient way to do, make things go well. But yeah, definitely fine if you don't have access to it. Um... it's if you do want access to it, it's at solve.it.com. And you can just sign up. And I want to show there's a really nice demo that Anthropic
4:02created. Called a customer service agent. And we're going to build this. And as you can see, there's a lot of code. And it does look pretty complicated. But actually you'd be surprised how easy it can be. So I'm going to show you. So basically what they do with this code is they
4:27create a little mini database with a couple of customers in it and the customers have some orders. We're actually going to use a more sophisticated version. And I'm actually saving myself a bit of time. I've got it here. So... Here's my list of orders. This is just like a database table. We could do this in SQLite. This is our primary key. Each order has a key. And then it links to a dictionary of things about that order. Then a customer also has a primary key.
4:56And then some information, and then they have some orders. So, unlike a proper database, we haven't... yeah, we haven't kind of set this up with SQLite or whatever, but the same structure is basically here. And so what they do in the example, the Anthropic example, is they set it up to to have an agent that can answer questions and do actions on this customer service database. So you could imagine this being your your company's customer service DB. And so here I've just created
5:26some functions. Uh, I've created a function called `getCustomerInfo` that gets a customer ID. Uh, and it returns `customers.get(customer_id)`. Or it'll also return `customer not found`. Same thing for orders, `return orders.get`. And `cancelOrder`. Uh, check that the order is in orders. And if so, set its status to canceled and return true to say I did cancel it.
5:55Okay, so that's where we're going to start. And we want to be able to answer questions like, can you tell me the email address for customer C1? For instance. Um... Okay. So... when you're coding in Solveit, it's a lot like coding in Jupyter or Colab if you've used a system like that. Um...
6:22so these messages, this is a code message. As you can see, and it uses the same kind of Monaco editor that VS Code uses when you go to edit it. Um... Uh, and I can hit B to create a new message. Rather than making it code, I could say prompt. And I could say, that's the wrong button.
6:44I could do this. And this little button I clicked here, I've turned off include in prompt, so the AI won't be able to see any of this information, and it can't see any of this information. Um...
6:58So it shouldn't be able to answer this, right?
7:00No, it should not be able to answer this. It's poor thing, it's probably going to be quite confused. So if I hit submit... so it's like, okay, I don't know. So interestingly though it it knows what I'm doing. And that's because something it did get, it does get all the unhidden history. So it got Anthropic's agent demo and it's got 'Let's replicate'. So it's got all that. So it knows I'm trying to do that, but it doesn't know
7:21how. So what we can do is if I hit X to cut and V to paste, I can actually tell the AI that it's actually allowed to use my Python functions. And to do that, in a prompt or a note...
7:42you just put ampersand backtick, the name of the function that you want to let the AI use and then another backtick. Or if you want to get fancy, you can actually even put them in a little list, comma separated list. But I'm not fancy. So you can put this in a prompt, so you can say like use ampersand backtick `get_customer_info` to tell me blah, or you can just any note anywhere,
8:10mention it once. And you can see why we'd want to do this. Just because you have something called `delete_database` somewhere in your dialogue, doesn't mean you want the AI to decide it can delete your database. So this is how you opt in to say, I'm going to let the AI know that these functions exist and I will allow it to call them. And you don't need any MCP servers,
8:35anything in this platform, in Solveit, like just having it as a function is enough. I don't know there's anything else that makes it this easy. But if you're using something else, maybe you can go and learn about how to set up an MCP server and you could kind of do it that way instead. But here in Solveit, this is it. And so if I now rerun this message, having moved it underneath here, 'cause it only ever sees stuff above it... and I'll run it again.
9:09Oh, hang on. You didn't run it. You edited the output without changing it.
9:16Run. Thank you. So I shift enter to rerun it. And this time it says, okay, I'll look up the customer information for C2. It says it looks like the customer database hasn't been set up yet. That probably means I didn't, I never ran this message. So we have no orders or customers. So let's see what happened. If you look behind the scenes at the details, it called `get_customer_info`. And it got, customers not defined. Okay, no worries. So I'll run that message. So you can see on the date here that it's just been run.
9:48And I'll run this again with shift enter, and it's going to do exactly the same thing. And we can see that it's answered, customer C2 is Jane at example.com. C2, Jane at example.com. And we can have a look here in the details. You can see it decided to call this function with this customer ID. And it got back this result. So that might look all a bit magic. Like, how did that happen?
10:23But actually, there is no magic to it. And we're going to drill all the way down to the bottom.
10:28I saw some discussion in the chat a little earlier like, 'Oh, what is... is there a definition of an agent?' There's many competing versions, but the general idea tends to be some concept of you have a language model that also has some tools and it's able to operate in a loop deciding which tools to use or which actions to take. And so that's the kind of functionality.
10:46The idea here is we'll demonstrate, oh, okay, you've just seen something that is an agent deciding to use one of several tools to answer a question, just right within Solveit. Take some definitions of of agent. And then we're now going to start like peeling away the layers of mystery to see like how that works. Yeah, this question, does Solveit replace LangGraph, LangChain? There's lots of different frameworks for orchestrating this kind of thing, and in our opinion, they often tend to overcomplicate the core idea, which is it can be as simple as just
11:15giving an LLM a few tools that it can use, and making it easy to define and use those tools is kind of the where that you make it fun and easy to explore these kinds of workflows.
11:24We don't use those things. So for us, yes, it replaces it, you know?
11:29But it depends on your particular needs. So somebody's asking, can you use MCP?
11:35And the answer is, absolutely you can. No problem. Because you can use anything that you can do in Python. So you can easily have a MCP client here instead. Um... so yeah, so behind the scenes, Anthropic is trained the model that we're using behind the scenes and when... and
12:00we're passing Anthropic all the information it needs automatically by basically using Python's reflection capabilities to figure out exactly what's available here, and the documentation for it and the comments and everything, and we're giving it all that to the AI and saying figure out what to call. So there's no particular danger here because the only tools it can call are the tools that we've said it can call. It can't call any arbitrary Python. Yeah, so these are the only
12:30tools that it can use currently 'cause that's all I've said it can use. So, yeah, so we could, say, what is the status of order 02? And again, as we discussed in the last lesson, really good idea to have a good speech-to-text setup. Personally, I'm using MacWhisper here.
12:47But there's lots of good options. Um... so if I submit that... same idea. Now this is all pretty straightforward, right? All it's done in each case is figuring out a function to call, and it's called the function. And we could do the same thing. So we could say `get_order_details`.
13:10And these are the arguments that are being passed. So in Python, you can pass a dictionary of arguments with this syntax. And you can see it gets back the result. Where things get interesting and that makes it more than just a tool call but makes it an agent is when it can decide to do multiple things, based on information. So a really interesting one to do would be to say...
13:46please cancel all orders for customer C1. So we haven't got information about customer C1 before, right? So it's gonna have to first of all find the orders for C1 and then go through each one of them and cancel them. So when we call that... It's like, I'll first look up customer C1. This
14:09customer has two orders. I'll cancel them. It says it's canceled them. And I'll say... Great, please double-check the status of those orders to confirm the cancellation went through successfully.
14:27So, that's an agent. And this might give you a sense of actually why we built Solveit, because we're an R&D lab. And most R&D labs aren't building stuff like this.
14:48But our R&D lab is unusual in that the goal of our lab is to figure out what AI is good for, so that we can help improve society by making that available to people. We're a public benefit corporation and that's our charted public benefit. So we needed a way for anything we
15:13could think of to be like, well, is AI good for that? And if so, how do we make it work really well? And so we built this so that we don't now have to go away and create a customer service agent system. We can just create anything we can envisage uh straight away. Um and I'll share this
15:35dialogue in the notes when it's done. But we, so we have a lot of interesting stuff at our at at our parent company, AnswerAI. For example, we we own a law firm called Virgil. And the reason we own a law firm is because we figured out that AI is pretty good for doing law stuff. And so
15:58our lawyers live in Solveit. You know, they do their contract drafting, they, you know, they do their all their research and so forth, pretty much in Solveit. My co-founder, Eric Ries, the author of The Lean Startup, has a new book coming out, which was written in this environment. Uh, he didn't have AI write any of the words for him, but he used it for his fact checking and for like checking for any possible issues and, you know, just to provide this kind of a canvas. Yeah, so...
16:37that's what... so this is, this is what an agent is, right? It's, it's something that has is an LLM that has access to some functions. These functions are written in Python. Python is a Turing-complete programming language. So now your LLM can do anything on your behalf, including doing things that then lead to it doing other things. How are we going, Johno? Is that a reasonable description?
17:07Yeah, I think that's relatively clear. And so the promise of this lesson is that we will build something like this for ourselves, yeah?
17:15Yeah. Exactly. I mean, we kind of have. You know, this is the highest level of build it yourself. We have now built an agent. And so most of actually the agents I build nowadays, I this is how I build them. Um... but let's go, let's go deeper and deeper, and create some more interesting agents
17:37that are going to be a bit more fun. So one thing that I kind of like to do as I go is to kind of keep my things nicely organized. So you can see I've got a little markdown header here, which is a note. And so if I now go to the next bit, we will create a...
18:02There's a question, do we get to see how Solveit implements the agentic features we just saw? Yes, the piece that we'll be building up is very close to what's running behind the scenes. And lots of questions around visibility. Yeah, everything above the message that you're currently like the prompt that you're currently executing, everything above that is visible, unless you've clicked the little icon to hide it. Or you've got so many things above it that they start to go out of context. But for the most part, it's everything above the AI can see what you can see, all the notes, code outputs, et cetera.
18:32So, we'll say, okay. Creating our own... Actually, before that, we'll do using tools to explore and learn.
18:47Okay. So, one of the things I find agents really good for and a lot of us, at fast.ai find it really good for is to learn about code and about code bases and kind of to answer questions about how things work so that we can then figure out how to make them better. So we actually have some,
19:11built-in tools, for this and you can bring them into your environment, by typing `fc`. Okay, let's just make sure I've run the first... Okay, by running, `fc tool info`. And, that actually uses, something called, dialogue metaprogramming to insert some messages in here, which is nice.
19:39And that's going to set up a set of tools for me. And the tools is going to be, if you've used `rg` before, it's like `grep`. There's something to run `grep` to search for things. You can run `sed`, which lets you modify files. And then it's got various view, create, insert, et cetera, functions to modify. And so this is basically the set of tools that, for example, Claude Code, has access to. So we should be able to do pretty much anything, that we do with Claude Code.
20:11Um... so today we're going to be learning how to do a tool loop. Um... and that example I showed,
20:28of orders and customers and so forth, is actually from also in the documentation of this... library we created called Claudette, which actually has something built into it called toolloop. And that's actually what we use inside Solveit. We're actually using toolloop. So the best way to kind of dig into how to agents work is let's how to see how this actual one works. So we could start
20:54doing that by bringing that information in. So we can say from Claudette, we'll bring in what it's got and we'll also do from `Claudette.toolloop` or bring in what that's got. And, you know, in this kind of environment, one of the nicest things I think you can do is to kind of instantly find out,
21:19about anything you're using, by first of all, hitting the question mark button, to get, you know, some quick documentation about something or double question mark button to get the full source code of something. And one of the things that question mark does is it tells you exactly
21:41where that is created. And so what we could do is to help, our agent to, tell us how toolloop works, how is it built? And then we could use that to dig into it. So to help it, let's cd into
22:01the directory, where that is. Um... sorry, `%cd`. And, let's see, we'll say, great. Uh, I want to learn how Claudette's toolloop works. Can you please read the documentation to see how to use it? And then, use your tools to investigate the source in the current directory and show me
22:30how it's all put together. As you use your tools, detail what you're doing and exactly what you find out along the way. And again, we can just help it out a little bit. So it can search just fine, but we may as well since it's here, let it know where the information is. And as you can imagine,
22:54we have a tool to read a URL, for instance. We have a tool to search the internet and so forth. In this case, I want it to think a bit more carefully about this and take a bit more time. So I'm going to click on the 'use thinking' button. This is just going to take a few more seconds up front for it to think through the process, kind of reasoning, as it's called. And I'll say submit.
23:16So that's what these little brains are. That's its thinking. So it's going to read the docs.
23:24And so, just to step back, what's going on here, we have this library that we've imported, Claudette, and there's a chat thing within this Claudette library that we want to understand. Right? So this is like an external library that you could just as easily import `plot` from Matplotlib or some piece of, NumPy or some AI library, whatever you're interested in understanding. We've just figured out where that library lives on disk, and then we've told Solveit to say, hey, use your tools for reading files and searching. Go and figure out how this piece of code that we've imported works.
23:59Yeah. And so, it's quite fun to see what it did. And so, you can actually see what, in Solveit, what its tools are that it's using behind the scenes. So in this case, it used a tool that's actually built in called `read_url`. You know, and by the way, if you're, interested in understanding, how `read_url` works, end keys now not working on this keyboard. We could just
24:27type in `read_url`, question mark, question mark. And you can see the source code for `read_url`, you know. So there's nothing that's... So all of our tools are just written as Python functions.
24:39We just use these exact approaches internally. So it read that URL and it got back the web page. And then it's like, okay, I see how to use it. Let me read the source code. So first of all, it viewed the directory it's in to find out all the files there. Then it viewed `toolloop.py`. And then okay, it's discovered that `toolloop` is using something called `chat`. So it needed to know where `chat` is defined. So it used `rg`. And said, up to five, look for `chat` in `core.py`.
25:15Got back the result that it's defined here at line 420. Um... kind of cool, isn't it? You know, let me view the `chat` definition. So it looked at `core.py` line 420 and got back the chat definition. And then it saw that called `call`. And then that called `tool_res`. And then it used tools. So at this point, I kind of just wanted to tell me what it's learned,
25:43or just to figure out what to do next. So we have a shortcut key, which is just, Command Shift Slash, which basically means, okay, what did you, what did you learn? Where did you get up to?
25:52And this is like continue because there's a limit on how many tool calls we do before we stop it continuing to do it forever. Like this, in this case, it's done nine or 10. We stop it continuing to do it forever. Yeah.
26:01Yeah, it could just be that it ran some stuff or I ran some stuff, you know, just means, you know...
26:08It's just basically saying to the AI, you decide what to do next. And it might just say like, hey, I think we've got all the information we need. This is going a lot deeper then. So it's probably going into more detail than we need. That's okay. Great. Uh, so I might just tell it like, that's probably enough detail at this point. Can you, give me a detailed explanation of everything you've learned, please, about, how this works and suggest some approaches I could use to learn more
26:33about it. So this is a really good use of agents, in my opinion. We're not using it to outsource our work, but to actually become more competent. So we're learning about something we didn't
26:55previously know. And this is saving me quite a bit of time. And, it can help me explore this, you know, so like I could either start poking through the source if I wanted to, or I could ask it to show me the bits as it goes on. And so here's some suggestions about things that I could try. So,
27:19I think this is pretty good. It's still going. Great. I'm going to, close these up. So if you hit 'O', that closes up the output. Just makes it a bit more tidy for me. And, let's, uh... What I...
27:40what I actually really wanted to show you here is, you know, it's a pretty small set of tools, and it may look kind of magic. Um... but it's not at all. So for example, you saw it ran `rg`. Um... well, what is that exactly? So again, we can just type `rg` and find out. And actually, it's a single line of code. It's like, well, that's cheating. It's calling another thing called `run_command`.
28:07So what is that exactly? `run_command`. Well, it's only running seven lines of code. And so the key thing it's doing is it's getting a command to run, and it's calling `run`. That's just,
28:24Python's built in, what is it, `subprocess.run` or something like that? So we could, you know, do for example... so you can if you hit 'W', it takes the code from the output and creates a new message with that code. And you can see here that it's passing, can disallow a particular regular
28:51expression or it can allow a particular regular expression. So if we wanted to make sure that the... AI was never doing anything in a parent directory, for instance, you know, we could actually override this and say... `disallow_re` equals... anything with... two full stops in
29:24it, for instance. And so that would mean that now anytime the LLM attempted to run `rg`, if it tried to access a parent directory, it wouldn't work. So yeah, that's that's that's that's all it took. Uh, and, you know, we can look at a few more. So actually it also used `view`, didn't it? It used `view` to both to list the directory and to look at a file. So we could do the same thing with
29:54`view`. Okay, still fits on a screen. So it gets passed a path. Remember we saw it try to view a file and like a particular set of lines in order to look at the definition of `chat`. So it creates the path, make sure it exists, sees if it's a directory, if it does, it lists it out. Otherwise if it's a file, it reads the text, grabs the range if you wanted one. That's it, right? So it's not
30:23magic at all, right? So these particular functions were predefined in this `fastcore.tools`. Um and so this is like the set of functions which, you know, we think are the ones you probably need to explore your file system, to make little edits, to basically kind of be like Claude Code, basically.
30:49But you can easily add more. And you know, this is where you might want to be a bit cautious about, you know, some of these like `insert` or `str_replace` can edit files on your file system.
31:09And so if, you know, something that can be quite helpful is you can actually create a new instance.
31:15So each instance in Solveit is a whole separate Linux virtual machine. And so then you can play there to your heart's content and there's not, you know, you don't have to worry. Or, of course, you could just hit the enable versioning button. And then every time you save, it's going to create something called a `git commit`. So if you hit 'S', you'll just automatically get a `git commit`.
31:37So you can always undo. So there's a few kind of ways you can, try to avoid getting into a, a nasty situation there. Once it's in Git, it's pretty hard for any tool to delete that. You don't have to set up GitHub or anything like that. You just click that button. And it's all, it's all done.
31:58So Jeremy, I want to ask you, like, so we've been inspecting the tools themselves and seeing that they're just Python functions. But the like the title of the section was like using these tools to explore and learn. Um and we kind of ran the agent, but we didn't actually like read any of the results, which I think is how most agents are used. But I just wanted to check like what is the sort of outcome that we're going for here other than seeing that these other tools are useful? Did you want to like, view its output or is that still coming next?
32:26So I'm not going to go through this. I'm actually going to go through all this myself because I already know how it works. So I'm going to take people through this myself. This is pretty close to how I would do it, to be honest. So it's like, okay, and in fact, this is basically what we're going to do next. It said, 'Hey, try creating your own tool and then create a chat class passing it, and then call tool loop.' The only problem is the tool they've created is boring. So I'm going to do this with a more interesting tool. But this is like the, remember the kind of the purpose of
32:55this was to say, 'Hey, here's how other people can learn... how to learn things.' Right. So if you're interested in learning how to create your own agent loop, you can follow the steps in this video, or you can have Solveit or any other AI try to teach you. So yeah, so let's go ahead and do that. So... Oh, now my three stopped working again. Great. Okay. Creating
33:24our own tool. Um... that was meant to be a note, so I'll hit command shift K. Remember we've got the keyboard shortcuts on basically everything. C means command or control depending on what you're on. Um... so actually, the tool I want to create is the most dangerous possible, but most crazy fun tool, which is to give it access to all of Python. So,
33:50what can go wrong? Give AI access to a Turing machine connected to the internet. This is the bit where it turns us all into paper clips. So if that happens, I apologize. So to give it access to Python, there's a number of ways to do it, but I've actually created in the past a,
34:09little thing called, `get_shell`, which is a little Python, shell. So we can just import it, and we can use it. And maybe we'll have a quick look at some of the documentation, for that.
34:29Um... or, you know, maybe actually, we could just say like... hey, I've never used this, particular shell implementation before. Could you do a bit of a search and tell me roughly how it works, please?
34:48Okay. So, you know, these are the two approaches you can use to, learning to use some new library or API or whatever. Personally, my approach normally actually though, is to go to the documentation for it. Because like it's pretty easy to read. Oh, okay. I get it. That's how you do it. It thinks I want to know how how it's implemented, which actually I don't.
35:14How... and that's because previously I was asking how something works. Um... Okay. Fair enough.
35:30Okay. So while that's happening... So, this is interesting, right? It actually wants to find out what's in this, and we didn't give it access to `dir` as a function. So it wants to do this. So to do this, I'll hit W, and that will pop that out into a message, right? And I'll run it, right? And then I can hit command shift slash. And that will allow
35:57it to continue with that information. So, this is a really nice way like is instead of always using tools is to kind of use this approach where it can decide, you know, what it wants to see as well.
36:12In general, this is like there was a question about, 'Oh, isn't it dangerous letting Solveit go off and do things?' For a lot of the work that we do, it's much more like having Solveit suggest some code to run and then running it ourselves and seeing what it is and seeing what the output is. And the tools are much more like, 'Oh, I'd like you to go read a few web pages and come back with an answer from the docs,' or I'd like you to go read a few Python files and figure out where some key function is defined.
36:36Anyhow, we're just going to use the docs. And so, what I'm going to do is I'm going to, so first of all, basically see I can say like `shell`. `shell`, no, I need to be in code.
36:53`shell.run_cell`. say `a=1`. `shell.run_cell`. Just passing it in text that's going to run in Python, right? And it's returning these execution results. And here's my result, `result=1`. So, I want to create a tool that the AI can use to do anything it wants in Python. So I'm going
37:22to call it `execute`. And then you basically have to say what parameters do you want? And you can see here that I've got some ghost text popping up. So I can just hit right arrow to accept that if I want. By default, you don't get that because by default you're in learning mode, which is kind of meant to encourage you not to rely on AI too much. And in learning mode,
37:47you can, ask for a ghost text by hitting, option full stop. Like so. So, we can then... give it a doc string. So this is a place where I'm guessing it can probably figure it out for itself. Yep, it did. So I'll just let it write that. So I kind of like to do it just this little bit of completions from time to time. Yes, full stop or period. Um... so I'll just say like,
38:16okay, get a result by calling `shell.run_cell`. Um... cool. And then we'll return `res.stdout` if `res.result` is none, else... I bet it can figure this out too. There we go. Okay. Um...
38:44And so for a Python function to be a tool, Jeremy, it needs, it needs types so the language model knows what kinds of parameters it can pass in.
38:52Yeah, exactly. So if you didn't have the colon string, that would not be usable. And it also needs a docstring, so it knows what this is for. And ideally, you should always also put a comment after each parameter so that it knows what each parameter is for. You don't have to, probably in this case, I could probably get away without it because it's pretty obvious. It's called code, it's something that says 'execute code,' but I'm in the habit of always adding them,
39:16because I think it makes it more reliable. So we could hopefully now do this. And it should give me a slightly nicer response. There you go. Okay. So that is now a tool. Now, you'll remember we don't let the AI use any function it wants to... because that could be dangerous. So to tell it that I
39:44want it to be allowed to use this tool, I can tell it. So I'll say... let's do Advent of Code. So for those of you that haven't done lesson one, Advent of Code is a fun little, programming puzzle thing.
40:05And it's got a nice little library that we can use, called, `aocd`, Advent of Code Data.
40:16And to use it, you have to make sure that you have, an environment variable, or in Solveit a, a secret available called `AOC_SESSION`. `AOC_SESSION`... in your environment.
40:34And if you've set that in your secrets, it'll be in your environment as well.
40:39Yeah. Yeah, exactly. Okay, there you go. So, I just confirmed that it is there. So, let me have a... So this, okay, so it gives you a sense of like, things not to use agents for.
40:54Don't have it solve your Advent of Code problems, you know, it would be like using a calculator, when you're a kid and you're meant to be solving it, learning your times tables.
41:02But just to show we can, let's try this. Um... I want to test the tool that I just created.
41:10Please use it to grab the Advent of Code 2024 puzzle 1 information and example data, and then try to solve the first part of the problem. Tell me your solution when you're done.
41:22Have a look at the `aocd` docs. The problem is available in the puzzle object. Okay. So, remember anything in a prompt or a note anywhere that has ampersand backticks and that its name, immediately
41:39becomes available everywhere from then on in the dialogue. look at the `aocd` docs at... So again, just give it a little bit of help. Advent of Code Data. Let's give it, give me the PyPI,
42:01see, home page. A bit surprised it didn't take me straight there. That's okay. So one little trick, by the way, for pages like this is, in GitHub is hit the raw link, and that'll give you the, the raw, you know, notebook JSON or in this case, README markdown. That's kind of what your, AI is going to be happiest reading. Uh, great. So I'm going to let it use thinking.
42:25Jeremy, I just realized I was confused by a question in the chat about the dollar sign. The dollar sign backtick syntax is to share variables, and so that in the first line of this should probably be an ampersand.
42:35Ampersand. Thank you. We haven't done dollar signs yet. That's for a future lesson. Okay, so we've written a, the most powerful and general possible tool here, which is the run any Python. And, it's running it, in a persistent shell. So it can import things, 'cause we didn't import `aocd`,
43:02we didn't, you know, do anything much, right? So it's gonna have to figure out everything about how to get the data, how to read the puzzle problem... and so... there we go. So it tried to do it. It got currently the import didn't work. It fixed it. It's finding out how to get the problem.
43:22Um... okay. There we go. It's got 11, which I know in my case, everybody has the... well, actually, the example data is always the same. But the official data is always different. And I actually remember from last week that that is the correct answer. So it... Let's see what happened.
43:40It tried to do all this, and it got a traceback. And so then hopefully it actually looked at the documentation. Yeah, it didn't look at the docs at all, by the looks of it. That's interesting. I guess it figured it already knew. So, yeah, it's correct. So it got the puzzle and... there we go. Got the problem. And then it wrote some code. So then I'd be like, cool. Um... great, please write out the source code of your solution and tell me how it works.
44:18This is how a lot of people use agents, you know? Like, they've got an opportunity to learn about something and improve their craft and whatever and they're like, 'Oh no, I'll just get the computer to do it for me.' And it's actually pretty rare in the world, or at least in my world, where I do a piece of work that I don't want to get better at it, you know? And I don't want to learn anything from it. So like, I... this is not how I would solve an Advent of Code puzzle.
44:46But you can. The thing is though like, there could be a place for this if you're like going to get a proof of concept, have it explore things, explain what it learned. For sure, you know. So it's like this is, I think we're trying to say there's a nuance here. It's not, 'Don't use AI.' It's not, 'Always use AI.' It's definitely not, 'Use AI as much as possible.' But as you'll see if you did
45:16the last lesson, I kind of showed at the start how I built kind of a series of abstractions to let me use Git in my code better. If you can, you know, develop your craft so you get better at creating richer abstractions, more sophisticated libraries, better understanding of the problems that you're solving... you're a bit in this positive feedback loop where you, you know,
45:42it's out of control. It's amazing. Like you just get better and better. And I don't want to brag, but like people keep telling, asking me, the most common question I get is, 'Jeremy, how do you get so much done?' you know? It's like, well, it's not 'cause I'm getting AI to do it for me. It's 'cause I'm on this journey of trying to develop my own superpowers. And like, I've seen people ask this about about our company as well, which is like, if you look at,
46:11we have nine people total. You know? And our, the amount of stuff we build and the quality of it, it's out of control. Like even Solveit we built. We built it using the framework called FastHTML we built. We started it using the styling framework, once the UI, that we built.
46:33We hosted it on the infrastructure we built. And yeah, it's like, I really think that this approach is a great way to to develop and also just to to enjoy your work. I don't know, Johno, do you have any thoughts before we keep moving on with our how to build an agent?
46:56There's... there's a part of it which is that it's extremely hard if you know, like, and just click the button, right? That says 'solve that Advent of Code day one.' And Advent of Code is a funny one because you're choosing to do that because it's a puzzle. But I know even if it's something that I've chosen to do for fun, that when there's that knowledge that just hopping right over there is just the like get an instant answer button, it's very, very hard not to click that. It's like,
47:23it's very rewarding to feel like, 'Oh, I built this thing.' And then you go like, 'Well, did I build this thing?' Not, not really, right? Um... but yeah, it's it's extremely tricky to like, I I don't want to be judgmental of people who look, your employer is asking you to do this thing and then you have the button that says do the thing. Like it is is very like the obvious answer is to click the button that does the thing. And it's very hard, and requires a lot more like
47:48willpower and intention to not click that button. And so we've just shown you like even in Solveit, you can have the equivalent to that button, right? You can give it some tools and say, 'Go to town, solve my problem,' and it will. You can put it in standard mode and just ask it for all the code and it'll write you two pages of code. And so, yeah, a lot of it comes down to that discipline of deciding, okay, this is a case where I'm doing Advent of Code because I want to get better at programming. I'm going to very intentionally choose to not take the easy route.
48:16It's like a drug with a bad delayed hangover. Yeah, the hangover is like, you know, some people I've I've heard say like a year later after leaning into more agentic coding, it's like, 'Oh my God, I have not only forgotten how to code well, but I've forgotten that I can, I've lost my confidence.' Yeah. I think this is a path to feeling like you're on a happy journey,
48:42you know, you're having fun, you're learning. But it probably requires either doing it as a hobby or being in the kind of workplace that actually appreciates human development, rather than maximizing the number of lines of code you show each day. We often write only a handful of lines of code, but our handfuls of lines of code tend to do an extraordinary amount of work.
49:06Um... Okay. So, let's see how to, keep drilling down. So how do we, how do we build this from lower level pieces? Um... So how does Solveit translate into production apps? Uh, I mean...
49:27we're going to show you, right? But it kind of, at the same time kind of it doesn't. Like I'm trying to show you how to create a positive feedback loop for you to get better at your craft. So how does that translate into production apps? Well, when you're better at your craft, you write better production apps, you know? Um... you can directly use it to build production apps. And indeed, Solveit and our our Plash deployment system and everything we build is built in these tools. But
49:56it's not really what this is about. So I just want to be really clear here to say is like the purpose of this is to get better at your craft. Um... you know, this how to become a software engineer in 10,000 hours from from Peter Norvig thing, it's like, you work at it over a period of years, and and get better. But I think you can get better faster now, you know,
50:23AI can help you as we're seeing. Um... Okay. And, you know, yeah, maybe that's not for everybody, which is fine. This is for those people who want to to do that. But I think, you know, all the world's I know a lot of the world's best computer scientists and software engineers and they basically all work this way. They don't try to get as many lines of code out each day.
50:49Okay, so... using Claudette's toolloop. Um... so again, I'm not going to go through all the details of getting... the AI or the docs, 'cause I I wrote Claudette, so I know how it works. Um... and so the focus here is I want to try and show you, you know, how how the toolloop works, and then
51:15we'll keep going deeper and deeper. So, Claudette is a pretty thin wrapper on top of the Anthropic API. So if you can use Claudette, you can use the entire Anthropic API and all the models they have.
51:28And basically every API looks pretty similar. We actually also have a library called Lisette, which you could check out if you like, which is actually, let's use any model from any provider.
51:41I'm using this one because there's less layers of abstraction to understand, you know, I'm trying to get closer to the metal here. Um... so let's uh... grab... Haiku 4.5. That's a pretty good model, but it's also pretty fast. And so if you looked at, lesson one, you'll notice again, I'm doing this usual thing of like printing everything I'm doing and going in very small steps. And it's funny
52:08like a lot of people say like, 'well that seems obvious,' but I don't know anybody almost outside of AnswerAI that actually codes like this. So you know, yeah, it's easy to see what to do, but like you actually have to practice it. You'll never have a bug, or at least a bug that's more than two lines of code long. So, I want to just, let's create something super simple, right? So we'll create an `adder` function. And I'm not going to bother adding, comments to these parameters 'cause
52:37I think it's so obvious what they are. I'm pretty sure that the, AI will figure it out. So we'll create an `adder` and we'll create a `multiplier`. But this is actually going to let us, you know, practice with some agentic stuff, because we can create things that are going to require multiple steps to solve. Uh, and again, I can probably let it finish this off for me. We'll do this one as well. Uh, I mean, full stop. Okay. So we could, try that. Hardly seems worth it. Plus two. Adder.
53:26These are separate arguments, right?
53:27Thank you. That's the whole point of it, right? Okay, and I guess to what I wanted there. So that's nice. Great. So... I want to test my tools. Can you use `adder` and `multiplier` to solve...
53:50I'm going to type that myself. So it's going to be ampersand backtick. Well, since I said we can use the fancy approach, we might as well go ahead and use the fancy approach. Not `multer`, comma, `multiplier`. Uh... solve... a bunch of numbers... times... a bunch of numbers... plus... a bunch of
54:17numbers. All right. So, you can see here, I've created two tools. I've made them available. And so the prompt should work. And so now we're going to... I think it does. We'll try to do the same thing from scratch ourselves. Check. Or we could also just check that it ran the right tools. Okay.
54:47So, to do it ourselves, we have to create a chat object. So most APIs are going to have something like a chat object, and we're going to dive deep into it to see exactly how this works as well, don't worry. And we're just going to go through one step at a time. So if you hit shift enter, you can find out the documentation for that. So it needs a model. And you can also pass in some
55:13tools. So, I'm going to pass in my model, and I'll say `tools` equals `adder`, comma, `multiplier`. Uh, there's my chat. Um... and so for this to work, you'll need an Anthropic API key secret set. So if you go to Anthropic's website, to their API, not to their `claude.ai`, you can sign up for their API and you can get a an Anthropic API key, pop it in your secrets and then you should be able to, use this. So if I go see... Hi, I am Jeremy.
55:51So this is no longer using Solveit's built-in AI. This is us somewhat manually using a library to query Anthropic's API directly.
56:00Mm. In fact... in order to ensure we don't get confused, you know what I'm going to do? I'm going to hit D. Um... No, that's fine. We won't get confused. So just nothing above this. In fact, I could just... I got to hit left arrow to fold it all up, right? So... Um... and then, yeah, let's create a new section for this and then we won't get confused.
56:28So this section is... Oh, I do have a section for it. Never mind. It's confused somewhere. Okay. And so when I said, 'Hi, I'm Jeremy,' it said, 'Hi, nice to meet you.' And because I told it here what its tools were, I told... it also tells me it has some tools available. So that's kind of nice.
56:53So then I could say... do you remember my name? Sure hope it does. So these are code cells, right? This is not prompts. So this is stuff you can run in any environment at all. Um... so this is actually how easy this is. So I'll hit comma to copy my prompt there. And then I'll hit V,
57:19but this time I'm going to say `pr` equals, and this is in a, code cell. Um... and, we should be able to simply say at this point, `result` equals `c.toolloop`. And as you can see,
57:40that... what we're going to expect to pass in here is a prompt. And then immediately we'll see what that looks like. Okay. So it's created something that's not directly viewable. And the reason for that is that a tool loop is something that kind of has to happen step by step. So
58:06Python has a really nice system for this, called iterators. And so we can, loop through it. And we could... we could print it, we could display it. Um... we'll start by printing it. Print... uh...
58:28No. Oh! Yes. Probably O. Okay, let's try that again. Print O. Okay, so away it's going, and it's doing stuff. It's a little bit hard to read that. But we can say from `IPython.display` import `display`, and this will give us a nicer, richer version of it. Um... so, let's do that
58:52again. `display`. There it goes. So, you can see the similar kind of thing, right? It's it's doing some text, and then it's, if we look in here, you can see that it's got this, tool call block
59:15with this input, the name. So you can probably imagine what Solveit's doing under the hood. It is actually using this exact function. And then we called that tool to get the result. And it's gone ahead and done it all for us. So, that's that's what it looks like. It it does look a lot simpler
59:43still than all this stuff that the Anthropic docs say. And the reason for that is that, you know, we try to make everything very ergonomic. We can do that as well, but behind the scenes, what that's actually doing is using something called JSON Schema. And so we can import something to see a JSON Schema. And basically we're automatically generating this. So if we call `get_schema`.
1:00:12The most interesting one might be... Let's look at it. Let's look at our `ex` one from earlier on, the one that it executes. So you can see it's got the description, it's got the name, it's got the parameter, it's got the type, description of the parameter. So these are the things behind the scenes that we're generating, in order to try to just make this a bit more ergonomic.
1:00:34So for Jeremy, for someone who's maybe less familiar with how LLMs work, what's happening right at the bottom of our code is we're sending off a piece of text to a model that's used to seeing long pieces of text, right? And it's going to be in a very particular format that Anthropic have defined, where you have a whole history of messages. And some of those messages are user messages, like Jeremy saying, hey, I'm Jeremy, hello. And the assistant messages saying, hey,
1:01:01I'm here to help. And then some of those messages are us asking it to do something and it replying back with a request to use a particular tool. And then some of those messages are us sending back the tool results or the tool definition or whatever. So it's this kind of back and forth exchange that ultimately turns out as a big serialized piece of text that gets fed to the model. And then it generates some new text at the end of that that gets sent back to us. But
1:01:27in between, we're just formatting it in this very particular format of basically a dialogue history, like a chat history between us and the model. And so all of this functionality here, like this get schema is a way to say, like, how do I take my Python function, turn that into an appropriately, you know, described piece of text. And then all of the chat calls and tool loops, are just ways of saying, okay, now I'll put a message from me and then see what the LLM says back. if it's a tool, you know, and so, yeah, that's, it's like just building up this conversation. Is that a fair summary of like the core that's going on?
1:02:00Definitely. We'll dig into that. Yeah, we'll dig into that right now, actually. Let's have a look.
1:02:04Before I do, I just want to answer this question about, we have an app that throws up the hotkeys being pressed? Yeah, we're going to do that, for the next lesson. I didn't want to distract from the key ideas here. I didn't want this to become like, 'how to use the Solveit platform?', because that's not really the important piece of this. We're trying to teach what's an agent, how are they made, and how do you work step by step. See how we're going like top down, gradually
1:02:32going lower and lower, to kind of understand things fully. And so we, we'll we'll drop in the notes our link also to the one and a quarter hour 'how to use Solveit' video that Johno and I did, and to a full text article version of that, written with the help of Solveit. And, yeah, and then from the next lesson, we will get much more into the detail of the platform on the basis
1:02:58that anybody who's got as far as lesson three must care enough, they want to know more of these details. So thanks for bearing with us. Yeah, so, actually let me just add with chat. Yeah, so let's look at that, right? Let's, so to go to the next level, and this is the nice thing about this top-down go deeper and deeper and deeper approach, you know, which is a kind of a very Solveit way
1:03:24of thinking about things, small steps, printing everything as you go. So in a learning environment here where we're trying to understand this, every time we get to another layer, which is like, okay, we've got `chat.toolloop`. How does that work? You know? And then, we'll see that. And then we'll be like, well, how does that work? History. Okay. And how did that work? And actually,
1:03:49I will say the approach I used earlier, which Johno was asking about, what do you do with this, is even if I didn't know how the toolloop worked, I would still actually close this up. I wouldn't read it. And I'd instead say to Solveit, okay, please take me through that. Because there's
1:04:18too much, right? And in particular, I'd make sure I'm in learning mode, which is the default. And in that mode, the prompt is going to encourage you to do really small steps. I turned off learning mode for this step because I wanted to show you what it looks like doing it all at once. Let's have a quick look. So if I hit B, right, it can't see anything underneath. Right? Okay. I could say like, wow, that looks amazing, but it's also kind of intimidating, to be honest. Can you help me
1:04:44take this, take me through this step by step, really slowly? What's the first simplest thing I could do here? Because now it's got all that information, and that the information's really for it rather than for me, right? So if I, let it answer this question, you know, we can start...
1:05:08okay, so hit W, right? Here it is. And, uh... Now this one looks simple enough, thank you. Can you show me what the next step is now? So that's what I would do, actually. You know? Otherwise, you'd like have to print it out and keep looking back at it. And then as you start exploring, you
1:05:33realize you want to go in a different direction to what it thought. You know? And so here I could, we've got one called `M` actually. Um... we could show the chat afterwards. Um... we could then hit Command Shift Slash just to let it know that we've run that, we can keep going. I can hit W.
1:06:01So yeah, I probably should have answered it that way earlier, Johno. Um... anyway, so we're kind of going through this. And so, yeah, you would do this for loop, blah blah blah. Okay.
1:06:14So we're not going to use toolloop though, right? So let's, rerun, let's rerun this. And let's pop it down here again. So we've got a nice fresh one to start with. And yeah, I'm not going to use the toolloop. So instead, I'm just going to call the chat directly, just like earlier when I did that,
1:06:38'Hi, I'm Jeremy,' thing. Right? So I'm just going to pass in my prompt. Right? Um... and just remind you what that prompt was. It's this. Right? So I'm just going to call that. It's just said, okay, I'm going to try to do this now. And it's got this request to do a tool call. Here, see,
1:07:05`type=tool_use`. This is a tool use message. It's saying, 'Please use this tool with these parameters, run it for me, and then tell me what happens.' That's how an agent works.
1:07:20That's basically it. Like Johno was saying, it's been trained with machine learning, stochastic gradient descent, check out the fast.ai course if you want to understand how that works.
1:07:31And it's been basically trained to spit out this particular set of text. Great. And so one of the things that you'll see in the big list of things that came out here is `r.stop_reason`.
1:07:53Now, it might be kind of surprising if you're used to using kind of VS Code and stuff like that, that it always seems to know exactly what to show in these lists. And the reason for that is that Solveit and other kind of advanced REPLs, like Jupyter and IPython, they do use a very different approach to VS Code. They don't guess what type R is and then they don't use type hints to find out what it supports. It actually looks inside the Python
1:08:21interpreter and finds out exactly what it is and finds out exactly what it supports. So you don't need any special typing at all to get this kind of really nice, exact, information. So here's my stop reason. And so stop reason says 'tool use.' So it said, okay, I tried to answer the question, and I didn't stop because I was finished. I stopped because I needed to use a tool.
1:08:49So the way... that Claudette is designed is if you now just pass it nothing at all, it uses the tool, gets the answer, and sends it back. And so we can just keep doing this, right? And, actually I should have put that in `r`. So let me just, so I'm gonna hit 'merge' here and then
1:09:12start again. Create a new chat. Run that, that... I'm going to put this in `r`. Print that. And the reason for that is I wanted to show you now if I say `r.stop_reason`... the turn's ended. So
1:09:37at this point, as... we've got the answer and it's finished. And if you look inside the chat object, there's an `h` for history attribute that has everything that's happened. So the user said, I want to test my tools. The assistant said, I'll solve this step-by-step. And the assistant said, please use this tool with these parameters. And then the user said, here's the tool result. Now
1:10:04we didn't do that, right? And that's because the chat object, which we're about to replicate from scratch, just called the tool and did it for us, right? And then the assistant said, cool, now I'll do this. And then it told us to use this, and then it told us to put in these parameters. And then we, behind the scenes, ran that and told it that this was the answer. And then the assistant said, great, there's my result. And... I'll hit shift O to turn this into a little collapsed block. So that we can see... There, there's the result. All right?
1:10:40Um... But, you know, we're doing this one step at a time. So let's now, automate that. So it should be easy enough. Let's create a new chat. And again, let's... call our function.
1:10:59So every time you make a new chat, you're cleaning the slate in terms of history, right? And that's the reason we're running that piece of code.
1:11:05Exactly. So if I show `c.h`, it gives me the history. So there's no history now. Yeah. Yeah.
1:11:10So what Johno was saying, the key idea here is on the Anthropic side, the model we're using, Haiku, Haiku has no idea what's happened in the past, which you'll see in a moment. It only knows it if we tell it. So we store the history of everything that's happened. And actually, as you'll see in a
1:11:30moment, all of that's being passed back each time. So now it's a fresh one. There's no history. So we're starting again. As far as it's concerned, it's never done this before. So what we can do now is loop it. So we can say while `r.stop_reason` is not equal to... and my one key's not working.
1:12:05This is where I wonder if it's going to guess what I want. I think it should. Yep, not equal to 'end turn'. So this is kind of nice, again, with AI, if you forget exactly how to spell it, it's safer just to have it do it for you. Then we'll say, okay, `r` now equals, just call the next line of the chat. And maybe we'll take a look at it. And that should be our agent. Woohoo! There it is.
1:12:36So I think, and you know, each layer we go deeper, hopefully more of the mystery of agents is being unveiled, right? So let's now say, doing it manually with no chat. Capital C.
1:12:57Okay. So chat did a lot of work for us, right? Called the function, kept track of the history, all that kind of thing. So before we do that, I'm just going to show you a bit more of what Johno was saying earlier, which is we'll actually create a chat, right? Never mind about tools for this one. And Johno will have a real evil laugh here. He loves messing with LLMs. So I'm going to say...
1:13:26Uh, for the record, I dislike this. You dislike this? Well, you always have an evil laugh when we do it, so I thought... I have an evil laugh, but it it feels extremely wrong to do what Jeremy is about to demonstrate.
1:13:36Okay, I'll take it back. Johno knows exactly what I'm going to do here. Hi, I am Jeremy. Geraby, oh dear. Jeraby. There's no Jerabys are there? Hi, I am Jeremy. Do you know what my name is?
1:14:04Okay, so there's `c.h`, right? Remember I'm telling you it's passing the whole history each time. So `c.h` zero is `role=user, content=Jeremy`. `content` equals... Johno. Anyway, either way, Johno is going to get blamed for this.
1:14:24Uh, you might want to... It's the whole thing right? 'Hi, I'm Johno.'
1:14:27Oh, yes. Thank you. Hi, I am Johno. Full stop. `c.h`. So now if we pass this back, this is the history, this is what happened. Johno said, hi, I'm Johno. And then we said, do you know what my name is? And it said, yep, your name's Jeremy. So we'll say... 'Wait, what? Are you sure that doesn't sound like my name if I remember correctly?'
1:15:03So, we now have a very apologetic, you're absolutely right. I apologize for the error.
1:15:09You're Johno, not Jeremy. I made a mistake. Okay. So you can see here that LLMs don't actually have a memory. They only have the memory that you tell them they have. Okay. So given that, we're going to need to do this without chat. We're going to need to kind of handle our own history.
1:15:41So the, thing in, Claudette that's the lowest level that's basically a very direct mapping to the underlying API, which is a pure send text here and there thing, is called the client. So we can create a client. Um... and so... oops, that's a note. It's be code. And if I hit shift tab, I can see, okay, just needs a model. So we're not passing in tools this time. That's way too fancy. We just a model. That's great. Um... so now if I say... 'Hi there, I am Jeremy.'
1:16:25And now if I say, 'Do you know what my name is?', it won't because I'm not using a chat. There is no history. It's never seen me before. We've never, we haven't met. Don't ask me. So that's how the, that's how the client works. It's just sending, behind the scenes, it's just sending network requests with this text and it's just getting text back. And the text back actually has all this other stuff in as well. And here's the stop reason, 'end turn', so it's all done,
1:16:54no tool calls, it's finished. Um... so, let's create a list with our tools. See if it can figure out which ones they are. Yeah, I'm not... it doesn't know what tools are meant to look like.
1:17:11So if I say `adder`, comma, `alt` full stop, `multiplier`, it guessed now. Great. Um... so, same basic idea, right? So there's no history here. So if we say, `C`... `pr`. So remember that's our math problem. And now we're going to have to pass in our tools every time. Okay,
1:17:37so let's put that in a response. Okay, so again, same thing. It knows what it wants to do. So, you know, what might be helpful here, I think, is to just, this is how I actually wrote a lot of stuff,
1:17:54in Claudette, is I kind of did stuff, you know, and, uh... to kind of figure out how everything should look like. And then I put it together. So we've already got this assistant response. We've got that tool used. But basically, we're going to need something like this, right?
1:18:20That's what, that's, that's what the history was before it then the assistant then came back. So I'm going to copy that. And we'll create a note here. Sample history.
1:18:33So if you're, just starting out earlier in your coding and you're wondering how Advent of Code applies to real life, it's often the case that you end up in something that suddenly feels just like the Advent of Code you've just been practicing, right? We have some some data that we're working with and we'd like it to match some other format and usually there's an example of what that should look like, right? The Anthropic API might have a a demo on their website showing the full JSON blob you should send. And so then the whole
1:18:59problem of how do I create a chatbot becomes the immediate problem of how do I turn my list of texts into something that looks exactly like this history that we need to recreate.
1:19:10I like this comment. I can... `b + tools + lm + fasthtml + fastcore = solveit`. That's not at all far from the truth. You know, like... that's a nice thing about creating nice little abstractions is we do tend to find we can go really fast now because we just plug together stuff we've made. And when we find something that doesn't exist, we kind of make that little piece.
1:19:38So we've got a few hundred repos. They're all things we, you know, we've all built them in the same way, which is using what kind of like my co-founder, Eric Ries, would call this kind of 'lean startup' approach. It's like you build something that needs the thing, and then we extract the thing out of it. So we don't start by writing a library. We start by writing a thing that we want to use, and we do everything manually,
1:20:05lots of repetition. Then we want to do it the second time somewhere else, we can pull out the repetitive bits and turn that into a library, refactor the original thing, because now we've got a library that only does exactly what we need. So you never end up with the 'YAGNI', you ain't going to need it, thing. You only end up building the stuff that... that you actually need.
1:20:28So this question here is, I think trying to say like, 'Hey, anything you automate, you're going to use, lose your skill at the thing that, like, where does it start? Where does it stop?' But it's an actually very interesting question, that people have studied for decades when they look at things like autopilot in airplanes, you know? And, anti-lock brakes in cars
1:20:54and... the question of what's the impact of using more automated things on your ability to do work is complex and nuanced. I, you know, used to run a company called Enlitic, which was providing AI tools to doctors and particularly to radiologists. It's a huge question. It was like, 'Okay,
1:21:19how do we build tools that allow radiologists to continue to become better at radiology, whilst also taking advantage of artificial intelligence?' And... I think in the end you kind of have to... study yourself a bit, you know? Like, I I type a lot still. If I only ever used voice, I probably would get a lot less good at typing. But if, if in the process I found using
1:21:47voice was just as good as typing, I wouldn't care. And this did happen to me some years ago when I got really bad RSI and I couldn't type for six months. And only using voice was horrible. Because a lot of things just are much better done with typing than voice, particularly with code. And it's going to keep changing, you know, so like, where is the point where... automating something
1:22:14is just as good, so much that you never need to do it yourself? Is the point definitely at which, well, there's no point being better at doing it yourself unless you enjoy it? So like, I spent a lot of time learning to get better at chess. Which is like kind of pointless because I, computer is better at me at chess, and I just did that because I wanted to. At the moment,
1:22:37I find... AI coders are really bad at software engineering in particular. They're not bad at some kinds of coding, but they're really bad at software engineering. It's very hard for them to learn software engineering because it's not in the training data. The training data doesn't have many examples at all showing the process of building something. Or the process of debugging. So it
1:23:03could be that given the lack of training data, maybe AI is never going to get particularly good at software engineering. You know, maybe it'll get good to a certain point. But it's always...
1:23:13I'm always thinking like, okay, what's the part of my craft that's valuable to get good at? And so at the moment, I find, you know, being good at software engineering has a really high amount of value. And so I study it, and I practice it. Um... but, you know, things keep things keep changing.
1:23:38All right. So I think we're, we're getting close to done. We've nearly got to the bottom. Um, which may sound surprising 'cause we haven't actually done much, but there's actually not much to agents. Uh, hopefully, as you're kind of seeing here. Um... so we basically need to kind of try to reconstruct this history. So... we've got in our response that we got back, there's this `r`. Oh,
1:24:08again, I'm in a note. It's got a code. Um... its content is a list and the second thing in the list is a `tool_use` block. And so, a very often, like I'm just often going back and...
1:24:24you know, editing slowly my previous pieces to kind of build up what I need. So you can see in my `tool_use` block, okay, I've got the input, I got okay, this is all useful stuff. So I'll call this my `tool_use` block. Look, I'm not enterprise coding here. It's fine. I'm only going to be using this for the next five minutes. So `tub`, I am not going to forget that `tub` means `tool_use` block.
1:24:46So it's fine to use small things for short amounts of time. So in terms of what I need from that, uh... basically what I need to do is to construct this response. So I'm going to need the result. So that means I'm going to need to call this thing. So I need the function. And so I only have the string. And this is where again, if... you can learn a lot about Python by going deep like this.
1:25:13And so I've done this for quite a few years now. So I know that actually in Python, if you type `multiplier`, it's actually just syntax sugar for typing `globals`, square bracket, string, `multiplier`. There's very, very little to Python in its heart. It's kind of like a bit of a lisp, really. So this is how I can, and I shouldn't write `multiplier` by hand, obviously. I should
1:25:39write `tool_use_block.name`. There's my function, right? And so then I need to call that with my, arguments. So as we briefly mentioned earlier, we learned that you can do that by using star star, given a dictionary. And we have a dictionary, it's called `input`. So it's going to be `tool_use_block.`, and I suspect it's going to figure this out for me. And it knows it's
1:26:06`.input` because one of the nice things about using, ghost text AI in this environment is it can see all of my previous messages. So it knows exactly what I've got. So that makes life nice and easy. So I've called my function, I've got my answer. That's great. And so this next bit,
1:26:25we know we want it to look like this. Yes. This is our content here. Great. So let's create our, `tool_result_content`. And so the `tool_use_id` is `tool_use_block.`, and you can see it right here, `id`. And the content, okay, that's a string, right? And so actually in practice, this is JSON.
1:26:59So we'll use `json.dumps` to grab our result. Okay. No point putting this on multiple lines when one line will do. So Control J to join. And we could just double check, of course, that looks okay, so we don't end up with bugs. It's from there. Okay. So you can see I've hardly had any bugs today, right? And that's because there's just such small steps. No problem. So now I,
1:27:29we need to basically put all that together with the appropriate user and assistant. So there's a nice tiny little function that we might just take advantage of from Claudette, called `make_messages`, and that's just going to go through and, add the, user and whatever stuff to it. Um... so `make_messages`, I'm going to pass in, and I'm going to have exactly the
1:27:57same messages that we saw. They're going to be the prompt, first of all. And then we need the responses content, right? So that's what... Is my trackpad doesn't work. So you can see we've got the assistant response content here. So we'll have that. Um... and then finally,
1:28:21we need our tool result contents. Okay. So we'll call that our messages. That's like our history, right? We're kind of generating it by hand. Let's see if that looks the same. Role, user, content, role, assistant, role, user. That looks the same to me. So assuming that it is, we can then say,
1:28:50okay, call the client passing in this created history. And get a new response. Okay. So you can see it's, it's... getting there. And so we, we generated the same thing as the history.
1:29:13We got the response. And here, believe it or not, is where I'm going to leave it, because that's your homework, right? I've, I've gone from the start, I've gone down, down, down, down. And there's like half a dozen more messages now to create the loop where you've then, I want
1:29:34you to have a function called `do_loop`. It should not call Claudette's toolloop. It should only call `c`, our client. You should be able to pass it a prompt and a list of tools and it should do it all for you. And to do that, I recommend you start by importing this dialogue. Okay. So if I hit this
1:30:03publish button here, share this with your friends, you're all my friends, so you may use it. Okay, there's the dialogue. So if you now click here and head over to here... hit 'upload file'...
1:30:28and paste it in. You will have a new dialogue. Submit. I think actually... and it's going to give it the same name. So it didn't work for me because I've already got it. If you haven't done language model programming before, this homework's going to be hard. Right? Um... if you have,
1:30:53it'll be trivially easy, but do it anyway. Um... but do it because it's going to test whether you, you get it, you know? And if you can do as small steps as possible, right? And if you're confused, like import my dialogue. And if you're confused like, 'why did Jeremy do that? How did this happen?' Like, just ask, right? Hit 'A' to add a new message above, or 'B' below, and add a
1:31:18prompt. And make sure you're in learning mode. And if you don't get its answer, just say like, 'I didn't understand that.' So if you hit, option enter or alt enter to submit, it'll create a new message underneath so you can immediately put underneath it. I didn't understand that. Can you take me through an example? You know, like, go at your pace so that... you know, you don't,
1:31:45you don't kind of fall off this thinking like, 'Oh, I kind of vaguely understand the idea, I think,' right? Do the homework. You know, study the dialogue. And, if if Solveit doesn't help you well enough, ask it first. Um... Johno, what else do we need to mention?
1:32:06I guess, yeah, also be sharing if you use this to do fun things. If you create some silly agent that does something entertaining, we'd love to see it. Despite our anti-agent rhetoric, we just like people seeing.
1:32:21Use it for the right thing.
1:32:22Use it for the right things, or for very amusing and silly things. Yeah, then also maybe Jeremy, so for next week, besides finishing off this demo and saying, 'Okay, how do I wrap this up into a loop and and make that do loop function?' Other things for people should be looking at, are we going to be looking at Advent of Code again or, you know, what are some other pieces of prep that people should do?
1:32:44Yeah, so the the purpose of this course is that it's kind of twofold. We're going to keep trying to show you cool things, like, 'what's an agent and how do I use it?' Or, 'teach me regular expressions,' or, 'how do I use NumPy?', 'what's broadcasting?', 'how do I, administer a Linux server?' These are all examples of things we'll do in in following lessons. But for each one, the
1:33:12purpose is really going to be about like, okay, look how dramatically easier it is to do these things and to learn these things if you do them in small steps and you can have taken advantage of this approach. So I would say practice, Advent of Code is a great place to practice, because you can go down to find a problem that's at your level, you know, or a bit beyond you, and
1:33:37see if you can use this approach to do something you weren't sure that you could do before. Or to use something that's not too beyond you to learn like how to do it faster or more concisely, learn something better about the programming language or algorithms and data structures.
1:33:54Yeah, but you know, or practice the stuff from lesson one, you know, we've got quite a few folks, quite a few students have written some really great articles and shared it with us. So, yeah, it's, it's really all about, practicing. Do you have any other suggestions, Johno?
1:34:12I guess just the the final prompt, so while you're working on this, so again, the thing that Jeremy said, the main homework finishing up this notebook. The good piece of self-reflection to assess, and I think these were questions that Jeremy had people asking last time, is... when you get to the end of it and you're done, did you write most of the code and, or at least understand every small piece before you did it? Are you learning things and having fun?
1:34:42Um... are you then reflecting on like, is there a nicer way I could have done that? So for example, you write your do loop function and it ends up maybe looking a little bit tangled or complicated.
1:34:53Are there ways that you can break out small pieces and redo it in a more elegant way? Um... okay, so yeah, reflecting, is there a better way I could have done it? Have I done it in small pieces and understood each piece? Thinking back on how you felt during the process and what worked and what didn't. Are there any other final pieces of reflection in that list or?
1:35:14Yeah, well I mean, so in the next lesson also we're going to dig deeper into the one of the inspirations for this, George Polya's classic math book, 'How to Solve It with Code,' sorry, not with code, just 'How to Solve It.' There was no code in 1945 when he wrote that. Um...
1:35:31and we'll talk about, you know, maybe encourage people who might even want to try buying the book.
1:35:37Which is a whole book, it's all math problems rather than coding problems, but using very similar techniques of like how to solve problems. Everything we do in the world, as we discussed, you know, last last week, or a very large amount of it, can basically be described as 'how to solve a problem'. So that's that's we'll we'll, you know, we'll we're going to keep digging into this every lesson until we're done and beyond.
1:36:06But I hope people enjoyed learning how agents worked and, feel like they, are a little closer to understanding this mystery than they were when they started. And, thank you very much, Johno.
1:36:19Yeah, thanks everybody for sticking with us. We'll see you in the next lesson.