-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (86 loc) · 2.96 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
95
96
97
import { initializeApp } from "firebase/app";
import { getDatabase, ref, push, get, remove } from "firebase/database";
const appSettings = {
databaseURL:
"https://wise-owl-e4e5f-default-rtdb.europe-west1.firebasedatabase.app/",
};
const app = initializeApp(appSettings);
const database = getDatabase(app);
const conversationInDb = ref(database);
const chatbotConversation = document.getElementById("chatbot-conversation");
const instructionObj = {
role: "system",
content: "You are a helpful assistant.",
};
document.addEventListener("submit", (e) => {
e.preventDefault();
const userInput = document.getElementById("user-input");
push(conversationInDb, {
role: "user",
content: userInput.value,
});
fetchReply();
const newSpeechBubble = document.createElement("div");
newSpeechBubble.classList.add("speech", "speech-human");
chatbotConversation.appendChild(newSpeechBubble);
newSpeechBubble.textContent = userInput.value;
userInput.value = "";
chatbotConversation.scrollTop = chatbotConversation.scrollHeight;
});
function fetchReply() {
get(conversationInDb).then(async (snapshot) => {
if (snapshot.exists()) {
const conversationArr = Object.values(snapshot.val());
conversationArr.unshift(instructionObj);
const response = await fetch("/.netlify/functions/openai", {
method: "POST",
body: JSON.stringify({ messages: conversationArr }),
});
const { reply } = await response.json();
push(conversationInDb, {
role: "assistant",
content: reply,
});
renderTypewriterText(reply);
} else {
console.log("No data available");
}
});
}
function renderTypewriterText(text) {
const newSpeechBubble = document.createElement("div");
newSpeechBubble.classList.add("speech", "speech-ai", "blinking-cursor");
chatbotConversation.appendChild(newSpeechBubble);
let i = 0;
const interval = setInterval(() => {
newSpeechBubble.textContent += text.slice(i - 1, i);
if (text.length === i) {
clearInterval(interval);
newSpeechBubble.classList.remove("blinking-cursor");
}
i++;
chatbotConversation.scrollTop = chatbotConversation.scrollHeight;
}, 50);
}
document.getElementById("clear-btn").addEventListener("click", () => {
remove(conversationInDb);
chatbotConversation.innerHTML =
'<div class="speech speech-ai">How can I help you?</div>';
});
function renderConversationFromDb() {
get(conversationInDb).then(async (snapshot) => {
if (snapshot.exists()) {
Object.values(snapshot.val()).forEach((dbObj) => {
const newSpeechBubble = document.createElement("div");
newSpeechBubble.classList.add(
"speech",
`speech-${dbObj.role === "user" ? "human" : "ai"}`
);
chatbotConversation.appendChild(newSpeechBubble);
newSpeechBubble.textContent = dbObj.content;
});
chatbotConversation.scrollTop = chatbotConversation.scrollHeight;
}
});
}
renderConversationFromDb();