Chapter 4. GitHub Copilot

In this chapter, we’re going to check out GitHub Copilot, which I’ll refer to as Copilot for short. It’s the big dog in the AI coding assistant world. People even call it a “killer application” of generative AI. This is primarily due to its advanced code suggestion, creation, and explanation capabilities, which significantly enhance developer productivity. By understanding and predicting coding patterns based on context, it not only saves time but also assists in writing more efficient and error-free code.

In this chapter, we’ll dive into the nitty-gritty of Copilot: how much it costs, how to set it up, and all the cool features it offers. And we’ll be real about its downsides too. Plus, I’ll toss in some handy tips to get the most out of it. We’re not stopping there: we’ll also see how Copilot is teaming up with other companies with the Copilot Partner Program. This chapter is all about seeing the full scope of what Copilot can do.

GitHub Copilot

Back in June 2021, Microsoft unveiled GitHub Copilot. GitHub’s CEO, Nat Friedman, described it in a blog post as an “AI pair programmer,” designed to speed up developers’ work by assisting them in completing tasks more efficiently.

Copilot is the result of a partnership between Microsoft and OpenAI. Initially, it harnessed a large language model named Codex, a variant of GPT-3 tailored for coding. Microsoft enhanced this LLM’s capabilities by integrating a new approach known as fill-in-the-middle (FIM), which better understands code context. This improved the quality of code suggestions and shortened response times. Powered by the more advanced GPT-3.5 Turbo, this enhancement led to a noticeable uptick in the acceptance rate of code suggestions. To improve security, the tool incorporated an AI system to instantly block risky coding patterns, focusing on vulnerabilities such as hardcoded credentials and SQL injections.

Pricing and Versions

A 30-day free trial of Copilot is available. Once that’s up, you’ve got three choices.

Copilot for Individuals

This plan costs $10 a month or $100 for an annual subscription. It offers features like multi-line function suggestions that can make your coding workflow smoother. A standout feature is its ability to accelerate test generation, a key factor in making sure your code is reliable and solid. Plus, it’s got a feature to screen out vulnerable coding patterns. It also prevents any suggestions that might mirror public code, ensuring that what you create is unique and original.

Copilot for Business

This package is more comprehensive and costs $19 per user per month. It includes everything from the Copilot for Individuals plan and some extra perks designed for businesses. It streamlines license management, which is a big plus for companies in handling their subscriptions and access. There’s also a feature for managing policies across the organization, helping to ensure uniformity in governance and consistency in all projects. A key highlight is its top-notch privacy safeguards, which are crucial for keeping sensitive business data secure. Additionally, it supports corporate proxies, guaranteeing secure and smooth connectivity throughout the company’s network.

GitHub Copilot Enterprise

This comes with a fee of $39 per month per user. The Enterprise plan’s key feature is that it allows for training the system on internal codebases. The Enterprise version bumps up the efficiency of developers, because the tool starts giving more spot-on code suggestions. It helps push for adopting better practices and sticking to security rules. It’s tailored to fit the way developers code internally and their favorite APIs, frameworks, and packages.

Another advantage of the Enterprise version is that you can train it on classic languages like COBOL and Fortran. General-purpose LLMs might not be as effective with these because there’s a lack of training data from public repositories. Of course, they wouldn’t have any clue about proprietary languages specific to your company.

The GitHub system also strengthens the model for the Enterprise edition by continuously scanning a company’s repositories. For instance, it zeroes in on recent pull requests and merges as well as thumbs-up and thumbs-down feedback. All this helps to spotlight the latest methods and strategies a company is using.

Having custom models helps spread know-how across the organization. The AI picks up and shares the subtle knowledge tucked away in the code. With ongoing training, the AI keeps pace with the changing codebase, making its help even more precise as time goes on. Still, it’s really important for organizations to handle the privacy and intellectual property risks that come with mixing these AI tools into their development process.

Note

In 2023, Gartner’s research showed that less than 10% of large enterprises had started using AI-assisted programming tools. This hesitancy is partly due to worries about security and precision. However, with the rapid advancements in the technology, it’s expected that more and more businesses will start adopting these tools in the near future. In short, the benefits they offer are just too significant to overlook.

Use Case: Programming Hardware

