GitHub Copilot API Proxy - A Flask application serving as a proxy server for GitHub Copilot API
Project description
GitHub Copilot API Proxy (ghc-api)
A Python Flask application that serves as a proxy server for GitHub Copilot API, providing OpenAI and Anthropic API compatibility with caching and monitoring capabilities.
Features
- OpenAI API Compatibility:
/v1/chat/completionsendpoint - Anthropic API Compatibility:
/v1/messagesendpoint with automatic request/response translation - Model Listing:
/v1/modelsendpoint listing available models - Model Name Mapping: Translate model names with exact and prefix-based matching
- Token Management: Automatic GitHub Copilot token refresh
- Vision Support: Handle image inputs and enable vision capabilities
- Memory Caching: Cache all requests and responses (up to 1000 entries)
- Web Dashboard: Real-time statistics and request browser
- Request Details: View full request/response bodies with JSON formatting
- Export/Import: Export and import request history as JSON Lines files
- Content Filtering: Remove or add content from system prompts and tool results
Installation
Install the package using pip:
pip install ghc-api
Or install from source:
pip install .
Usage
Start the server with the ghc-api command:
ghc-api
By default, the server will start on http://localhost:8313.
Command Line Options
-p PORTor--port PORT: Specify the port to listen on (default: 8313)-a ADDRESSor--address ADDRESS: Specify the address to listen on (default: localhost)-cor--config: Generate a YAML config file in~/.ghc-api/config.yaml--help: Show help message
Configuration
The application looks for a configuration file at ~/.ghc-api/config.yaml. You can generate this file using:
ghc-api --config
The config file contains:
# Server Settings
address: localhost
port: 8313
debug: false
# GitHub Copilot Account Type
# Options: "individual", "business", "enterprise"
account_type: individual
# Version settings (used to build request headers)
vscode_version: "1.93.0"
api_version: "2025-04-01"
copilot_version: "0.26.7"
# Model Name Mappings
model_mappings:
# Exact match mappings
exact:
opus: claude-opus-4.5
sonnet: claude-sonnet-4.5
haiku: claude-haiku-4.5
# Prefix match mappings
prefix:
claude-sonnet-4-: claude-sonnet-4
claude-opus-4.5-: claude-opus-4.5
# Content Filtering
system_prompt_remove: [] # Strings to remove from system prompts
system_prompt_add: [] # Strings to append to system prompts
tool_result_suffix_remove: [] # Strings to remove from end of tool results
Token Management
The application follows this priority for getting the GitHub token:
GITHUB_TOKENenvironment variable- Token file at
~/.ghc-api/github_token.txt - Interactive GitHub Device Flow authentication
API Endpoints
OpenAI Compatible
POST /v1/chat/completions- Chat completionsPOST /chat/completions- Chat completions (without v1 prefix)GET /v1/models- List available modelsGET /models- List available models (without v1 prefix)
Anthropic Compatible
POST /v1/messages- Messages API (Anthropic format)
Dashboard & Monitoring
GET /- Web dashboard with statisticsGET /requests- Request browser pageGET /api/stats- JSON statistics endpointGET /api/requests- Paginated list of requestsGET /api/requests/search- Full-text search in request/response bodiesGET /api/requests/export- Export all requests as JSON Lines filePOST /api/requests/import- Import requests from JSON Lines fileGET /api/request/<id>- Individual request detailsGET /api/request/<id>/request-body- Request body onlyGET /api/request/<id>/response-body- Response body only
Example Usage
With OpenAI Python SDK
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:8313/v1",
api_key="not-needed" # Token is managed by the proxy
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
With Anthropic Python SDK
import anthropic
client = anthropic.Anthropic(
base_url="http://localhost:8313",
api_key="not-needed" # Token is managed by the proxy
)
message = client.messages.create(
model="claude-sonnet-4",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}]
)
print(message.content[0].text)
With cURL
# Chat completions
curl http://localhost:8313/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Hello!"}]
}'
# List models
curl http://localhost:8313/v1/models
Dashboard
Access the web dashboard at http://localhost:8313/ to:
- View overall statistics (total requests, data transfer)
- See per-model usage statistics
- See per-endpoint analytics
- Browse recent requests
- View detailed request/response bodies
Architecture
- Modular Design: Organized into separate modules for maintainability
main.py- Entry point and configuration loadingapp.py- Flask application factoryconfig.py- Configuration constants and model mappingscache.py- Request caching and statisticstranslator.py- OpenAI/Anthropic format translationstreaming.py- Streaming response handlingtoken_manager.py- GitHub token managementroutes/- API endpoint handlers (openai, anthropic, dashboard)
- Thread-Safe Caching: Uses threading locks for concurrent access
- Memory-Based Storage: No external database dependencies
- RESTful API Design: Follows REST conventions
License
MIT License
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 ghc_api-1.0.6.tar.gz.
File metadata
- Download URL: ghc_api-1.0.6.tar.gz
- Upload date:
- Size: 46.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82223b49b3aeaf13bd90a4dffa2f868537f75f68eae3c64cbcace707596b5e29
|
|
| MD5 |
15f730f2f89a6382851b481e10aa3f64
|
|
| BLAKE2b-256 |
bb8e26601db1881e56eb6017f28b1b129741938bb77071de072f2a3952fe7d7a
|
File details
Details for the file ghc_api-1.0.6-py3-none-any.whl.
File metadata
- Download URL: ghc_api-1.0.6-py3-none-any.whl
- Upload date:
- Size: 52.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0979f5e5d0a5137ed406fe424b31ee8698fef1c2bdd5d637478489bbe4084a99
|
|
| MD5 |
47154e2f3aba448263c5cce0d865382a
|
|
| BLAKE2b-256 |
22f9f5a487615c25a6710df4066a0df60545bdf604c037e55f31540fc2443db2
|