VS Code正式推出自定义指令功能,让开发者根据项目特定需求调教AI生成代码的质量和风格。
2025 年 3 月 26 日,Rob Conery(@robconery.com)、Burke Holland(@burkeholland)
本月早些时候,我们宣布 Visual Studio Code 中的自定义指令(custom instructions)正式全面可用。通过自定义指令,你可以向 Copilot 提供有关团队工作流、特定风格偏好、模型可能不了解的库等具体上下文。
本文将深入介绍什么是自定义指令、如何立即使用它们显著改善 GitHub Copilot 的输出,以及一个今天就能尝试的全新预览功能——“prompt 文件”(prompt files)。
GitHub Copilot 可以大幅提升你的开发效率,但要学会如何有效编写 prompt,仍然需要一个过程。为了得到想要的正确答案,你可能得尝试好几次,通常还需要换一种方式表述问题或 prompt。即便如此,Copilot 依然能够加快你的工作流程!
但你是否知道,可以对 GitHub Copilot 进行“调校”,让它更贴合你的需求?只需要一个简单的 Markdown 文件,你就能向 Copilot 提供具体指令,帮助它更好地理解你的项目。本文将带你了解如何创建和使用自定义指令——这是 VS Code 最近发布的一项新功能。
来试一下:在项目中创建一个名为 .github/copilot-instructions.md 的文件。这个文件用于存放帮助 Copilot 更好地理解项目的指令。Copilot 会自动读取它,因此不需要进行任何额外操作。
将下面这些指令添加到文件中:
# Copilot Instructions
This project is a web application that allows users to create and manage tasks. The application is built using React and Node.js, and it uses MongoDB as the database.
## Coding Standards
- Use camelCase for variable and function names.
- Use PascalCase for component names.
- Use single quotes for strings.
- Use 2 spaces for indentation.
- Use arrow functions for callbacks.
- Use async/await for asynchronous code.
- Use const for constants and let for variables that will be reassigned.
- Use destructuring for objects and arrays.
- Use template literals for strings that contain variables.
- Use the latest JavaScript features (ES6+) where possible.
标题并非必需,但它有助于明确这个文件的用途。其余内容是一组帮助 Copilot 更好地理解项目的指令。你可以根据项目需求对这些指令进行定制。
现在打开 Chat 视图,输入一个极其简短的命令,例如 tail recursion。你不必写“如何在 JavaScript 中实现尾递归”之类的完整问题,只需输入 tail recursion,然后按 Enter。你会发现,Copilot 能够理解项目上下文,并给出相关答案。
请注意,即使你使用的 prompt 短得多,Copilot 仍然给出了更加详细的回答,而且格式符合你的预期,例如使用两个空格缩进、camelCase 等。
这就是自定义指令的威力!不过,它能做的远不止这些……
GitHub Copilot 为 VS Code 增添了许多实用功能,包括生成 commit message、审查选中的代码,甚至生成测试!
这些功能都很有用,但你很可能有自己的一套做法。好消息是,只需覆盖 workspace 中的几个设置,就可以对它们进行个性化配置。
来试一下:打开 Command Palette(⇧⌘P;Windows、Linux 上为 Ctrl+Shift+P),输入短语 workspace settings。你应该会看到一个名为 Preferences: Open Workspace Settings (JSON) 的菜单项。选中它,然后将以下设置添加到 workspace 中:
{
"github.copilot.chat.commitMessageGeneration.instructions": [
{
"text": "Be extremely detailed with the file changes and the reason for the change. Use lots of emojis."
}
]
}
保存 settings.json 文件并关闭它。Activity Bar 中的 Source Control 图标现在应该会提示你存在已更改的文件。如果你正在一个没有 Git 仓库的测试目录中工作,可以直接通过 Source Control 视图创建仓库。只需选择 Initialize Repository 按钮,然后按照指引操作即可。
有趣的部分来了:打开 Source Control 视图,选择 commit message 输入框中的闪光图标。这会让 Copilot 为你生成 commit message。你会发现,它生成的 commit message 极其详细,而且使用了大量 emoji!
将 settings.json 文件中的这些指令注释掉,然后再次生成 commit message。此时你应该会看到一条简略得多、也没有使用任何 emoji 的 commit message。
编写优质且详细的 commit message 是一项需要时间才能掌握的技能。不过借助 Copilot,你可以节省大量时间,同时得到质量更好的 commit message。
你的编码规范可能分散在多个文件中,而且这些文件未必采用 Markdown 格式。也许 JavaScript、Python 和 Go 各有一套规范。你可能还有关于数据库使用方式的规范,包括采用哪些数据类型、命名约定、如何处理连接字符串等。
你不必把所有规范合并到一个文件中。可以继续让它们各自独立,同时仍然使用 Copilot 来协助处理所有这些规范!
来试试吧!打开 .vscode/settings.json 文件,也就是 workspace settings,然后添加以下设置:
{
"github.copilot.chat.codeGeneration.instructions": [
{
"file": "./docs/javascript-styles.md"
},
{
"file": "./docs/database-styles.md"
}
]
}
这段配置指示 Copilot 在 docs 目录中查找两个文件:javascript-styles.md 和 database-styles.md。你可以添加任意数量的文件,Copilot 会按照你指定的顺序读取它们。
你可以在这里添加任何想要的文件,包括当前 sprint 的规格说明。我们甚至见过有人将 Gherkin 规格作为自定义指令!
模型往往过于客气。它们经常道歉,而且当你指出某件事有误时,它们几乎从不会坚持自己的立场,也不会为先前作出的决定辩护。当你想把工作完成时,这可能会很烦人。你可以通过几条关于回复方式的指令,覆盖模型这种过度热心的语气。
- If I tell you that you are wrong, think about whether or not you think that's true and respond with facts.
- Avoid apologizing or making conciliatory statements.
- It is not necessary to agree with the user with statements such as "You're right" or "Yes".
- Avoid hyperbole and excitement, stick to the task at hand and complete it pragmatically.
始终使用肯定式表达向模型下达指令非常重要,不要使用否定式表达,因为模型需要知道应该做什么,而不是不应该做什么。与其说 don't do,不如说 avoid。
大胆尝试,享受其中的乐趣!有位程序员表示,他喜欢通过生成俳句形式的测试,让自己的 test suite 更有活力。这看起来有点极端,但如果你正在开发一个业余项目,为什么不试试呢?你可以添加这样的指令:
- Generate tests in haiku format.
- Use 5-7-5 syllable structure.
- Use nature themes and imagery.
GitHub Copilot 也能将代码文件理解为自定义指令!例如,如果你有一个 SQL 文件,可以把它添加到设置中,Copilot 就会利用它来帮助生成数据访问代码。
如果你偏爱某种特定的数据访问工具,可以在项目中添加一个示例,Copilot 在生成数据访问代码时就会参考它。
来试试吧!下面是我们的一位团队成员用来为 PostgreSQL 数据库生成 Sequelize model(Node.js ORM)的示例:
//Template for a Sequelize model
const { DataTypes, Model } = require('sequelize');
class User extends Model {
//static or factory methods
//instance methods
}
const init = function(sequelize){
User.init({
//schema goes here
}, {
hooks: {},
tableName: "users"
underscored: true,
sequelize
})
}
exports.createUser = function(sequelize){
init(sequelize);
return User;
}
在设置中引用这个文件,GitHub Copilot 就会结合这个模板和你的 SQL 文件,为项目生成 model。无论在 Edit 模式还是 Ask 模式下,都可以这样做。只需输入 prompt generate the data access for the project,砰!魔法就发生了!
prompt 文件允许你为自己和团队编写可复用的 prompt。它既有助于确保一致性,也能减少需要反复编写的 prompt——这件事做多了可能会相当乏味。
例如,假设你想创建一个可复用的 prompt,根据数据库 schema 创建 interface。你可以创建一个包含数据库 schema 的 prompt 文件。Copilot 也非常乐意帮你生成用于完成这项工作的 SQL 脚本。
prompt 文件存放在 .github/prompts 目录中,它们就是普通的 Markdown 文件,文件名采用 *.prompt.md 格式。
例如,你可以创建一个 .github/prompts/database_users.prompt.md 文件,其中包含数据库说明:
# User Authentication
Our application will have `users` with the following information:
- The standard user information, including: `name`, `email` (unique).
- A link for a "magic" login, which will be a `GUID` as well as the expiration date for the link.
- Social login account IDs for Microsoft, GitHub, and Google.
- The timestamp of the last login.
- The timestamp for when the user was created.
接下来,你可以创建另一个名为 generate-interface.prompt.md 的 prompt 文件,其中包含生成 interface 的指令。prompt 文件之间可以互相引用,因此只需使用一个简单的 Markdown 链接,就能将数据库 schema 传入这个 prompt 文件。
Generate a TypeScript interface for the table specified by the user. Refer to the [user schema](database_users.prompt.md).
要在 Chat 中使用这些 prompt 文件,可以使用附加按钮,也就是回形针图标,或者使用 .。从菜单中选择 Prompts,然后选择要使用的 prompt 文件。你会发现,当我使用 generate-interface.prompt.md 文件时,它会自动引入 database_users.prompt.md 文件。
VS Code 是一个可自定义的助手。经过恰当配置后,它可以成为团队工作流中不可或缺的一部分。借助自定义指令,控制权掌握在开发者手中。