-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathscript.js
100 lines (87 loc) · 2.86 KB
/
script.js
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
// 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
// https://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.
// script.js
// Authors: kylephillips@ bencobley@
import { GoogleGenerativeAI } from "https://esm.run/@google/generative-ai@0.21.0";
import * as mapsFunction from "./function-declarations.js";
import { presets } from "./presets.js";
import { html, render } from "https://esm.run/lit";
const client = new GoogleGenerativeAI("your_key_here");
const systemInstruction = mapsFunction.systemInstructions;
const functionDeclarations = mapsFunction.declarations.map(declaration => ({
...declaration,
callback: (args) => {
const { location, caption } = args;
renderPage(location, caption);
},
}));
const chat = async (userText) => {
try {
const temperature = 2; // High temperature for answer variety
const {response} = await client
.getGenerativeModel(
{model: 'models/gemini-2.0-flash-exp', systemInstruction},
{apiVersion: 'v1beta'}
)
.generateContent({
contents: [
{
role: "user",
parts: [{text: userText}],
},
],
generationConfig: {temperature},
tools: [{functionDeclarations}]
});
const call = response.functionCalls()[0];
if (call) {
functionDeclarations[0].callback(call.args);
}
} catch (e) {
console.error(e);
}
};
async function init() {
renderPage("%"); // Start by rendering with empty location query: shows earth
if (
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches
) {
document.documentElement.removeAttribute("data-theme"); // Use default (dark)
} else {
document.documentElement.setAttribute("data-theme", "light");
}
}
init();
function renderPage(location, caption = "") {
const root = document.querySelector("#root");
caption = caption.replace(/\\/g, '');
render(
html`
<div id="map">${mapsFunction.embed(location)}</div>
${caption
? html`<div id="caption"><p>${caption}</p></div>`
: ""}
<div id="presets-container">
<span id="take-me-somewhere">Take me somewhere...</span>
<div id="presets">
${presets.map(
([name, message]) =>
html`<button @click=${() => chat(message)} class="preset">
${name}
</button>`
)}
</div>
</div>
`,
root
);
}