An interesting case study of custom models concerns Advanced Micro Devices (AMD). Founded in 1969, the company is a pioneer of CPUs (central processing units). Today the company is a leader in semiconductors for data centers, embedded systems, gaming platforms, and PCs.

Before diving in, let’s set the stage by reviewing some basics about developing for hardware systems. It’s a whole different ballgame than building software for something like a web app. The key challenge is that developers need to know the hardware system inside out. Unlike regular software, which works on all-purpose computers, firmware talks directly to the hardware. This demands a kind of precision and compatibility that’s much more exacting.

This level of precision is critical because mistakes in firmware development can lead to some seriously expensive consequences. Just one error might mean a financial hit in the millions. And it’s not just about the money—time is a huge factor too. Fixing a firmware issue usually involves revisiting the manufacturing process, and that can add months to the timeline. Such a delay affects not just the release schedule but also how competitive the product is in the market.

Clearly, the “move fast and break things” mindset common in Agile software development doesn’t work in this setting. The risks are too big for this approach. That’s why firmware developers have to put considerable time and effort into making detailed plans and doing extensive testing. This careful approach makes sure the firmware is as solid and error-free as it can be before it gets paired with the hardware.

When AMD took a look at Copilot in 2023, it set really high standards, and there was a fair amount of understandable skepticism. In a pilot project, AMD created a custom version of Copilot for various hardware description languages (HDLs) like Verilog and SystemVerilog. HDLs are specific types of programming languages tailored for outlining the architecture, design, and function of electronic circuits, especially the digital logic ones. They’re crucial for modeling and simulating electronic systems at different levels of abstraction.

The results of the pilot turned out much better than anticipated. Surprisingly, the style of the code generated by Copilot actually aligned more with AMD’s standards than what their own programmers were producing. The improvement was so significant that some of the programmers even switched from using Vim, a highly customizable text editor, to adopting Visual Studio Code as their IDE.

Use Case: Shopify

Another interesting case study is Shopify. This company runs a platform that lets customers set up ecommerce websites. Shopify has about 10% of the market in the United States and 6% in Europe.

No doubt, this has created a need for a massive infrastructure. Consider that there are around 300 public repositories and about 5,000 private ones. Plus, Shopify is doing about 1,500 deployments to their code every day.

Shopify was one of the first companies to jump on the Copilot bandwagon, and it’s been a game-changer for making developers more productive. Currently, close to 2,000 Shopify developers use the tool. And here’s the cool part: 70% of them say it’s helpful, and 75% use it a lot. About 26% of the code suggestions from Copilot are accepted.

Sure, there are a few features that haven’t caught on, like the integration with the command-line interface (CLI). But despite this, many developers use code completion and chat on a daily basis.

Here are some interesting takeaways:

Value of code suggestions

Even if a developer doesn’t use a given suggestion, it’s not a total loss. Any suggestion can spark ideas for writing even better code.

Rate of adoption

Usage usually picks up as time goes on, which should not be surprising. It takes time to tweak daily workflows and get used to new features. There’s also a learning curve with Copilot.

Uptake by senior developers

In the early days of adoption of Copilot, the more experienced developers weren’t too keen on using it. They tended to view it as more of a toy than a serious tool. But as time went on and they noticed other developers getting real results, they started to warm up to it.

Learning enhancement

Shopify noticed that Copilot is good at nudging people to try out a new language or framework. For example, there was a noticeable uptick in adoption of Rust.

About a million lines of the Shopify codebase has been written using this tool, showing that Copilot is a very big deal for this business.

Use Case: Accenture

Accenture is a massive professional services organization that helps clients improve operations and growth through innovative technologies and systems. The company has over 733,000 employees in more than 120 countries.

In 2023, Accenture tested Copilot with 450 of its internal developers. The firm didn’t set any specific tasks or objectives. Instead, managers just asked everyone to go about their work as they normally would.

The trial with Copilot at Accenture spanned six months. So, what happened? In terms of coding, there was a 35% rate of acceptance of Copilot’s suggestions, with 88% of those changes sticking even after code reviews. Productivity saw a remarkable boost, too. There was a 50% increase in pull requests and a 15% rise in the merge rate. Efficiency also made a big leap forward, with 50% more builds and a 45% increase in their success rate. And the developers? They were really happy with it: a whopping 96% felt they were successful from day one.

