-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (33 loc) · 977 Bytes
/
index.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
const cors = require('cors');
const bodyParser = require('body-parser');
const express = require('express');
const dotenv = require('dotenv').config();
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
// organization: process.env.ORGANIZATION,
apiKey: process.env.API_KEY,
});
const openai = new OpenAIApi(configuration);
const app = express();
const port = 8080;
app.use(cors());
app.use(bodyParser.json())
app.post('/', async (req, res) => {
try {
const response = await openai.createCompletion({
model: "text-davinci-002",
prompt: req.body.message,
max_tokens: 2000,
temperature: 0.1
});
res.json({
message: response.data
})
} catch (e) {
console.error(e);
res.status(500).json({ error: e.message });
}
})
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
})