This project facilitates real-time communication between FastAPI WebSocket connections and OpenAI's WebSocket connections, including compatibility with Azure OpenAI. It acts as a relay server that can be extended for security and customization purposes.
- Overview
- Setup and Installation
- Configuration
- Usage
- Development
- Benefits and Use Cases
- Acknowledgments
This application uses FastAPI to provide a WebSocket interface that connects with OpenAI's WebSocket API. The project is structured to handle real-time data exchange, making it suitable for scenarios where latency and live updates are crucial. It also supports integration with Azure OpenAI.
- Python 3.8+
- An OpenAI API Key or Azure OpenAI API Key
-
Clone the repository:
git clone https://github.com/Geo-Joy/openai-realtime-fastapi cd openai-realtime-fastapi
-
Set up a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
-
Create a
.env
file in the root directory with the following content. Use the appropriate URL depending on whether you are using OpenAI or Azure OpenAI:OPENAI_API_KEY="your-openai-api-key" OPENAI_REALTIME_URL="wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01" USE_AZURE_OPENAI=False
For Azure OpenAI, replace the URL as follows and set
USE_AZURE_OPENAI
toTrue
:OPENAI_API_KEY="your-azure-openai-api-key" OPENAI_REALTIME_URL="wss://<sub-domain>.openai.azure.com/openai/realtime?api-version=2024-10-01-preview&deployment=gpt-4o-realtime-preview" USE_AZURE_OPENAI=True
Configuration settings are managed using environment variables loaded from a .env
file. The main configuration file is config.py
, which retrieves values like API_KEY
and VENDOR_WS_URL
from the environment.
To start the application, use Uvicorn to run the FastAPI server:
uvicorn main:app --reload
The application should now be running on http://localhost:8000
.
main.py
: Initializes the FastAPI app and includes routers for various endpoints.health_check.py
: Provides a simple health check route.realtime.py
: Manages the WebSocket connection and data relay logic.
The openai-realtime-fastapi project can act as a relay server with various advantages:
-
Security and Privacy:
- API Credential Protection: By routing requests through a server, API keys are kept secure and hidden from client-side exposure.
- Sensitive Data Handling: Execute sensitive operations server-side to prevent leakage or manipulation from client-side interactions.
-
Controlled Environment:
- Centralized Logic: Define and manage business logic server-side, ensuring uniform application behavior and more straightforward updates.
- Restricted Access: Permit only specific types of requests and responses between the client and third-party services, enhancing security and rule enforcement.
-
Authentication and Authorization:
- Flexible Authentication Mechanisms: Implement various authentication strategies (e.g., OAuth, JWT, API Key) based on application requirements to ensure secure client-server interactions.
- Single Sign-On (SSO): Enable SSO to streamline user access, reducing the need for multiple login credentials, while keeping vendor connections and API interactions hidden from the end-user.
-
Reduced Client Complexity:
- Clients can interface with a simplified API rather than directly interact with complex third-party services.
- Streamline client-side applications since logic and API integrations are managed server-side.
-
Flexibility for Tests and DevOps:
- Mock or alter API responses for different environments without impacting production clients.
- Seamlessly switch between different service providers or deployments.
- FastAPI
- OpenAI API
- Azure OpenAI
- Contributor: Geo Joy (https://www.linkedin.com/in/-itsg/)