Skip to content

A FastAPI proxy server that seamlessly turns GitHub Copilot's chat completion capabilities into OpenAI compatible API service.

License

Notifications You must be signed in to change notification settings

yuchanns/copilot-openai-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🤖 Copilot OpenAI API

Python 3.10+ FastAPI Docker License Image Tags Image Size

A FastAPI proxy server that seamlessly turns GitHub Copilot's chat completion capabilities into OpenAI compatible API service.

✨ Key Features

🚀 Advanced Integration

  • Seamless GitHub Copilot chat completion API proxy
  • Real-time streaming response support
  • High-performance request handling

🔐 Security & Reliability

  • Secure authentication middleware
  • Automatic token management and refresh
  • Built-in CORS support for web applications

💻 Universal Compatibility

  • Cross-platform support (Windows and Unix-based systems)
  • Docker containerization ready
  • Flexible deployment options

🚀 Prerequisites

  • Python 3.10+
  • pip (Python package manager)
  • GitHub Copilot subscription
  • GitHub authentication token

📦 Installation

  1. Clone the repository:
git clone https://github.com/yuchanns/copilot-openai-api.git
cd copilot_provider
  1. Install dependencies:
# Install PDM first if you haven't
pip install -U pdm

# Install project dependencies using PDM
pdm install --prod

⚙️ Configuration

  1. Set up environment variables:
# Windows
set COPILOT_TOKEN=your_access_token_here
set COPILOT_SERVER_PORT=9191          # Optional: Server port (default: 9191)
set COPILOT_SERVER_WORKERS=4          # Optional: Number of workers (default: min(CPU_COUNT, 4))

# Unix/Linux/macOS
export COPILOT_TOKEN=your_access_token_here
export COPILOT_SERVER_PORT=9191       # Optional: Server port (default: 9191)
export COPILOT_SERVER_WORKERS=4       # Optional: Number of workers (default: min(CPU_COUNT, 4))

📝 Note:

  • COPILOT_TOKEN: Required for authentication. If not set, a random token will be generated.
  • COPILOT_SERVER_PORT: Optional. Controls which port the server listens on.
  • COPILOT_SERVER_WORKERS: Optional. Controls the number of worker processes.
  1. Configure GitHub Copilot:
    • Windows users: Check %LOCALAPPDATA%/github-copilot/
    • Unix/Linux/macOS users: Check ~/.config/github-copilot/

Required configuration files:

  • apps.json or hosts.json (containing GitHub OAuth token)
  • token.json (will be created automatically)

💡 How to get a valid Github Copilot configuration?

Choose any of these official GitHub Copilot plugins:

After installing and signing in, configuration files will be automatically created in your system's config directory.

🚀 Usage

Choose between local or Docker deployment:

🖥️ Local Run

Start the server with:

pdm dev

🐳 Docker Run

Launch the containerized version:

# Unix/Linux/macOS
docker run --rm -p 9191:9191 \
    -v ~/.config/github-copilot:/home/appuser/.config/github-copilot \
    ghcr.io/yuchanns/copilot-openai-api

# Windows
docker run --rm -p 9191:9191 ^
    -v %LOCALAPPDATA%/github-copilot:/home/appuser/.config/github-copilot ^
    ghcr.io/yuchanns/copilot-openai-api

The Docker setup:

  • Maps port 9191 to your host
  • Mounts your Copilot configuration
  • Provides identical functionality to local deployment

🔄 Making API Requests

Access the chat completion endpoint:

curl -X POST http://localhost:9191/chat/completions \
  -H "Authorization: Bearer your_access_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [{"role": "user", "content": "Hello, Copilot!"}]
  }'

🔌 API Reference

POST /chat/completions

Proxies requests to GitHub Copilot's API.

Required Headers:

  • Authorization: Bearer <your_access_token>
  • Content-Type: application/json

Request Body:

  • Follow GitHub Copilot chat completion API format

Response:

  • Streams responses directly from GitHub Copilot's API

🔒 Authentication

Secure your endpoints:

  1. Set COPILOT_TOKEN in your environment
  2. Include in request headers:
    Authorization: Bearer your_access_token_here
    

⚠️ Error Handling

The server provides clear error responses:

  • 401: Missing/invalid authorization header
  • 403: Invalid access token
  • Other errors are propagated from GitHub Copilot API

🛡️ Security Best Practices

  • Configure CORS for your specific domains (default: *)
  • Safeguard your COPILOT_TOKEN and GitHub OAuth token
  • Built-in token management with concurrent access protection

📄 License

Licensed under the Apache License 2.0 - see the LICENSE file for details.

About

A FastAPI proxy server that seamlessly turns GitHub Copilot's chat completion capabilities into OpenAI compatible API service.

Topics

Resources

License

Stars

Watchers

Forks

Packages