– 代理会话日,2月19日

语境即一切:通过自定义指令获得更佳的 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 更好地理解您的项目的指令。您可以自定义这些指令以适应您的项目需求。

现在,打开 Chat 视图并使用一个极其简洁的命令提示它,例如 tail recursion。您不必询问“如何在 JavaScript 中实现尾递归”或类似问题。只需键入 tail recursion 并按 Enter。请注意,Copilot 了解您的项目语境并为您提供相关的答案。

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

请注意,Copilot 提供了更详细的答案,格式符合您的预期(2 个空格、驼峰命名法等),即使您使用了更小的提示

这就是使用自定义指令的力量!但还有更多您可以做的事情……

自定义所有内容!

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

这些都很有用,但您很可能拥有自己的方式来执行这些操作。好消息是,您可以自定义它们,方法是在您的工作区中覆盖一些设置。

尝试一下:打开命令面板 (⇧⌘P (Windows, Linux Ctrl+Shift+P)) 并键入短语“workspace settings”。您应该会看到一个菜单选项,显示 Preferences: Open 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 仓库的测试目录,可以通过源代码管理视图创建一个。只需选择 Initialize Repository 按钮并按照说明操作即可。

现在乐趣开始了:打开源代码管理视图并选择提交消息输入字段中的闪电图标。这将指示 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.

您可以添加任何您喜欢的文件,包括您当前 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.

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

尝试并享受乐趣!一位程序员报告说,他们喜欢通过以俳句的形式生成测试来活跃他们的测试套件。这似乎有点极端,但如果您正在处理一个业余项目,为什么不呢?您可以添加如下指令

- 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).

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

Animated gif showing how to use prompt files in chat.

结论

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

编码愉快!

© . This site is unofficial and not affiliated with Microsoft.