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.
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 numbersand 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.
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 numbersThen 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.
# 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).
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.
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.