Context is all you need:透過自訂指令獲得更好的 AI 成果

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

本月初,我們宣布了 Visual Studio Code 自訂指令 (custom instructions) 正式全面推出。自訂指令能讓您為 Copilot 提供具體的背景資訊,例如團隊的工作流程、特定的程式風格偏好、模型可能不熟悉的函式庫等等。

在本文中,我們將深入探討什麼是自訂指令、如何運用它們來大幅提升 GitHub Copilot 的使用成果,以及介紹一個名為「提示詞檔案 (prompt files)」的全新預覽功能,您現在就可以開始嘗試。

簡潔的提示詞,更棒的成果:透過簡單的 Markdown 檔案實現

使用 GitHub Copilot 可以大幅加速您的開發流程,但在有效地編寫提示詞 (prompting) 上仍有學習曲線。有時候需要嘗試幾次才能得到想要的答案,這通常意味著必須重新表述您的問題或指令。即便如此,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 個空格縮排、camelCase 命名法等)的答案。

這就是使用自訂指令的強大之處!但您能做到的遠不止於此……

自訂一切!

GitHub Copilot 為 VS Code 帶來了許多強大功能,包括產生提交訊息 (commit messages)、檢查選取的程式碼,甚至是產生測試!

這些功能固然實用,但您可能已有自己的一套做法。好消息是,您可以透過覆寫工作區中的幾項設定來進行個人化調整。

試試看:開啟命令面板 (Command Palette) (⇧⌘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 檔案並關閉它。現在,活動列 (Activity Bar) 中的原始碼管理 (Source Control) 圖示應該會顯示有檔案變更。如果您是在沒有 Git 儲存庫的測試目錄中工作,可以直接透過原始碼管理檢視建立一個。只需選擇 Initialize Repository 按鈕並按照指示操作即可。

重頭戲來了:開啟原始碼管理檢視,並點選提交訊息輸入欄中的星號 (sparkle) 圖示。這會指示 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.

重要提示:務必以肯定句而非否定句給予模型指令,因為它們需要知道「要做什麼」,而不是「不要做什麼」。與其說「不要做」,不如說「請避免」。

盡情實驗並享受樂趣吧!有位開發者表示,他喜歡透過產生俳句 (haiku) 風格的測試來活躍他的測試套件。這聽起來有點極端,但如果您只是在進行個人專案,何樂而不為呢?您可以加入如下的指令:

- 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 檔案,為您自動產生專案中的模型。您可以在編輯 (Edit) 或詢問 (Ask) 模式下操作。只需輸入提示詞 generate the data access for the project,碰!神奇的事情發生了!

介紹提示詞檔案 (Prompt files)

提示詞檔案讓您能為自己和團隊編寫可重複使用的提示詞。這有助於維持規範的一致性,同時減少編寫提示詞的繁瑣過程。

舉例來說,假設您想建立一個可重複使用的提示詞,根據資料庫結構描述 (schema) 來產生介面。您可以建立一個包含資料庫結構描述的提示詞檔案。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).

若要在 Chat 中使用這些提示詞檔案,請使用附件按鈕(迴紋針圖示)或使用 。從選單中選擇 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.