-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (51 loc) · 2.12 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
const express = require('express');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');
dotenv.config(); // Load environment variables from .env file
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(express.static('public')); // Serve static files from the 'public' directory
app.post('/api/generate', async (req, res) => {
const { userInput } = req.body;
if (!userInput) {
return res.status(400).json({ error: 'User input is required' });
}
// Verify that environment variables are loaded
console.log('OPENAI_API_KEY:', process.env.OPENAI_API_KEY);
console.log('OPENAI_ORG_ID:', process.env.OPENAI_ORG_ID);
console.log('OPENAI_PROJECT_ID:', process.env.OPENAI_PROJECT_ID);
try {
const fetch = await import('node-fetch');
const response = await fetch.default('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
'OpenAI-Organization': process.env.OPENAI_ORG_ID,
'OpenAI-Project': process.env.OPENAI_PROJECT_ID,
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: "You are an expert marketing strategist and brand manager. Please provide detailed and specific recommendations with a 1 sentence explanation for each recommendation. Don't return anything else" },
{ role: "user", content: userInput }
],
max_tokens: 150
}),
});
const data = await response.json();
if (response.ok) {
res.json(data);
} else {
res.status(response.status).json(data);
}
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
module.exports = app;