在 VS Code 中试用

上下文是关键:使用自定义指令获得更好的 AI 结果

2025 年 3 月 26 日,作者:Rob Conery,@robconery.com;Burke Holland,@burkeholland

本月初,我们宣布了 Visual Studio Code 中自定义指令的正式发布。自定义指令是您向 Copilot 提供关于您团队工作流程、特定风格偏好、模型可能不了解的库等具体上下文的方式。

在本文中,我们将深入探讨自定义指令是什么,以及您今天如何使用它们来显著改善使用 GitHub Copilot 获得的结果,甚至还有一个名为“提示文件”的全新预览功能,您今天就可以试用。

使用简单的 Markdown 文件实现更小的提示词,更好的结果

使用 GitHub Copilot 可以极大地提升您的开发流程,但在有效提供提示方面存在一个学习曲线。可能需要尝试几次才能获得您想要的正确答案,这通常意味着需要重新措辞您的问题或提示。即使如此,Copilot 仍然会加速您的工作流程!

但是您知道吗,您可以“调整” GitHub Copilot 以更好地满足您的需求?通过使用一个简单的 Markdown 文件,您可以为 Copilot 提供特定的指令,帮助它更好地理解您的项目。在本文中,我们将引导您完成创建和使用 自定义指令 的过程,这是 VS Code 中的一项新发布功能。

试试这个:在您的项目中创建一个名为 .github/copilot-instructions.md 的文件。此文件将包含有助于 Copilot 更好地理解您的项目的指令。它会被 Copilot 自动读取,因此您无需执行任何特殊操作即可使其工作。

Screenshot showing the Explorer view in VS Code, highlighting the  file. The file is still empty.

将这些指令添加到文件中

# 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 更好地理解您的项目。您可以根据项目的需要自定义这些指令。

现在,打开“聊天”视图,并使用一个极其简洁的命令向其提供提示,例如 tail recursion。您不必问“如何在 JavaScript 中进行尾递归”或类似的问题。只需键入 tail recursion 并按 Enter。请注意,Copilot 理解您项目的上下文,并为您提供相关的答案。

Screenshot that shows the Copilot response for "tail recursion" in the Chat view.

请注意,即使您使用了小得多的提示词,Copilot 仍然为您提供了更详细的答案,其格式符合您的预期(2 个空格、camelCase 等)。

这就是使用自定义指令的力量!但您可以做的远不止于此...

自定义所有内容!

GitHub Copilot 为 VS Code 添加了许多强大的功能,包括生成提交消息、审查选定的代码,甚至生成测试!

这些都很有用,但您可能已经有自己的方式来做这些事情。好消息是您可以通过覆盖工作区中的一些设置来个性化它们。

试试这个:打开命令面板(⇧⌘P (Windows, Linux Ctrl+Shift+P)),然后输入短语“workspace settings”。您应该会看到一个菜单选项显示 首选项:打开工作区设置 (JSON)。选择该选项并将以下设置添加到您的工作区

{
  "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 文件并关闭。活动栏中的“源代码管理”图标现在应该指示您已更改文件。如果您在没有 Git 仓库的测试目录中工作,您可以通过“源代码管理”视图直接创建一个。只需选择初始化仓库按钮并按照说明操作。

有趣的地方来了:打开“源代码管理”视图,然后选择提交消息输入字段中的闪光图标。这指示 Copilot 为您生成提交消息。请注意,它生成的提交消息非常详细,并使用了大量的表情符号!

Screenshot that shows the Source Control view, showing a colorful commit message for the latest commit.

注释掉 settings.json 文件中的指令,然后再次生成提交消息。您应该会看到一条详细程度低得多的提交消息,并且不使用任何表情符号。

编写良好、详细的提交消息是一项需要时间学习的技能。但有了 Copilot,您可以节省大量时间,并获得更好的消息。

全面采用自定义指令

您的编码标准可能保存在独立的、格式不同于 Markdown 的文件中。您可能有一个用于 JavaScript 的文件,一个用于 Python 的文件,还有一个用于 Go 的文件。您可能还有关于如何使用数据库的标准 - 您使用的数据类型、命名约定、连接字符串处理等等。

您不必将所有这些合并到一个文件中。您可以将它们分开,仍然使用 Copilot 来帮助您处理所有这些!

让我们试试看!打开您的 .vscode/settings.json 文件(工作区设置),并添加以下设置

{
  "github.copilot.chat.codeGeneration.instructions": [
    {
      "file": "./docs/javascript-styles.md"
    },
    {
      "file": "./docs/database-styles.md"
    }
  ]
}

这指示 Copilot 在 docs 目录中查找两个文件:javascript-styles.mddatabase-styles.md。您可以添加任意数量的文件,Copilot 将按照您指定的顺序查找它们。

Screenshot that shows the Explorer view, highlighting the two files in the  folder and showing the custom instructions in the  file that reference these docs files.

您可以在此处添加任何您喜欢的文件,包括您当前冲刺阶段的规范。我们甚至见过有人将 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.

重要的是始终以肯定的方式而不是否定方式向模型提供指令,因为它们需要知道要做什么,而不是知道不要做什么。与其说“不要做”,不如说“避免”。

实验并享受乐趣!一位程序员报告说,他们喜欢通过俳句的方式生成测试来活跃他们的测试套件。这听起来有点极端,但如果您正在进行一个爱好项目,为什么不呢?您可以添加如下指令:

- Generate tests in haiku format.
- Use 5-7-5 syllable structure.
- Use nature themes and imagery.

还有一件事...

GitHub Copilot 也可以将代码文件理解为自定义指令!例如,如果您有一个 SQL 文件,您可以将其添加到您的设置中,Copilot 将使用它来帮助生成您的数据访问代码。

如果您有特别喜欢的数据库访问工具,您可以在项目中添加一个示例,Copilot 在生成数据库访问代码时将使用该示例。

试试看!这里有一个示例,我们团队的一位成员曾用它来为 PostgreSQL 数据库生成一个 Sequelize 模型(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 文件使用此模板为您生成项目中的模型。您可以在编辑模式或询问模式下执行此操作。只需使用提示词 generate the data access for the project,然后砰!魔法发生了!

介绍提示文件

提示文件允许您为您自己和您的团队编写可重用的提示词。这有助于强制执行一致性,同时减少提示词的输入,因为提示词输入可能会变得乏味。

例如,假设您想创建一个可重用的提示词,用于根据数据库模式创建接口。您可以创建一个包含数据库模式的提示文件。Copilot 非常乐意为您生成一个 SQL 脚本来完成此操作。

提示文件应放在 .github/prompts 目录中,并且是名称格式为 *.prompt.md 的 Markdown 文件。

您可能有一个 .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 的提示文件,其中包含生成接口的指令。提示文件可以相互引用,因此您可以将数据库模式作为简单的 Markdown 链接传递到此提示文件中。

Generate a TypeScript interface for the table specified by the user. Refer to the [user schema](database_users.prompt.md).

要在聊天中使用这些提示文件,请使用附件按钮(回形针图标)或使用 。从菜单中选择提示词,然后选择要使用的提示文件。请注意,当我使用 generate-interface.prompt.md 文件时,它会自动拉取 database_users.prompt.md 文件。

Animated gif showing how to use prompt files in chat.

结论

VS Code 是一个可定制的助手,如果配置得当,可以成为团队工作流程不可或缺的一部分。通过自定义指令,控制权掌握在开发者手中。

快乐编程!