自定义 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 帮助的质量和相关性,同时保持对编码过程的控制。