A reverse proxy that translates OpenAI API requests to Azure OpenAI Service, allowing you to use Azure OpenAI models with any OpenAI-compatible client. Available in both Python (Flask) and Node.js (Express) implementations.
The Node.js version supports multiple models and allows you to specify the model in the request body, while the Python version is a simpler implementation that uses a single deployment.
The API will emulate a endpoint of /v1/models, allowing you to list available models and make chat completions requests similar to the OpenAI API.
{
"object": "list",
"data": [
{
"id": "Phi-4",
"object": "model",
"created": 1686935002,
"owned_by": "Microsoft"
},
{
"id": "gpt-4",
"object": "model",
"created": 1686935002,
"owned_by": "openai"
},
{
"id": "gpt-3.5-turbo",
"object": "model",
"created": 1686935002,
"owned_by": "openai"
},
...
]
}Note: When using different models, ensure you set the correct API version in your environment variables or code. The default API version is set to 2024-12-01-preview, which is compatible with most of the latest Azure OpenAI models.
- For Python version: Python 3.7 or higher
- For Node.js version: Node.js 14 or higher
- Azure OpenAI Service subscription
- Azure OpenAI deployment (e.g., GPT-4, GPT-3.5-turbo)
- Clone this repository:
git clone https://github.com/Purestreams/AzureAPI-to-OpenaiAPI.git
cd AzureAPI-to-Openai- Choose your preferred implementation:
Install Python dependencies:
pip install flask flask-cors requestsInstall Node.js dependencies:
npm install express cors axiosYou can configure the proxy using either direct code modification or environment variables (recommended for production).
Set the following environment variables:
export AZURE_OPENAI_ENDPOINT="https://your-resource-name.openai.azure.com"
export AZURE_OPENAI_API_KEY="your_azure_openai_api_key_here"
export AZURE_OPENAI_DEPLOYMENT="your-deployment-name"
export AZURE_OPENAI_API_VERSION="2024-05-01-preview"
export PRIVATE_PASSWORD="your_private_password_here"# Replace these placeholders with your actual values
AZURE_OPENAI_ENDPOINT = "https://your-resource-name.openai.azure.com"
AZURE_OPENAI_API_KEY = "your_azure_openai_api_key_here"
AZURE_OPENAI_DEPLOYMENT = "your-deployment-name" # e.g., "gpt-4"
AZURE_OPENAI_API_VERSION = "2024-05-01-preview"
private_password = "your_private_password_here"The Node.js version uses environment variables by default, but you can modify the fallback values:
const AZURE_OPENAI_ENDPOINT = process.env.AZURE_OPENAI_ENDPOINT || "your-endpoint-here";
const AZURE_OPENAI_API_KEY = process.env.AZURE_OPENAI_API_KEY || "your-key-here";
const AZURE_OPENAI_DEPLOYMENT = process.env.AZURE_OPENAI_DEPLOYMENT || "your-deployment-here";
const AZURE_OPENAI_API_VERSION = process.env.AZURE_OPENAI_API_VERSION || "2024-05-01-preview";
const private_password = process.env.PRIVATE_PASSWORD || "your-password-here";- Endpoint: Found in the Azure portal under your OpenAI resource → "Keys and Endpoint"
- API Key: Also found in "Keys and Endpoint" section
- Deployment Name: The name you gave to your model deployment in Azure OpenAI Studio
- API Version: Use the latest available version (check Azure OpenAI documentation)
Choose your preferred implementation:
python main.pynode index.jsBoth servers will start on http://0.0.0.0:5005 by default.
Use the proxy just like the OpenAI API, but point to your local server:
curl -X POST http://localhost:5005/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_private_password_here" \
-d '{
"model": "gpt-4",
"messages": [
{
"role": "user",
"content": "Hello, how are you?"
}
]
}'For streaming responses, add "stream": true to your request:
curl -X POST http://localhost:5005/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_private_password_here" \
-d '{
"model": "gpt-4",
"messages": [
{
"role": "user",
"content": "Tell me a story"
}
],
"stream": true
}'import openai
client = openai.OpenAI(
base_url="http://localhost:5005/v1",
api_key="your_private_password_here"
)
response = client.chat.completions.create(
model="gpt-4", # This will be mapped to your Azure deployment
messages=[
{"role": "user", "content": "Hello!"}
]
)
print(response.choices[0].message.content)import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'http://localhost:5005/v1',
apiKey: 'your_private_password_here',
});
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'user', content: 'Hello!' }
],
});
console.log(response.choices[0].message.content);Proxies chat completion requests to Azure OpenAI Service.
Headers:
Content-Type: application/jsonAuthorization: Bearer <your_private_password>
Request Body: Same as OpenAI API chat completions format.
Response: Returns the Azure OpenAI response in OpenAI API format.
- Change the default password: Replace
your_private_password_herewith a strong, unique password - HTTPS in production: Use HTTPS in production environments
- Environment variables: Consider moving sensitive configuration to environment variables
- Rate limiting: Implement rate limiting for production use
- Network security: Restrict access to trusted networks/IPs if needed
- Use a production WSGI server like Gunicorn or uWSGI:
pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:5005 main:app- Environment variables for configuration:
import os
AZURE_OPENAI_ENDPOINT = os.getenv('AZURE_OPENAI_ENDPOINT')
AZURE_OPENAI_API_KEY = os.getenv('AZURE_OPENAI_API_KEY')
# ... etc- Use PM2 for process management:
npm install -g pm2
pm2 start index.js --name "azure-openai-proxy"
pm2 startup
pm2 save- Set production environment:
export NODE_ENV=production- Use environment variables (already implemented in the Node.js version)
- Reverse proxy with nginx or similar for SSL termination and load balancing
- HTTPS: Always use HTTPS in production environments
- Rate limiting: Implement rate limiting for production use
- Monitoring: Set up logging and monitoring for your chosen implementation
| Feature | Python (Flask) | Node.js (Express) |
|---|---|---|
| Performance | Good for I/O bound tasks | Excellent for concurrent connections |
| Memory Usage | Moderate | Lower |
| Ecosystem | Rich Python ecosystem | Rich npm ecosystem |
| Configuration | Direct code or env vars | Environment variables (recommended) |
| Production Ready | Use with Gunicorn/uWSGI | Use with PM2 or similar |
| Deployment | Traditional server deployment | Cloud-native friendly |
Choose based on your team's expertise and infrastructure preferences.
- 401 Unauthorized: Check that your private password matches in both client and server
- 503 Service Unavailable: Verify your Azure OpenAI endpoint and API key are correct
- 500 Internal Server Error: Check the server logs for specific error details
- Module/Package not found:
- Python: Run
pip install flask flask-cors requests - Node.js: Run
npm install express cors axios
- Python: Run
- Port already in use: Change the port in the code or stop the conflicting service
- Import errors: Ensure you're using the correct Python environment
- WSGI issues: Use
gunicornfor production instead of the built-in server
- Environment variables: The Node.js version requires environment variables to be set
- Process management: Use PM2 or similar for production deployments
Both implementations include error logging. Check the console output for detailed error messages.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Available in both Python (Flask) and Node.js (Express) implementations
- Built for Azure OpenAI Service compatibility
- Designed for seamless integration with OpenAI API clients