-
Notifications
You must be signed in to change notification settings - Fork 16
/
server.js
71 lines (52 loc) · 1.51 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Configuration, OpenAIApi } from "openai";
import dotenv from "dotenv";
import express from "express";
dotenv.config();
const app = express();
const port = process.env.PORT ? process.env.PORT : 8080;
const chat_log = new Map();
let start = Date.now();
console.log("starting:", start);
function clearMap() {
const millis = Date.now() - start;
if (Math.floor(millis / 1000) > 60 * 5) {
chat_log.clear();
start = Date.now();
console.log("chat log reloaded!");
}
}
const openai = new OpenAIApi(
new Configuration({
apiKey: process.env.APIKEY,
})
);
async function askAI(question, userId) {
const userLog = chat_log.get(userId) ? chat_log.get(userId) : [];
const promptionList = [...userLog, { role: "user", content: question }];
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: promptionList,
});
const answer = completion.data.choices[0].message;
// console.log(answer);
if (answer) {
clearMap();
chat_log.set(userId, [...promptionList, answer]);
}
return answer.content;
}
app.get("/", (req, res) => {
res.send("Hello ChatGPT!");
});
app.get("/chat/:userId/:message", (req, res) => {
if (req.params.message && req.params.userId) {
askAI(req.params.message, req.params.userId)
.then((reply) => res.send({ reply }))
.catch((err) => console.log(err));
} else {
res.send({ reply: "ChatGPT error..." });
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});