-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
84 lines (77 loc) · 2.48 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
send_box = document.getElementById('message');
send_button = document.getElementById('send-button');
conversation = document.getElementById('conversation');
send_button.addEventListener('click', send_msg);
async function send_msg() {
message = send_box.value;
send_box.value = 'Typing...';
send_box.disabled = true;
create_conversation('user', message);
response = await generateContent(message);
create_conversation('', response);
send_box.value = '';
send_box.disabled = false;
console.log(message);
}
function create_conversation(type, message){
if (type == 'user') {
var divv = `<div class="user message">
<div class="identity">
<i class="user-icon">u</i>
</div>
<div class="content">
<p>${message}</p>
</div>
</div>`
console.log(divv);
conversation.innerHTML += divv;
}
else{
var divv = ` <div class="assistant message">
<div class="identity">
<i class="gpt user-icon">G</i>
</div>
<div class="content">
${message}
</div>
</div>`
console.log(divv);
conversation.innerHTML += divv
}
}
async function generateContent(userInput) {
const API_KEY = "AIzaSyDEjmxXFxXix4CpiKkWRGH7o_jJWlCh4bU";
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=${API_KEY}`;
const requestBody = {
contents: [
{
role: "user",
parts: [{ text: userInput }]
}
],
generationConfig: {
temperature: 1,
topK: 0,
topP: 0.95,
maxOutputTokens: 8192,
stopSequences: []
},
safetySettings: [
{ category: "HARM_CATEGORY_HARASSMENT", threshold: "BLOCK_MEDIUM_AND_ABOVE" },
{ category: "HARM_CATEGORY_HATE_SPEECH", threshold: "BLOCK_MEDIUM_AND_ABOVE" },
{ category: "HARM_CATEGORY_SEXUALLY_EXPLICIT", threshold: "BLOCK_MEDIUM_AND_ABOVE" },
{ category: "HARM_CATEGORY_DANGEROUS_CONTENT", threshold: "BLOCK_MEDIUM_AND_ABOVE" }
]
};
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(requestBody)
});
const responseData = await response.json();
const output = responseData.candidates[0].content.parts[0].text;
htmll = marked.parse(output);
return htmll;
}