-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
32 lines (27 loc) · 993 Bytes
/
app.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
// app.js
const express = require('express');
const bodyParser = require('body-parser');
const { getGroqChatCompletion } = require('./groqUtils'); // Assuming you store Groq API functions in groqUtils.js
const app = express();
const port = process.env.PORT || 3000;
// Middleware
app.use(bodyParser.json());
app.use(express.static('public')); // Serve static files from 'public' directory
// Routes
app.post('/api/chat', async (req, res) => {
const { message } = req.body;
if (!message) {
return res.status(400).json({ error: 'Message is required' });
}
try {
const chatCompletion = await getGroqChatCompletion(message);
res.json({ completion: chatCompletion.choices[0]?.message?.content || "" });
} catch (error) {
console.error('Error fetching chat completion:', error);
res.status(500).json({ error: 'Failed to fetch chat completion' });
}
});
// Start server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});