-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
executable file
·69 lines (52 loc) · 1.97 KB
/
main.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
import { GoogleGenerativeAI, HarmBlockThreshold, HarmCategory } from "@google/generative-ai";
import MarkdownIt from 'markdown-it';
import './style.css';
// 🔥 https://g.co/ai/idxGetGeminiKey 🔥
let API_KEY = import.meta.env.VITE_GEMINI_KEY
let form = document.querySelector('form');
let promptInput = document.querySelector('input[name="prompt"]');
let output = document.querySelector('.output');
let message = document.querySelector('.message');
message.style.display = 'none';
form.onsubmit = async (ev) => {
ev.preventDefault();
message.style.display = 'block'
try {
// Assemble the prompt by combining the text with the chosen image
let contents = [
{
role: 'user',
parts: [
{ text: `You are a knowledgeable travel guide specializing in Karachi.
When a visitor asks you about their upcoming trip using the
variable "${promptInput.value}", provide a comprehensive response.
Include detailed daily itineraries, top dining spots, and must-see attractions.
Ensure you account for travel logistics, such as travel times and
operational hours of venues.`, }
]
}
];
// Call the gemini-pro model, and get a stream of results
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({
model: "gemini-pro",
safetySettings: [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
],
});
const result = await model.generateContentStream({ contents });
// Read from the stream and interpret the output as markdown
let buffer = [];
let md = new MarkdownIt();
for await (let response of result.stream) {
buffer.push(response.text());
output.innerHTML = md.render(buffer.join(''));
}
} catch (e) {
output.innerHTML += '<hr>' + e;
}
};
// You can delete this once you've filled out an API key