forked from heiko-hotz/gemini-multimodal-live-dev-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
131 lines (112 loc) · 3.85 KB
/
index.html
File metadata and controls
131 lines (112 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE html>
<!--
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<title>Gemini WebSocket Test</title>
<style>
.header-section {
padding: 20px;
margin-bottom: 30px;
background-color: #f5f5f5;
border-bottom: 1px solid #ddd;
}
.header-section h1 {
margin-top: 0;
color: #333;
}
.header-section p {
margin-bottom: 0;
color: #666;
}
#output {
padding: 20px;
border-top: 2px solid #ddd;
}
</style>
</head>
<body>
<div class="header-section">
<h1>Gemini WebSocket Test</h1>
<p>This is a simple demonstration of WebSocket communication with the Gemini API, showing a single exchange between user and model. It illustrates the fundamental principles of interacting with the API at a low level, without using an SDK.</p>
</div>
<div id="output"></div>
<script>
const output = document.getElementById('output');
const apiKey = '<YOUR_API_KEY>';
const host = 'generativelanguage.googleapis.com';
const endpoint = `wss://${host}/ws/google.ai.generativelanguage.v1alpha.GenerativeService.BidiGenerateContent?key=${apiKey}`;
const ws = new WebSocket(endpoint);
ws.onopen = () => {
console.log('WebSocket connection is opening...');
logMessage('Connected to Gemini');
const setupMessage = {
setup: {
model: "models/gemini-2.0-flash-exp",
generation_config: {
response_modalities: ["text"]
}
}
};
console.log('Sending setup message:', setupMessage);
ws.send(JSON.stringify(setupMessage));
};
ws.onmessage = async (event) => {
try {
console.log('Event:', event);
// Handle Blob data
const responseText = await new Response(event.data).text();
const response = JSON.parse(responseText);
console.log('Response:', response);
// Handle setup complete response
if (response.setupComplete) {
const userMessage = "Hello! Are you there?";
logMessage('You: ' + userMessage);
const contentMessage = {
client_content: {
turns: [{
role: "user",
parts: [{ text: userMessage }]
}],
turn_complete: true
}
};
console.log('Sending content message:', contentMessage);
ws.send(JSON.stringify(contentMessage));
}
// Handle model response
else if (response.serverContent?.modelTurn?.parts?.[0]?.text) {
const modelResponse = response.serverContent.modelTurn.parts[0].text;
logMessage('Gemini: ' + modelResponse);
}
} catch (error) {
console.error('Error parsing response:', error);
logMessage('Error parsing response: ' + error.message);
}
};
ws.onerror = (error) => {
console.error('WebSocket Error:', error);
logMessage('WebSocket Error: ' + error.message);
};
ws.onclose = (event) => {
console.log('Connection closed:', event);
logMessage(`Connection closed - Code: ${event.code}, Reason: ${event.reason}`);
};
function logMessage(message) {
const messageElement = document.createElement('p');
messageElement.textContent = message;
output.appendChild(messageElement);
}
</script>
</body>
</html>