Find helpful tutorials or share inspiring use cases on AI technology for higher education.

Shared content may not reflect the policies of Tilburg University on the use of AI. 

GitHub Copilot Prompt Guide: Get the Most Out of Your AI Coding Assistant

In this article, we will explore various techniques on how to improve the performance of GitHub Copilot, an AI-powered coding assistant that helps you write code faster and more efficiently. Specifically, we will focus on Prompts, specific instructions you give to Copilot to guide its code suggestions. While we at tilburg.ai, have extensively discussed prompts for ChatGPT leading to this series, applying it effectively in the context of GitHub Copilot requires some unique considerations. Knowing how to clearly communicate your coding goals to Copilot can improve the quality of its code suggestions.

Copilot’s Inline Code Suggestions

As you type, GitHub Copilot offers inline code suggestions, which can range from completing the current line to generating an entirely new block of code. You have the option to accept the suggestion in its entirety, choose only part of it, or simply continue typing to ignore it.

When an inline suggestion appears, you can easily accept it by pressing the Tab key.

In this article, we will explore how to prompt GitHub Copilot to improve the quality of these inline code suggestions.

Prompting in GitHub Copilot

A prompt is a request that you make to GitHub Copilot. This could be done in natural language by asking questions to the Copilot Chat, similar to how you use ChatGPT. However, it could also be a code snippet that you ask Copilot to complete. Both of these are directly mentioned when interacting with Copilot. While these prompts are what you directly provide, Copilot operates on much more than just your input. It also considers the surrounding code in your file and any related chat history to deliver its suggestions.

Large Language Models (LLMs) are designed to make predictions based on the context and prompts provided. Therefore, Copilot works best when it has enough context to understand your task and what kind of help you need. Just like you would explain the background to a classmate when asking for help with a specific programming problem, you should provide the same context to Copilot. Translating this means that your prompt and the file you have opened are your speaking tools to your assistant. Therefore, the more contextually rich your input or prompt is, the better the prediction or output will be. This means it is all about context, context, and context.

The way you Prompt Copilot

1. Provide A Top Level Comment

Let’s start with the conversation opener. When you ask someone for help, the first thing you tell them is what you’re working on and what you ultimately want to achieve. Similarly, adding a top-level comment in the file you’re working on can help Copilot grasp the overall context of what you’re creating. This top-level comment is then followed by more detailed instructions in the form of comments and code.

Here is a simple example. In the first two lines, we specify our goals for an R script. Then, we break down the intermediate steps. You iterate step by step through the different stages and let GitHub Copilot assist you with writing the code in between.

R
# PIAAC Data Analysis Project
# Perform an exploratory data analysis (EDA) on the PIAAC dataset with the following features:

# - Load necessary libraries including tidyverse, ggplot2, dplyr, readr, and summarytools
# - Load the PIAAC dataset from a CSV file
# - Display the first few rows of the dataset
# - Check for and handle missing values appropriately
# - Generate summary statistics for the dataset
# - Create visualizations to explore the data, including:
#   - Age distribution histogram
#   - Literacy score distribution histogram
#   - Scatter plot of literacy score vs. age
#   - Box plot of literacy scores by education level
# - Perform correlation analysis between age and literacy score
# - Save the cleaned dataset to a new CSV file

Make sure you include specific details about what you need and provide a thorough description to give Copilot as much information as possible. This will help guide GitHub Copilot to offer better suggestions and give it a clear goal to work towards.

2. Iterate on your prompts

When you ask for help, you know the specific requirements the final result must meet. Therefore, you might be inclined to give the most efficient instructions possible. Do this, this, and this, and then you’ll understand the intention as the explainer. However, flip the conversation and reason from the perspective that you’re speaking to someone who doesn’t know your specific context. What requirements, and what specific values need to be considered? This is also how constructing a good prompt for GitHub Copilot works.

For example, the prompt below is vague. It doesn’t provide any context or boundaries for GitHub Copilot to generate relevant suggestions.

Write some code for my python file

