-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
38 lines (29 loc) · 810 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
const {OpenAI} = require('openai');
require('dotenv').config();
const API_KEY = {
'apiKey': process.env.API_KEY
}
const openai = new OpenAI(API_KEY);
const PORT = 8000;
const express = require('express');
const cors = require('cors');
const app = express();
app.use(express.json());
app.use(cors());
app.post("/completions", async (req, res) => {
try {
const completion = await openai.chat.completions.create({
messages: [
{
role: "user",
content: req.body.message
}
],
model: "gpt-3.5-turbo",
});
res.send(completion.choices[0].message);
} catch (e) {
console.log(e)
}
})
app.listen(PORT, () => console.log(`Your server is running on ${PORT}`))