forked from render-examples/express-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (51 loc) · 1.54 KB
/
app.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require('dotenv').config()
const express = require("express");
const { Configuration, OpenAIApi } = require("openai");
const app = express();
const cors = require('cors');
const port = process.env.PORT || 3001;
const local = process.env.LOCAL || false;
// CORS
app.use(cors())
app.use((req, res, next) => {
const origin = req.headers.origin;
const allowedOrigins = [
'https://knandersen.github.io/',
'https://www.kevinandersen.dk',
'https://kevinandersen.dk',
local
]
if (allowedOrigins.includes(origin)) {
res.header("Access-Control-Allow-Origin", origin);
}
res.header("Access-Control-Allow-Methods", "GET");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
next();
})
// OpenAI config
const configuration = new Configuration({
apiKey: process.env.OPENAI_SECRET,
});
const openai = new OpenAIApi(configuration);
const completionTemplate = {
model: "text-curie-001",
prompt: "",
temperature: 0.6,
max_tokens: 35,
}
// Endpoints
app.get("/connect", (req, res) => {
res.type('text').send(`connected`)
});
app.get("/completion", async (req, res) => {
const prompt = req.query.prompt;
let input = completionTemplate;
input.prompt = prompt;
const response = await openai.createCompletion(input).catch(err => console.log(err));
if (response.data) {
res.type('text').send(response.data.choices[0].text);
} else {
res.send(503).send("Service unavailable")
}
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));