-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
65 lines (52 loc) · 1.9 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
//do not forget to get your API key and paste it inside the API_KEY variable at line 3
const API_KEY = ''; // <------- enter your Api key here
const submitButton = document.querySelector('#submit');
const outputElement = document.querySelector('#output');
const inputElement = document.querySelector('input');
const historyElement = document.querySelector('.history');
const buttonElement = document.querySelector('button');
let historyData = [];
async function getMessage() {
console.log('clicked');
const userMessage = inputElement.value;
if (!userMessage) return; // Exit if the input is empty
const options = {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: "mixtral-8x7b-32768",
messages: [{ role: "user", content: userMessage }],
max_tokens: 100
})
};
try {
const response = await fetch('https://api.groq.com/openai/v1/chat/completions', options);
const data = await response.json();
console.log(data);
const aiMessage = data.choices[0]?.message?.content || "No response";
outputElement.textContent = aiMessage;
if (aiMessage && userMessage) {
const messageIndex = historyData.length;
historyData.push({ userMessage, aiResponse: aiMessage });
const pElement = document.createElement('p');
pElement.textContent = userMessage;
pElement.addEventListener('click', () => showResponse(messageIndex));
historyElement.append(pElement);
}
clearInput();
} catch (error) {
console.error('Error fetching Groq completion:', error);
}
}
function showResponse(index) {
const { aiResponse } = historyData[index];
outputElement.textContent = aiResponse;
}
function clearInput() {
inputElement.value = '';
}
submitButton.addEventListener('click', getMessage);
buttonElement.addEventListener('click', clearInput);