-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
93 lines (77 loc) · 2.41 KB
/
app.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const express = require('express');
const { fetchStreamedChatContent } = require('streamed-chatgpt-api');
const cors = require('cors');
const app = express();
const port = process.env.PORT || 3000;
const corsOpts = {
origin: '*',
methods: [
'GET',
'POST',
],
allowedHeaders: [
'Content-Type',
],
};
app.use(cors(corsOpts));
// express middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
// Create an object to store chat history
const chatHistory = {};
const historyLength = 5; // Change this value to set the desired chat history length
// Define a route to serve the index.html file
app.get('/', (_, res) => {
res.sendFile('index.html', { root: __dirname + '/public' });
});
app.post('/chat', (req, res) => {
const apiKey = process.env.OPENAI_API_KEY;
console.log(apiKey);
const { message, username } = req.body;
// Check if username is provided
if (!username) {
return res.status(400).send('A username is required.');
}
// Initialize chat history for the user if it doesn't exist
if (!chatHistory[username]) {
chatHistory[username] = [];
}
// Update chat history with the user's message
chatHistory[username].push({ role: 'user', content: message });
// Prepare the message input with the user's chat history
// just giving an empty string as system role to keep chat ouput short
const messageInput = [
{
role: 'system',
content: '',
},
];
const recentHistory = chatHistory[username].slice(-historyLength);
recentHistory.forEach(msg => {
messageInput.push(msg);
});
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.setHeader('Transfer-Encoding', 'chunked');
let completeResponse = '';
console.log(messageInput)
fetchStreamedChatContent({
apiKey,
messageInput,
maxTokens: 1000,
temperature: 0.1,
}, (content) => {
res.write(content);
completeResponse += content;
}, () => {
res.end();
chatHistory[username].push({ role: 'assistant', content: completeResponse });
},
() => {
res.write("I'm sorry, there was an error processing your request.");
res.end();
});
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});