Although this is an exaggerated example, it immediately highlights the need for more specific instruction. So, we iterated on the prompt to be more specific. However, adding specificity to your prompt is harder than it sounds. It’s difficult to know, from the start, which details you should include about your goal to generate the most useful suggestions from GitHub Copilot. That’s why we encourage experimentation.

The version of the prompt below is more specific than the one above, but it doesn’t clearly define the input and output requirements.

Implement a function to calculate the average gdp per capita

We experimented with the prompt once more by setting boundaries and outlining what we wanted the function to do. We also rephrased the comment so the function was clearer (giving GitHub Copilot a clear intention to verify against).

Implement the function calculate_average_gdppercapita that takes a list of countries, a list of gdp's and a list of popu as input and returns the average grade as a floating-point number

3. Funnel Your Comments

Start general then get specific. When writing a prompt for Copilot, first give a broad description of the goal or scenario. Then list any specific requirements. These requirements articulate the logic and steps it needs to follow to achieve that goal. GitHub Copilot better understands your goal when you break things down.

Here’s an example of us providing GitHub Copilot with step-by-step instructions for reversing a function:

Let GitHub Copilot generate the code after each step, rather than asking it to generate a bunch of code all at once.

4. Good coding practices

Adopt consistent and specific naming conventions for variables and functions that clearly reflect their intended purpose. As we may have implicitly mentioned, GitHub Copilot tends to follow the style of code you have been writing so far.

GitHub Copilot adapts to the naming conventions you employ in your code. For instance, if you typically use camelCase for your variables, Copilot will propose camelCase variable names. Similarly, if you prefer snake_case, it will suggest variables in snake_case.

The following type of person often needs everything to be clearly explained, which is why it is important to specify the exact function of variables or functions when naming them. The name of your variables and functions matters. If you have a function named try or between, GitHub Copilot will not be able to give you the best completion because it isn’t able to infer intent from the names. Therefore, use a more specific name like “input_string” or “output_file.” This will help the code completion assistant understand the context of the variable and generate more relevant suggestions.

By consistently using best coding practices in your code, Copilot will start recognizing and applying your preferred conventions more accurately in its suggestions.

5. Set up Copilot with Sample Code

There is a well-known Dutch expression ‘goed voorbeeld doet goed volgen‘, example is better than precept, which captures the coming tip well. So far we have mainly instructed our code completion assistant on what it should do. However, instead of just telling it, we can also show it what to do, thereby Copilot will automatically use our preferred coding style.

This technique is called shot prompting. For a detailed explanation of shot prompting, the technique we are using here, check out this article.

Guide Copilot in the right direction by copying and pasting sample code that closely resembles what you’re aiming for into your editor. By providing a small example, you can help Copilot generate suggestions that align with the language and tasks you’re working on. Once it offers the code that meets your needs, you can delete the sample code from the file.

Let’s walk through an example where you use shot prompting to generate code that involves iterating over a list and performing operations on each element.

Step 1: Provide a Few Examples in Comments

Python
# Example 1: Define a function that doubles each element in a list
def double_list_elements(lst):
    return [x * 2 for x in lst]

# Example 2: Define a function that squares each element in a list
def square_list_elements(lst):
    return [x * x for x in lst]

# Now, define a function that cubes each element in a list

Step 2: Let GitHub Copilot Generate the Code

Based on the examples provided, GitHub Copilot will likely generate the following function:

Python
def cube_list_elements(lst):
    return [x * x * x for x in lst]

Step 3: Verify and Adjust as Needed

Review the generated code to confirm it meets your expectations. If necessary, you can adjust the code or the initial examples to refine the output.

Explanation

  • Few-Shot Prompting: The comments with example functions (double_list_elements and square_list_elements) provide Copilot with a clear pattern for iterating over a list and applying an operation to each element.
  • Guided Output: Copilot uses these examples to generate the cube_list_elements function, which follows the same pattern of iteration and element-wise operation.

While GitHub Copilot is a valuable tool for generating code suggestions, it’s important to remember that it cannot replace your own programming skills and expertise. AI models are only as effective as the data they’ve been trained on. Therefore, it’s crucial to use these tools as support rather than relying on them completely.