自訂 GitHub Copilot 聊天回應
GitHub Copilot Chat 可以在提供正確的上下文時生成程式碼和回應,以符合您的程式編寫慣例和專案需求。 您可以不要在每個聊天提示中重複新增這項資訊,而是將此內容儲存在檔案中,並自動將其包含在每個聊天要求中。
在 Visual Studio Code 中自定義 AI 回應有三種主要方式:
自定義指示:定義工作的常見指導方針,例如產生程式代碼、執行程式代碼檢閱或產生認可訊息。 這些描述應如何執行工作,並可指定編碼作法、慣用技術、安全性規則或認可訊息格式。
提示檔案:定義常見工作的可重複使用提示。 這些獨立的提示訊息描述了應該執行的動作,並可以包含支架組件、執行程式碼審查、建立逐步指南或產生實施計劃。
自定義聊天模式:定義聊天的運作方式、可使用的工具,以及它如何與程式代碼基底互動。 範例包括具有只讀存取權的規劃模式、存取外部資源的研究模式,或前端開發等角色特定模式。
自定義指示
自定義指示可讓您描述取得符合特定程式代碼撰寫做法和技術堆疊之回應的指導方針。 自定義指示會自動將這項資訊併入每個聊天要求,而不是在每個聊天查詢中手動包含此內容。
Note
自訂指令不會影響程式碼補全。
自定義指示的類型
Visual Studio Code 支援三種方式來定義自定義指示:
| 類型 | Description | 使用案例 |
|---|---|---|
.github/copilot-instructions.md |
工作區中的單一 Markdown 檔案,自動包含在所有要求中,跨編輯器支援。 | 一般編碼作法、慣用技術、全專案需求。 |
.instructions.md files |
具有 glob 模式支援的多個 Markdown 檔案、工作區或使用者設定檔儲存體。 | 特定任務指示,精確控制指令的套用時機。 |
| Visual Studio Code 設定 | 特定情境中使用者/工作區設定的指示。 | 程式碼產生、測試產生、認可訊息、程式碼檢閱、PR 描述。 |
您可以結合這些方法,但應避免指令之間發生衝突,因為沒有設定優先順序。
自訂指示範例
一般編碼指導方針:
---
applyTo: "**"
---
# Project coding standards
## Naming Conventions
- Use PascalCase for component names, interfaces, and type aliases.
- Use camelCase for variables, functions, and methods.
- Prefix private class members with underscore (_).
- Use ALL_CAPS for constants.
## Error Handling
- Use try/catch blocks for async operations.
- Implement proper error boundaries in React components.
- Always log errors with contextual information.
TypeScript 和 React 指導方針:
---
applyTo: "**/*.ts,**/*.tsx"
---
# TypeScript and React standards
Apply the [general coding guidelines](./general-coding.instructions.md) to all code.
## TypeScript Guidelines
- Use TypeScript for all new code.
- Follow functional programming principles where possible.
- Use interfaces for data structures and type definitions.
- Prefer immutable data (const, readonly).
- Use optional chaining (?.) and nullish coalescing (??) operators.
## React Guidelines
- Use functional components with hooks.
- Follow the React hooks rules (no conditional hooks).
- Use React.FC type for components with children.
- Keep components small and focused.
- Use CSS modules for component styling.
使用 .github/copilot-instructions.md 檔案
將自定義指示儲存在工作區根目錄的檔案中 .github/copilot-instructions.md ,以描述程式碼撰寫做法、慣用技術和專案需求。 這些指示僅適用於工作區,而且會自動包含在每個聊天要求中。
安裝步驟:
- 將
github.copilot.chat.codeGeneration.useInstructionFiles設定為true。 - 在工作區根目錄建立
.github/copilot-instructions.md。 - 描述使用自然語言和 Markdown 格式的指示。
Note
此檔案適用於Visual Studio Code、Visual Studio和 GitHub.com。
使用 .instructions.md 檔案
針對特定工作、程式設計語言、架構或項目類型建立多個 .instructions.md 檔案。 這些可以根據檔案模式自動套用,或手動附加至聊天提示。
檔案位置:
-
工作區檔案:儲存在
.github/instructions資料夾中,只能在工作區內使用。 - 使用者檔案:儲存在 Visual Studio Code 配置檔中,可在多個工作區之間取得,並透過 [設定同步] 進行同步處理。
檔案結構:
---
description: "Brief description of the instructions file"
applyTo: "**/*.ts,**/*.tsx" # Glob pattern for automatic application
---
# Instructions content in Markdown format
建立和使用說明文件:
- 從命令選擇區執行
Chat: New Instructions File。 - 選擇工作區或使用者位置。
- 在 Markdown 中輸入名稱和作者指示。
- 使用
Chat: Configure Instructions來編輯現有的檔案。
手動附件:
- 在 [聊天] 檢視中:新增內容 > 指示
- 命令面板:
Chat: Attach Instructions
自動應用程式:搭配 glob 模式使用 applyTo 中繼資料 (適用於所有要求的 **,適用於目標應用程式的特定模式)
在設定中指定自定義指示
針對特定案例,在 Visual Studio Code 設定中設定自定義指示:
| Scenario | Setting |
|---|---|
| 程式碼產生 | github.copilot.chat.codeGeneration.instructions |
| 測試產生 | github.copilot.chat.testGeneration.instructions |
| 程式代碼檢閱 | github.copilot.chat.reviewSelection.instructions |
| 認可訊息 | github.copilot.chat.commitMessageGeneration.instructions |
| PR 標題/描述 | github.copilot.chat.pullRequestDescriptionGeneration.instructions |
將指示定義為文字或參考外部檔案:
"github.copilot.chat.codeGeneration.instructions": [
{
"text": "Always add a comment: 'Generated by Copilot'."
},
{
"text": "In TypeScript always use underscore for private field names."
},
{
"file": "general.instructions.md"
},
{
"file": "db.instructions.md"
}
]
自訂指示的最佳做法
- 保持指示簡潔:每個指令都應該是單一的簡單語句。
- 避免外部參考:不要參考外部程式碼標準或資源。
- 依主題組織:將指示分割成多個檔案,以取得更好的組織。
- 啟用小組共用:將指示儲存在版本控制的檔案中,以進行小組共同作業。
-
使用目標應用程式:利用
applyTo屬性進行檔案特定指示。 - 有效率地參考:參考提示檔案中的自定義指示,以避免重複。
Summary
GitHub Copilot Chat 中的自定義指示可讓您定義每個聊天要求中自動包含的程式代碼撰寫做法、慣用技術和專案需求。 藉由使用 .github/copilot-instructions.md 檔案、 .instructions.md 檔案或 Visual Studio Code 設定,您可以確保 AI 產生的回應符合編碼標準和專案需求。 此方法可增強 AI 協助的品質和相關性,同時維持對程式代碼撰寫的控制權。