| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | |
| 5 | from fastapi import APIRouter, WebSocket |
| 6 | |
| 7 | from vllm.logger import init_logger |
| 8 | |
| 9 | from .connection import RealtimeConnection |
| 10 | |
| 11 | logger = init_logger(__name__) |
| 12 | |
| 13 | |
| 14 | router = APIRouter() |
| 15 | |
| 16 | |
| 17 | @router.websocket("/v1/realtime") |
| 18 | async def realtime_endpoint(websocket: WebSocket): |
| 19 | """WebSocket endpoint for realtime audio transcription. |
| 20 | |
| 21 | Protocol: |
| 22 | 1. Client connects to ws://host/v1/realtime |
| 23 | 2. Server sends session.created event |
| 24 | 3. Client optionally sends session.update with model/params |
| 25 | 4. Client sends input_audio_buffer.commit when ready |
| 26 | 5. Client sends input_audio_buffer.append events with base64 PCM16 chunks |
| 27 | 6. Server processes and sends transcription.delta events |
| 28 | 7. Server sends transcription.done with final text + usage |
| 29 | 8. Repeat from step 5 for next utterance |
| 30 | 9. Optionally, client sends input_audio_buffer.commit with final=True |
| 31 | to signal audio input is finished. Useful when streaming audio files |
| 32 | |
| 33 | Audio format: PCM16, 16kHz, mono, base64-encoded |
| 34 | """ |
| 35 | app = websocket.app |
| 36 | serving = app.state.openai_serving_realtime |
| 37 | |
| 38 | connection = RealtimeConnection(websocket, serving) |
| 39 | await connection.handle_connection() |