-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (35 loc) · 1.14 KB
/
server.js
File metadata and controls
41 lines (35 loc) · 1.14 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
38
39
40
41
const express = require('express');
const axios = require('axios');
const dotenv = require('dotenv');
const path = require('path');
dotenv.config();
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.get('/api/generate-response/', async (req, res) => {
try {
const response = await axios.post(
'https://api.openai.com/v1/chat/completions',
{
model: 'gpt-3.5-turbo',
messages: [{ role: 'system', content: 'Write me a funny short story with lots of creativity. Have it be about whatever you want!' }],
max_tokens: 250,
temperature: 1,
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
},
}
);
const answer = response.data.choices[0].message.content.trim();
res.status(200).json({ answer });
} catch (error) {
console.error('Error generating response:', error.message);
res.status(500).json({ error: error.message });
}
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});