در حال بارگذاری ...

Prompting Essentials Overview0:08

All right, next up is prompting essentials. That's what we're gonna call this video. So here's the deal. Uh, you can browse the web and find dozens and dozens and dozens and dozens of best practices, so to speak. Uh, but from my experiences, this whole thing is evolving as the models mature. And what was the best practice a year ago? You don't necessarily need to do anymore. So I'm not sure how helpful it is if I tell you 48 best practices, but I will tell you three core patterns

Technique 1: Be Explicit0:29

So I'm not sure how helpful it is if I tell you 48 best practices, but I will tell you three core patterns that you should get in the habit of. The first technique is to be explicit. And this is precisely why voice to text tooling can be so incredibly helpful when interacting with ai. It encourages you to be a little more verbose than you otherwise might. And this is a good thing. So, for example, if I were not a programmer

And this is a good thing. So, for example, if I were not a programmer and I was working on this Laravel workshop, uh, application, remember it's basically, uh, Twitter or X clone. Uh, maybe if I wanted to add a bookmarking feature, but I wasn't a software developer, well, maybe I would go to AI and I would say, and bookmarking. And you know what? The AI is decent, right? The agent will review the context and it'll figure things out.

The agent will review the context and it'll figure things out. It'll look at the application information as you see here, and it won't do the worst job in the world. But again, you are a programmer, right? Maybe the way you interact with AI should be just a little bit more mature than the way, for example, my daughter or my young son might interact with it. Okay? So with that in mind, why don't we abort this entirely

or my young son might interact with it. Okay? So with that in mind, why don't we abort this entirely and instead rewrite it? Now, here's a little tip. You may find that it's actually a little difficult to be explicit because you haven't yet fully thought through the feature, and that's okay. No judgment. We've all been there. But in these situations, maybe take a moment and a and I can still help us out.

Brainstorming Prompts with AI1:58

But in these situations, maybe take a moment and a and I can still help us out. And here's how I'm gonna switch to the ask section. We're not writing code yet. We're brainstorming with the ai, and let's do it. Let, let's ask it to help us prepare a prompt. I'm thinking about adding a bookmarking feature to this application, and I would like some help preparing a detailed prompt. So here's what I know so far.

a detailed prompt. So here's what I know so far. I know that I want a bookmarks link in the main sidebar. Next, every single post should offer a button that one clicked, bookmarks that post for the current user. Next, there should be a dedicated page to view all of my bookmarks, and that's what that bookmark link in the sidebar should direct me to. And then finally, I think we should make bookmarks private. I can view my own bookmarks,

And then finally, I think we should make bookmarks private. I can view my own bookmarks, but I shouldn't have access to any other user's bookmarks. Can you help me work on the prompt for this? Alright, so there we go. We're gonna give that a run. And here's what I want you to notice. At this point, I'm not thinking what are the, what are the best practices for prompting I need to include here? No, I'm, I'm just talking to it the way I would talk

for prompting I need to include here? No, I'm, I'm just talking to it the way I would talk to a real human. It's going to evaluate this, help me, it might suggest some things. And then at that point, once I have figured this out a bit more, then I can get started on the feature. Alright? So it's going to take a look at our application details. Fine, it wants to take a look at our database schema.

to take a look at our application details. Fine, it wants to take a look at our database schema. Alright? And already that's getting to work. So while this wraps up, I want you to notice how this works. And the way an agent behaves, it's not like it does everything in one go. Instead it's kind of recursive. It will take what you're asking and try to solve the first question. And then once it has solved that question,

and try to solve the first question. And then once it has solved that question, it moves on to the next thing. What's the next thing I should do? Okay, that's done. Now what's the next thing I should do? And it keeps calling itself recursively, uh, which is kind of cool and it's a good way to think of it. Anyways, that seems to be done. So let's go full screen and see what it came up with. Alright, so let's take a look.

So let's go full screen and see what it came up with. Alright, so let's take a look. We want to implement a bookmarking feature. Here are the requirements. And notice the requirements are quite extensive. It's a full document. And this is great at this point. I could go over all of it, I can tweak it. Uh, I can decide if I like the suggested API that they have here. What do I think about the routing, your MRIs?

API that they have here. What do I think about the routing, your MRIs? Uh, should there be certain relationships? Uh, this is really excellent, I think. Okay, so now of course I'm not gonna do this as part of a video, but I could copy this whole thing, tweak it, and then use that as part of my prompt for the actual feature. Next step is technique number two. Mm-hmm. Think small and show examples.

Technique 2: Think Small4:38

Next step is technique number two. Mm-hmm. Think small and show examples. Alright, so in the first technique, we were rather broad, weren't we? We gave it the full feature request and we worked on that prompt to make it as targeted and explicit as possible. However, what you might prefer to do is to break that feature into tiny little miniature features that AI helps you with.

that feature into tiny little miniature features that AI helps you with. That way you remain in control and, and you remain the architect every step of the way. So in this case, why don't we, let's do this, let's get started. I wanna begin working on a new bookmarking feature. Let's create a new Git branch for this. And to start, I need a migration for a bookmarks table. I need a bookmark eloquent model.

