Introduction to GitHub Copilot#
Introduction#
In this lesson we will start exploring how to use GitHub Copilot to generate code for us. We do this by creating natural language prompts (i.e., writing instructions for what you want the code to do, the way you would say it to another human), and sometimes starting to type a bit of code to get the process started.
Attention
Because of the generative nature of LLMs like GitHub Copilot, the results will not be the same every time you give it the same prompt. Thus as you’re following along with this lesson, Copilot may generate different code that you see here. That’s normal! You can also ask Copilot to generate alternative code solutions to your prompt, by pressing Option+[
(or Start+[
on Windows) after you get the first code suggestion (note that you have to wait until Copilot generates a suggestion before pressing those keys). If it suggests what you see in the lesson, you can press Tab
to accept it, which will make it easiest to follow along.
If you don’t get the same code as in this lesson, don’t worry. Some of us who tested this lesson actually got fewer errors from Copilot than illustrated in this lesson. If you’re not getting the same code from Copilot as you see here, we have two suggestions:
If the code works, you can probably just keep working through the lesson. There is some risk that it will create problems later (e.g., if Copilot suggests a different variable name than used in this lesson). In that case, the next point is relevant:
Try to use the processes explained here for interpreting error messages, and Copilot’s descriptions and suggested code. It’s much more important to get familiar with engaging in the process of debugging, through the thought processes explained here, than to worry about getting different suggestions from Copilot.
Prerequisities#
You should already have followed all the steps in the chapter, Set Up Your Computer for Data Science, including installing VS Code and all the recommended extensions. This includes the GitHub Copilot extension. You should also have set up your GitHub account in VS Code, and logged in to the GitHub Copilot extension using your GitHub account (the extension will routinely pop up messages with a button for you to do this, if you installed the extension but didn’t connect it to your GitHub account yet).
As noted earlier, GitHub Copilot is a paid add-on from GitHub. It is free for students, but you need to get access by signing up for the GitHub Student Developer Pack. Others with academic afilliations, like professors and postdocs, can also get free access to GitHub Copilot by applying for the GitHub Teacher Benefits. If you are not affiliated with an educational institution, however, you will need to get a paid subscription to GitHub Copilot in order to use it.
Once you have GitHub Copilot installed and connected to your GitHub account, you can start using it in VS Code. You may have turned it off to do the lessons in the previous chapter. If so, you can turn it back on by clicking on the GitHub Copilot icon in the bottom right of the VS Code window (if unsure, move your cursor across the bottom toolbar until you see Activate Copilot
).
How to Prompt Copilot#
In a previous lesson we learned about comments, which are lines in your code that are not executed, but are there to help you and others understand what the code is doing. In Python, comments start with a #
symbol. Python will not treat anything after the #
symbol as code; it will ignore it.
With GitHub Copilot activated, it will read your comments as prompts, and use them to generated suggested code. For example, if you type the following code into a cell in a Jupyter notebook:
# create a list of numbers
and then press Enter
, Copilot will generate the following code:
# create a list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The suggestions appear as fainter text than code you type yourself. That’s how you can tell it’s a suggestion. You can accept the suggestion by pressing Tab
. If you press Tab
, the suggestion will be inserted into your code, and will appear normal brightness rather than faded. If you don’t want to accept the suggestion, you can press Option+]
(Start+]
on Windows) to get an alternative suggestion (Copilot doesn’t always have alternative suggestions though). If you don’t want to accept any of the suggestions, you can just keep typing your own code.
Caution
Copilot will often generate code if you type a prompt without the leading #
that marks a comment. However, this is not recommended, because when you try to run the cell, Python will try to execute the prompt as code, and will give you an error.
The great thing about using comments to generate code is that your code retains the prompts you used, which aids in transparency and reproducibility (to the extent that Copilot produces consistent results). It also means that for your future self, or others who might try to understand your code, the comments will be there to help them understand what the code is doing. Including comments like this has always been good practice, but often people focused on writing the code and included comments as an afterthought, if at all. With Copilot, you can write the comments first, and then use them to generate the code, which ensures a better quality of documentation.
Write Your First AI-Assisted Code#
In the code cell below, type the following prompt as a comment:
# create a list of numbers
Then press Tab
to get a suggestion from Copilot. If you like the suggestion (which will likely be the same as what you see below), press Tab
to accept it, and then Shift+Enter to run the Jupyter cell.
Tip
Sometimes you may accidentally accept a suggestion from Copilot that you didn’t mean to. If that happens, you can undo it by pressing Cmd+Z
on a Mac (Ctrl+Z
on Windows). Or, you might hit the wrong key and the suggestion will disappear. In this case, you can backspace otherwise move your cursor to the end of the prompt line, and hit Enter
again to get the suggestion back.
# create a list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Hopefully this didn’t generate an error, but it also didn’t generate any output — because we didn’t ask it to. Let’s add a prompt to ask it to print the list of numbers. In the code cell below, type the following two prompts as comments, then press Tab
to get a suggestion from Copilot. If you like the suggestion, press Tab
to accept it, and then Shift+Enter to run the Jupyter cell.
# create a list of numbers
# print the list of numbers
# create a list of numbers
# print the list of numbers
print(numbers)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In my case, this generated the code you see above, which prints the list of numbers. Even though we asked it to create a list of numbers, it didn’t regenerate the code that defines the list of numbers, because it already did that in the previous cell. This is one of the many cool things about Copilot — it is sensitive to the entire context of your notebook (i.e., code cells above and below the cell you’re working on), and will use that context to generate suggestions. It’s even sensitive to other files in your project, as we will see soon.
Combine Two Lists#
We’re hopeful that you remembered how to create a list in Python from the previous chapter — that’s pretty fundamental. But do you remember how to combine two lists? Maybe not. It’s a bit tricky, because if you get it wrong you’ll end up with a list of lists, rather than a single list. Copilot can help with this. In the code cell below, define the two lists as shown (Copilot will probably help you with this), then type the following prompt as a comment, then press Tab
to get a suggestion from Copilot (from here onward, we’re just going to tell you what prompts to use, and assume you’ll always remember to use Tab
to accept a suggestion, and Shift+Enter to run the cell).
Tip
You may also notice that Copilot not only generates code from prompts — it often will generate your prompts for you! Once you get over the initial spookiness (or apparent telepathy) of this, you’ll realize that this is actually a very useful feature, because it can help you learn how to phrase prompts, and save you time typing. However, sometimes the prompts it generates are not quite what you want, so you may need to either cycle through alternative suggestions (Option+[
for Mac or Start+[
for Windows), or just type the prompt you actually want.
participant_1_data = [1, 3, 5, 7, 9]
participant_2_data = [2, 4, 6, 8, 10]
# Combine the above two lists into a new list called all_participant_data
all_participant_data = participant_1_data + participant_2_data
# print the list all_participant_data
print(all_participant_data)
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Now, how was it that we sort a list? We did that in the previous chapter, but it’s not something you do every day, so you might not remember. Copilot can help with that too. In the code cell below, write a prompt to ask for the all_participant_data
to print out, sorted.
# print the values in all_participant_data, sorted from smallest to largest
print(sorted(all_participant_data))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
When I started tying the prompt above, Copilot had a couple of suggestions, but neither of them are what I wanted to do. So I just typed the prompt I wanted, and Copilot generated the code you see above.
Tip
Copilot operates pretty fast, normally – especially with relatively simple prompts like these. However, sometimes it takes longer, but it is working. Other times, it’s not actively trying to generate code for you. This is typically because it doesn’t realize you’re waiting for it to generate something. This is still an emerging technology!
The way you can determine whether Copilot is trying to generate code or not is to look at the Copilot icon in the bottom right of the VS Code window (the same icon you use to activate/deactivate Copilot). If it looks like a face, Copilot is waiting for you. However, if it looks like a spinning circle, Copilot is still trying to generate something for you. Be patient.
Other times, however, Copilot is just not generating anything. We’ve found a few tricks to get Copilot going if you have a prompt and it’s not generating code:
Try deleting any empty lines in the cell below the prompt.
Move your cursor to the end of the prompt, delete the last character, and then type it again. This also works if you’ve copied and pasted a prompt from another cell.
Sometimes after typing a prompt you will need to hit
Enter
twice. Typically this occurs to enforce good programming style: if your prompt is going to generate more than one line of code, Copilot often requires you to have a blank line between the prompt and the code it generates. This makes the code more readable.Likewise, after Copilot has generated a line of code for you, and you want to type another prompt, hit
Enter
twice so that there is an empty line between the previous line of code, and the next prompt. This also makes the code more readable.Sometimes after typing a prompt and hitting
Enter
, Copilot will generate a suggestion, but then it will disappear. If this happens, you can get it back by moving your cursor to the end of the prompt and hittingEnter
again.Another thing you may encounter is that Copilot generates more code than you asked for or need. Copilot will often try to anticipate further things you want to do with your code, and generate code for that. This can be useful, but sometimes it’s not what you want. In that case, you can just delete the code you don’t want.
We have also (rarely) encountered a situation in which Copilot will “go dark”. That is, we type a prompt, hit Enter
, and nothing happens. The Copilot toolbar icon doesn’t spin, and it doesn’t generate any suggestions. This is sometimes because it doesn’t understand what you’re asking it to do. Other times, you may need to save the file, quit VS Code, and restart VS Code.
Summary#
GitHub Copilot is an AI-assisted coding tool that can generate code for you, based on prompts you write using natural language
Copilot is sensitive to the context of your notebook, and can generate code based on code in other cells, or even other files in your project
Copilot is sensitive to the wording of your prompts, and will generate different code based on how you phrase your prompts
Copilot is sensitive to the formatting of your prompts, and will generate different code based on how you format your prompts (for example, breaking down a problem into steps and writing each step as a separate prompt, or a separate sentence in a longer prompt)
Because of the generative nature of LLMs like GitHub Copilot, the results will not be the same every time you give it the same prompt (in fact, Copilot may generate different answers than you see in this lesson to the same prompts, when you try it.)
You can also ask Copilot to generate alternative code solutions to your prompt, by pressing
Option+[
(orStart+[
on Windows) after you get the first code suggestion (note that you have to wait until Copilot generates a suggestion before pressing those keys)Copilot is not perfect, and may generate code that doesn’t work, or that doesn’t do what you want it to do. It’s important to read and understand the code it generates, and to test it to make sure it’s doing what you want it to do.
Copilot is not a replacement for learning to code. It’s a tool to help you code faster, and to help you learn to code. But you still need to learn to code, and to understand what you’re doing. On the bright side, Copilot can help you learn to code, by generating code for you that you can then read and try to understand.