Adding custom instructions for GitHub Copilot

Note

  • This feature is currently in public preview and is subject to change.
  • Custom instructions are currently only supported for Copilot Chat in VS Code and Visual Studio.

About custom instructions for GitHub Copilot

GitHub Copilot can provide chat responses that are tailored to the way your team works, the tools you use, or the specifics of your project, if you provide it with enough context to do so. Instead of repeatedly adding this contextual detail to your chat questions, you can create a file that automatically adds this information for you. The additional information is not displayed in the chat, but is available to Copilot to allow it to generate higher quality responses.

Prerequisites

  • A custom instructions file (see the instructions below).

  • The Use Instruction Files (VS Code) or Enable custom instructions (Visual Studio) option enabled in your settings. See Enabling or disabling custom instructions later in this article.

    Note

    The setting is enabled by default in VS Code and disabled by default in Visual Studio.

Creating a custom instructions file

  1. In the root of your repository, create a file named .github/copilot-instructions.md.

    Create the .github directory if it does not already exist.

  2. Add natural language instructions to the file, in Markdown format.

    Whitespace between instructions is ignored, so the instructions can be written as a single paragraph, each on a new line, or separated by blank lines for legibility.

Sample instructions

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.

Writing effective custom instructions

The instructions you add to the .github/copilot-instructions.md file should be short, self-contained statements that add context or relevant information to supplement users' chat questions.

The following types of instructions are unlikely to work as desired and may cause problems with other areas of Copilot:

  • Requests to refer to external resources when formulating a response
  • Instructions to answer in a particular style
  • Requests to always respond with a certain level of detail

The following instructions are therefore unlikely to have the intended result:

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.

Custom instructions in use

The instructions in the .github/copilot-instructions.md file are available for use by Copilot Chat as soon as you save the file. The complete set of instructions will be automatically attached to requests that you submit in either the Copilot Chat view, or in inline chat, in VS Code and Visual Studio.

Custom instructions are not visible in the Chat view or inline chat, but you can verify that they are being used by Copilot by looking at the References list of a response. If custom instructions were added to the prompt that was sent to the model, the .github/copilot-instructions.md file is listed as a reference. You can click the reference to open the file.

Screenshot of the Copilot Chat view in VS Code with a reference for the copilot-instructions.md file.

Enabling or disabling custom instructions

You can choose whether or not to have custom instructions added to your chat questions. To do this, select or clear the option in the settings for the Copilot extension.

In VS Code

  1. Open the Setting editor by using the keyboard shortcut Command+, (Mac) / Ctrl+, (Linux/Windows).
  2. Type instruction file in the search box.
  3. Select or clear the checkbox under Code Generation: Use Instruction Files.

In Visual Studio

  1. In the Visual Studio menu bar, under Tools, click Options.

    Screenshot of the Visual Studio menu bar. The "Tools" menu is expanded, and the "Options" item is highlighted with an orange outline.

  2. In the "Options" dialog, type custom instructions in the search box.

  3. Select or clear the checkbox for (Preview) Enable custom instructions to be loaded from .github/copilot-instructions.md files and added to requests.

为 GitHub Copilot 添加自定义说明

Note

  • 此功能目前为 公共预览版 形式,可能会有改动。
  • 当前仅支持为 VS Code 与 Visual Studio 中的 Copilot Chat 添加自定义说明。

关于 GitHub Copilot 的自定义说明

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

先决条件

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

  • 设置中启用的“使用说明文件”(VS Code) 或“启用自定义说明”(Visual Studio) 选项。******** 请参阅本文后面的启用或禁用自定义说明

    Note

    默认情况下,该设置在 VS Code 中启用、在 Visual Studio 中禁用。

创建自定义说明文件

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

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

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

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

示例说明

.github/copilot-instructions.md 文件示例包含三条说明,这些说明将添加到所有聊天问题中。

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.

编写有效的自定义说明

添加到 .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 使用。 系统会自动将完整的全部说明附加到在 VS Code 和 Visual Studio 中的 Copilot Chat 视图中或内联聊天中提交的请求中。

自定义说明在“聊天”视图或内联聊天中不可见,但可以通过查看回答的“引用”列表来验证 Copilot 是否正在使用这些说明。 如果将自定义说明添加到发送到模型的提示中,引用信息中会列出文件 .github/copilot-instructions.md。 可以单击引用信息来打开该文件。

VS Code 中 Copilot Chat 视图的屏幕截图,其中包含 copilot-instructions.md 文件的引用条目。

启用或禁用自定义说明

可以选择是否要将自定义说明添加到聊天问题。 要执行此操作,可在 Copilot 扩展的设置中选择或清除相应选项。

在 VS Code 中

  1. 使用键盘快捷方式 Command+, (Mac)/Ctrl+, (Linux/Windows) 打开“设置编辑器”。
  2. 在搜索框中键入 instruction file
  3. 选中或清除“代码生成: 使用说明文件”**** 下的复选框。

