-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (34 loc) · 926 Bytes
/
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
const OpenAI = require("openai");
const { Configuration, OpenAIApi } = OpenAI;
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const port = 3001;
require("dotenv").config();
const configuration = new Configuration({
organization: "org-7unE3qksFvn3VcT494PaduJp",
apiKey: process.env.CHATGPT_API,
});
const openai = new OpenAIApi(configuration);
app.use(bodyParser.json());
app.use(cors());
app.post("/", async (req, res) => {
const { message } = req.body;
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `${message}`,
max_tokens: 2048,
temperature: 0,
});
if (response.data) {
if (response.data.choices) {
res.json({
message: response.data.choices[0].text,
});
}
}
});
app.listen(port, () => {
console.log("Example app port: " + port);
});