GitHub Copilot ➜ OpenAI API proxy. Serverless

Demo

Demo Here.

For what

GitHub Copilot available free of charge for confirmed students, teachers and maintainers popular open source projects.

Even if you do not meet the above criteria, the cost of a paid subscription
(Individual) is more affordable than a similar offering from OpenAI.
It costs only $10 and still provides access to GPT-4.

If the above options do not suit you, try a completely free alternative: openai-gemini.

For what

GitHub Copilot chat is available exclusively in select IDEs.

Project openai-github-copilot allows it to be used with a much wider range of tools: it provides a common OpenAI-compatible API that can be deployed for free.
(However, a subscription to GitHub Copilot is still required.)

Serverless?

Although the proxy runs in the cloud, it does not require server maintenance to function.
It can be easily deployed on Cloudflare for free (with generous
restrictionssuitable for personal use).

Running a proxy locally is also possible, although this option is more convenient for development.

Where to begin

First you need to create an account on Cloudflare.

The easiest way to deploy is to manually paste the content src/worker.mjs
as index.js on https://workers.cloudflare.com/playground (see button Deploy).

Note:

This is a slightly simplified version of the code that does not implement token caching,
and the page is missing /token.
To deploy a fully functional version, see the next section.

Advanced Instructions

Important:

If your Cloudflare account has just been created and you don't have any Workers yet, some of the links mentioned below may not be available.
Therefore, it is recommended to create at least a trial “Hello World” Worker: Deploy “Hello World” script.
You can call it “copilot”.

Deploy to Cloudflare Workers

After clicking this button, you will be taken through the process of forking the repository on Github, and then you will be required to enter your Cloudflare account details – id and an access token (you will need to create it).
These steps are required for continuous integration (CI).

Attention:
The initial deployment should fail because it requires KV namespacewhich does not exist yet.

Switch to Workers & Pages / KV and create namespace With name “copilot-KV”.
Then copy it ID and change the file wrangler.toml in your repository fork, replacing the existing one id to the copied value.

Once you publish these changes (in the web interface or via git push), CI will automatically deploy the repository to Cloudflare Workers, this time without any problems.

Manual deployment

Instead of “expand button” you can also work with the project locally using

cli:

  • Create KV namespace: wrangler kv:namespace create KV
  • Start the service locally: wrangler dev
  • Deploy to Cloudflare: wrangler deploy

How to use

If you open your newly created proxy in a browser, you will only see the message 404 Not Found. This is expected since the API is not intended to be directly accessed through a browser.

To use it you should enter API address And Github Copilot access token into the appropriate fields in your software settings.

Not all programs allow you to set a non-standard OpenAI API address, but many do (although these settings can sometimes be deeply hidden).

API base/address/URL/…

Typically, you need to specify the API base in this format:

https://copilot.YOURSUBDOMAIN.workers.dev/v1

However, some programs may expect an address without ending /v1:

https://copilot.YOURSUBDOMAIN.workers.dev

The corresponding field may be designated as “OpenAI proxy“.
You may need to search under “Advanced settings” or similar.
Or in some config file (it’s worth studying the details in the documentation).

Some command line utilities may require you to set an environment variable, for example:

set OPENAI_BASE_URL=https://copilot.YOURSUBDOMAIN.workers.dev/v1

… or:

set OPENAI_API_BASE=https://copilot.YOURSUBDOMAIN.workers.dev/v1

API Key

First of all you need to get access token Github with Copilot support (which should start with ghu_or with gho_).
Once deployed, this project provides a web page /tokenwhich helps create such a token (see demo).
Also in the section scripts You can also find alternative methods for obtaining a token.

Enter this token in the “OpenAI API key” in your program settings.
Alternatively, it could be in some kind of config file (it's worth looking in the documentation for details).

Some command line utilities may require you to set an environment variable, for example:

set OPENAI_API_KEY=ghu_...

Technical Notes

Copilot provides the following endpoints:

  • /v1/chat/completions

    • tools and other associated properties are ignored.
    • The output is processed to eliminate differences from OpenAI.
      Property model always equal to “gpt-4”.
  • /v1/embeddings

    • input:
      only supports array of strings
    • encoding_format, dimensions: Ignored.
    • The output is not modified in any way and has some minor differences from OpenAI:
    • missing property object ("object": "embedding")
    • missing property model ("model": "text-embedding-3-small")
  • /v1/models: Copilot does not provide a list of models, so it was determined empirically and this API endpoint is served by itself openai-github-copilot.
  • /token: Displays a web page that allows you to obtain an access token for Github Copilot.

If you have any questions, I will be happy to answer in the comments.

Enjoy using it!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *