Module (03): (GHDR) Introduction to Prompt Engineering #8
MohamedRadwan-DevOps
announced in
Documentation
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Module (03): (GHDR) Introduction to Prompt Engineering
Document Type: GHDR (GitHub Documentation Reference)
Scope: This document provides the official extracted content from GitHub Docs corresponding to Module (02): Introduction to Prompt Engineering. It preserves the original wording exactly as published (with example languages converted to C# where applicable), extending and complementing the Microsoft Learn module with additional verified information.
Prompt engineering for GitHub Copilot Chat
A prompt is a request that you make to GitHub Copilot. For example, a question that you ask Copilot Chat, or a code snippet that you ask Copilot to complete. In addition to your prompt, Copilot uses additional context, like the code in your current file and the chat history, to generate a response.
Follow the tips in this article to write prompts that generate better responses from Copilot.
Start general, then get specific
When writing a prompt for Copilot, first give Copilot a broad description of the goal or scenario. Then list any specific requirements.
Example (C# version):
Give examples
Use examples to help Copilot understand what you want. You can provide example input data, example outputs, and example implementations.
Example (C# version):
Unit tests can also serve as examples. Before writing your function, you can use Copilot to write unit tests for the function. Then, you can ask Copilot to write a function described by those unit tests.
Break complex tasks into simpler tasks
If you want Copilot to complete a complex or large task, break the task into multiple simple, small tasks. For example:
Avoid ambiguity
Avoid ambiguous terms. For example, don’t ask "what does this do" if "this" could be the current file, the last Copilot response, or a specific code block. Instead, be specific:
Ambiguity can also apply to libraries:
Indicate relevant code
If you are using Copilot in your IDE to get suggestions as you code, open any relevant files and close irrelevant files. Copilot will use the open files to understand your request.
If you are using Copilot Chat in your IDE, open the file or highlight the code that you want Copilot to reference. You can also use keywords to manually supply context to Copilot Chat. For example, you can add the @workspace chat participant in VS Code, or @project in JetBrains IDEs. See GitHub Copilot Chat cheat sheet.
Experiment and iterate
If you don’t get the result that you want, iterate on your prompt and try again.
If you are using Copilot to get suggestions as you code, you can delete the suggestion entirely and start over. Or you can keep the suggestion and request modifications.
If you are using Copilot Chat, you can reference the previous response in your next request. Or, you can delete the previous response and start over.
Keep history relevant
Copilot Chat uses the chat history to get context about your request. To give Copilot only the relevant history:
Follow good coding practices
If you aren't getting the responses you want when you ask Copilot for suggestions or explanations in your codebase, make sure that your existing code follows best practices and is easy to read. For example:
Tip
You can ask Copilot to add comments, refactor, or split large functions into smaller, testable methods.
Source: Prompt engineering for GitHub Copilot Chat
GitHub Copilot Chat cheat sheet
You can enhance your experience of Copilot Chat with a variety of commands and options. Finding the right command or option for the task you are working on can help you achieve your goals more efficiently. This cheat sheet provides a quick reference to the most common commands and options for using Copilot Chat.
For information about how to get started with Copilot Chat in the GitHub website, see Asking GitHub Copilot questions in GitHub.
Mentions
Use @ mentions to attach relevant context directly to your conversations. Type @ in the chat prompt box to display a list of items you can attach, such as:
Slash commands
Use slash commands to avoid writing complex prompts for common scenarios. To use a slash command, type / in the chat prompt box, followed by the command name.
Available slash commands may vary, depending on your environment and the context of your chat. To view a list of currently available slash commands, type / in the chat prompt box of your current environment. Below is a list of some of the most common slash commands for using Copilot Chat.
Command — Description
Source: Getting started with prompts for GitHub Copilot Chat
About customizing GitHub Copilot responses
Note
This version of this article is about custom instructions on the GitHub website. Click the tabs above for other environments.
About customizing Copilot responses
GitHub Copilot can provide responses that are tailored to your personal preferences, 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 prompts, you can create custom instructions that automatically add this information for you. The additional information is not displayed, but is available to Copilot to allow it to generate higher quality responses.
Types of custom instructions
There are three main types of custom instructions that you can use to customize Copilot responses on the GitHub website:
About personal instructions
Note
Personal custom instructions are currently only supported for GitHub Copilot Chat in GitHub.
You can customize how Copilot Chat responds to you on GitHub.com by adding personal instructions, which Copilot will only apply to you. You do this in a popup on the Copilot Chat page on GitHub.com. See Adding personal custom instructions for GitHub Copilot.
About repository custom instructions
You can use three types of repository custom instructions in Copilot on GitHub.com:
copilot-instructions.mdfile in the.githubdirectory of the repository.NAME.instructions.mdfiles within the.github/instructionsdirectory in the repository.AGENTS.md,CLAUDE.md, orGEMINI.md.Support for repository custom instructions
The following table shows which Copilot features support custom instructions in various environments. The table uses these markers:
.github/copilot-instructions.mdfile)..github/instructions/**/NAME.instructions.mdfiles).AGENTS.mdfiles).AGENTS.md,CLAUDE.mdorGEMINI.mdfiles).Note
Copilot code review only reads the first 4,000 characters of any custom instruction file. Any instructions beyond this limit will not affect the reviews generated by Copilot code review. This limit does not apply to Copilot Chat or Copilot coding agent.
About organization custom instructions
Note
This feature is currently in public preview and is subject to change.
Support: Organization custom instructions are currently only supported for Copilot Chat on GitHub.com, Copilot code review on GitHub.com and Copilot coding agent on GitHub.com.
Organization owners can add instructions for Copilot, to tailor responses to the needs and preferences of your organization. For example, you can choose to always have Copilot respond in your company's language of choice or with a particular style. Custom instructions defined in an organization's Copilot settings are used for all members of the organization, irrespective of whether they receive their Copilot subscription from that organization.
Some examples of instructions you could add are:
Precedence of custom instructions
Multiple types of custom instructions can apply to a request sent to Copilot. 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.
Order of precedence (highest first):
.github/instructions/**/NAME.instructions.mdfile.github/copilot-instructions.mdfileAGENTS.mdfile)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 Adding repository custom instructions for GitHub Copilot.
Using custom instructions
Custom instructions consist of natural language instructions and are most effective when they are short, self-contained statements. Consider the scope over which you want the instruction to apply when choosing whether to add an instruction on the personal, repository, or organization level.
Common use cases and examples:
Writing effective custom instructions
The instructions you add to your custom instruction file(s) should be short, self-contained statements that provide Copilot with relevant information to help it work in this repository. Because the instructions are sent with every chat message, they should be broadly applicable to most requests you will make in the context of the repository.
The exact structure you utilize for your instructions file(s) will vary by project and need, but the following guidelines provide a good starting point:
Example instructions file:
You should also consider the size and complexity of your repository. The following types of instructions may work for a small repository with only a few contributors, but for a large and diverse repository, these may cause problems:
Examples that may not have the intended results:
Source: About customizing GitHub Copilot responses
Getting started with prompts for GitHub Copilot Chat
You can ask Copilot Chat specific questions about your project or general software questions. You can also ask Copilot Chat to write code, fix errors, write tests, and document code.
Use the tabs above to select the environment where you are using Copilot Chat.
Some of the following example prompts use chat participants (preceded by @), slash commands (preceded by /), or chat variables (preceded by #). For more information on keywords in prompts, see Asking GitHub Copilot questions in your IDE.
Ask general software questions
Examples:
Ask questions about your project
Examples:
To give Copilot the correct context, try some of these strategies:
#selection,#file,#editor,#codebase, or#git.Write code
You can ask Copilot to write code for you. For example:
When Copilot returns a code block, the response includes options to copy the code, or to insert the code at your cursor, into a new file, or into the terminal.
Ask questions about alerts from GitHub Advanced Security features
You can ask Copilot about security alerts in repositories in your organization from GitHub Advanced Security features (code scanning, secret scanning, and Dependabot alerts). For example:
Set up a new project
Use the /new slash command to set up a new project. For example:
Copilot will suggest a directory structure and provide a button to create the suggested files and contents. To preview a suggested file, select the file name in the suggested directory structure.
Create a new notebook
Use the /newNotebook slash command to set up a new Jupyter notebook. For example:
Fix, improve, and refactor code
If your active file contains an error, use the /fix slash command to ask Copilot to fix the error.
You can also make general requests to improve or refactor your code.
Write tests
Use the /tests slash command to ask Copilot to write tests for the active file or selected code. For example:
The /tests slash command writes tests for existing code. If you prefer to write tests before writing code (test driven development), omit the /tests command. For example:
Ask questions about Visual Studio Code
Use the @VScode chat participant to ask specific questions about Visual Studio Code. For example:
Ask questions about the command line
Use the @Terminal chat participant to ask specific questions about the command line. For example:
Source: Getting started with prompts for GitHub Copilot Chat
How GitHub Copilot handles data
As you explore adopting GitHub Copilot, you might wonder what data it collects, how that data is used, transferred, stored, and where its lifecycle ends. In this guide, we’ll describe GitHub Copilot’s data pipeline, and explain how your data is kept safe while being used to provide the most accurate code suggestions and responses possible.
In this guide, you will learn how GitHub Copilot:
Wherever you’re using GitHub Copilot—in a code editor, on the command line, on GitHub.com, or elsewhere—the data pipeline consists of a few distinct components: the user input, the input enrichment and prompt construction tools, and the large language model (LLM) at the core. Speaking broadly, GitHub Copilot gathers input and context, and uses these elements to build a prompt, which it then sends to the LLM. The LLM then formulates a response or code suggestion, which is returned to the user.
1. GitHub Copilot gathers context and builds a prompt
Much like humans, LLMs provide better answers when they have more context. For GitHub Copilot, the process of gathering context begins with the end user, wherever they’re interacting with GitHub Copilot. GitHub Copilot can gather various elements to identify relevant context, including content in the file you are editing, neighboring or related files within a project, repository URLs, or file paths. In GitHub Copilot Chat, context can also include highlighted code and previous questions and responses. You can stop GitHub Copilot from using certain content as context for code completions by updating your content exclusion settings. For full details on how GitHub Copilot treats code and customer data, make sure to visit the GitHub Copilot Trust Center.
Understanding how GitHub Copilot gathers and uses context can help you and your developers get the best responses. For example, if a developer has a file that describes business logic open in their code editor, they should also open the file containing the unit tests, which describe how the business logic should operate. GitHub Copilot will then include that context to build the prompt.
2. Prompts are tested for toxic language, relevance, and hacking attempts
Once the prompt is built, it passes securely to a proxy service hosted in a GitHub-owned Microsoft Azure tenant. Traffic is routed to regions based on capacity; we cannot guarantee any traffic will stay within a single region. On the proxy, we first test the prompt for toxic language—things like hate speech, sexual content, violence, and evidence of self-harm—and relevance. We don’t want GitHub Copilot to engage with model baiting, vulgar language, or questions unrelated to code and software development. At the same time, we also guard against prompt hacking by detecting and blocking attempts to trick the model into revealing information on how its code works and how it generates suggestions.
Assuming all tests pass, the prompt is passed to the large language model.
3. The model produces its responses
Like the proxy server and the GitHub Copilot API, the GitHub Copilot LLMs are hosted in GitHub-owned Azure tenants. These LLMs consist of AI models created by OpenAI that have been trained on natural language text and source code from publicly available sources, including code in public repositories on GitHub.
How data is treated at this point depends on a few configuration options and the specific GitHub Copilot feature you’re working with.
For full details on how data is securely and responsibly handled, make sure to visit the GitHub Copilot Trust Center.
4. GitHub Copilot’s responses are tested for code quality, certain unique identifiers and matching public code
Each response produced by the model passes through the proxy server for post-processing. Here, the response is checked for toxic language and relevancy, just as the prompt was, but also must pass three additional checks:
If a response fails any one of these filters—toxic language, relevancy, code quality, unique identifiers, or suggestions matching public code—it is either truncated or discarded entirely. Responses that pass all five tests are returned to the user.
How does GitHub Copilot identify matching code?
Our research suggests that about 1% of GitHub Copilot’s suggestions match public code. The important caveat is that these matches aren't evenly distributed across all suggestions. We see matches far more often when GitHub Copilot lacks context, such as when a developer starts a new project or file. When existing application code provides context, we almost never see a match.
Regardless of context, the public code filter identifies matching code by stripping out all whitespace (e.g. tabs, spaces) and comparing suggestions over ~150 characters against an index of all the public data on GitHub.com. When the filter is enabled, developers should only receive unique suggestions with no match in the public corpus. If there's a match, GitHub Copilot discards the suggestion and moves on to the next one in the queue.
5. GitHub Copilot returns a response or code suggestion to the user
Finally, the response or code suggestion is returned to the user. If the suggestion is returned in the code editor, the model may produce multiple suggestions, and the developer can choose which suggestion to accept or reject. In some IDEs (e.g. VS Code, Visual Studio, JetBrains IDEs), developers may also choose to accept a partial suggestion, word-by-word. Once accepted, they can edit the code.
And then the process starts all over again.
Tip
GitHub Copilot has been shown to increase developer productivity by up to 55% on well-defined tasks.
Source: How GitHub Copilot handles data
Beta Was this translation helpful? Give feedback.
All reactions