-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (30 loc) · 904 Bytes
/
Copy pathserver.js
File metadata and controls
35 lines (30 loc) · 904 Bytes
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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { OpenAI } = require('openai');
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(bodyParser.json());
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
dangerouslyAllowBrowser: true,
});
app.post('/api/generate', async (req, res) => {
const { prompt } = req.body;
try {
const response = await openai.completions.create({
model: "text-davinci-003",
prompt,
max_tokens: 50,
});
res.json(response);
} catch (error) {
console.error('Error generating completion:', error);
res.status(500).json({ error: error.message });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});