-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (39 loc) · 1.17 KB
/
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
41
42
43
44
45
46
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
import env from "dotenv";
import { Configuration, OpenAIApi } from "openai";
const app = express();
env.config();
app.use(cors());
app.use(bodyParser.json());
const configuration = new Configuration({
organization: "org-daUY9yESlEpOHHKBPkyqCLer",
apiKey: process.env.API_KEY,
});
const openai = new OpenAIApi(configuration);
app.listen("3080", () => console.log("Listening on port 3080"));
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.post("/", async(req, res) => {
const { message } = req.body;
try {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `${message}`,
max_tokens: 400,
temperature: 0.5,
});
res.json({ message: response.data.choices[0].text });
} catch (error) {
// console.log(error)
// res.send(error).status(400)
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error.message);
}
}
});