And to start, I need a migration for a bookmarks table. I need a bookmark eloquent model. And let's see, the bookmarks table should consist of a user id, a post ID and timestamps. Yeah. So from this point of view, I'm almost treating AI like my, my workhorse, my assistant here. So it creates a new branch. It's gonna create the model for me. All right? And while that's doing its thing,

It's gonna create the model for me. All right? And while that's doing its thing, it looks like it updated the migration. So let's just take a look at that right now. Create bookmarks table and notice it has the user ID and the post ID and the timestamps. Great. Now it looks like it's trying to scan our models. Perhaps it's gonna add some relationships for us. Yeah, keep that in mind. It's not always gonna stick precisely to what you ask.

Yeah, keep that in mind. It's not always gonna stick precisely to what you ask. Uh, there are some prompting techniques to help with that, but even still, sometimes it's gonna do its own thing. All right? And that's done. So we can see everything that's been done here. Created a handful of files. And by the way, if we ever wanna undo everything, we can click this rollback button here, which is actually incredibly helpful if you want to,

Technique 3: Verify with Tests6:19

we can click this rollback button here, which is actually incredibly helpful if you want to, if you wanna take a a second stab at it. Okay, so now at this point I have a bookmark model. I of course have a create bookmarks migration. And then finally we have a bookmark factory that contains all of the, uh, the relevant attributes here. So with that, let's move on to technique number three. My final technique for you is to offer the H agent away to verify completedness.

My final technique for you is to offer the H agent away to verify completedness. And often a good way to get started is through a test, of course. So let me show you, let's create a new test using pest and we'll call it bookmark, uh, bookmarking test. All right? So I can open that up now. All right. So this will mostly be a model test. A post can be bookmarked by a user. All right? And now, yeah, we're just gonna write the API

A post can be bookmarked by a user. All right? And now, yeah, we're just gonna write the API that we would love to have, right? So I could say something like, uh, given I have a post and a a user, if I call user bookmark post, then that should be reflected in the db, right? Something along those lines. So I'm gonna interact with this code the way I want, but it's not actually gonna work yet

So I'm gonna interact with this code the way I want, but it's not actually gonna work yet because we haven't written that logic. That's where the AI will take over and implement, uh, my requirements. All right? So let's say we have a post, we'll create a factory for one. Next, let's create a user. All right? Next, if I call user bookmark, so notice in this case, I'm just gonna interact with

All right? Next, if I call user bookmark, so notice in this case, I'm just gonna interact with that method and that API, even though it doesn't exist, great user bookmark post. And then what is my verification? And this is really important. This will determine whether or not the feature has been implemented, uh, the way I would, uh, expect. And now, yeah, of course we could do a database check.

the way I would, uh, expect. And now, yeah, of course we could do a database check. So I could say, well, I assert that the database has within the bookmarks table a record like this, right? So yeah, this is kind of the, um, I dunno the manual way we could do it, but it doesn't really verify the API itself. But yeah, we could keep that if we want, if it would help, uh, the agent.

But yeah, we could keep that if we want, if it would help, uh, the agent. I could also do something like, well, let's grab the users bookmarks and I'm going to expect to for that to have a count of one, right? These are the things we can do. So again, none of this works, but it will work in just a minute. So let's open up our agent, create a new chat.

but it will work in just a minute. So let's open up our agent, create a new chat. I'm going to give it some additional context, which is the current file. And here we go. So I'm still working on this bookmarking feature. Have a look at bookmarking test where I have created a test to describe the API that I would like. Your job is to implement it and you will know that you are successful when

Your job is to implement it and you will know that you are successful when the test returns green. Just keep in mind that you should never hard code values just for the sake of making the test pass. It should always work in a general way in addition to the test returning green. Alright? So once again, maybe, maybe a little jumbled in my speech there. Uh, I could clean it up, but I don't really have to.

maybe a little jumbled in my speech there. Uh, I could clean it up, but I don't really have to. This should be clear enough. So let's go. All right, that's done. So let's take a look. If I expand this, it updated it looks like three or four files and it implemented the test. So let's have a look. Let's give it a run. And it returns green, kind of amazing, right? So if we have a look at the relationship, sure enough it new to add this and then it creates one sends through the post.

So if we have a look at the relationship, sure enough it new to add this and then it creates one sends through the post. On the post. It adds the fillable fields if you like. And by the way, if you don't like that, then of course your guideline can be explicit that we are unguarding our attributes. But yeah, here's the key thing to be aware of with this approach. I think of it sort of like developer driven ai. We remain in control the entire process.

Developer-Driven AI Approach10:12

I think of it sort of like developer driven ai. We remain in control the entire process. It's almost the polar opposite to vibe coating, right? And it doesn't mean vibe coating's bad, not by any stretch. It's just a different way of doing things With vibe coating, you're hitting approve, approve, approve. Yep, it's good. I don't care what the code says. Yep, yep, yep. Keep going, right? With this approach, now we remain in control. We want to know what the API is,

With this approach, now we remain in control. We want to know what the API is, we wanna define the interface. And then the AI is simply our assistant who will implement it for us. Lemme know what you think.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