-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
108 lines (84 loc) · 3.33 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
document.addEventListener("DOMContentLoaded", () => {
const chatBox = document.getElementById("chat-box");
const userInput = document.getElementById("user-input");
const sendBtn = document.getElementById("send-btn");
const appendMessage = (role, content) => {
const messageDiv = document.createElement("div");
messageDiv.classList.add("message");
messageDiv.classList.add(role);
messageDiv.innerText = content;
chatBox.appendChild(messageDiv);
chatBox.scrollTop = chatBox.scrollHeight;
};
const sendMessage = async () => {
const userMessage = userInput.value.trim();
if (userMessage !== "" ) {
// Create message container for user message
const userMessageContainer = document.createElement('div');
userMessageContainer.classList.add('message-container', 'user-message');
const userMessageLabel = document.createElement('div');
userMessageLabel.classList.add('message-label');
//userMessageLabel.textContent = 'You';
const userMessageContent = document.createElement('div');
userMessageContent.classList.add('message');
userMessageContent.textContent = userMessage;
userMessageContainer.appendChild(userMessageLabel);
userMessageContainer.appendChild(userMessageContent);
// Append the user message container to the chat box
chatBox.appendChild(userMessageContainer);
// Clear input field
userInput.value = "";
try {
const response = await fetch("/api/chat", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message: userMessage }),
});
const data = await response.json();
// Create message container for PrismALM's message
const botMessageContainer = document.createElement('div');
botMessageContainer.classList.add('message-container', 'bot-message');
const botMessageLabel = document.createElement('div');
botMessageLabel.classList.add('message-label');
//botMessageLabel.textContent = 'PrismALM';
const botMessageContent = document.createElement('div');
botMessageContent.classList.add('message');
botMessageContent.textContent = data.message;
botMessageContainer.appendChild(botMessageLabel);
botMessageContainer.appendChild(botMessageContent);
// Append PrismALM's message container to the chat box
chatBox.appendChild(botMessageContainer);
} catch (error) {
console.error("Error:", error);
}
}
};
sendBtn.addEventListener("click", sendMessage);
userInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
sendMessage();
}
});
});
// import { config } from "dotenv"
// config()
// import { OpenAI } from "openai"
// import readline from "readline"
// const openAi = new OpenAI({
// apiKey: process.env.API_KEY,
// })
// const userInterface = readline.createInterface({
// input: process.stdin,
// output: process.stdout,
// })
// userInterface.prompt()
// userInterface.on("line", async input => {
// const response = await openAi.chat.completions.create({
// model: "gpt-3.5-turbo",
// messages: [{ role: "user", content: input }],
// })
// console.log(response.choices[0].message)
// userInterface.prompt()
// })