-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
69 lines (61 loc) · 1.76 KB
/
main.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
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
import { ChatGPTAPI } from 'chatgpt';
import pTimeout from 'p-timeout';
import * as fs from 'fs';
let ChatGPTSessionToken =
'';
async function getChatGPTReply(content) {
const api = new ChatGPTAPI({ sessionToken: ChatGPTSessionToken });
await api.ensureAuth();
const threeMinutesMs = 3 * 60 * 1000;
const response = await pTimeout(api.sendMessage(content), {
milliseconds: threeMinutesMs,
message: 'ChatGPT timed out waiting for response',
});
return response;
}
app.get('/', async function (request, response) {
try {
const msg = request.query.msg;
if (!msg) {
response.send('问题不能为空哦');
return;
}
const resMsg = await getChatGPTReply(msg);
response.send(resMsg);
} catch (err) {
console.log(err);
response.send('错误');
}
});
app.post('/', async function (request, response) {
try {
const data = request.body;
if (!data.msg) {
response.send({ msg: '', retMsg: '问题不能为空哦' });
return;
}
const resMsg = await getChatGPTReply(data.msg);
response.send({ msg: data.msg, retMsg: resMsg });
} catch (err) {
console.log(err);
response.send({msg: '', retMsg: '错误' });
}
});
const sleep = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
// 本地运行开启以下
const PORT = 8848;
app.listen(PORT, async () => {
const config = await fs.readFileSync('./config.yaml');
ChatGPTSessionToken=config.toString().match(/TOKEN\: "(.*)"/)[1]
console.log(`应用正在监听 ${PORT} 端口!`);
});
export default app ;