-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (44 loc) · 1.3 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
// server.js
const express = require('express');
const app = express();
const port = 3000;
require('dotenv').config();
const axios = require('axios');
const path = require('path');
// Middleware to parse JSON requests
app.use(express.json());
// Serve static files from the project root
app.use(express.static(path.join(__dirname, '/')));
// Endpoint to handle AI feedback requests
app.post('/api/ai-feedback', async (req, res) => {
const prompt = req.body.prompt;
if (!prompt) {
return res.status(400).send('Prompt is required.');
}
try {
const response = await axios.post(
'https://api.openai.com/v1/chat/completions',
{
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: prompt }],
max_tokens: 150,
temperature: 0.7,
},
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
}
);
const feedback = response.data.choices[0].message.content.trim();
res.json({ feedback });
} catch (error) {
console.error(error.response ? error.response.data : error.message);
res.status(500).send('Error communicating with OpenAI API');
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});