-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.js
58 lines (50 loc) · 1.8 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
const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// Use body-parser to handle JSON request bodies
app.use(bodyParser.json());
// Serve static HTML page
app.use(express.static('public'));
// Analyze feedback route
app.post('/analyze-feedback', async (req, res) => {
const feedback = req.body.feedback;
if (!feedback) {
return res.status(400).json({ error: 'Feedback is required' });
}
try {
const openaiResponse = await axios.post(
'https://api.openai.com/v1/chat/completions',
{
model: 'gpt-3.5-turbo', // Using GPT-3.5 for better understanding and response
messages: [
{
role: 'system',
content: 'You are a helpful assistant that can analyze sentiment in text.'
},
{
role: 'user',
content: `Please analyze the sentiment of this feedback: "${feedback}".`
}
],
max_tokens: 150,
},
{
headers: {
'Authorization': `Bearer OPENAI_API_KEY`, // Replace with your OpenAI API key
'Content-Type': 'application/json'
},
}
);
// Parse the response from OpenAI
const analysis = openaiResponse.data.choices[0].message.content.trim();
return res.json({ sentimentAnalysis: analysis });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Something went wrong with the analysis' });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});