Skip to content

Latest commit

 

History

12 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Azure OpenAI Proxy (FastAPI)

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.

Table of Contents

Prerequisites

  • Python 3.8 or newer
  • An Azure subscription with an OpenAI resource deployed
  • Valid Azure AD credentials with permissions to access the OpenAI resource

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/azure_oai_proxy.git
    cd azure_oai_proxy
  2. Create and activate a virtual environment (optional but recommended):

    python3 -m venv .venv
    source .venv/bin/activate
  3. Install the Python dependencies:

    pip install --upgrade pip
    pip install -r requirements.txt

Configuration

  1. Copy the example environment file and set your credentials:
    cp .env-example .env
  2. Edit .env and 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
    

Usage

Start the FastAPI server with Uvicorn:

uvicorn main:app --host 0.0.0.0 --port ${PORT:-8899} --reload

The proxy will listen on http://localhost:8899 by default.

API Documentation

Open your browser and navigate to:

  • Swagger UI: http://localhost:8899/docs
  • ReDoc: http://localhost:8899/redoc

Environment Variables

  • 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 is 8899.

Endpoints

  • 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.

Client setting

License

Licensed under the MIT License. See LICENSE for details.

About

A proxy from other API calls to Azure OpenAI that using Azure authentication

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages