in this repo tell me how is the gemini2.0 realtime multimodal api used?
also how are the websockets used.
also tell me how the realtime voice communication with gemini model implemented
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
1. How is the Gemini 2.0 Real-Time Multimodal API Used?
The Gemini 2.0 Real-Time Multimodal API is used in this repository to provide real-time audio and video interactions, including streaming voice and video data to the Gemini model and receiving responses in real-time. Here's a breakdown of its usage in the repo:
Key Components Involved:
MultimodalLiveClient
This is a WebSocket-based client that connects to the Gemini 2.0 API via a WebSocket endpoint (wss://generativelanguage.googleapis.com/ws/...).
It handles sending and receiving real-time messages for audio, video, or other multimodal inputs.
useLiveAPI Hook
This hook integrates MultimodalLiveClient into a React application.
It manages the WebSocket connection, configuration, and state (e.g., connected/disconnected, volume control).
Real-Time Input Integration
Audio and video streams are captured using navigator.mediaDevices.getUserMedia and processed into chunks (e.g., audio as audio/pcm and video as image/jpeg).
These chunks are sent to the Gemini API via the sendRealtimeInput method of MultimodalLiveClient.
Example of Real-Time API Usage:
Connecting to the API:
ts
const client = new MultimodalLiveClient({ url, apiKey });
await client.connect(config);
url: WebSocket endpoint for Gemini API.
config: Includes the model to use (models/gemini-2.0-flash-exp).
The WebSocket listens for incoming messages from Gemini:
ts
ws.addEventListener("message", async (evt: MessageEvent) => {
if (evt.data instanceof Blob) {
this.receive(evt.data); // Processes the received message
}
});
Incoming messages include:
Audio responses: Played back using the AudioStreamer class.
Text responses: Processed as part of ServerContent.
Events Emitted:
The WebSocket emits custom events based on the type of response:
audio: For real-time audio playback.
content: For server-generated text or other multimodal responses.
setupcomplete: Indicates the setup is successful.
turncomplete: Indicates the server has completed processing a turn.
3. How Is Real-Time Voice Communication with the Gemini Model Implemented?
Real-time voice communication is implemented using a combination of audio recording, WebSocket communication, and audio playback. Here's the complete flow:
1. Capturing and Streaming User Audio:
Audio Recording:
The AudioRecorder class uses MediaStream to capture audio via the user's microphone.
The audio is processed into chunks of audio/pcm data, which are converted to base64: