Edit

Share via


Manage chat context with references

Scope your questions to GitHub Copilot Chat by using references, in addition to its implicit contextual knowledge, to get better answers.

In this article, you learn how to get better answers by providing more information to Copilot Chat:

  • Understand the implicit context Copilot leverages behind the scenes
  • Scope the chat to specific files using references
  • Attach images to your prompt to provide additional context and better illustrate your ideas
  • Review the source used by Copilot to generate the answer
  • Use different threads for each of your Copilot chats to maintain chat history and different context in each

Prerequisites

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

Implicit context

Visual Studio automatically provides context to the chat prompt based on your current activity.

The following information is implicitly included in the chat context:

  • The currently selected text in the active editor.
  • The current file active in the editor.

Depending on your prompt, Copilot reads the contents of the active file. To explicitly include the contents of another file in the chat context, select the suggested file using the + button in the chat view to Add attachment.

Screenshot of implicit context in Copilot Chat.

Reference: scope Copilot results

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.

Screenshot of references in Copilot Chat.

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 file reference 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.

Reference an image

With vision integration for Copilot Chat in Visual Studio 17.14 and later, you can attach images to your chat prompt, providing Copilot with additional context for improved responses. Use images in combination with scope and other contextual features such as slash commands to generate tailored responses.

In the following example, Copilot interprets the attached image to generate a plan and suggest code to create a colorful Breakout game from scratch.

Screenshot of an example scenario of adding an image in Copilot Chat.

Reference output window

You can now use the output window as context in chat to ask questions and get help with output logs. Supported output windows include Build, Debug, Tests, Source Control, Package Manager, or any other active output window pane.

Reference output logs in chat in one of the following ways:

  • Use #output to reference it directly in chat.
  • Select the ➕ icon in the chat input and add Output logs as context.
  • Ask Copilot directly, for example, Check my output logs and help me fix this error.
  • Right-click in the Output Window and select Explain with Copilot to attach it.

Screenshot of output window as context 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
Update my UI in App.tsx to resemble this image Uploaded image

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: manage chat history context with threads

As you iterate and send multiple chat prompts in a chat session, Copilot uses the history of chat prompts and responses as context for your current chat prompt. This means that you can ask follow-up questions or clarify your previous question without having to repeat the context. For example, you can ask "How does this differ from ...", "Now add a test case", "explain in more detail", and more.

To start over with a new chat session and discard the current context, start a new thread in the chat view. This is useful when you want to move to a different topic and avoid the previous context and history. Use threads to keep conversations focused on the task at hand, and keep the context clear so the answers are based on relevant history.

New chat thread

Select Create new thread or Ctrl+N in the chat window to start a new thread.

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. You can use Ctrl+PgDown for previous thread or Ctrl+PgUp for next thread in the chat window. Ctrl+Shift+T expands the thread dropdown.

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