feat: implement OpenAI-compatible Realtime API with authentication and management features
- Added support for public Realtime API, including new routes for managing API keys and handling WebRTC connections. - Introduced RealtimeApiKey model and associated CRUD operations for admin management of API keys. - Implemented authentication mechanisms for API keys and client secrets. - Enhanced environment configuration with new secrets for Realtime API. - Created OpenAIRealtime session management and event processing for real-time interactions. - Updated schemas and settings to accommodate new features and ensure compatibility with existing systems.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""UTF-8 JSON serializer and transport builder for public Realtime WebSockets."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from fastapi import WebSocket
|
||||
from pipecat.frames.frames import (
|
||||
Frame,
|
||||
InputTransportMessageFrame,
|
||||
OutputTransportMessageFrame,
|
||||
OutputTransportMessageUrgentFrame,
|
||||
)
|
||||
from pipecat.serializers.base_serializer import FrameSerializer
|
||||
|
||||
from services.pipecat.transports import build_serialized_ws_transport
|
||||
|
||||
|
||||
class OpenAIRealtimeJSONSerializer(FrameSerializer):
|
||||
async def serialize(self, frame: Frame) -> str | bytes | None:
|
||||
if self.should_ignore_frame(frame):
|
||||
return None
|
||||
if isinstance(
|
||||
frame,
|
||||
(OutputTransportMessageFrame, OutputTransportMessageUrgentFrame),
|
||||
) and isinstance(frame.message, dict):
|
||||
return json.dumps(frame.message, ensure_ascii=False, separators=(",", ":"))
|
||||
return None
|
||||
|
||||
async def deserialize(self, data: str | bytes) -> Frame | None:
|
||||
if isinstance(data, bytes):
|
||||
message: Any = {
|
||||
"type": "_invalid_binary_frame",
|
||||
"event_id": None,
|
||||
}
|
||||
else:
|
||||
try:
|
||||
message = json.loads(data)
|
||||
except json.JSONDecodeError:
|
||||
message = {"type": "_invalid_json", "event_id": None}
|
||||
return InputTransportMessageFrame(message=message)
|
||||
|
||||
|
||||
def build_openai_websocket_transport(websocket: WebSocket):
|
||||
return build_serialized_ws_transport(
|
||||
websocket,
|
||||
serializer=OpenAIRealtimeJSONSerializer(),
|
||||
sample_rate=24000,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user