GitHub Copilot

Using Artificial Intelligence to Write Code

Matthew DeHaven

February 5, 2025

Course Home Page

What is GitHub Copilot?

  • An AI programmer tool
  • Can be accessed
    • in the browser
    • in Visual Studio Code
    • in other IDEs
  • Provides both code completion and chat features

Documentation (some examples are included in this presentation)

Github Copilot Subscriptions

  • Free (limited number of requests)
  • Pro (unlimited requests, alternative AI models)

The Github Student Developer Pack gives you access to the Pro version of Copilot.

Github Copilot Icon in Status Bar

Tells you that Copilot is active, opens some settings and options.

Ghost Text

  • Shows up in the editor

Accepting Ghost Text

  • Tab
  • Cmd + Right Arrow to accept part of the ghost text

Alternative Suggestions

Hover over the ghost text to see alternative suggestions.

Suggestions from Comments

Copilot will use information in comments to provide suggestions.

Inline Chat

  • Cmd + I to open inline chat

Chat Sidebar

  • Ctrl + Cmd + I to open chat sidebar

Chat Sidebar

Chat sidebar provides more “traditional” interface.

Fixing errors with Copilot

You can see Copilot in other places in VS Code.

Whenever you see the sparkle icon, Copilot can be used to help you.

Fixing errors with Copilot

Click on the sparkle icon to see what Copilot can do.

Fixing errors with Copilot

Copilot will provide suggestions for fixing the error.

Prompts

From Github Copilot documentation, three ‘S’ principle for prompts:

  1. Simple
  2. Specific
  3. Short

Providing Context

You can provide context to Copilot with…

  • @workspace
  • @github
  • @vscode
  • @terminal

Providing References

You can provide specific references to Copilot with…

  • #file
  • #editor
  • #selection i.e. what is highlighted
  • #terminalLastCommand
  • and more

Common Commands

There are some common commands that can be used with…

  • /fix
  • /explain
  • /new
  • /help
  • /clear

Copilot Default References

By default, Copilot uses the following for references

  • currently open files
  • highlighted text
  • comments in the code
  • previously asked questions (in chat)

Descriptive Code

Being descriptive in your code can help Copilot provide better suggestions.

  • Use descriptive variable names
  • Use comments
  • Use descriptive function names

All of these will improve (1) Copilot and (2) your code.

Available AI Models

  • OpenAI GPT 4o (default)
  • OpenAI o1
    • One of the “chain-of-thought” reasoning models, which have a better performance for logic (but require more compute)
  • OpenAI o3-mini
    • A smaller but more efficient chain-of-thought model
  • Claude 3.5 Sonnet
    • Competition with the GPT 4o model

Different models have different use limits (depending on your Github subscription). The details of which are changing quite frequently as new models are developed.

Copilot suggestions matching public code

  • One option you can enable/disable is whether Copilot suggestions should match public code.
  • This can be configured in your GitHub Copilot settings.
  • When enabled, Copilot will use public code repositories to enhance its suggestions, potentially providing more accurate and relevant code snippets.
  • This can, however, be a licensing concern (especially for for-profit projects) and so there is an option to disable this feature.

Custom Instructions

  1. Turn on the option github.copilot.chat.codeGeneration.useInstructionFiles in VS Code settings
  2. Create a file at .github/copilot-instructions.md

GitHub Documentation & Example File Content

We use Bazel for managing our Java dependencies, not Maven, so when talking about Java packages, always give me instructions and code samples that use Bazel.

We always write JavaScript with double quotes and tabs for indentation, so when your responses include JavaScript code, please follow those conventions.

Our team uses Jira for tracking items of work.

GitHub Copilot with R

Regular Expressions (regex)

Regular expressions are a powerful tool for working with text data.

  • matching patterns in strings

This tool exists and works in a similar way in many programming languages.

R Regex

Regular expressions ask for a pattern to be matched in a string.

regexpr(pattern = "a", text = "banana")
[1] 2
attr(,"match.length")
[1] 1
attr(,"index.type")
[1] "chars"
attr(,"useBytes")
[1] TRUE