Upon seeing these results, Accenture made the call to roll out Copilot globally to its 50,000 developers.

Security

Gartner surveyed more than 2,000 chief information officers (CIOs) and discovered that 66% of them plan to invest the most resources in cyber- and information security in 2023. This trend has been consistent for years.

At a time when cyber threats are becoming more complex and widespread, a security breach could mean big financial losses, harm to reputation, legal troubles, and disruptions in operations. Plus, with data privacy regulations getting stricter, CIOs have to be vigilant about compliance to dodge fines and keep customer trust.

That’s why for GitHub, security is a major focus of its Copilot program. GitHub has developed a system based on LLMs that spots and fixes insecure coding patterns right as they happen.

Then there are also GitHub’s Advanced Security features assisted by Copilot. Here are the main ones:

Code scanning

In real time, Advanced Security will search for security vulnerabilities and coding errors.

Secret scanning

The product can root out secrets like keys and tokens that have been checked into private repositories.

Dependency review

This shows the implications of dependency changes. It also provides details of vulnerabilities when you merge a pull request.

Note

For students, teachers, and maintainers of popular open source projects, Copilot is free, but there is a verification process.

Getting Started

To get started with Copilot, you first need to create a GitHub account. GitHub is an online service that supports version control and collaborative software development. It’s built on Git, a tool that offers code review and project management features.

After setting up your account, click on your profile photo at the top right of the screen. You’ll see a drop-down menu, as shown in Figure 4-1.

Select Copilot and then click Enable GitHub Copilot. You can select the type of plan and then click Continue. You’ll provide your payment details, assuming you do not qualify for a free account.

Figure 4-1. Begin enablement of Copilot from the profile photo drop-down menu

Codespaces and Visual Studio Code

There are two ways to use Copilot. One way is to access it through Codespaces, a cloud-based development environment that runs on Visual Studio Code (VS Code) and is available directly on https://github.com.

Alternatively, you can opt for the Copilot extension if you’re using the desktop version of Visual Studio Code. In this book, we’re going to focus on using the VS Code extension.

The IDE itself is free. Figure 4-2 shows the main screen of VS Code. You can run this system on various platforms, including Windows (versions 7, 8, 10, and 11); macOS; and various Linux distributions like Ubuntu, Debian, Fedora, and more.

Figure 4-2. The main screen for Visual Studio Code includes an Activity Bar and an area to generate and display code

On the left is the Activity Bar, which is a stack of icons. You can use these for loading files and folders. You can also select the fifth icon, which is the group of squares, to go to the area to install extensions (Figure 4-3).

If you enter “GitHub Copilot” in the search box, you’ll see a list of extensions. Select the top one and click Install.

Then look at the bottom right of the screen. If you see the Copilot icon, then you have the service available.

Figure 4-3. You can install extensions in VS Code, such as for Copilot

In the middle of the screen, you will see the code to print “Hello, Copilot!” This was created by using the following prompt, which Copilot turned into code:

Prompt: # write a “Hello, Copilot!” program

The # character specifies a comment. This is one of the ways to instruct Copilot to generate code.

Notice that the code is in Python. Why so? The extension for the file is py. Copilot uses the file extension to determine what language to use.

At the top right of the screen, there is an icon to run the program. Click it, and VS Code will launch the terminal. You will then see the message printed out.

Suggestions

Getting started with Copilot in VS Code is straightforward. As you begin typing your code, the tool kicks in, offering you code suggestions and generating code snippets based on your input.

Suppose you type the header for a function in Python:

def find_factorial(number):

Copilot will promptly suggest a complete function body, as you can see in Figure 4-4. This generated coding is called ghost text and is highlighted in gray.

Figure 4-4. Copilot suggests code when a user writes a function header

Keep in mind that the code might look a bit different on your machine, and that’s normal since the underlying LLM works according to a complex set of probabilities.

But here’s the thing: GitHub gets that you’re trying to write a function for calculating a factorial based on a parameter. So, it suggests the If–Then structure you need to get the right result.

You can hit Tab to accept the code suggestion. But if it’s not what you’re looking for, just press the Escape key to skip it.

If you hover over the generated code, you will see some more options, as shown in Figure 4-5.

