📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

How To Get Better AI Reponses from GitHub Copilot in Seconds!

James Montemagno7:15

Transcription

One of the things that folks struggle with when they get started using AI tools is getting the AI to respond in coding sort of standards and practices like your coding. You want the AI to code like you and give you formatted code that follows your standards.

Now, when you provide it context, so when you're giving it code files, it will automatically take a look at that and try to sort of respond back with things that are similar. So if you use Pascal or camel casing or underscores, it'll try to do that. But we can guide GitHub Copilot and AI tools by giving it more context. And to do that with GitHub Copilot and VS or VS Code or wherever else you're using GitHub Copilot, you can use custom instructions. It defines guidelines or rules for generating code, performing code reviews, or even generating commit messages. So you can give it coding practices, prefer technologies, project requirements. And think of it as like the same exact rules that you would tell another team member about how we like to structure our code. You need to give copilot this information that is sent with every chat request so it knows how to respond back. So that way it doesn't need to scan your codebase every single time.

So what would that look like? Well, custom instructions can be added into a GitHub folder and that's going to basically describe the code generation instructions and those are sent with every request. You can also break them down into smaller files like instruction files for things like just apply these to C# or apply this just to Razer for example.

So what would that look like? Well, here inside the VS Code docs, I love it because it breaks it down. Like here's some general sort of coding standards like use Pascal for these versus use camel for this, use prefix for this, use all caps for this. Here's how I like to do error handling for TypeScript or React. Here's my TypeScript guidelines and my React guidelines. And then each of these instructions will be sent based on the code that it is analyzing and modifying.

Now there are tons of great GitHub repos out there that you can go and find instructions in. But there's a great resource on awesome copilot on GitHub that will also give you custom instructions, reusable prompts and chat mode. For example, if you're getting started with, I don't know, Blazer, you could tap on that. And here for example is a markdown file that you could add or get started with at least and customize to your liking when working with Blazer. So different naming conventions, Blazer and Net guidelines, error handling, and different API and performance optimizations when using Blazer Server or Web Assembly. Now, of course, you know about your project. So if it's web assembly, you give it web assembly things. If it's Blazer Server, you could give it Blazer Server things, for example, which are cool.

But it's actually even easier than ever to generate and get started or even update your existing copilot instructions in VS Code. Here I am inside of the latest Visual Studio Code insiders build 102 and we can see that this is my tiny shop application. So I have the Blazer front end and ASP.NET Core backend and I don't have any files inside this GitHub folder. So, I could go and I could add a new file, copilot instructions, or I can go into chat and I can see this little gear here to customize chat. This is where it's going to enable me to add prompt files, instructions, tool sets, custom chat modes, but also autoupdate instructions. So when I tap on that, it's going to send a prompt off to the agent and it's going to ask it to analyze the workspace or create or even update the existing one and do it based on different requirements for core commands for building. Give it highle architecture including major packages and services, repo specific styles like formatting, typing, naming, error handling. If you have existing rules, it will take a look at those. And then it will also then generate this existing co-pilot instructions over here. It's going to also patch or merge it if it already exists, which is really nice. So, it's going to use markdown heading bullets and then generate for you.

So, what it just did really, really quickly here in this project is it went off and it scanned the entire codebase, looked at all the different f looks at all the different code inside of it, and it generated a new co-pilot instructions here. So, let's take a look. So what it's done is it said here's some core commands that you can use on a build run. Uh here's some Azure developer CLI things. There's no test projects found. So I could add that in there. Here's the back end. So it has the products which is minimal APIs uses inmemory or SQLite database. Has a API here random failures for error error demos which is great. Uh so that's nice to know. Here's the Blazer front end data models and then different coding standards here. This is super nice, right? So here's my here's my C sort of guidelines. Here's my JSON property names for DTO's Blazer in general, right? So it even tells it, you know, use it concise code, it's demo focus, there's no health checks, telemetry, advanced resiliency, things like that. Uh, which is nice. And then agent mode rules. So it tells it stuff not to do, for example, and things to prefer. So this is super duper nice.

So if I come back over into agent mode over here, let me open up the chat. I'm going to create a new thread and let's go ahead and keep and continue. I'm going to say all right, let's add a new uh API endpoint uh for users into the back end. And the first thing that we'll see is inside of the chat is that it is using a reference of the copilot instructions here. So, we can see that it is adding this into it. So, it's going to ask me and clarify here what should it create and I'm going to say yes. Yes, let's do all CRUD. And those all look good. And let's do in memory. So, it's actually looked and it's taken a look at those copilot instructions and ask me questions about how I want to implement it, which is nice. And before I might just have guessed of how I wanted it to, but now because I've given it information with the copilot instructions, it's now going to return back code that is more in line with my style that's here. So here is my DTO, my user with the JSON property names, which I like. So I'm going to update my map user endpoints. And hopefully what we'll see over here is that we have our user endpoints just like this. And what's great is that it now knows how to exactly run my terminal commands. And I can give it more specific information on it as well. So now it's going to go ahead and build this up just like that.

Now, as I start to work in my project, I can always come back, click on this, and autoupdate instructions again. and then we'll update it and give it more context as my project grows. Anyways, that is how easy it is to get started adding those copilot instructions into your project. So, here it is just right here. You can add one or you can go ahead and add multiple that break it down into smaller chunks, but it is the very first thing that you should do. And big shout out to the VS Code team for adding this one-click button to make it super simple to add some beautiful co-pilot instructions that you can then massage exactly to your liking. So go off, add some custom instructions today. [Music]