-
Notifications
You must be signed in to change notification settings - Fork 16
/
reverse.js
79 lines (59 loc) · 1.91 KB
/
reverse.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
72
73
74
75
76
77
78
import { ChatGPTUnofficialProxyAPI } from "chatgpt";
import dotenv from "dotenv";
import express from "express";
dotenv.config();
const app = express();
const port = process.env.PORT ? process.env.PORT : 8765;
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 ChatGPTUnofficialProxyAPI({
accessToken: process.env.OPENAI_ACCESS_TOKEN,
apiReverseProxyUrl: "https://bypass.churchless.tech/api/conversation",
});
async function askAI(question, userId) {
const userLog = chat_log.get(userId) ? chat_log.get(userId) : undefined;
const systemMessage = `You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each responseIf you are generating a list, do not have too many items.
Current date: ${new Date().toISOString()}\n\n`;
const completion = userLog
? await openai.sendMessage(question, {
systemMessage,
...userLog,
})
: await openai.sendMessage(question, {
systemMessage,
});
const answer = completion.text;
// console.log(answer);
if (answer) {
clearMap();
chat_log.set(userId, {
parentMessageId: completion.parentMessageId,
conversationId: completion.conversationId,
});
}
return answer.replace("\n\n", "");
}
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}`);
});