-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
executable file
·59 lines (49 loc) · 1.9 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
import { GoogleGenerativeAI, HarmBlockThreshold, HarmCategory } from "@google/generative-ai";
import MarkdownIt from 'markdown-it';
import { maybeShowApiKeyBanner } from './gemini-api-banner';
import './style.css';
let API_KEY = 'API KEY';
let form = document.querySelector('form');
let topic = document.querySelector('#topic');
let customInstruction = document.querySelector("#customInstruction");
let output = document.querySelector('.output');
let subject = document.querySelector('#subject');
let grade = document.querySelector('#class');
form.onsubmit = async (ev) => {
ev.preventDefault();
output.textContent = 'Generating...';
try {
// Assemble the prompt by combining the text with the chosen image
let contents = [
{
role: 'user',
parts: [
{ text: "Make a test paper for topic: " + topic.value + ' Subject: ' + subject.value + ' Class: ' + grade.value + ' Preferences: ' + customInstruction.value + " Give all the questions in a single response, & do what you are told, nothing more"}
]
}
];
// Call the multimodal model, and get a stream of results
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash", // or gemini-1.5-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
maybeShowApiKeyBanner(API_KEY);