-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
73 lines (59 loc) · 1.56 KB
/
app.js
File metadata and controls
73 lines (59 loc) · 1.56 KB
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
import { sendToOpenAi } from "./src/sendChatWithChain.js";
import {
getChat,
saveChat,
getAllChats,
deleteChat,
} from "./src/fileHandller.js";
import express from "express";
import cors from "cors";
const app = express();
const port = 3003;
app.use(cors());
app.use(express.json());
// Routes
app.get("/", (req, res) => {
res.send("<h1>I think you are lost!</h1>");
});
app.post("/question", async (req, res) => {
let params = req.body;
if(!params || params == undefined || Object.keys(params).length == 0){
params = req.query
}
params = params.params
const responseAi = await sendToOpenAi(
params.question,
getChat(params.userId, params.chatId)
);
const chatObj = {
send: params.question,
response: responseAi,
};
await saveChat(chatObj, params.userId, params.chatId);
const allChats = getAllChats(params.userId);
res.send(allChats);
});
app.post("/alluserChat", (req, res) => {
let userId = req.body.userId;
if(!userId || userId == undefined){
userId = req.query.userId
}
const allChats = getAllChats(userId);
res.send(allChats);
});
app.post("/deleteChat", (req, res) => {
let params = req.body;
if(!params || params == undefined || Object.keys(params).length == 0){
params = req.query
}
params = params.params
const result = deleteChat(params.userId, params.chatId);
const allChats = getAllChats(params.userId);
if (allChats) {
res.send(allChats);
}
});
// listener
app.listen(port, () => {
console.log(`Ai app listening on port ${port}`);
});