This Python project implements a proxy server that forwards JSON requests to the Azure OpenAI API, handling Azure AD authentication and providing an interactive API documentation UI via Swagger.
- Python 3.8 or newer
- An Azure subscription with an OpenAI resource deployed
- Valid Azure AD credentials with permissions to access the OpenAI resource
-
Clone the repository:
git clone https://github.com/yourusername/azure_oai_proxy.git cd azure_oai_proxy -
Create and activate a virtual environment (optional but recommended):
python3 -m venv .venv source .venv/bin/activate -
Install the Python dependencies:
pip install --upgrade pip pip install -r requirements.txt
- Copy the example environment file and set your credentials:
cp .env-example .env
- Edit
.envand populate the following variables:AZURE_OPENAI_ENDPOINT=https://<your-azure-openai-endpoint>.openai.azure.com/ AZURE_TENANT_ID=<your-azure-tenant-id> AZURE_CLIENT_ID=<your-azure-client-id> AZURE_CLIENT_SECRET=<your-azure-client-secret> PORT=8899
Start the FastAPI server with Uvicorn:
uvicorn main:app --host 0.0.0.0 --port ${PORT:-8899} --reloadThe proxy will listen on http://localhost:8899 by default.
Open your browser and navigate to:
- Swagger UI:
http://localhost:8899/docs - ReDoc:
http://localhost:8899/redoc
AZURE_TENANT_ID: Azure AD tenant ID.AZURE_CLIENT_ID: Service principal client ID.AZURE_CLIENT_SECRET: Service principal client secret.AZURE_OPENAI_ENDPOINT: Base URL of your Azure OpenAI resource.PORT: (Optional) HTTP port for Uvicorn, default is8899.
-
GET /check: A simple endpoint to check if the proxy is working.- Request: None.
- Response: JSON object with status and response message.
-
POST /v1/chat/completions: Forward a JSON body to the Azure OpenAI chat completion API with optional streaming support.- Request: JSON body with parameters compliant with the Azure OpenAI specification, including messages, model, temperature, max_tokens, etc.
- Response: JSON response from Azure OpenAI, with streaming support if requested.
-
Example of VSCode Cline
- The API key can be any text
-
Open AI URL of VSWizard[https://github.com/dqj1998/VSWizard.git]
Licensed under the MIT License. See LICENSE for details.
