A Python wrapper for GitHub Copilot and GitHub Models using the official openai SDK.
Project description
copilot-python
一個使用官方 openai 套件實作的 GitHub Copilot / GitHub Models 包裝庫。
功能
- 直接使用官方
openai.OpenAI - 支援 GitHub OAuth Device Flow 取得 Token
- 支援手動提供 Fine-grained PAT / API Key
- 支援 Windows / macOS / Linux
前置需求
若使用 GitHub Models,常見端點為:
https://models.github.ai/inference- 或你的 OpenAI 相容代理端點
內建 OAuth Client ID:本庫已內建公開的 GitHub OAuth App client_id,可直接使用 OAuth Device Flow,無需額外設定。
安裝本庫
pip install .
或直接安裝依賴:
pip install openai
快速開始
1. 手動提供 API Key / Token
from openai import OpenAI
client = OpenAI(
base_url="https://models.github.ai/inference",
api_key="github_pat_xxxxxxxxx",
)
response = client.chat.completions.create(
messages=[
{"role": "system", "content": "你是一個有用的助手。"},
{"role": "user", "content": "請解釋什麼是量子糾纏。"},
],
model="gpt-4o",
temperature=1,
max_tokens=4096,
top_p=1,
)
print(response.choices[0].message.content)
2. 使用 CLI 命令取得 Token(推薦)
最簡單的方式是使用命令列工具:
copilot-python login
這會開啟瀏覽器進行 GitHub 授權,並輸出 access token。
3. 程式碼中使用 OAuth Device Flow
from openai import OpenAI
from copilot_python import login_device_flow
# 使用內建 client_id,無需額外提供
token = login_device_flow()
client = OpenAI(
base_url="https://models.github.ai/inference",
api_key=token,
)
4. 使用 CopilotClient
from copilot_python import CopilotClient
client = CopilotClient(api_key="github_pat_xxxxxxxxx")
reply = client.ask(
"如何寫一個 HTTP 伺服器?",
model="gpt-4o",
system_prompt="你是一個有用的助手。",
)
print(reply.choices[0].message.content)
建議 Token 類型
常見情境:
- OAuth token:
gho_... - Fine-grained PAT:
github_pat_... - GitHub App user-to-server token:
ghu_... - 視服務而定,可能需要
models或Copilot Requests權限
命令列工具
取得 Token
copilot-python login
這會:
- 顯示 GitHub 裝置授權連結
- 自動開啟瀏覽器(可用
--no-open-browser停用) - 等待授權完成
- 輸出 access token 到 stdout
完整選項:
copilot-python login [--client-id CLIENT_ID] [--scope SCOPE] [--no-open-browser]
主要 API
login_device_flow()
程式碼中走 GitHub OAuth Device Flow,回傳 access token。
from copilot_python import login_device_flow
# 使用內建 client_id
token = login_device_flow()
# 或自訂 client_id 和 scope
token = login_device_flow(
client_id="你的 client_id",
scope="repo user",
)
create_client()
建立已填入 base_url 與 api_key 的官方 OpenAI client。
from copilot_python import create_client
client = create_client(base_url="https://models.github.ai/inference", api_key="...")
ask_copilot()
最方便的單次呼叫入口,回傳官方 SDK 的 completion 物件。
ask_copilot(
prompt,
*,
model,
api_key=None,
token=None,
base_url="https://models.github.ai/inference",
system_prompt=None,
)
CopilotClient.ask()
適合重複呼叫時使用。
例外
CopilotErrorCopilotAuthenticationErrorCopilotHTTPErrorCopilotTimeoutError
注意事項
- 這個專案現在是 基於官方
openaiSDK 呼叫 OpenAI 相容聊天 API。 - 已內建 GitHub OAuth App client_id,可直接使用
copilot-python login或login_device_flow()。 base_url可指向 GitHub Models 或其他 OpenAI 相容端點。- 若手動提供 PAT,請確認 Token 權限符合目標服務需求。
- OAuth Device Flow 的
scope參數取決於你使用的 GitHub OAuth App,內建的 client_id 支援標準 OAuth scopes(如repo,user等)。
相容性
舊命令 copilot-wrapper login 和舊導入 from copilot_wrapper import ... 仍保留向後相容。
測試
python -m unittest discover -s tests -v
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file copilot_python-0.1.0.tar.gz.
File metadata
- Download URL: copilot_python-0.1.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
146b6eb9b2ebca11757d89b93bf66c47e2d835445090e06e3f29df9c4f54e33b
|
|
| MD5 |
5bc47ec076692f7121c1552d869ab05d
|
|
| BLAKE2b-256 |
5defa5e80a76c8c416eeb5f16a904da5de80155638b63cf554fa59f5b31eeae9
|
File details
Details for the file copilot_python-0.1.0-py3-none-any.whl.
File metadata
- Download URL: copilot_python-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eba239e13378ded5cb1cacf480d393cfc1aee939b2908b2177134515d0f42158
|
|
| MD5 |
a293d60ff124385828f1ac1a0dfbdb75
|
|
| BLAKE2b-256 |
d10dd56cf2805d6a48b4154b6796954f6c6e974ccce0e2c554aba7de3ba75539
|