在 Visual Studio 中

  1. 在 Visual Studio 菜单栏中的“工具”下,单击“选项” 。

    Visual Studio 菜单栏的屏幕截图。 “工具”菜单已展开,“选项”项以橙色边框突出显示。

  2. 在“选项”对话框中的搜索框中键入 custom instructions

  3. 选中或清除复选框“(预览)允许加载 .github/copilot-instructions.md 文件中的自定义说明并将其添加到请求中”****。

Cómo agregar instrucciones personalizadas para GitHub Copilot

Note

  • Esta característica está actualmente en versión preliminar pública y está sujeta a cambios.
  • Actualmente, solo se admiten instrucciones personalizadas para Copilot Chat en VS Code y Visual Studio.

Acerca de las instrucciones personalizadas para GitHub Copilot

GitHub Copilot puede proporcionar respuestas de chat adaptadas a la forma en que trabaja tu equipo, las herramientas que usas o los detalles específicos del proyecto, si le proporcionas suficiente contexto para ello. En lugar de agregar este detalle contextual en repetidas ocasiones a tus preguntas de chat, puedes crear un archivo que agregue esta información de forma automática. La información adicional no se muestra en el chat, pero está disponible para Copilot con el fin de que pueda generar respuestas de mayor calidad.

Requisitos previos

  • Un archivo de instrucciones personalizado (consulta las instrucciones a continuación).

  • La opción Use Instruction Files (VS Code) o Enable custom instructions (Visual Studio) habilitada en la configuración. Consulta Cómo habilitar o deshabilitar instrucciones personalizadas más adelante en este artículo.

    Note

    De forma predeterminada, la configuración está habilitada en VS Code y deshabilitada en Visual Studio.

Creación de un archivo de instrucciones personalizadas

  1. En la raíz del repositorio, crea un archivo con el nombre .github/copilot-instructions.md.

    Crea el directorio .github si no existe todavía.

  2. Agrega instrucciones de lenguaje natural al archivo en formato Markdown.

    Se ignora el espacio en blanco entre las instrucciones, por lo que estas pueden escribirse como un solo párrafo, cada una en una nueva línea o separadas por líneas en blanco para mejorar la legibilidad.

Instrucciones de muestra

Este ejemplo de un archivo .github/copilot-instructions.md contiene tres instrucciones que se agregarán a todas las preguntas de chat.

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.

Redacción de instrucciones personalizadas eficientes

Las instrucciones que agregues al archivo .github/copilot-instructions.md deben ser breves, autosuficientes y agregar contexto o información pertinente para complementar las preguntas de chat de los usuarios.

Es poco probable que los siguientes tipos de instrucciones funcionen como se desea y pueden causar problemas con otras áreas de Copilot:

  • Solicitudes para hacer referencia a recursos externos al formular una respuesta
  • Instrucciones para responder con un estilo determinado
  • Solicitudes para responder siempre con cierto nivel de detalle

Por lo tanto, es poco probable que las instrucciones siguientes tengan el resultado previsto:

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.

Instrucciones personalizadas en uso

Las instrucciones del archivo .github/copilot-instructions.md están disponibles para que Copilot Chat las use tan pronto como guardes el archivo. El conjunto completo de instrucciones se asociará automáticamente a las solicitudes que envíes en la vista de Copilot Chat o en el chat en línea, en VS Code y en Visual Studio.

Las instrucciones personalizadas no aparecen en la vista de chat ni en el chat en línea, pero puedes comprobar que Copilot las está usando si examinas la lista de referencias de una respuesta. Si se han agregado instrucciones personalizadas a la solicitud que se ha enviado al modelo, el archivo .github/copilot-instructions.md aparece como referencia. Puedes hacer clic en la referencia para abrir el archivo.

Captura de pantalla de la vista de Copilot Chat en VS Code con una referencia al archivo copilot-instructions.md.

Cómo habilitar o deshabilitar instrucciones personalizadas

Puedes elegir si quieres que se agreguen instrucciones personalizadas a tus preguntas de chat o no. Para ello, activa o desactiva la opción en la configuración de la extensión de Copilot.

En VS Code

  1. Abre el editor de configuración mediante el método abreviado de teclado Command+, (Mac)/Ctrl+, (Linux/Windows).
  2. Escriba instruction file en el cuadro de búsqueda.
  3. Activa o desactiva la casilla en Code Generation: Use Instruction Files.

En Visual Studio

  1. En la barra de menús de Visual Studio, en Herramientas, haz clic en Opciones.

    Captura de pantalla de la barra de menús Visual Studio. El menú "Herramientas" se expande y el elemento "Opciones" aparece resaltado con un contorno naranja.

  2. En el cuadro de diálogo "Options", escribe custom instructions en el cuadro de búsqueda.

  3. Activa o desactiva la casilla para (Preview) Enable custom instructions to be loaded from .github/copilot-instructions.md files and added to requests.