Skip to main content

为 GitHub Copilot 添加存储库自定义说明

在仓库中创建一个文件,自动向询问 Copilot Chat 的问题中添加信息。

Note

此功能目前为 公共预览版,可能会更改。

Repository custom instructions are currently supported for Copilot Chat in Visual Studio, VS Code and on the GitHub website.

本文的此版本适用于在 GitHub 上使用存储库自定义说明。 单击上面的选项卡,获取有关在其他环境中使用自定义说明的信息。

有关可用于自定义 GitHub Copilot Chat 回复的方法的概述,请参阅“About customizing GitHub Copilot Chat responses”。

关于 GitHub Copilot Chat 的存储库自定义说明

GitHub Copilot 可基于你团队的工作方式、你使用的工具或项目的具体情况(如果提供足够的相关信息来满足此前提)提供量身定制的聊天响应。 无需重复将此详细信息添加到聊天问题中,而是可以在存储库中创建一个自动添加此信息的文件。 聊天中不会显示这些附加信息,但这些信息可供 Copilot 使用以便其能够生成更优质的响应。

示例

This example of a .github/copilot-instructions.md file contains three instructions that will be added to all chat questions.

We use Bazel for managing our Java dependencies, not Maven, so when talking about Java packages, always give me instructions and code samples that use Bazel.

We always write JavaScript with double quotes and tabs for indentation, so when your responses include JavaScript code, please follow those conventions.

Our team uses Jira for tracking items of work.

仓库自定义指令的先决条件

  • 自定义说明文件(请参阅以下说明)。

创建存储库自定义说明文件

  1. 在存储库的根目录中,创建名为 .github/copilot-instructions.md 的文件。

    如果尚无 .github 目录,则创建该目录。

  2. 以 Markdown 格式在该文件中添加自然语言说明。

    系统会忽略说明信息间的空格,因此可将信息编写为一个段落,每个段落位于一行上,或用空白行分隔,以保持其可读性。

要查看你的指令实际效果,请前往 https://github.com/copilot,附加包含指令文件的仓库,然后开始一次对话。

你是否已成功将自定义指令文件添加到你的仓库中?

编写有效的存储库自定义说明

添加到 .github/copilot-instructions.md 文件的说明应为简短的自包含语句,这些语句包含可补充用户聊天问题的上下文或相关信息。

你还应考虑仓库的大小和复杂度。 以下类型的指令可能适用于仅有少数贡献者的小型仓库,但对于大型且多样化的仓库,可能会影响 Copilot 的其他区域:

  • 要求在提供的回答中引用外部资源
  • 有关按特定风格回答的说明
  • 要求始终以特定详细级别的信息来回答

例如,以下指令可能无法产生预期效果:

Always conform to the coding styles defined in styleguide.md in repo my-org/my-repo when generating code.

Use @terminal when answering questions about Git.

Answer all questions in the style of a friendly colleague, using informal language.

Answer all questions in less than 1000 characters, and words of no more than 12 characters.

使用中的存储库自定义说明

保存文件后,.github/copilot-instructions.md 文件中的说明便可即刻供 Copilot Chat 使用。 完整的说明集将自动添加到与包含说明文件的存储库相关的聊天提示中。

在 Copilot Chat 的沉浸式视图 (github.com/copilot) 中,可以通过附加包含指令文件的仓库来开始一次使用仓库自定义指令的对话。

每当 Copilot Chat 使用仓库自定义指令时,指令文件都将作为生成的响应的引用进行添加。 要查看是否使用了仓库自定义指令,请展开“Chat”面板中聊天响应顶部的引用列表,检查是否列出了 .github/copilot-instructions.md 文件。

展开的“References”列表的屏幕截图,其中用深橙色边框突出显示了“copilot-instructions.md”文件。

可以单击引用信息来打开该文件。

Note

  • It is possible for multiple types of custom instructions to apply to a conversation. Personal instructions take the highest priority, followed by repository instructions, with organization instructions prioritized last. However, all sets of relevant instructions are still combined and provided to Copilot Chat.
  • Whenever possible, you should avoid providing conflicting sets of instructions. If you are concerned about response quality, you can also choose to temporarily disable repository instructions. See 为 GitHub Copilot 添加存储库自定义说明.

启用或禁用存储库自定义说明

可以选择是否要将自定义说明添加到聊天问题。

  1. 单击“Chat”面板顶部或沉浸式页面右上角的 按钮。

  2. 单击“Disable custom instructions”或“Enable custom instructions”。********

    Note

    在沉浸式模式下,只有当附加了包含自定义说明文件的仓库时,才会看到这些选项。

你的选择将一直保留,直到你更改它。