5

I am currently exploring GitHub Copilot, and I am interested in using it programmatically, i.e., invoking it from code. As I understand, GitHub Copilot is an IDE plugin, which makes me wonder how it can be automated or controlled programmatically. We know Copilot uses OpenAI models behind the scene as an LLM.

GitHub Copilot does not provide API access to control it programmatically.

Clarification:

  • It's important to note that the plugin, once downloaded and installed, completes my code automatically. Copilot used the OpenAI models such as gpt-3.5 or gpt-4 behind the scene. I know very well the OpenAI chat or text completion models.So that's not my question

  • My question is how to capture the top three suggestions provided by Copilot in an automated fashion.

  • For example, for any given autocomplete task to Copilot, the task is to record the code suggestions and save them into a file.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Exploring
  • 2,493
  • 11
  • 56
  • 97
  • How do you mean? Once you download it, it auto-complete's your code. Is it not doing that for you? I guess, please expand your question for clarity. Thank you. – jsibs Jul 25 '23 at 16:30
  • Did you look at the vim plugin for copilot at https://github.com/github/copilot.vim ? This appears to be calling an LSP through a JSON-RPC api. The code isn't exactly easy to understand, but this would be a decent start, I believe. – Ingo Aug 09 '23 at 15:35
  • @Ingo the most promising lines are [this](https://github.com/TerminalFi/LSP-copilot/blob/78c3f492f63ccfa755b1c7716f548333cfd0e1cc/plugin/plugin.py#L285) and [this](https://github.com/TerminalFi/LSP-copilot/blob/78c3f492f63ccfa755b1c7716f548333cfd0e1cc/plugin/utils.py#L178). I'm really lost in this so sharing to see if anybody can figure this stuff out. :^) – doneforaiur Aug 09 '23 at 15:49
  • @lngo the reference you have given is a community implementation utilizing the Codex/GPT API. My question is, how can I capture the top three suggestions offered by Copilot? – Exploring Aug 10 '23 at 14:39

1 Answers1

1

GitHub, doesn't publish their APIs publicly, as of yet.

However, it was speculated that GitHub Copilot used OpenAI's Codex (which is now deprecated). According to this, you can use OpenAI's chat models for code completion, suggestion, etc. Though from my experience, the response time varies. Also, there's no guarante that it will output only code.

Check example below;

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {
     "role": "system",
     "content": "You are a helpful assistant. Assistant will output only and only code as a response."
    },
    {
      "role": "user",
      "content": "Write a Python function that takes as input a file path to an image, loads the image into memory as a numpy array, then crops the rows and columns around the perimeter if they are darker than a threshold value. Use the mean value of rows and columns to decide if they should be marked for deletion."
    }
  ],
  temperature=0,
  max_tokens=1024
)

Which will output;

import numpy as np
from PIL import Image

def crop_dark_borders(image_path, threshold):
    # Load the image
    image = Image.open(image_path)
    # Convert the image to a numpy array
    image_array = np.array(image)
    
    # Calculate the mean value of each row and column
    row_means = np.mean(image_array, axis=1)
    col_means = np.mean(image_array, axis=0)

    ...

Edit; On a second thought, I wouldn't use ChatCompletion for this because the task is not chat based at all. Instead I would use Completion and supply the whole code file to it as an input. This has it's own limitations too. For example you wouldn't be able to provide to the model what's after the cursor.

doneforaiur
  • 1,308
  • 7
  • 14
  • 21
  • However, this method doesn't capture the code completion suggestions that GitHub Copilot provided. The code snippet here merely demonstrates how to invoke OpenAI APIs. My requirement is to record the real-time suggestions provided by the GitHub Copilot plugin. – Exploring Jul 26 '23 at 05:08
  • @Exploring Have you tried running your IDE's requests through a proxy, so that all the copilot API requests are logged? From there you can write your own API wrapper which will get the autocompletions from GitHub. – Mave Jul 26 '23 at 05:26
  • @Mave, was doing just that. Altough that might get you banned pretty quick, the top suggestions are not invoked on every key stroke I think. – doneforaiur Jul 26 '23 at 05:38
  • @Exploring, your requirements are still unclear. Invoking the extension, invoking the API, record the real-time suggestions, these are all different tasks. I'm currently investigating how the API can be accessed, is that okay? – doneforaiur Jul 26 '23 at 05:39
  • @doneforaiur the question is never how to invoke OpenAI API. Its just a simple python API call using OpenAI https://platform.openai.com/docs/api-reference/authentication. – Exploring Jul 26 '23 at 23:55
  • My question is "how to automate Github Copilot and record its autocompletion." – Exploring Jul 26 '23 at 23:56
  • 1
    Did some digging. Capturing packets was a no go for me, couldn't decrypt TLS. So I checked neovim's Copilot support. They mentioned to install Nodejs, that was odd. Then I've found an custom plugin [LSP-Copilot](https://github.com/TerminalFi/LSP-copilot/tree/78c3f492f63ccfa755b1c7716f548333cfd0e1cc) which requieres you to install [LSP](https://github.com/sublimelsp/LSP) which uses JSON-RPC to communicate with the LSP-Copilot (and it's the same thing for neovim too). Skimmed through the source code for LSP-Copilot and it seems promising. Sharing this because somebody also might help. – doneforaiur Jul 27 '23 at 15:24