This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AiConsoleChat.cjs
311 lines (269 loc) · 10.9 KB
/
AiConsoleChat.cjs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Program: AiConsoleChat.cjs
See readme.txt for more information
Kristian Virtanen, 2023
krisu.virtanen@gmail.com
GNU General Public License v3.0
Source code https://github.com/EkBass/AiConsoleChat
*/
// Set your own API key here
// Get it from https://beta.openai.com/
// For the record, hardcoding API key is not recommended.
// I do it here for simplicity.
// Use environment variables instead.
const MyApiKey = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// human and AI messages are seperated by a tags.
const humanName = "Krisu"; // name of human
const aiName = "Bob"; // name of AI
const humanStart = "<" + humanName + ">"; // human start tag
const humanEnd = "</" + humanName + ">"; // human end tag
const aiStart = "<AI>"; // AI start tag
const aiEnd = "</AI>"; // AI end tag
const codeTagStartAlias = "<<<"; // code start tag alias
const codeTagEndAlias = ">>>"; // code end tag alias
const codeTag = "\n<code>\n"; // code start tag
const codeTagEnd = "\n</code>\n"; // code end tag
const logShort = "logShort.txt"; // short log file
const logFile = "log.txt"; // log file
const fileOpenTag = "<file>"; // file open tag when sending file to AI
const fileCloseTag = "</file>"; // file close tag when sending file to AI
// include fs, its used to read files
const fs = require('fs');
// include readline, its used to read user input from keyboard
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'human: '
});
// include OpenAIApi, the hero of this program
const { Configuration, OpenAIApi } = require("openai");
const { fail } = require('assert');
const configuration = new Configuration({
apiKey: MyApiKey,
});
const openai = new OpenAIApi(configuration);
// file "context.txt" includes instructions and context for the AI.
// It is allways passed to the AI as starting point for the conversation.
const context = fs.readFileSync("context.txt", "utf8") + "\n";
// variable shortHistory is used to store short history of chat.
let shortHistory = shortHistoryAdjust("", "");
// variable which stores AI codes aslong as program is running
// if user types ---catch, the code is written to file "catch.txt"
// its global now, so it can be used in other functions
// could have done better, but this is just a demo
let catchCode = "";
// So basicly program starts here
console.clear();
console.log("AiConsoleChat.cjs\nType ---help for help\n\n");
// program runs in loop until user types ---exit, ---quit or ---bye
rl.prompt(`${humanName}: `);
rl.on('line', async (userInput) => {
userInput = userInput.trim();
// tokenise user input in function tokeniseUserInput by spaces and return array
let tokenArray = tokeniseUserInput(userInput);
const firstToken = tokenArray[0];
// decisions by userInput
// exit application
if (firstToken === "---exit" || firstToken === "---quit" || firstToken === "---bye") {
rl.close();
console.log("Bye!");
process.exit();
}
// clear screen
if(firstToken === "---clear" || firstToken === "---cls") {
console.clear();
}
// if ---catch and show, then show catchCode
if(firstToken === "---catch" && tokenArray[1] === "show") {
console.log(catchCode);
}
// if ---catch and clear, then clear catchCode
if(firstToken === "---catch" && tokenArray[1] === "clear") {
catchCode = "";
console.log("catchCode cleared");
}
// if ---catch and write, then write catchCode to file
if(firstToken === "---catch" && tokenArray[1] === "file:" && tokenArray[2] !== "") {
// write catchCode to file
fs.writeFileSync(tokenArray[2], catchCode);
console.log("Code written to file " + tokenArray[2]);
// let's leave code there until new one comes
}
// reset short history
if(firstToken === "---reset") {
shortHistory = "";
}
// print short history
if(firstToken === "---history") {
console.log(shortHistory);
}
// show help
if (firstToken === "---help") {
console.log(fs.readFileSync("help.txt", "utf8"));
}
// show context
if (userInput === "---context") {
console.log(context);
}
// Load textfile in user input
if (userInput.startsWith("---load")) {
// check if file exists
if (fs.existsSync(tokenArray[1])) {
// load file
let file = fs.readFileSync(tokenArray[1], "utf8");
// add file to user input and send if by replacing user input
userInput = fileOpenTag + file + fileCloseTag;
// file must output to console
console.log(tokenArray[11] + ": " + file);
} else {
console.log("File not found!");
}
}
try {
// send userInput to AI if it is not a command or empty
if (userInput !== "" && !userInput.startsWith("---")) {
// create prompt
const prompt = createPrompt(userInput);
// adjust shortHistory with new message from user
shortHistory = shortHistoryAdjust(shortHistory, prompt);
// update log
writeLog(logFile, prompt);
// send prompt to OpenAI
const completion = await openai.createCompletion({
model: "text-davinci-003",
prompt: context + prompt,
temperature: 0.9,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0.0,
presence_penalty: 0.6,
});
// receive response from OpenAI
let response = completion.data.choices[0].text;
// catch AI code
catchCode = catchAiCode(response);
// AI adds tags to the response itself, we just add timestamp
response += getTimeStamp();
// add response to shortHistory
shortHistory = shortHistoryAdjust(shortHistory, response);
// update log
writeLog(logFile, response);
// output AI response after a few stylings are done
console.log(styleAiResponse(response));
}
} catch (error) {
console.log("Something is fucked up!!! " + error);
}
rl.prompt(`\n${humanName}: `);
// end of main loop
});
// End of main program
// function catches contents of code from last AI message.
// If user then types ---catch, the code is written to file "catch.txt"
function catchAiCode(message) {
// check if message contains <code> tag
if(message.includes("<code>")) {
// get code from message
let code = message.split("<code>")[1].split("</code>")[0];
// add code to catchCode
catchCode += code;
}
return catchCode;
}
// function tokenises user input by spaces and returns array
function tokeniseUserInput(userInput) {
let userInputArray = userInput.split(" ");
return userInputArray;
}
// function styles AI response
function styleAiResponse(response) {
// for chat console, we change AI to Bob
response = response.replace(aiStart, aiName + ": ");
response = response.replace(aiEnd, "");
// Force linebreak after each 40 characters
// But not in the middle of a word or if inside <code> tag
let responseArray = response.split(" ");
let responseArrayNew = [];
let lineLength = 0;
let codeTag = false;
for (let i = 0; i < responseArray.length; i++) {
// check if we are inside <code> tag
if(responseArray[i].includes("<code>")) {
codeTag = true;
}
if(responseArray[i].includes("</code>")) {
codeTag = false;
}
// if line is too long, add linebreak
if(lineLength > 40 && !codeTag) {
responseArrayNew.push("\n");
lineLength = 0;
}
// add word to new array
responseArrayNew.push(responseArray[i]);
// add length of word to lineLength
lineLength += responseArray[i].length;
}
// join array to string
response = responseArrayNew.join(" ");
// replace AI as Bob
response = response.replace(aiStart, aiName + ": ");
response = response.replace(aiEnd, "");
// but keep code tags for better viewing
return response;
}
// function creates prompt for OpenAI
function createPrompt(userInput) {
let prompt = humanStart + userInput + humanEnd + getTimeStamp();
// code alises "<<<" and ">>>" converted as <code> and </code> tags
prompt = prompt.replace(codeTagStartAlias, codeTag);
prompt = prompt.replace(codeTagEndAlias, codeTagEnd);
return prompt;
}
// function returns a timestamp in form DD:MM:YY HH:MM:SS
function getTimeStamp() {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
return "\n<ts>" + day + "." + month + "." + year + " " + hour + ":" + minute + ":" + second + "</ts>\n";
}
// function adjusts shortHistory. If it is too long, it is shortened.
// If it is empty, it is read from file.
// If it is ok, newMessage is added to it.
function shortHistoryAdjust(shortHistory, newMessage) {
// if shortHistory is empty, read it from file
if(shortHistory.length < 1) {
shortHistory = fs.readFileSync(logShort, "utf8");
}
// if check lenght of shortHistory > 1024
while(shortHistory.length > 1024) {
// get location of first timestamp
let firstTimestamp = shortHistory.indexOf("</ts>");
// remove everything before first timestamp
shortHistory = shortHistory.slice(firstTimestamp + 5);
// keep looping until shortHistory is ok
}
// lenght is ok, so we add newMessage to shortHistory
shortHistory += newMessage;
// write shortHistory to file
fs.writeFile(logShort, shortHistory, (err) => {
if (err) {
console.log("Error writing file: " + err);
}
});
return shortHistory;
}
// Write log to file
function writeLog(logFile, logMessage) {
fs.appendFile(logFile, logMessage, (err) => {
if (err) {
console.log("Error writing file: " + err);
}
});
}
// Thank you and goodbye