forked from SathiraSriSathsara/AI-Chabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (53 loc) · 1.85 KB
/
server.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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const Groq = require("groq-sdk");
const app = express();
const groq = new Groq({
apiKey: process.env.GROQ_API_KEY
});
// Configure middleware
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); // Add JSON body parser
// Store chat history
let chatHistory = [];
// Render index page
app.get('/', (req, res) => {
res.render('index', { chatHistory });
});
// Handle chat message submission
app.post('/send-chat', async (req, res) => {
const userMessage = req.body.message;
chatHistory.push({ role: 'user', content: userMessage });
try {
const chatCompletion = await getGroqChatCompletion(userMessage);
const aiMessage = chatCompletion.choices[0].message.content;
// Check if the AI response contains code
const containsCode = /```/.test(aiMessage);
chatHistory.push({ role: 'ai', content: aiMessage, containsCode });
// Send the AI response and code indicator back to the client
res.status(200).json({ role: 'ai', content: aiMessage, containsCode });
} catch (error) {
chatHistory.push({ role: 'ai', content: 'Error communicating with AI. Please try again.' });
res.status(500).json({ error: 'Error communicating with AI' });
}
});
// Function to get AI response
async function getGroqChatCompletion(userMessage) {
return groq.chat.completions.create({
messages: [
{
role: "user",
content: userMessage
}
],
model: "mixtral-8x7b-32768"
});
}
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server started on port http://localhost:${PORT}`);
});