2024-09-28 | Visual Studio Code | Using Custom Instructions with Copilot to enhance our prompts
VS Code Copilot自定义指令:让AI读懂项目,智能生成代码
标签
媒体详情
- 上传日期
- 2025-06-22 19:40
- 来源
- https://www.youtube.com/watch?v=cu9zZAFmoDg
- 处理状态
- 已完成
- 转录状态
- 已完成
- Latest LLM Model
- gemini-2.5-pro
转录
speaker 1: I was looking over the release notes for the August 2024 versus code release 1.93. And this word stood out to me right here. This word is like catnip for me. Experimental. This is neat. Custom Copilot instructions. What are these things? Well, if we come over here, what you can read is that you can have user settings, global settings for your installation, or workspace settings where you can hijack Copilot you and hijack the chat and prepend instructions. So here you can see two examples of text, but you can also prepend files. Now, this right away, this is experimental. As you can see by this word right here, this is experimental. But I want na show you what's possible. And if you have any ideas, it's all about the feedback right now. So leave some comments with your ideas. All right, let's see what we can do with this thing. I have a little sample project to open. And what I'm going to do now is I am going to add these experimental bits, and so I'm going to go to preferences and workspace and open the settings right here. And I'm going to flip back, I'm going to copy this, and I'm going to just drop it right in there. Good. Okay. Well, as you can see, I have created a code style md file, and I don't need these text plurbs right here. One thing I want to point out is this file can change as you move along. Maybe you're going to have specs in here. Maybe you'll have other files as you're about to see for right now. I'm just going to start with code style and my code styles, markdown document, as you can see, name of the project and the directory structure that we like to work with. All right, let's see if everything worked. And here I'm going to say what is the best structure for my application? And good. As you can see, it used a reference up here. It pulled in costyled out mv and it's going to do that for every single chat. And there we go. Config lib models, repository services. Very good. Tess. Okay, this looks pretty good. Here's what I'm gonna do. Why not let's do this. Generate a bash script. I like to work with bash scripts to generate code and do other things. The reason why is I can then review what is going on before I actually do it. So let's do that now. In fact, let's go back over here and I am gonna to do nit sh. I want to paste that in there and straight away I can see my underscore application, etc. I don't really want all that. So what I'm gonna to do, change all occurrences. Backed that out. Good. All right, so let's run this. I'm gonna hit shift command backslash and then I'm am going to say, source a knit sh directory structure created. Boom, I've got my project. So that's pretty neat. All of this, this structure right here, all of this was set up right here in our costyle md file. And so Copilot knew what's going on. It's pretty fun to PaaS in files, especially markdown files with specifications for style and other things. But what else can we send in? What if we sent in sql file? So here I have the chinook test database and it is just the table definition in here. I don't have any inserts because if I had the inserts it would be too big. So if you want na play along, make sure this file is just the table definition. So now that I've put that in here, let's go back to setting stop json. I'm gonna to hit shift command d to copy that file down. And then I'm gonna to say, okay, let's see what we get up to now. We'll go back over to chat and I'm gonna to say generate models using salize. See what Copilot does good? It's lled in, it's pulled in. There we go. As you can see, Copilot, if it has a reference to the sql file, can generate these models really well. In fact, I didn't spend enough time on this when I was recording this live, and I really should have, now that I'm editing and looking back at it, look at what Copilot created. Just from using a sql file as a reference, it was able to create the associations to other models, noability composite primary keys. Take a look at the track model and all of the many to many associations that are involved here. It's pretty mind blowing. In fact, let's change this. In fact, if I wanted to come in here, what if I said using drizzle? Drizzle is a popular one, and there we go. So depending on what data access tool you're using, Copilot can use this sql file and it can do all kinds of things if it knows about your database. I could add full text indexing here if I wanted to, and so on. All right, so here's what I'm gonna to do. Let's go back and say generate a bash script for the sel is models. All right. Wow, look at that. This is interesting. It is generating the models using the si cli. Okay. Wow. All right. I'm am going to copy this and I'm going to come back over here and double click and go to models donsh. I like having shell scripts. Let's collapse this so we can have a read. Let's see. Initialize that. We don't need to make move the models into lib don't need to do that all let's see if this works so I'm going to say source models that is each creating a model and a migration. Well that is a lot of work done for us. That's that's that's pretty neat. Okay, let's close up everything and I'm gonna to do one last deal here. Let's do some housekeeping because what I want na do now is I'm gonna to create a folder and I'm gonna to call it docs and I'm gonna to move code style in there and move that. And then also I want create a folder called scripts and let's move our init and our models in here. And the nice thing about this is, this is sort of tracking what we've been doing with Copilot, because if we ever go through a code review, it's a good idea to have the output here so we can review it. So it's not magical. All right, so let's go into docs and what I'm gonna to do now is I'm gonna to put cart md inside of here. I'm going to say the shopping cart. Okay, so now we have a specification here and we are building out our documentation and we can also now hand this off to Copilot. So let's go into settings and this needs to be docs code style and we are going to duplicate this line down. And now we're going to do cart md. This is pretty neat. So now what I can do is I can say generate generate the cart service. And so logic is going to go into a cart service as dictated in my style guide. And it's also creating a cart test for me. Oh my goodness, look at this. Okay, so it is great. It is using a uuid, which is what I expect, creating a cart, setting the customer, customer required. Very nice. Add the item. We have some checks up there. Now again, the idea here is I want some shopping cart code. This will get me off the ground. This isn't supposed to be final copilots, not here to write your application for you. It's just here to get you started. And then here we go, the cart service. And then we have tests for it. And it's good. It's good. Except I don't like the test to have more than one expectation redo the tests to have just one assertion. And there we go. That's much better. We just have now one assertion per test. I like this. Okay, let's copy this. And this is going to go into lib and then services and we're going to do cart dujai'm going to drop this in here. Let's check the references. I don't know if I have uuid installed, I might have to install that. And then we're gonna to go down to tests and drop that in there. And I know that lib cart services, there we are. That's where that's gonna to live. Okay, what do you think the chances are that this is gonna to work? Well, I know I need to install a few things. So let's do that really quickly. I'm going to do npm install. Npm install, just dash d pm euid. Cool. And then let's set this to npm rentis. Going to be just just, all right. Well, I'm pretty sure this isn't going to work, but I'm going to try it. Npm rentit is no doubt going to fail. Seven past. What is this? Your test suite must contain at least one test. Interesting. Oh, do I have more tests in here? I do. Oh, wow. Oh, I have all of these that were generated for me. You've got to be kidding me. There is no way that this is going to work straight away. I'm gonna to delete these all right, let's rerun everything. There's no way that's gonna to work. Oh my goodness. 1:7 failed. Oh looks like I have a config test Dutch I asked that is because well that's okay we're not using this anyway I'm going to delete this. Very good. Okay, let's run this. Oh my goodness. I have to tell you this this this is a complete surprise which is fine. Well, in the span of about 15 minutes, I have a pretty serviceable application. Of course, there's a lot more to do. There's a lot of refactor and refinement. But the idea here of being able to set these instructions using documents, copile is now aware of my project. And it's able to go in, generate models, as you've seen, generate service classes, know exactly where to put them based on my preferences. And if I start building out my documentation, telling Copilot about the documentation of my application, it can then keep ongoing and generating the starter code for me. All right, well, that's it for me. Thank you so much for watching. This has been fun, happy coding.
最新摘要 (详细摘要)
核心摘要
本演示介绍了 Visual Studio Code 1.93 版本中一项名为“自定义 Copilot 指令”的实验性功能。该功能允许用户通过工作区设置,让 Copilot 在处理每个请求时自动引用指定的项目文件(如 Markdown 规范、SQL 数据库定义等),从而赋予其项目上下文感知能力。
演示者通过一个15分钟的实战项目,展示了该功能的强大潜力:
1. 项目结构生成:利用一个定义了代码风格和目录结构的 Markdown 文件,指导 Copilot 成功生成了整个项目的脚手架脚本。
2. 数据模型生成:通过引用一个 SQL 数据库表结构文件,Copilot 能够为指定的 ORM(如 Sequelize 或 Drizzle)精确生成所有数据模型,并正确处理了模型间的复杂关联。
3. 业务与测试代码生成:基于一份购物车服务的功能规范文档,Copilot 自动生成了完整的服务层逻辑及其配套的单元测试代码,并能根据进一步指令优化测试用例。
尽管过程中需要进行少量手动清理,但最终生成的应用程序基础代码成功通过了全部7个测试用例。结论是,自定义指令极大地提升了 Copilot 的智能化水平,使其能遵循项目规范、理解上下文,自动化生成高度定制化的初始代码,从而显著加速开发进程。演示者强调,该功能目前仍处于实验阶段,鼓励用户积极提供反馈。
功能介绍:实验性自定义 Copilot 指令
演示者介绍了 VS Code 2024年8月发布的1.93版本中的“实验性自定义 Copilot 指令”功能。该功能允许用户通过全局或工作区设置,向 Copilot 的聊天会话预置指令或文件引用。这意味着 Copilot 在每次交互时都会首先参考这些预设的上下文,从而使其行为更符合特定项目的需求。该功能目前仍处于实验阶段。
第一步:通过规范生成项目结构
为展示该功能,演示者首先创建了一个 code-style.md 文件,其中定义了项目的推荐目录结构。随后,他在工作区的 settings.json 文件中配置 Copilot 引用此 Markdown 文件。当他向 Copilot 提问如何构建应用结构时,Copilot 准确地根据文件内容给出了建议,并生成了一个 Bash 脚本(init.sh)来一键创建所有必需的目录。
第二步:结合数据库定义生成数据模型
接下来,演示者将一个包含数据库表定义的 SQL 文件(chinook test database)添加到了 Copilot 的自定义指令中。基于此文件,他向 Copilot 发出指令,要求使用 Sequelize ORM 生成数据模型。Copilot 成功地分析了 SQL 结构,生成了精确的 JavaScript 模型代码,甚至正确识别了模型间的关联(associations)、可空性(nullability)、复合主键以及多对多关系。演示者还展示了只需更改提示词,Copilot 就能切换到另一个工具(如 Drizzle)生成相应代码。最后,他让 Copilot 创建了一个自动化脚本(models.sh),用于通过 Sequelize CLI 批量生成所有模型和迁移文件。
第三步:根据服务规范生成业务逻辑与测试
为了生成具体的业务代码,演示者创建了一份新的规范文件 cart.md,详细描述了购物车服务的功能需求,并将其与之前的风格指南一同添加至 Copilot 的指令中。当被要求“生成购物车服务”时,Copilot 同时生成了:
1. 服务逻辑:一个遵循规范的 cart.js 文件,包含添加商品、设置客户等功能,并内置了必要的逻辑校验。
2. 测试代码:一套完整的单元测试用例。
演示者还展示了与 Copilot 的互动优化能力:在指出初始测试用例包含多个断言后,他要求 Copilot 将其重构为“每个测试仅包含一个断言”,Copilot 随即生成了符合要求的优化版本。
最终验证:运行测试与成果
在将生成的代码文件放置到正确的项目目录下、安装了 uuid 等依赖并配置好 Jest 测试环境后,演示者运行了测试。经过对 Copilot 自动生成的额外测试文件进行简单清理后,最终测试套件成功运行,结果显示 7个测试通过 (7 passed)。演示者对 Copilot 生成的代码能够“开箱即用”表示非常惊喜。
核心结论
自定义 Copilot 指令通过赋予 AI “项目感知能力”,使其能够深刻理解项目特有的规范和结构。这种“文档驱动开发”的模式,让开发者可以通过维护规范文档来指导 Copilot 生成高度一致且符合要求的初始代码。在短短15分钟内,一个功能可用的应用基础框架得以构建,证明了该功能在提升开发效率方面的巨大潜力。不过,此功能尚处于实验阶段,其未来发展将依赖于用户的反馈和贡献。