Redigera

Dela via


Get better answers by setting the context for GitHub Copilot Chat in Visual Studio

You can use GitHub Copilot Chat in Visual Studio to get coding information and support, such as syntax, programming concepts, context-specific code help, test cases, debugging, and more, without leaving the IDE. Copilot Chat is integrated into Visual Studio, which means it can understand what you're working on by gathering useful clues from what you have in the IDE.

In addition to your prompt, Copilot uses additional context, like the code in your current file and the chat history, to generate a response. In this article, you learn how to get better answers by providing more information to Copilot Chat including:

  • Use slash commands to quickly specify common tasks like /explain to get code explanations.
  • Scope the chat to specific files using references.
  • Review the source used by Copilot to generate the answer.
  • Use different threads for each of your Copilot chats so you can maintain different contexts in each.

Learn more about AI-assisted development in Visual Studio and how to use Copilot Chat in Visual Studio.

Prerequisites

To get started using GitHub Copilot Chat in Visual Studio, you need:

Support for GitHub Copilot Chat will be provided by GitHub and can be reached at https://support.github.com.

Prompting guidance

The guided chat experience in Visual Studio 17.12 and later helps refine your prompts to clarify context for better answers. GitHub Copilot Chat now guides you with clarifying questions when context is unclear.

Screenshot of guided chat experience with GitHub Copilot Chat.

Use slash commands in Copilot Chat for common tasks

Slash commands in Copilot Chat help you set the intent quickly for common development tasks. By using specific slash commands to form your question, you can get better answers without having to write out long questions.

You can use slash commands in a chat window, or directly inline in the code that you're looking to modify, using inline code assistance. Commands that help modify or add to the code file you have open in the editor will work both in the inline code assistant and the chat windows whereas commands for more general coding questions work only in the chat pane.

Command Usage Chat window Inline chat
/doc Add comments for specified or selected code.
Examples:
- /doc DeleteBasketAsync method in BasketService.cs
- select desired code and enter /doc
Yes Yes
/explain Get code explanations.

Examples:
- /explain the AddItemToBasket method in BasketService.cs
- select desired code and enter /explain
Yes Yes
/fix Propose a fix for problems in the selected code.
Examples:
- /fix the SetQuantities method in BasketService.cs
- select desired code and enter /fix
Yes Yes
/generate Generate code to answer specified question.
Example: /generate code to add two numbers in Calculator.cs
Yes Yes
/help Get help on using Copilot Chat.
Example: /help
Yes Yes
/optimize Analyze and improve running time of the selected code.
Examples:
- /optimize the AddItemToBasket method in BasketService.cs
- select desired code and enter /optimize
Yes Yes
/tests Create unit tests for the selected code.
Example: select desired code and enter /tests using XUnit Framework
Yes Yes

Screenshot of slash commands in inline chat view and chat windows.

Reference: scope Copilot results to a particular file or entire solution

You can ask your coding related questions in natural language and GitHub Copilot Chat will answer these in the context of the codebase open in Visual Studio. With references you can get more specific about the information you want Copilot to consider when answering your question.

By selecting a specific context in your codebase, you're able to form better questions easily without having to write out or paste long pieces of information. Specifying the context also enables Copilot to provide you with more relevant answers.

Reference a file

To easily reference a file in GitHub Copilot Chat, simply add a # symbol at the beginning of the file name. For example, if you have a file named BasketService.cs, refer to it in the chat as #BasketService.cs.

Screenshot of references in Copilot Chat.

Reference a method, class, or function

With Visual Studio 2022 version 17.11, you can now reference a specific method, class, or function in GitHub Copilot Chat.

To easily reference a method, class, or function in GitHub Copilot Chat, simply add a # symbol at the beginning of the method, class, or function name. For example, if you have a method named BasketAddItem, refer to it in the chat as #BasketAddItem.

Screenshot of references to methods in GitHub Copilot Chat in Visual Studio.

Reference the entire solution

Use @workspace to refer to the solution active in the IDE for context. When using @workspace for context, Copilot Chat leverages the information about the files, projects, and configurations that are currently open and being worked on within your IDE. This enables Copilot Chat to provide more relevant and context-aware suggestions and answers.

Screenshot of referencing solution context in Copilot Chat.

With Visual Studio 2022 version 17.11, GitHub Copilot Enterprise subscribers can now use @github in chat to include context from their entire repository and to search the web (if search is enabled by your admin). To learn more about the collection of GitHub-specific skills that Copilot can use to answer your question when using @github, see Using GitHub skills for Copilot.

Screenshot of using GitHub skills in Copilot Chat.

Usage examples

Here are some examples of using references for context control:

Example Context used by Copilot to form the question
What is the purpose of #MyFile.cs: 66-72? Exact section of the file
Where are the tests in #BasketService.cs? BasketService.cs
/explain the #AddItemToBasket in #BasketService.cs AddItemToBasket method in BasketService.cs
Is there a delete basket method in this @workspace Current solution open in the IDE
I have a test method named #TestCalculator. How can I ensure that it's being executed correctly? TestCalculator method
Could you explain the differences between classes #BasketService and #OrderService? BasketService class and OrderService class
In my @workspace where is #AddItemToBasket? Current solution open in the IDE

Review the sources used by Copilot Chat

Copilot Chat displays the context it used after every result, so that you can tell what was taken into account when answering your question. When you ask a Copilot Chat a question and get a response in the chat window, a References dropdown appears below the response. The entries in the References dropdown list show you the context referenced by Copilot Chat to generate that response. This information can help you modify your question to get better and more relevant answers.

Screenshot of References used dropdown in Copilot Chat.

Organize: isolate chats with Copilot into threads

If you’re using Copilot Chat extensively to ask questions as you code, you can organize your conversations in a way that keeps them on-topic. Copilot Chat for Visual Studio now provides an easy way to start new conversations (threads) to keep them focused on the task at hand, and keep the context clear so the answers are based on relevant history.

New chat thread

You can start a new thread by selecting the Create new thread in the chat window.

Screenshot of Create new thread icon in Copilot Chat.

Switch chat thread

You can select between multiple ongoing threads to provide the right historical context for your question.

Screenshot of switching between ongoing threads in Copilot Chat.

Promote inline chat to the chat window

With Visual Studio 2022 version 17.11, you can now preserve the history of your inline chat by promoting it to the chat window. Select Continue in chat window... to maintain a record and context of the conversation, and continue in the chat window.

Screenshot of promoting ongoing thread in inline chat to the chat window.

Best practices

Copilot Chat uses the chat history to get context about your request. To give Copilot only the relevant history:

  • Use threads to start a new conversation for a new task.
  • Delete requests that are no longer relevant or that didn’t give you the desired result.

Keep the chat conversation open and continue to iterate and prompt Copilot to improve the suggested solution. Copilot has both the context of the generated code and your current conversation history. As you keep asking additional questions, Copilot further refines the response according to your requirements. See Prompt engineering for Github Copilot for strategies on effective prompting to improve your Copilot results.

Next steps