-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (30 loc) · 1.06 KB
/
server.js
File metadata and controls
37 lines (30 loc) · 1.06 KB
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
// server.js
const express = require('express');
const cors = require('cors');
const OpenAI = require('openai');
const app = express();
const port = 3000;
app.use(cors());
app.use(express.json());
const apiKey = ' sk-_08cnPc2peJMu4kYLe7RTf8oqAFPK9-S0akcOk08QdT3BlbkFJZ5FvcsfxqfuFk9dsXld6F0B9yMCIC3WCLHeStOTCwA'; // Replace with your OpenAI API key
const openai = new OpenAI({
apiKey: apiKey,
});
app.post('/emotion-color', async (req, res) => {
const { emotion } = req.body;
try {
const completion = await openai.chat.completions.create({
model: 'gpt-4', // Adjust the model as necessary
messages: [
{ role: 'user', content: `What color best represents the emotion: "${emotion}"? Respond with an RGB HEX code only.` }
],
});
res.json({ color: completion.choices[0].message.content.trim() });
} catch (err) {
console.error('Error:', err);
res.status(500).json({ error: 'An error occurred.' });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});