-
Notifications
You must be signed in to change notification settings - Fork 0
/
mattGPT.js
75 lines (69 loc) · 2.06 KB
/
mattGPT.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
const url = 'https://api.openai.com/v1/chat/completions';
const apiKey = '';
const form = document.querySelector('form');
const promptInput = document.querySelector('#prompt');
const chatLog = document.querySelector('.chat-log');
form.addEventListener('submit', e => {
e.preventDefault();
let value = promptInput.value;
if (value !== '') {
createMessageInstace(value);
askMattGPT(value);
handleScroll();
promptInput.value = '';
}
})
function createMessageInstace(prompt) {
chatLog.innerHTML +=
`
<div class="message-instance-container">
<div class="message user-message">
<div class="content">
<div class="message-image">
<img src="./user.png" alt="User image">
</div>
<p style class="prompt">${prompt}</p>
</div>
</div>
<div class="message ai-message">
<div class="content">
<div class="message-image">
<img src="./logo11.svg" alt="SymbioticBot logo">
</div>
<p class="thinking">Pondering</p>
</div>
</div>
</div>
`
}
function askMattGPT(prompt) {
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: prompt
}
],
max_tokens: 200,
})
})
.then(res => res.json())
.then(data => updateMessage(data))
}
// scrolls chatlog to bottom
function handleScroll() {
chatLog.scrollTop = chatLog.scrollHeight;
}
function updateMessage(message) {
const p= document.querySelector('.thinking');
p.textContent = message.choices[0].message.content;
p.classList.remove('thinking');
handleScroll();
}