-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
206 lines (153 loc) · 5.28 KB
/
index.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
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
const readline = require('readline');
const simpleLLMChain = require('./mymodules/simpleLLMChain');
const simpleSequentialChain = require('./mymodules/simpleSequentialChain');
const { prompt_templates } = require('./prompts/prompts');
const utilities = require('./mymodules/utilities');
const fs = require('fs');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function displayMenu() {
let cont=0;
prompt_templates.forEach((template) => {
console.log(template.id + '. ' + template.description+'\n');
cont++;
});
// if (process.env.MYDEBUG === "true") {
// console.log('6. this is for test\n');
// }
console.log((cont+1)+ '. Exit\n');
}
function continueOrExit(rl) {
rl.question('Keep going? S/N ', (scelta) => {
const regexS = /^[Ss]$/;
const regexN = /^[Nn]$/;
if (regexS.test(scelta)) {
// if the answer is "S" or "s"
displayMenu();
askForChoice();
} else if (regexN.test(scelta)) {
console.log('---------------- MENU END-------------------');
console.log('---------------- GOODBYE-------------------');
rl.close();
} else {
console.log('Please type S to continue or N to exit');
continueOrExit(rl);
}
});
}
function handleTask(choice) {
console.log('Executing task ' + choice);
switch (choice) {
case "1":
rl.question('Enter the text to simplify: ', (text) => {
simpleLLMChain.simplifyText(text)
.then((res) => {
console.log(`Response= ${res}\n\n`);
if (process.env.LOG_ENABLED === "true") {
utilities.logFile(text, res);
}
continueOrExit(rl);
}).catch((err) => {
console.log('Something went wrong in simplifying text:');
console.error(err.error.message);
});
});
break;
case "2":
rl.question('Enter the English text to correct: ', (text) => {
simpleLLMChain.fixEnglish(text).then((res) => {
console.log(`Response= ${res}\n\n`);
if (process.env.LOG_ENABLED === "true") {
utilities.logFile(text, res);
}
continueOrExit(rl);
}).catch((err) => {
console.log('Something went wrong while fixing English:');
console.error(err.error.message);
});
});
break;
case "3":
rl.question('Enter the text to translate: ', (text) => {
rl.question('Now, the target language (for instance, italian): ', (targetLanguage) => {
simpleLLMChain.translateFromOneLanguageToAnother(text, targetLanguage).then((res) => {
console.log(`Response= ${res}\n\n`);
if (process.env.LOG_ENABLED === "true") {
utilities.logFile(text, res);
}
continueOrExit(rl);
}).catch((err) => {
console.log('Something went wrong while translating:');
console.error(err.error.message);
});
})
});
break;
case "4":
rl.question('Enter the text to summarize: ', (text) => {
simpleLLMChain.summarize(text).then((res) => {
console.log(`Response= ${res}\n\n`);
if (process.env.LOG_ENABLED === "true") {
utilities.logFile(text, res);
}
continueOrExit(rl);
}).catch((err) => {
console.log('Something went wrong while summaring:');
console.error(err.error.message);
});
});
break;
case "5":
rl.question('Enter the word so I can find a synonym and antonym: ', (text) => {
simpleLLMChain.findSynonymAntonym(text).then((res) => {
console.log(`Response= ${res}\n\n`);
if (process.env.LOG_ENABLED === "true") {
utilities.logFile(text, res);
}
continueOrExit(rl);
}).catch((err) => {
console.log('Something went wrong while finding synonyms and antonyms:');
console.error(err.error.message);
});
});
break;
case "6":
try{
fs.readFile(process.env.BLOG_FILE, 'utf8', (err, text) => {
if (err) {
console.error('Error while reading file:', err);
rl.close();
return;
}
simpleSequentialChain.fixEnglishAndCreateSummaryLinkedin(text).then((res) => {
console.log(`Response= ${res}\n\n`);
if (process.env.LOG_ENABLED === "true") {
utilities.logFile(text, res);
}
continueOrExit(rl);
}).catch((err) => {
console.log('Something went wrong while creating blog post for Linkedin:');
console.error(err.error.message);
});
});
}catch(e){
console.log(e);
}
break;
case "7":
console.log('---------------- MENU END-------------------');
console.log('---------------- GOODBYE-------------------');
rl.close();
break;
}
}
function askForChoice() {
rl.question('\nSelect an option from the list below: ', (choice) => {
handleTask(choice);
});
}
// Execution
displayMenu();
askForChoice();