Skip to content

Commit 1c7e10e

Browse files
Merge pull request #584 from MervinPraison/claude/issue-192-20250603_080328
feat: add Azure OpenAI realtime API support via OPENAI_BASE_URL
2 parents 7b8dd1f + 0d05be9 commit 1c7e10e

4 files changed

Lines changed: 60 additions & 12 deletions

File tree

docs/ui/realtime.mdx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ To use the Realtime Voice Interface, follow these steps:
3636
```bash
3737
export OPENAI_API_KEY="your-api-key-here"
3838
```
39+
40+
<Accordion title="Azure OpenAI Configuration">
41+
To use Azure OpenAI instead of standard OpenAI, configure these environment variables:
42+
43+
```bash
44+
export OPENAI_API_KEY="your-azure-api-key"
45+
export OPENAI_BASE_URL="https://your-resource.openai.azure.com/openai/deployments/your-deployment-name"
46+
export OPENAI_MODEL_NAME="gpt-4o-realtime-preview"
47+
```
48+
49+
The realtime interface will automatically detect the base URL and adjust the WebSocket connection accordingly.
50+
</Accordion>
3951

4052
3. Launch the Realtime Voice Interface:
4153
```bash
@@ -56,9 +68,24 @@ Once the interface is launched:
5668

5769
You can configure various aspects of the Realtime Voice Interface:
5870

59-
- Model selection: Choose different AI models for processing.
60-
- Voice settings: Adjust voice characteristics for the AI's speech output.
61-
- Audio settings: Configure input/output audio formats and quality.
71+
### Environment Variables
72+
73+
| Variable | Description | Default |
74+
|----------|-------------|---------|
75+
| `OPENAI_API_KEY` | Your OpenAI or Azure OpenAI API key | Required |
76+
| `OPENAI_BASE_URL` | Custom base URL for OpenAI-compatible APIs (e.g., Azure OpenAI) | `https://api.openai.com/v1` |
77+
| `OPENAI_MODEL_NAME` | Model to use for realtime API | `gpt-4o-mini-realtime-preview-2024-12-17` |
78+
79+
### Model Selection
80+
Choose different AI models for processing. Supported models include:
81+
- `gpt-4o-realtime-preview`
82+
- `gpt-4o-mini-realtime-preview-2024-12-17`
83+
84+
### Voice Settings
85+
Adjust voice characteristics for the AI's speech output through the session configuration.
86+
87+
### Audio Settings
88+
Configure input/output audio formats and quality. The interface uses PCM16 format by default for optimal compatibility.
6289

6390
## Troubleshooting
6491

src/praisonai/praisonai/ui/realtime.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def import_tools_from_file(file_path):
229229
@cl.on_chat_start
230230
async def start():
231231
initialize_db()
232-
model_name = os.getenv("MODEL_NAME", "gpt-4o-mini-realtime-preview-2024-12-17")
232+
model_name = os.getenv("OPENAI_MODEL_NAME") or os.getenv("MODEL_NAME", "gpt-4o-mini-realtime-preview-2024-12-17")
233233
cl.user_session.set("model_name", model_name)
234234
cl.user_session.set("message_history", []) # Initialize message history
235235
logger.debug(f"Model name: {model_name}")
@@ -382,7 +382,7 @@ async def on_audio_start():
382382
openai_realtime = cl.user_session.get("openai_realtime")
383383

384384
if not openai_realtime.is_connected():
385-
model_name = cl.user_session.get("model_name", "gpt-4o-mini-realtime-preview-2024-12-17")
385+
model_name = cl.user_session.get("model_name") or os.getenv("OPENAI_MODEL_NAME") or os.getenv("MODEL_NAME", "gpt-4o-mini-realtime-preview-2024-12-17")
386386
await openai_realtime.connect(model_name)
387387

388388
logger.info("Connected to OpenAI realtime")
@@ -435,7 +435,7 @@ def auth_callback(username: str, password: str):
435435
@cl.on_chat_resume
436436
async def on_chat_resume(thread: ThreadDict):
437437
logger.info(f"Resuming chat: {thread['id']}")
438-
model_name = os.getenv("MODEL_NAME") or "gpt-4o-mini-realtime-preview-2024-12-17"
438+
model_name = os.getenv("OPENAI_MODEL_NAME") or os.getenv("MODEL_NAME") or "gpt-4o-mini-realtime-preview-2024-12-17"
439439
logger.debug(f"Model name: {model_name}")
440440
settings = cl.ChatSettings(
441441
[

src/praisonai/praisonai/ui/realtimeclient/__init__.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,22 @@ class RealtimeAPI(RealtimeEventHandler):
9393
def __init__(self, url=None, api_key=None):
9494
super().__init__()
9595
self.default_url = 'wss://api.openai.com/v1/realtime'
96-
self.url = url or self.default_url
96+
97+
# Support custom base URL from environment variable
98+
base_url = os.getenv("OPENAI_BASE_URL")
99+
if base_url:
100+
# Convert HTTP/HTTPS base URL to WebSocket URL for realtime API
101+
if base_url.startswith('https://'):
102+
ws_url = base_url.replace('https://', 'wss://').rstrip('/') + '/realtime'
103+
elif base_url.startswith('http://'):
104+
ws_url = base_url.replace('http://', 'ws://').rstrip('/') + '/realtime'
105+
else:
106+
# Assume it's already a WebSocket URL
107+
ws_url = base_url.rstrip('/') + '/realtime' if not base_url.endswith('/realtime') else base_url
108+
self.url = url or ws_url
109+
else:
110+
self.url = url or self.default_url
111+
97112
self.api_key = api_key or os.getenv("OPENAI_API_KEY")
98113
self.ws = None
99114

@@ -559,9 +574,9 @@ async def connect(self, model=None):
559574
if self.is_connected():
560575
raise Exception("Already connected, use .disconnect() first")
561576

562-
# Use provided model or default
577+
# Use provided model, OPENAI_MODEL_NAME environment variable, or default
563578
if model is None:
564-
model = 'gpt-4o-mini-realtime-preview-2024-12-17'
579+
model = os.getenv("OPENAI_MODEL_NAME", 'gpt-4o-mini-realtime-preview-2024-12-17')
565580

566581
await self.realtime.connect(model)
567582
await self.update_session()
@@ -732,7 +747,7 @@ async def ensure_connected(self):
732747
if not self.is_connected():
733748
try:
734749
logger.info("Attempting to reconnect to OpenAI Realtime API...")
735-
model = 'gpt-4o-mini-realtime-preview-2024-12-17'
750+
model = os.getenv("OPENAI_MODEL_NAME", 'gpt-4o-mini-realtime-preview-2024-12-17')
736751
await self.connect(model)
737752
return True
738753
except Exception as e:

src/praisonai/praisonai/ui/realtimeclient/tools.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@
2222
tavily_api_key = os.getenv("TAVILY_API_KEY")
2323
tavily_client = TavilyClient(api_key=tavily_api_key) if tavily_api_key else None
2424

25-
# Set up OpenAI client
26-
openai_client = OpenAI()
25+
# Set up OpenAI client with support for custom base URL
26+
openai_base_url = os.getenv("OPENAI_BASE_URL")
27+
openai_api_key = os.getenv("OPENAI_API_KEY")
28+
29+
if openai_base_url:
30+
openai_client = OpenAI(base_url=openai_base_url, api_key=openai_api_key)
31+
else:
32+
openai_client = OpenAI(api_key=openai_api_key)
2733

2834
query_stock_price_def = {
2935
"name": "query_stock_price",

0 commit comments

Comments
 (0)