Figure 4-5. Hovering over the generated code evokes more options, visible at the upper right

You can click Accept to accept the code suggestion, just like pressing the Tab key, or you can select Accept Word to select one word in the code at a time. If you click the three dots, you will get a menu for either accepting a line or to always show the toolbar.

Clicking on the 1/2 on the left lets you see alternatives for the generated code; in Figure 4-5, there are two alternatives, and the first one is currently showing. Clicking “>” would take you to version 2. Then clicking “<” would take you back to version 1.

You can also use the shortcut keys listed in Table 4-1 to navigate the options.

Table 4-1. Shortcut keys for scrolling through code suggestions
OS Next suggestion Previous suggestion
macOS Option (⌥) or Alt+] Option (⌥) or Alt+[
Windows Alt+] Alt+[
Linux Alt+] Alt+[

If you press Ctrl+Enter, VS Code will create a new tab to show 10 alternative solutions (Figure 4-6).

Figure 4-6. Pressing Ctrl+Enter brings up a separate tab displaying 10 different versions of the code

To select one of the alternatives, click Accept Solution above that code. Otherwise, close the tab to reject all of them.

Comments

Earlier, in “Codespaces and Visual Studio Code”, we looked at how you can prompt Copilot to generate code using one or more comments. To do this, you indicate the comment in the syntax of the programming language you’re working in.

But you can do more. You can use comments to interact with the system as if it were ChatGPT. Let’s check out an example:

Prompt: # q: What is a class in object-oriented programming?

Copilot: # a: A class is a blueprint for creating objects. An object has properties and methods (functions) associated with it. Almost everything in Python is an object.

You can see the answer is also in the context of the language you are coding in. Now say you ask Copilot about how the system works:

Prompt: # q: What is the shortcut for accepting an inline suggestion?

Copilot: # a: tab

You can ask Copilot about features of VS Code:

Prompt: # q: What is the shortcut for the user settings in VS Code?

Copilot: # a: Ctrl + ,

You can also ask it about shortcut keys for other IDEs:

Prompt: # q: What is the shortcut for a new file in the Atom editor?

Copilot: # a: ctrl + n

Note that Copilot knows I’m on a Windows system and provides the code based on this platform.

Chat

Copilot Chat is like ChatGPT, but for your coding environment. It’s handy for chatting about code, such as when you need to figure out syntax, squash bugs, sort out test cases, or just get your head around different programming ideas. It’s powered by OpenAI’s GPT-4 and GPT-3.5 Turbo, plus some of Microsoft’s own LLMs.

You can dive into Chat right from https://github.com. It’s great for analyzing code, handling pull requests, peeking at documentation, or just asking whatever coding questions you’ve got. Plus, Chat is available on the GitHub apps for both iOS and Android, so you can take it with you on the go.

But right now, let’s focus on how this works with VS Code. This means adding a new extension. Just look up “GitHub Copilot Chat.” Once you install it, you’ll see a new icon pop up on the left side of your screen. It’s got two little chat bubbles, as you can see in Figure 4-7.

Figure 4-7. An icon for Chat has appeared on the left of the screen

At the top of the Chat panel, you see a group of icons in a menu. The first, consisting of horizontal lines and a small x, clears all your chat threads. This is important due to the way context works. When you type in a prompt, the LLM checks out the conversation history. If your chat’s all over the place topic-wise, you might end up with some less-than-helpful answers. That’s why it’s a good idea to keep each chat focused on a particular topic and start new chats for new topics.

The next icon, with a person and a chat bubble, allows you to send feedback about the system. Then there is the history button, a clock, which shows the prior threads.

If you click the three dots, you can use the chat inside the editor, where there is a lot more room for your chats.

At the bottom of the Chat box, there is an input area to ask a question of the system.

The Chat system has numerous shortcuts. One is @workspace, which refers to the files open in your workspace. For example, if you have a calculator program in Python, you can enter:

Prompt: @workspace /explain

Figure 4-8 shows that Chat has analyzed the program file and provided an in-depth explanation.

Figure 4-8. Chat can be used to explain code, for example, a calculator program

You can ask Chat to explain just a part of the code. Highlight the section you’re interested in and press the right button on your mouse. This will bring up a menu (Figure 4-9).

Figure 4-9. Focus the chat on a segment of your code by highlighting it and pressing the right mouse button

When you’ve told Chat to focus on just part of the code, you can not only explain the code but also fix it, generate documentation, or create tests.

Another helpful shortcut is the /new command. It will create a scaffold for a new project or feature. Here are some examples:

Prompt: /new Create a Python script to scrape data from a website

Prompt: /new Generate a Java class for a student with fields for name, age, and grade

Prompt: /new Build a simple REST API using Flask

Prompt: /new Create a JavaScript function to sort an array of numbers in ascending order

Prompt: /new Design a SQL database schema for a blog, with tables for users, posts, and comments

In response to these prompts, the Chat system will show the main steps and then provide the code listing. You also have several options for what to do with it. As you can see in Figure 4-10, there is a menu bar at the top.

Figure 4-10. At the top of the code listing for Chat, there are options for what to do with the code

The first icon will copy the code, and the next one will insert it where the cursor is in the file (you can also use Ctrl+Enter). If you select the three dots, you can insert the code into a new file or the terminal.

There’s also a shortcut for creating a new Jupyter Notebook: /newNotebook. You should specify what you want the notebook to do. Here are some sample prompts:

Prompt: /newNotebook Create a notebook to visualize data from a csv file using matplotlib

Prompt: /newNotebook Create a notebook to preprocess and clean a dataset for machine learning

Prompt: /newNotebook Create a notebook to implement a simple linear regression model using scikit-learn

Prompt: /newNotebook Create a notebook to analyze time series data

Prompt: /newNotebook Create a notebook to scrape data from a website and analyze it

Chat also provides a /terminal shortcut. You can ask questions about or get help with terminal or command line operations, such as with navigating directories, running scripts, or installing packages. Here are some prompts:

Prompt: /terminal What is the command to list all environment variables?

Prompt: /terminal How do I use grep to find specific text in multiple files?

Prompt: /terminal How can I use the find command to search for files by their permissions?

Prompt: /terminal How do I redirect the output of a command to a file?

Prompt: /terminal How do I use awk to process text files?

Then there is @vscode, which is called an agent. With this, you can ask questions or get help about some aspects of VS Code such as a feature, navigation, configuration, or an extension.

Here are some sample prompts:

Prompt: @vscode How do I split the editor into multiple windows?

Prompt: @vscode How can I customize my keyboard shortcuts?

Prompt: @vscode How do I set up a multi-root workspace?

Prompt: @vscode How do I configure task automation in VS Code?

Prompt: @vscode How can I set up and use Docker with VS Code?

Finally, there is the /api command. You can ask questions about or get help with aspects of API development or usage including creation, testing, or integration. Here are some examples:

Prompt: /api How do I make a POST request with JSON data in Node.js?

Prompt: /api How can I handle CORS issues in an Express.js API?

Prompt: /api How do I authenticate a user in a Django REST API?

Prompt: /api How can I paginate results in a Rails API?

Prompt: /api How do I version an API in ASP.NET Core?

You can also use /api in the context of VS Code:

Prompt: @vscode /api How do I create a new command in VS Code extension?

Prompt: @vscode /api How can I read and write settings in VS Code extension?

Prompt: @vscode /api How do I create a new webview panel in VS Code extension?

Prompt: @vscode /api How can I interact with the file system in VS Code Extension?

Prompt: @vscode /api How do I add a new item to the activity bar in VS Code Extension?

Note that if you want to clear the chat, you can use /clear.

Inline Chat

You can use the Chat feature inside your code listing. You will highlight code and then press Ctrl+I for Windows or Cmd+I for macOS. You’ll then see a popup, such as in Figure 4-11.

Figure 4-11. The Inline Chat feature allows you to chat within your code

According to GitHub, Inline Chat has become a popular feature. After all, it allows a developer to stay within their “flow.”

You can use the shortcuts—which we described earlier—to ask questions about the code. The explanation will show up on the Chat panel.

Notice that there is also a orange sparkle icon next to the code. If you click it, you will get a few options—to fix the code, explain it, or rewrite it—which you can see in Figure 4-12.

Figure 4-12. Pressing the orange sparkle icon allows you to interact directly with the code

Let’s try an example using Inline Chat. We’ll use this prompt:

Prompt: Create a function for a bubble sort

Chat creates code for this. But suppose we want to see if there is a faster type of sort. Highlight the function and execute Inline Chat. Figure 4-13 shows the response.

Figure 4-13. Inline Chat has responded to a request to create a faster sort than the bubble sort on the top

Chat suggests a quick sort and shows this in “diff mode.” This allows for comparing and contrasting the two approaches. You can either accept the new code in its entirety or just parts of it.

Open Tabs

When you’re working on a standard software development project, your IDE is usually packed with a bunch of files, each playing its own part in the app’s framework. Take a web project using React as an example. You’re going to see .jsx and .js files for the components, along with HTML and CSS files, not to mention .json and .env files too.

What does this mean to Copilot? It’s a big help. Copilot gets better the more it understands the context of your work. This means it takes into account everything in the current file you’re working on. As for the other open files, the LLM doesn’t scan them all. It zeroes in on the parts that are relevant to what you’re doing. Also, Copilot does not look at the rest of the files in your project, likely to respect your privacy.

Command-Line Interface

You can use Copilot in your CLI. Copilot’s two main functions there are to explain commands and to suggest them.

To do this, you need to install the GitHub CLI and then log in to your GitHub account:

gh auth login

Next, you will install Copilot:

gh extension install github/gh-copilot

You can upgrade the extension:

gh extension upgrade gh-copilot

Here’s an example of asking Copilot to explain a CLI command:

Prompt: gh copilot explain xcopy

Figure 4-14 shows the output.

Figure 4-14. Copilot explains the xcopy command in a CLI

Here’s how to get Copilot to suggest a command:

Prompt: gh copilot suggest

Next, Copilot will ask what type of command you want help with—a generic shell command, a gh command, or a git command—so specify this. Then it will ask what you would like the command to do. Here’s an example:

Prompt: What are the commands for viewing and setting environment variables in the system?

Figure 4-15 shows Copilot’s response, which includes suggestions for commands to use. You can copy this or ask Copilot for more help, such as to explain the command or revise it.

Figure 4-15. Copilot’s suggest feature in the CLI offers commands according to the parameters you provide

Copilot Partner Program

Developers often spice up their apps and get more out of their data by using third-party tools. Take Splunk, for example. It’s a favorite for digging into logs and data analytics. Developers lean on Splunk to keep an eye on their apps and fix problems fast, thanks to its powerful search and report features. Then there’s New Relic, which is all about making sure your app runs smoothly. It gives you real-time insights and diagnostics, helping you spot and fix performance issues and make your app run better for a great user experience. And let’s not forget Datadog. It’s a key player for monitoring both your infrastructure and applications. With its wide range of integrations, Datadog lets developers gather, mix, and show off data from all sorts of places, helping them make smart choices about how to boost performance and manage resources.

Considering the importance of these systems, GitHub has introduced the Copilot Partner Program. It features an expanding lineup of popular integration platforms, all accessible through plugins.

One of the early partners in this program is DataStax, the company behind Astra DB, a vector database designed for crafting advanced AI applications. Thanks to a plugin, developers can speed up database creation using Chat. For instance, Chat offers code suggestions for a database’s structure and metadata, all in line with coding standards. This leads to code that’s neater and easier to maintain.

To ask a question, use the @datastax tag. Some sample prompts include:

Prompt: Tell me about my chat_prod database

Prompt: Tell me about the schema of a table

Prompt: Write a SQL query to read from the chat table

Note

Mary-Brenda Akoda, an AI researcher with a patent in the field of AI for ophthalmology, is an avid user of Copilot. She says, “One time, I wrote code for an entire model development process in under an hour using Copilot. This was a task that would typically have demanded a lot more of my time and effort. Interestingly, it is actually when I had to use an online IDE that didn’t support Copilot that I realized how tedious and slow the development process can be.”

Conclusion

Copilot has quickly become a must-have AI tool for coding, greatly enhancing developer productivity and code quality. This chapter has given you a rundown of its key features, including its ability to suggest complex code blocks, its solid security measures, its impressive performance, and its ease of use in VS Code. Features like Chat and Inline Chat in Copilot make it possible to interact with the AI as you’re in the middle of coding. Although it’s still a tool that needs human guidance, Copilot is definitely a peek into the future of AI-driven coding.

Get AI-Assisted Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.