-
Notifications
You must be signed in to change notification settings - Fork 164
/
chat.html
96 lines (88 loc) · 2.8 KB
/
chat.html
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
<!doctype html>
<!--
* @license
* 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>
<meta charset="utf-8" />
<link rel="shortcut icon" type="image/svg+xml" href="favicon.svg" />
<link rel="stylesheet" href="utils/main.css" />
<link
href="https://fonts.googleapis.com/css?family=Roboto:400,700"
rel="stylesheet"
type="text/css"
/>
<title>Generative AI - Chat</title>
</head>
<body>
<header>Generative AI - Chat</header>
<div class="container">
<div id="chat-history"></div>
</div>
<div class="form-container">
<form id="form">
<input id="prompt" />
<button type="submit">Send</button>
</form>
<template id="thumb-template">
<img class="thumb" />
</template>
</div>
<script type="module">
import {
getGenerativeModel,
scrollToDocumentBottom,
updateUI,
} from "./utils/shared.js";
const promptInput = document.querySelector("#prompt");
const historyElement = document.querySelector("#chat-history");
let chat;
document
.querySelector("#form")
.addEventListener("submit", async (event) => {
event.preventDefault();
if (!chat) {
const model = await getGenerativeModel({ model: "gemini-1.5-flash" });
chat = model.startChat({
generationConfig: {
maxOutputTokens: 100,
},
});
}
const userMessage = promptInput.value;
promptInput.value = "";
// Create UI for the new user / assistant messages pair
historyElement.innerHTML += `<div class="history-item user-role">
<div class="name">User</div>
<blockquote>${userMessage}</blockquote>
</div>
<div class="history-item model-role">
<div class="name">Model</div>
<blockquote></blockquote>
</div>`;
scrollToDocumentBottom();
const resultEls = document.querySelectorAll(
".model-role > blockquote",
);
await updateUI(
resultEls[resultEls.length - 1],
() => chat.sendMessageStream(userMessage),
true,
);
});
</script>
</body>
</html>