Instead, we can ask for all of the matches (g for global).,

gregexpr(pattern = "a", text = "banana")
[[1]]
[1] 2 4 6
attr(,"match.length")
[1] 1 1 1
attr(,"index.type")
[1] "chars"
attr(,"useBytes")
[1] TRUE

R grep()

“Global Regular Expression Print”

This function will return the words with a positive match.

words <- c("apple", "banana", "cherry")

grep(pattern = "a", x = words, value = TRUE)
[1] "apple"  "banana"

Actually, the default is to return the index of the match.

grep(pattern = "a", x = words)
[1] 1 2

R gsub()

Another common use is to replace pattern matches.

variables <- c("mean_gdp", "median_gdp", "mean_income", "median_income")

gsub(pattern = "_", replacement = " ", x = variables)
[1] "mean gdp"      "median gdp"    "mean income"   "median income"

Regex Patterns

Special characters in regex pattern:

  • . any character
  • ^ start of string
  • $ end of string

Number of pattern to match

  • * zero or more of the preceding character
  • + one or more of the preceding character
  • ? zero or one of the preceding character
  • {n} exactly n (some number) of the preceding character

Regex Patterns

Character classes

  • [abc] a, b, or c
  • [a-z] any lowercase letter
  • [A-Z] any uppercase letter
  • [0-9] any digit
  • [^abc] not a, b, or c

Escape character

  • \ to escape special characters
    • i.e. \^ to match the character ^

Extracting Phone Numbers

text <- "If you have any questions about our services, please do not hesitate to contact our customer support team. You can reach us at (123) 456-7890 for general inquiries. For technical support, please call (987) 654-3210. Additionally, our sales department can be contacted at (555) 123-4567 for any questions related to new purchases or product information. We are here to assist you with any needs you may have."

pattern <- "\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}"

# Extract phone numbers
phone_numbers <- regmatches(text, gregexpr(pattern, text))
phone_numbers
[[1]]
[1] "(123) 456-7890" "(987) 654-3210" "(555) 123-4567"

Look Ahead and Look Behind

If you want to match a pattern but not include part of it in the match, you can use look ahead and look behind.

  • Look ahead (?=...)
  • Look behind (?<=...)

where ... is the pattern to match.

I find this very useful for separating out multi-variable names.

Look Ahead and Look Behind

variables <- c("mean_gdp", "median_income")

la_pattern <- "[a-z]+(?=_)"
lb_pattern <- "(?<=_)[a-z]+"

regmatches(variables, gregexpr(la_pattern, variables, perl = TRUE))
[[1]]
[1] "mean"

[[2]]
[1] "median"
regmatches(variables, gregexpr(lb_pattern, variables, perl = TRUE))
[[1]]
[1] "gdp"

[[2]]
[1] "income"

Saving the Day (from XKCD)

Writing Regular Expressions

  • Regular expressions can be difficult to write
  • Copilot can help you write regular expressions!

Another example

Regular Expressions in R

If you do ever use regex in R, I recommend the stringr package.

We haven’t introduced packages yet, but stringr has intuitive functions for working with text data and regex.

library(stringr)

str_detect(string = "banana", pattern =  "a")
[1] TRUE
str_remove(string = "banana", pattern = "a")
[1] "bnana"
str_replace(string = "banana", pattern = "a", replacement = "o")
[1] "bonana"
str_extract(string = "banana", pattern = "na")
[1] "na"

Copilot and learning R

  • Copilot can help you learn to code
  • It can also replace some knowledge you’ll now never need
    • you probably do not need to memorize regex patterns
  • But you need to understand what you are doing
    • Knowing “how” to program is still important

AI is currently

  • very good at generic coding tasks,
  • but your research will require some non-generic tasks.

Summary

Summary

  • GitHub Copilot in VS Code
    • Code completion
    • Inline Chat
    • Chat Sidebar
  • Copilot and R
    • Regular Expressions

Live Coding Example

  • Copilot in VS Code
    • Writing a for loop or an lapply with Copilot

In Class Exercise

If you can access the online version, try it out!

  • ask a question about regular expressions