參加你附近的 ,瞭解 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 更好地理解您專案的指令。您可以自定義這些指令以適應您專案的需求。

現在,開啟聊天檢視並用一個極其簡潔的命令提示它,例如 尾遞迴。您不必問“我如何在 JavaScript 中進行尾遞迴”或類似的問題。只需輸入 尾遞迴 並按下 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.

您可以在這裡新增任何您喜歡的檔案,包括您當前正在進行的衝刺(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 檔案,為您在專案中生成模型。您可以在編輯模式或提問模式下執行此操作。只需使用提示 為專案生成資料訪問程式碼,然後——砰!魔法發生了!

介紹提示檔案

提示檔案允許您為您自己和您的團隊編寫可重用的提示。這有助於強制執行一致性,同時減少可能變得乏味的提示過程。

例如,假設您想建立一個可重用的提示,該提示將根據資料庫模式建立一個介面。您可以建立一個包含資料庫模式的提示檔案。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 是一個可定製的助手,當配置得當時,可以成為團隊工作流程中不可或缺的一部分。有了自定義指令,控制權就掌握在開發者手中。

編碼愉快!