Adding Guidelines Files0:00
All right, next up, let's figure out how to add support for guidelines files, so things like agent.md or claud.md in your project root. When you interact with your agent, it automatically consumes that file. Let's see how. So let's test this out. In my project root, I'll add a new file. And yeah, once again, in the wild, I guarantee you've seen claud.md or agents. md. So for fun, Larry is our mascot at layercast, so why don't we just imagine this
md. So for fun, Larry is our mascot at layercast, so why don't we just imagine this agent is called Larry, so you create a Larry.md file in your project root. Okay, so this is where you specify your project guidelines. And generally, they should be fairly brief. Even bullet points would be fine. So stuff like this, coding requirements, and maybe favor, test over PHP unit. If there's a specific style you want, maybe for eloquent, all of your fields should be
If there's a specific style you want, maybe for eloquent, all of your fields should be guarded or unguarded by default. Maybe you don't like doc blocks, so never use PHP doc blocks ever, you know, stuff like that. Whatever the requirements are for your team and your projects, you can specify them here generally as briefly as you can. Remember, if you do full paragraphs here, you just build up and you blow out
Rethinking Instructions Design1:14
generally as briefly as you can. Remember, if you do full paragraphs here, you just build up and you blow out your token usage. Okay, so let's leave it just like this as a demo. Next, I'm going to return to our main agent file. And if I scroll down, these are the instructions at the moment. All right, so let's figure out, like, how do we allow for this? Because we have default instructions. But now the default instructions might be this as well as the guidelines, okay?
Because we have default instructions. But now the default instructions might be this as well as the guidelines, okay? But then if you create a subclass, as we've done here, you can override that method. So in this case, for our little chatbot agent, the instructions are a jerk and your sarcastic blah, blah, blah. Hmm, what can we do here? If we were to append right here and say, like, all right, this is our default, right?
If we were to append right here and say, like, all right, this is our default, right? So we could do something like this, return default. And then we could say, read, Larry dot nd, concatenate, and then return, yeah, we can definitely do that and we should. But also, as soon as you create a subclass and you override it, no longer are we reading Larry dot md. Okay, so with that in mind, I think we should tweak this a little bit.
Larry dot md. Okay, so with that in mind, I think we should tweak this a little bit. What if instead of instructions, why don't we use the name persona? Oh, what kind of persona are you going to take? So if we scroll up here, we will change this to persona, excuse me. Let's inline this once again, all right, so this is the default persona, but a subclass can override it. So now we've split that out and once again, I will update this as well. Cool.
So now we've split that out and once again, I will update this as well. Cool. So now chatbot has a persona and our agent has a default persona. Now we can, we can declare the instructions directly at the top level and maybe we even make that private or protected effectively. So the instructions will be the default persona, also the Larry dot nd guidelines. And at least for now, that's all we're going to require, but we're actually going to append
And at least for now, that's all we're going to require, but we're actually going to append to it in future episodes. Okay, let's go. Let's begin by reproducing what we had originally. So I could say, well, the default instructions are whatever we have for persona and then we'd return that at the bottom. Okay, so now we've reproduced, if I can type correctly, we've reproduced what we had in
Testing Default Instructions3:40
Okay, so now we've reproduced, if I can type correctly, we've reproduced what we had in the last episode. Next, to illustrate this, this time, why don't we write a test? So PHP artisan make test and we'll call it agent test. All right. And yeah, it prepares the agent instructions. And really, this is just a unit test, but that's okay. All right. So let's instantiate an agent.
All right. So let's instantiate an agent. And then if I were to call the instructions, yeah, of course, I would expect to get our default here. So expect agent instructions to be this. All right. Give it a run. Oh, of course it fails. Call the problem.
Oh, of course it fails. Call the problem. Okay. Fair enough. Okay. That's fine. Actually, it makes sense for it to be public, really. Okay. And of course, it passes. Great.
Loading Project Guidelines4:40
And of course, it passes. Great. Okay. Now we can incrementally append to it. So right here, also the larry.nd file, if it's available. So maybe we could say this, if file exists and let's look in the base path, we 're going to assume a Laravel project right now, but maybe we shouldn't do that, of course, in real life.
course, in real life. If we're building an agent that's not dependent upon a Laravel project, then we would structure this in a slightly different way. Anyways, we're going to look for a larry.nd file. And yet if that exists, well, why don't we go ahead and pull it in? Of course, we could do file get contents and we could inline this like that. And then we could say instructions, concatenate, and I don't know, maybe like two new lines.
And then we could say instructions, concatenate, and I don't know, maybe like two new lines. And then the contents of that file. So yeah, think about it. This is not far from what's actually happening with the agents you use. If you have a guidelines file, they're going to consume it and include it with your prompt. So if I were to run the test again, it's probably going to fail because now l arry.nd exists. Yep.
arry.nd exists. Yep. Let's see the difference. Yep. So originally we expected just this, but now we have this as well as the contents of that file. And that would be included, again, with our prompt as the instructions. So what I might even do is say right up here, new line and then add heading like project
So what I might even do is say right up here, new line and then add heading like project guidelines and then two more new lines. Yeah. Now one more time, if we run it, all right, you're helpful AI assistant and here are your project guidelines and then we would append those there. Okay. So now with that in mind, when we do this, we're kind of mixing an actual project with
Testing Optional Guidelines6:31
So now with that in mind, when we do this, we're kind of mixing an actual project with our agent code base. But yeah, what if I were to delete this entirely, let's come back to our test. This is going to pass once again, let's write another one. It prepares the agent instructions or change it to it loads or it optionally loads larry.nd guidelines. All right. So let's do our setup.
All right. So let's do our setup. We're going to say given when then basically. So given we have an agent and a larry.nd file that has something in it, when I call the instructions method, then I expect to see the contents of those guidelines, right? And yeah, why don't we just do this? Why don't we do file put contents to larry.nd and then we'll say, you know, use past instead
Why don't we do file put contents to larry.nd and then we'll say, you know, use past instead of PHP unit, that's a fine stub. Okay. So now I could assert the full contents, which maybe I should, or I could take a shortcut and say, it needs to contain this at least then, and that's, that's fine. Okay. So let's see if that's right. And then when we're done, by the way, we will unlink that file.
So let's see if that's right. And then when we're done, by the way, we will unlink that file. All right, give it a run and yet that passes as well. All right. Next, why don't we extract this to a variable, call it guidelines or something. All right. So given we have an agent and we have a guidelines file that contains this will now when I grab the agent instructions, I expected to contain that section as well as the default persona.
Using Instructions in Prompts8:03
the agent instructions, I expected to contain that section as well as the default persona. Cool. So now if I switch back, let's switch to full screen, take a look at it. Actually incredibly simple. So prepare the default instructions for the agent that will grab the persona. If we do have a larry MD file and the project route, then fetch that otherwise you can ignore it and then return it. And then finally, when we run the model, or is it a model, we're going to have
it and then return it. And then finally, when we run the model, or is it a model, we're going to have to refactor this class a little bit. But when we run the model, we include those instructions and notice these are not included as part of the actual chat. So it's not part of the history, it's not part of the input. That would be bad because at the point where we reach the maximum number of messages and
That would be bad because at the point where we reach the maximum number of messages and if we compact, you would lose your initial guidelines and we don't want to do that in this scenario. So instead, we include that all as part of the agent instructions and then we provide the conversation itself. All right, let's move on to something else in the next episode.
All right, let's move on to something else in the next episode.
