-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (32 loc) · 958 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
const { Configuration, OpenAIApi } = require("openai");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const configuration = new Configuration({
organization: "org",
Key: "key",
});
const openai = new OpenAIApi(configuration);
// cors-> help to send messages to and from different ports
// create an express api that calls the function above
const app = express();
app.use(bodyParser.json());
app.use(cors());
const port = 3080;
app.post("/", async (req, res) => {
const { message, currentModel } = req.body;
console.log(message, "message");
const response = await openai.createCompletion({
model: `${currentModel}`,
prompt: `${message}`,
max_tokens: 3000,
temperature: 0.5,
});
//console.log()
res.json({
message: response.data.choices[0].text,
});
});
app.listen(port, () => {
console.log(`Example app listening at http:// localhost :${port}`);
});