Spaces:
Running
Running
Update chat.js
Browse files
chat.js
CHANGED
@@ -1,39 +1,60 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
const
|
4 |
-
const
|
5 |
-
const
|
6 |
-
|
7 |
-
const app = express();
|
8 |
-
app.use(bodyParser.json());
|
9 |
-
|
10 |
-
// Erstellen des LangChain-Modells
|
11 |
-
const llm = LangChain.CTransformers({
|
12 |
-
model: 'TheBloke/CodeLlama-7B-GGUF',
|
13 |
-
model_type: 'llama',
|
14 |
-
max_new_tokens: 1096,
|
15 |
-
temperature: 0.2,
|
16 |
-
repetition_penalty: 1.13,
|
17 |
-
gpu_layers: 2
|
18 |
-
});
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
}
|
26 |
|
27 |
-
|
28 |
-
const
|
29 |
-
|
30 |
-
|
31 |
-
res.status(500).send({ error: 'Error processing request' });
|
32 |
}
|
33 |
-
});
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
});
|
|
|
1 |
+
document.addEventListener("DOMContentLoaded", function() {
|
2 |
+
const messageInput = document.getElementById("message-input");
|
3 |
+
const sendMessageButton = document.getElementById("send-message");
|
4 |
+
const conversationHistory = document.getElementById("conversation-history");
|
5 |
+
const newConversationButton = document.getElementById("new-conversation");
|
6 |
+
const clearConversationsButton = document.getElementById("clear-conversations");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
function sendMessage() {
|
9 |
+
const userMessage = messageInput.value;
|
10 |
+
if (!userMessage) return;
|
11 |
+
addToConversationHistory("You", userMessage);
|
12 |
+
getBotResponse(userMessage).then(botMessage => {
|
13 |
+
addToConversationHistory("Bot", botMessage);
|
14 |
+
});
|
15 |
+
messageInput.value = "";
|
16 |
}
|
17 |
|
18 |
+
function addToConversationHistory(sender, message) {
|
19 |
+
const messageElement = document.createElement("p");
|
20 |
+
messageElement.textContent = `${sender}: ${message}`;
|
21 |
+
conversationHistory.appendChild(messageElement);
|
|
|
22 |
}
|
|
|
23 |
|
24 |
+
async function getBotResponse(message) {
|
25 |
+
try {
|
26 |
+
const response = await fetch('/chatbot', {
|
27 |
+
method: 'POST',
|
28 |
+
headers: {
|
29 |
+
'Content-Type': 'application/json'
|
30 |
+
},
|
31 |
+
body: JSON.stringify({ query: message })
|
32 |
+
});
|
33 |
+
|
34 |
+
if (!response.ok) {
|
35 |
+
throw new Error('Network response was not ok');
|
36 |
+
}
|
37 |
+
|
38 |
+
const responseData = await response.json();
|
39 |
+
return responseData.reply;
|
40 |
+
} catch (error) {
|
41 |
+
console.error('Error fetching bot response:', error);
|
42 |
+
return "Sorry, there was an error.";
|
43 |
+
}
|
44 |
+
}
|
45 |
+
|
46 |
+
sendMessageButton.addEventListener("click", sendMessage);
|
47 |
+
messageInput.addEventListener("keypress", function(event) {
|
48 |
+
if (event.key === "Enter") {
|
49 |
+
sendMessage();
|
50 |
+
}
|
51 |
+
});
|
52 |
+
|
53 |
+
newConversationButton.addEventListener("click", function() {
|
54 |
+
conversationHistory.innerHTML = "";
|
55 |
+
});
|
56 |
+
|
57 |
+
clearConversationsButton.addEventListener("click", function() {
|
58 |
+
conversationHistory.innerHTML = "";
|
59 |
+
});
|
60 |
});
|