-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (29 loc) · 907 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
import { Configuration, OpenAIApi } from "openai";
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
const configuration = new Configuration({
organization: "org-CTNzTKu6zgT3hu4HsrBjtR84",
apiKey: process.env.OPENAI_API_KEY
});
const openai = new OpenAIApi(configuration);
const app = express();
const port = 8000;
app.use(bodyParser.json());
app.use(cors);
app.post("/", async (req, res) => {
const {messages} = req.body;
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{"role": "system", "content": "You are a designed helpful chatGPT bot."},
...messages
]
})
res.json({
completion: completion.data.choices[0].message
});
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});