Maurice Wu
Published on

自定义 VSCode copilot 指令

有 4 种形式的指令可以自定义上下文信息:

  • Code-generation instructions
  • Test-generation instructions
  • Code review instructions
  • Commit message generation instructions

全局自定义

可以通过 VSCode settings 设置:

{
  "github.copilot.chat.codeGeneration.instructions": [
    {
      "text": "Always add a comment: 'Generated by Copilot'."
    },
    {
      "text": "In TypeScript always use underscore for private field names."
    },
    {
      "file": "code-style.md" // import instructions from file `code-style.md`
    }
  ]
}

Copilot 会在之后的每一次 generation 操作中附加这些额外的上下文信息。 例如:要求生成的 commit message 必须是中文

{
  "github.copilot.chat.commitMessageGeneration.instructions": [
    { "text": "Use conventional commit message format." },
    { "text": "Only genereate Chinese message." }
  ]
}

抑或者是要求使用 vitest 来测试 Components, 使用 Jest 测试 JavaScript code.

  "github.copilot.chat.testGeneration.instructions": [
    {
      "text": "Always use vitest for testing React components."
    },
    {
      "text": "Use Jest for testing JavaScript code."
    },
  ],

单独指定

有时候我们只想在某一次 chat 或者 generation 中添加额外的信息(例如生成 i18n 词条),那我们可以将 prompt 放在单独的文件中,然后通过添加附件📎 的方式将其包含进来,共同组成最终的 prompt。

步骤:

  1. VSCode settings 设置 "chat.promptFiles": true
  2. 在 .github/prompts 下面添加具体的 prompt 文件。 如:.github/prompts/i18n-prompt.md
从用户输入的JSON或者文本输入中提取出词条,并将这些词条归纳为 zh-CN 的 i18n JSON.

## 要求:

1. 不要重复输出词条
2. key 值是词条的 snake 形式的英文翻译,要求尽量简短,不能超过 8 个单词。
3. 只输出一个表示这些词条的 i18n JSON ,其他任何多余的东西不要返回

## example

.....
  1. 在 CHAT 或者 inline chat 面板中点击📎按钮,选择 prompts.
  2. 在对话框中输入其他信息。
输入:
公司
该项为必填项
营销组织

生成结果:

courier-new-fallback-font

3S 原则

3S 原则可以帮助我们写出好的指令, 更好地和 Copilot 交互。

Simple

不要让 Copilot 一次性完成多个任务,一个指令里面的任务应该尽量简单和单一。 要记住,大模型输出的越多,那么幻觉也会越多。

❌ an express api that returns api weather data and use swagger.

✅ 将该目标分解成多个步骤

  1. express app typescript
  2. simple express router typescript
  3. integrate swagger into this project

Specific

指令应该明确和清晰, 尽量包含必要的上下文。 例如,通过 @workspace agent 让 Copilot 获取更多的上下文。

Short

指令尽量简短,不需要使用人类冗长的说话方式。