Inspired by https://github.com/drathier/stack-overflow-import
Project description
Copilot Importer
Why write code when you can import it directly from GitHub Copilot?
What is Copilot Importer?
The copilot
python module will dynamically generate any function imported
by leveraging the GitHub Copilot service.
How do I use Copilot Importer?
You can install copilot-importer via pip (e.g. pip install copilot-importer
).
Additionally and importantly, you need a GitHub Copilot API token. See How do I get an authentication token.
Once you have your token, set it to the environment variable
GITHUB_COPILOT_TOKEN
.
export GITHUB_COPILOT_TOKEN=xxxxxxxxxxxxxxxxxxxx
Finally, before the dynamic importing feature is enabled, you must run the
copilot.install
method.
# Enable copilot importer
from copilot import install
install()
After all of the above has been taken care of, you should be able to import anything you want directly from GitHub Copilot:
>>> from copilot import install
>>> install()
>>> from copilot import base64_encode
>>> base64_encode(b"test")
b'dGVzdA=='
>>> from copilot import base64_decode
>>> base64_decode(base64_encode(b"test"))
b'test'
>>> from copilot import quicksort
>>> quicksort([5,2,3,4])
[2, 3, 4, 5]
You can also output the code of imported functions like so:
>>> from copilot import say_hello
>>> print(say_hello._code)
def say_hello():
print("Hello, World!")
How do I get an authentication token?
To obtain an authentication token to the Copilot API, you will need a GitHub account with access to Copilot.
copilot-importer
has an authentication CLI built-in, which you can use to
fetch your copilot authentication token. To star the authentication process
after installing copilot-importer
, simply run
copilot-auth
OR
python -m copilot
OR
python -c "from copilot.authflow import run; run()"
Once you have started the authentication flow, you will be prompted to enter a device authorization code to https://github.com/login/device.
After entering the correct code, you will be asked to authorize GitHub for VSCode to access your account. This is expected, as the Copilot API is only accessible to the VSCode plugin.
Once approved, you should see a Copilot access token printed to the terminal.
Example:
$ copilot-auth
Initializing a login session...
YOUR DEVICE AUTHORIZATION CODE IS: XXXX-XXXX
Input the code to https://github.com/login/device in order to authenticate.
Polling for login session status until 2021-07-17T17:26:01.618386
Polling for login session status: authorization_pending
Polling for login session status: authorization_pending
Successfully obtained copilot token!
YOUR TOKEN: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
EXPIRES AT: 2021-07-17T21:21:39
You can add the token to your environment e.g. with
export GITHUB_COPILOT_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Credits
- Inspiration taken from stack-overflow-import
- GitHub for providing GitHub Copilot.
- molenzwiebel for working out the Copilot API and helping with the code.
- akx for giving a quick review of the code.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file copilot-import-1.0.0.tar.gz
.
File metadata
- Download URL: copilot-import-1.0.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.8.6 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0709fe886a014e46d0bf3d19838960f1e74236922d566df412ddee6370043b4 |
|
MD5 | 55333d0570cc46a46544c535c190b270 |
|
BLAKE2b-256 | 02fcc1f299335f0ba0391c5046ef49b59686a6fe85c07f319d0681c1f179f49e |
File details
Details for the file copilot_import-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: copilot_import-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.8.6 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50f2d35f6ccf59079f9de9825493b564e22ab7e75db224c52e6498578576a6f5 |
|
MD5 | bd248df1f1165d756551a4e4b20b9659 |
|
BLAKE2b-256 | 131461beb21e4f109861e97c8bee4d25f70d5a80471a34015cfa9d55f4572388 |