Upload folder using huggingface_hub
Browse files- static/javascript/rag_cleaner.js +88 -63
- static/javascript/rag_mgr.js +29 -83
- static/javascript/rag_rqs.js +3 -2
- static/javascript/ualog.js +1 -1
- static/js/ragrqs.min.js +15 -12
static/javascript/rag_cleaner.js
CHANGED
@@ -2,90 +2,115 @@
|
|
2 |
|
3 |
const removeChars = (txt) => {
|
4 |
const charsRm = /[\u00AD\u200B\u200C\u200D\u2060\uFEFF]/g;
|
5 |
-
txt = txt.replace(charsRm,
|
6 |
-
txt = Array.from(txt)
|
|
|
|
|
7 |
return txt;
|
8 |
};
|
9 |
|
10 |
const replaceChars = (txt) => {
|
11 |
const charsSrp = /[\u00A0\u2000-\u200A\u202F\u205F\u3000\t\r\f\v]/g;
|
12 |
-
txt = txt.replace(charsSrp,
|
13 |
-
txt = txt.replace(/ +/g,
|
14 |
return txt.trim();
|
15 |
};
|
16 |
|
17 |
const removeTag = (txt) => {
|
18 |
-
txt = txt.replace(/<<<|>>>|<<|>>/g,
|
19 |
return txt;
|
20 |
};
|
21 |
|
22 |
function cleanDoc(txt) {
|
23 |
try {
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
} catch (e) {
|
39 |
-
|
40 |
-
|
41 |
}
|
42 |
-
}
|
43 |
|
44 |
-
function cleanResponse(txt){
|
45 |
try {
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
} catch (e) {
|
59 |
-
|
60 |
-
|
61 |
}
|
62 |
-
}
|
63 |
|
64 |
-
function
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
text = text.replace(/\n2,}/g, '\n');
|
75 |
-
// text = text.replace(/\n{2,}/g, "\n");
|
76 |
-
// Rimuove gli spazi bianchi extra all'inizio di ogni riga
|
77 |
-
text = text.replace(/^[ \t]+/gm, "");
|
78 |
-
// Sostituisce i trattini bassi multipli con una linea orizzontale
|
79 |
-
// text = text.replace(/_{3,}/g, '\n\n' + '-'.repeat(40) + '\n\n');
|
80 |
-
// Formatta gli elenchi puntati per una migliore leggibilità
|
81 |
-
text = text.replace(/^(\s*[-*•])(\s*)/gm, "\n$1 ");
|
82 |
-
// Formatta gli elenchi numerati per una migliore leggibilità
|
83 |
-
text = text.replace(/^(\s*\d+\.)(\s*)/gm, "\n$1 ");
|
84 |
-
// Aggiunge una riga vuota prima e dopo i blocchi di codice
|
85 |
-
text = text.replace(/(```[\s\S]*?```)/g, "\n\n$1\n\n");
|
86 |
-
// Aggiunge un'andata a capo dopo ogni punto, eccetto quando seguito da newline o fine stringa
|
87 |
-
// text = text.replace(/\.(?!\n|$)/g, '.\n');
|
88 |
-
// Rimuove gli spazi bianchi extra alla fine di ogni riga
|
89 |
-
text = text.replace(/[ \t]+$/gm, "");
|
90 |
-
return text;
|
91 |
}
|
|
|
2 |
|
3 |
const removeChars = (txt) => {
|
4 |
const charsRm = /[\u00AD\u200B\u200C\u200D\u2060\uFEFF]/g;
|
5 |
+
txt = txt.replace(charsRm, "");
|
6 |
+
txt = Array.from(txt)
|
7 |
+
.filter((ch) => !/^C/.test(ch.codePointAt(0)))
|
8 |
+
.join("");
|
9 |
return txt;
|
10 |
};
|
11 |
|
12 |
const replaceChars = (txt) => {
|
13 |
const charsSrp = /[\u00A0\u2000-\u200A\u202F\u205F\u3000\t\r\f\v]/g;
|
14 |
+
txt = txt.replace(charsSrp, " ");
|
15 |
+
txt = txt.replace(/ +/g, " ");
|
16 |
return txt.trim();
|
17 |
};
|
18 |
|
19 |
const removeTag = (txt) => {
|
20 |
+
txt = txt.replace(/<<<|>>>|<<|>>/g, "");
|
21 |
return txt;
|
22 |
};
|
23 |
|
24 |
function cleanDoc(txt) {
|
25 |
try {
|
26 |
+
txt = removeTag(txt);
|
27 |
+
txt = removeChars(txt);
|
28 |
+
txt = replaceChars(txt);
|
29 |
+
txt = txt.replace(/\n/g, " ");
|
30 |
+
txt = txt.replace(/\t/g, " ");
|
31 |
+
txt = txt.replace(/ +/g, " ");
|
32 |
+
txt = txt.replace(/\n\s*\n/g, "\n");
|
33 |
+
txt = txt.replace(/[“”]/g, '"');
|
34 |
+
txt = txt.replace(/[‘’]/g, "'");
|
35 |
+
txt = txt.replace(/[«»„“]/g, '"');
|
36 |
+
txt = txt.replace(/[–—]/g, "-");
|
37 |
+
txt = txt.replace(/\\[nrt]/g, "");
|
38 |
+
txt = txt.replace(/[^\w\sàèéìòùÀÈÉÌÒÙáéíóúÁÉÍÓÚäëïöüÄËÏÖÜâêîôûÂÊÎÔÛçÇñÑ.,;:!?'"()-]/g, "");
|
39 |
+
return txt.trim();
|
40 |
} catch (e) {
|
41 |
+
xerror(e);
|
42 |
+
return "Errore di codifica nel documento";
|
43 |
}
|
44 |
+
}
|
45 |
|
46 |
+
function cleanResponse(txt) {
|
47 |
try {
|
48 |
+
txt = removeChars(txt);
|
49 |
+
txt = replaceChars(txt);
|
50 |
+
txt = txt.replace(/\t/g, " ");
|
51 |
+
txt = txt.replace(/ +/g, " ");
|
52 |
+
txt = txt.replace(/\n\s*\n/g, "\n");
|
53 |
+
txt = txt.replace(/[“”]/g, '"');
|
54 |
+
txt = txt.replace(/[‘’]/g, "'");
|
55 |
+
txt = txt.replace(/[«»„“]/g, '"');
|
56 |
+
txt = txt.replace(/[–—]/g, "-");
|
57 |
+
txt = txt.replace(/\\[nrt]/g, "");
|
58 |
+
txt = txt.replace(/[^\w\sàèéìòùÀÈÉÌÒÙáéíóúÁÉÍÓÚäëïöüÄËÏÖÜâêîôûÂÊÎÔÛçÇñÑ.,;:!?'"()-]/g, "");
|
59 |
+
return txt.trim();
|
60 |
} catch (e) {
|
61 |
+
xerror(e);
|
62 |
+
return "Errore di codifica nela risposta";
|
63 |
}
|
64 |
+
}
|
65 |
|
66 |
+
function cleanOutput(txt) {
|
67 |
+
try {
|
68 |
+
txt = removeChars(txt);
|
69 |
+
txt = replaceChars(txt);
|
70 |
+
txt = txt.replace(/\t/g, " ");
|
71 |
+
txt = txt.replace(/ +/g, " ");
|
72 |
+
txt = txt.replace(/\n\s*\n/g, "\n");
|
73 |
+
txt = txt.replace(/[“”]/g, '"');
|
74 |
+
txt = txt.replace(/[‘’]/g, "'");
|
75 |
+
txt = txt.replace(/[«»„“]/g, '"');
|
76 |
+
txt = txt.replace(/[–—]/g, "-");
|
77 |
+
txt = txt.replace(/\\[nrt]/g, "");
|
78 |
+
txt = txt.replace(/[^\w\sàèéìòùÀÈÉÌÒÙáéíóúÁÉÍÓÚäëïöüÄËÏÖÜâêîôûÂÊÎÔÛçÇñÑ.,;:!?'"()-]/g, "");
|
79 |
+
return txt.trim();
|
80 |
+
} catch (e) {
|
81 |
+
xerror(e);
|
82 |
+
return "Errore di codifica nell'output";
|
83 |
+
}
|
84 |
+
}
|
85 |
+
|
86 |
+
function list2text(docList) {
|
87 |
+
const docDict = {};
|
88 |
+
|
89 |
+
function getLabel(txt) {
|
90 |
+
const match = txt.match(/<<<(.*?)>>>/);
|
91 |
+
return match ? match[1] : null;
|
92 |
+
}
|
93 |
|
94 |
+
for (let doc of docList) {
|
95 |
+
const docName = getLabel(doc);
|
96 |
+
// const content = doc.replace(/<<<.*?>>>/g, ''); //TODO
|
97 |
+
const content = doc.replace(/<<<.*?>>>/, "");
|
98 |
+
if (docName in docDict) {
|
99 |
+
docDict[docName] += `\n ${content}`;
|
100 |
+
} else {
|
101 |
+
docDict[docName] = content;
|
102 |
+
}
|
103 |
+
}
|
104 |
+
const lst = [];
|
105 |
+
for (let [docName, content] of Object.entries(docDict)) {
|
106 |
+
lst.push(`Documento: ${docName}.\n${content}`);
|
107 |
+
}
|
108 |
+
return lst.join("\n\n");
|
109 |
+
}
|
110 |
|
111 |
+
// <<<doc_name>>> => Documento: doc_name
|
112 |
+
function subResponseDOcTag(txt) {
|
113 |
+
const regex = /<<<(.*?)>>>/;
|
114 |
+
const result = txt.replace(regex, (match, p1) => `Documento: ${p1}`);
|
115 |
+
return result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
}
|
static/javascript/rag_mgr.js
CHANGED
@@ -18,7 +18,7 @@
|
|
18 |
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
19 |
*/
|
20 |
"use strict";
|
21 |
-
const MAX_PROMPT_LENGTH = 1024 *
|
22 |
// decremento dopo errore per tokens eccessivi
|
23 |
const PROMPT_DECR = 1024 * 2;
|
24 |
|
@@ -68,13 +68,13 @@ const Rag = {
|
|
68 |
}, 0);
|
69 |
return rspsl;
|
70 |
},
|
71 |
-
|
72 |
const maxl = MAX_PROMPT_LENGTH;
|
73 |
const rspsl = this.responsesLength();
|
74 |
const d = " ";
|
75 |
-
let s = `${msg} mx:${maxl} lft:${
|
76 |
xlog(s);
|
77 |
-
s = `${msg}${d}${
|
78 |
//nsg plft.length prgt.length responses.length
|
79 |
UaLog.log(s);
|
80 |
},
|
@@ -104,10 +104,10 @@ const Rag = {
|
|
104 |
}
|
105 |
return size;
|
106 |
},
|
107 |
-
getPartDoc(
|
108 |
-
const
|
109 |
-
|
110 |
-
return [
|
111 |
},
|
112 |
|
113 |
errorInfo(err) {
|
@@ -129,11 +129,6 @@ const Rag = {
|
|
129 |
DataMgr.readDbDocNames();
|
130 |
DataMgr.readDbDocs();
|
131 |
this.ragQuery = query;
|
132 |
-
// this.ragContext = ""; //XXX
|
133 |
-
// this.ragResponse = "";
|
134 |
-
// this.responses = [];
|
135 |
-
// this.prompts = [];
|
136 |
-
// ThreadMgr.rows=[];
|
137 |
let ndoc = 0;
|
138 |
|
139 |
try {
|
@@ -142,30 +137,24 @@ const Rag = {
|
|
142 |
if (doc.trim() == "") continue;
|
143 |
const doc_name = DataMgr.doc_names[i];
|
144 |
const doc_entire_len = doc.length;
|
145 |
-
UaLog.log(`${doc_name} (${doc_entire_len}) `);
|
146 |
xlog(`${doc_name} (${doc_entire_len}) `);
|
|
|
|
|
147 |
++ndoc;
|
148 |
let npart = 0;
|
149 |
-
let partLft = "";
|
150 |
-
let partRgt = "";
|
151 |
let decr = 0;
|
152 |
let prompt = "";
|
153 |
-
|
|
|
|
|
154 |
while (true) {
|
155 |
let partSize = this.getPartSize(doc, promptDoc("", query), decr);
|
156 |
-
if (partSize < 10)
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
prompt = promptDoc(partLft, query);
|
163 |
-
// xlog("------------");
|
164 |
-
// xlog("decr:", decr);
|
165 |
-
// xlog("partSize:", partSize);
|
166 |
-
// xlog("partLft.lemngth:", partLft.length);
|
167 |
-
// xlog("partRgt.lemngth:", partRgt.length);
|
168 |
-
// xlog("prompt.length;", prompt.length);
|
169 |
const payload = getPayloadDoc(prompt);
|
170 |
let text;
|
171 |
try {
|
@@ -174,8 +163,8 @@ const Rag = {
|
|
174 |
} catch (err) {
|
175 |
const e = this.errorInfo(err);
|
176 |
if (e.errorType === ERROR_TOKENS) {
|
177 |
-
UaLog.log(`Error tokens.1 ${
|
178 |
-
|
179 |
decr += PROMPT_DECR;
|
180 |
continue;
|
181 |
} else {
|
@@ -184,62 +173,19 @@ const Rag = {
|
|
184 |
throw s;
|
185 |
}
|
186 |
}
|
|
|
187 |
npart++;
|
188 |
-
doc =
|
189 |
-
|
190 |
-
|
191 |
-
}
|
192 |
-
|
193 |
-
|
194 |
-
}
|
195 |
-
}
|
196 |
} catch (error) {
|
197 |
alert("requestDocsRAG(1)\n" + error);
|
198 |
xerror(error);
|
199 |
}
|
200 |
|
201 |
-
// elenco risposte accumulate => contesto
|
202 |
-
/*
|
203 |
-
try {
|
204 |
-
let resps = list2text(this.responses);
|
205 |
-
let decr = 0;
|
206 |
-
let text = "";
|
207 |
-
let prompt = "";
|
208 |
-
|
209 |
-
while (true) {
|
210 |
-
resps = this.truncInput(resps, decr);
|
211 |
-
prompt = promptToContext(resps, query);
|
212 |
-
const payload = getPlayloadContext(prompt);
|
213 |
-
|
214 |
-
try {
|
215 |
-
text = await HfRequest.post(payload);
|
216 |
-
if (!text) return "";
|
217 |
-
} catch (err) {
|
218 |
-
const e = this.errorInfo(err);
|
219 |
-
if (e.errorType === ERROR_TOKENS) {
|
220 |
-
UaLog.log(`Error tokens.2 ${prompt.length}`);
|
221 |
-
xlog(`Error tokens. ${prompt.length}`);
|
222 |
-
decr += PROMPT_DECR;
|
223 |
-
continue;
|
224 |
-
} else {
|
225 |
-
xerror(err);
|
226 |
-
const s = this.errorTotext(err);
|
227 |
-
throw s;
|
228 |
-
}
|
229 |
-
}
|
230 |
-
break;
|
231 |
-
}
|
232 |
-
const pl = prompt.length;
|
233 |
-
// text = cleanContext(text);
|
234 |
-
this.ragContext = text;
|
235 |
-
this.saveToDb();
|
236 |
-
UaLog.log(`Contesto (${pl})`);
|
237 |
-
} catch (error) {
|
238 |
-
xerror(error);
|
239 |
-
alert("requestDocsRAG(2)\n" + error);
|
240 |
-
throw error;
|
241 |
-
}
|
242 |
-
*/
|
243 |
const resps = list2text(this.responses);
|
244 |
this.ragContext = resps;
|
245 |
this.saveToDb();
|
@@ -349,7 +295,7 @@ const Rag = {
|
|
349 |
const e = this.errorInfo(err);
|
350 |
if (e.errorType === ERROR_TOKENS) {
|
351 |
UaLog.log(`Error tokens.5 ${prompt.length}`);
|
352 |
-
|
353 |
decr += PROMPT_DECR;
|
354 |
continue;
|
355 |
} else {
|
|
|
18 |
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
19 |
*/
|
20 |
"use strict";
|
21 |
+
const MAX_PROMPT_LENGTH = 1024 * 85;
|
22 |
// decremento dopo errore per tokens eccessivi
|
23 |
const PROMPT_DECR = 1024 * 2;
|
24 |
|
|
|
68 |
}, 0);
|
69 |
return rspsl;
|
70 |
},
|
71 |
+
ragLog(msg, lftLen, rgtLen) {
|
72 |
const maxl = MAX_PROMPT_LENGTH;
|
73 |
const rspsl = this.responsesLength();
|
74 |
const d = " ";
|
75 |
+
let s = `${msg} mx:${maxl} lft:${lftLen} rgt:${rgtLen} arr:${rspsl}`;
|
76 |
xlog(s);
|
77 |
+
s = `${msg}${d}${lftLen}${d}${rgtLen}${d}${rspsl}`;
|
78 |
//nsg plft.length prgt.length responses.length
|
79 |
UaLog.log(s);
|
80 |
},
|
|
|
104 |
}
|
105 |
return size;
|
106 |
},
|
107 |
+
getPartDoc(pRgt, partSize) {
|
108 |
+
const pLft = pRgt.substring(0, partSize);
|
109 |
+
pRgt = pRgt.substring(partSize).trim();
|
110 |
+
return [pLft, pRgt];
|
111 |
},
|
112 |
|
113 |
errorInfo(err) {
|
|
|
129 |
DataMgr.readDbDocNames();
|
130 |
DataMgr.readDbDocs();
|
131 |
this.ragQuery = query;
|
|
|
|
|
|
|
|
|
|
|
132 |
let ndoc = 0;
|
133 |
|
134 |
try {
|
|
|
137 |
if (doc.trim() == "") continue;
|
138 |
const doc_name = DataMgr.doc_names[i];
|
139 |
const doc_entire_len = doc.length;
|
|
|
140 |
xlog(`${doc_name} (${doc_entire_len}) `);
|
141 |
+
UaLog.log(`${doc_name} (${doc_entire_len}) `);
|
142 |
+
|
143 |
++ndoc;
|
144 |
let npart = 0;
|
|
|
|
|
145 |
let decr = 0;
|
146 |
let prompt = "";
|
147 |
+
let lft = "";
|
148 |
+
let rgt = "";
|
149 |
+
|
150 |
while (true) {
|
151 |
let partSize = this.getPartSize(doc, promptDoc("", query), decr);
|
152 |
+
if (partSize < 10) {
|
153 |
+
break;
|
154 |
+
}
|
155 |
+
[lft, rgt] = this.getPartDoc(doc, partSize);
|
156 |
+
this.ragLog(`${ndoc},${npart + 1}`, lft.length, rgt.length);
|
157 |
+
prompt = promptDoc(lft, query);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
const payload = getPayloadDoc(prompt);
|
159 |
let text;
|
160 |
try {
|
|
|
163 |
} catch (err) {
|
164 |
const e = this.errorInfo(err);
|
165 |
if (e.errorType === ERROR_TOKENS) {
|
166 |
+
UaLog.log(`Error tokens.1 ${lft.length}`);
|
167 |
+
xerror(`Error tokens. ${prompt.length}`);
|
168 |
decr += PROMPT_DECR;
|
169 |
continue;
|
170 |
} else {
|
|
|
173 |
throw s;
|
174 |
}
|
175 |
}
|
176 |
+
|
177 |
npart++;
|
178 |
+
doc = rgt;
|
179 |
+
const docText = `<<<${doc_name}>>>\n${text}`; //TODO
|
180 |
+
this.responses.push(docText);
|
181 |
+
// this.ragLog(`${doc_name} ${ndoc},${npart}`, lft.length, rgt.length);
|
182 |
+
} // end while
|
183 |
+
} // end for
|
|
|
|
|
184 |
} catch (error) {
|
185 |
alert("requestDocsRAG(1)\n" + error);
|
186 |
xerror(error);
|
187 |
}
|
188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
const resps = list2text(this.responses);
|
190 |
this.ragContext = resps;
|
191 |
this.saveToDb();
|
|
|
295 |
const e = this.errorInfo(err);
|
296 |
if (e.errorType === ERROR_TOKENS) {
|
297 |
UaLog.log(`Error tokens.5 ${prompt.length}`);
|
298 |
+
xerror(`Error tokens. ${prompt.length}`);
|
299 |
decr += PROMPT_DECR;
|
300 |
continue;
|
301 |
} else {
|
static/javascript/rag_rqs.js
CHANGED
@@ -19,7 +19,7 @@
|
|
19 |
*/
|
20 |
|
21 |
"use strict";
|
22 |
-
const VERS = "0.1.
|
23 |
|
24 |
var xlog = console.log;
|
25 |
var xerror = console.error;
|
@@ -118,7 +118,8 @@ function elencoRisposte(e) {
|
|
118 |
if (rs.length == 0) return;
|
119 |
const text = rs
|
120 |
.map((x, i) => {
|
121 |
-
|
|
|
122 |
})
|
123 |
.join("\n");
|
124 |
wnds.wpre.show(text);
|
|
|
19 |
*/
|
20 |
|
21 |
"use strict";
|
22 |
+
const VERS = "0.1.40 (28-07-2024)";
|
23 |
|
24 |
var xlog = console.log;
|
25 |
var xerror = console.error;
|
|
|
118 |
if (rs.length == 0) return;
|
119 |
const text = rs
|
120 |
.map((x, i) => {
|
121 |
+
x = subResponseDOcTag(x);
|
122 |
+
return `\n[${i + 1}]\n ${x.trim()}`;
|
123 |
})
|
124 |
.join("\n");
|
125 |
wnds.wpre.show(text);
|
static/javascript/ualog.js
CHANGED
@@ -64,7 +64,7 @@ var UaLog = {
|
|
64 |
maxHeight: "400px",
|
65 |
overflow: "auto",
|
66 |
// scrollbarColor: "#ab3205 #ffffff",
|
67 |
-
scrollbarColor: "
|
68 |
scrollbarWidth: "auto",
|
69 |
};
|
70 |
for (const k in msg_css) msg.style[k] = msg_css[k];
|
|
|
64 |
maxHeight: "400px",
|
65 |
overflow: "auto",
|
66 |
// scrollbarColor: "#ab3205 #ffffff",
|
67 |
+
scrollbarColor: "#027876 #454444",
|
68 |
scrollbarWidth: "auto",
|
69 |
};
|
70 |
for (const k in msg_css) msg.style[k] = msg_css[k];
|
static/js/ragrqs.min.js
CHANGED
@@ -2,7 +2,9 @@ const removeChars=a=>{a=a.replace(/[\u00AD\u200B\u200C\u200D\u2060\uFEFF]/g,"");
|
|
2 |
function cleanDoc(a){try{return a=removeTag(a),a=removeChars(a),a=replaceChars(a),a=a.replace(/\n/g," "),a=a.replace(/\t/g," "),a=a.replace(/ +/g," "),a=a.replace(/\n\s*\n/g,"\n"),a=a.replace(/[\u201c\u201d]/g,'"'),a=a.replace(/[\u2018\u2019]/g,"'"),a=a.replace(/[\u00ab\u00bb\u201e\u201c]/g,'"'),a=a.replace(/[\u2013\u2014]/g,"-"),a=a.replace(/\\[nrt]/g,""),a=a.replace(/[^\w\s\u00e0\u00e8\u00e9\u00ec\u00f2\u00f9\u00c0\u00c8\u00c9\u00cc\u00d2\u00d9\u00e1\u00e9\u00ed\u00f3\u00fa\u00c1\u00c9\u00cd\u00d3\u00da\u00e4\u00eb\u00ef\u00f6\u00fc\u00c4\u00cb\u00cf\u00d6\u00dc\u00e2\u00ea\u00ee\u00f4\u00fb\u00c2\u00ca\u00ce\u00d4\u00db\u00e7\u00c7\u00f1\u00d1.,;:!?'"()-]/g,
|
3 |
""),a.trim()}catch(b){return xerror(b),"Errore di codifica nel documento"}}
|
4 |
function cleanResponse(a){try{return a=removeChars(a),a=replaceChars(a),a=a.replace(/\t/g," "),a=a.replace(/ +/g," "),a=a.replace(/\n\s*\n/g,"\n"),a=a.replace(/[\u201c\u201d]/g,'"'),a=a.replace(/[\u2018\u2019]/g,"'"),a=a.replace(/[\u00ab\u00bb\u201e\u201c]/g,'"'),a=a.replace(/[\u2013\u2014]/g,"-"),a=a.replace(/\\[nrt]/g,""),a=a.replace(/[^\w\s\u00e0\u00e8\u00e9\u00ec\u00f2\u00f9\u00c0\u00c8\u00c9\u00cc\u00d2\u00d9\u00e1\u00e9\u00ed\u00f3\u00fa\u00c1\u00c9\u00cd\u00d3\u00da\u00e4\u00eb\u00ef\u00f6\u00fc\u00c4\u00cb\u00cf\u00d6\u00dc\u00e2\u00ea\u00ee\u00f4\u00fb\u00c2\u00ca\u00ce\u00d4\u00db\u00e7\u00c7\u00f1\u00d1.,;:!?'"()-]/g,""),
|
5 |
-
a.trim()}catch(b){return xerror(b),"Errore di codifica nela risposta"}}
|
|
|
|
|
6 |
return a},saveArray(a,b){b=JSON.stringify(b);UaDb.save(a,b)},readArray(a){a=UaDb.read(a);return 0==a.trim().length?[]:JSON.parse(a)},saveJson(a,b){b=JSON.stringify(b);UaDb.save(a,b)},readJson(a){return(a=UaDb.read(a))?JSON.parse(a):{}}},DataMgr={docs:[],doc_names:[],linkToName(a){a=a.split("/");return a[a.length-1]},async loadDoc(a){try{const b=await requestGet(a),c=cleanDoc(b),d=this.linkToName(a);this.doc_names.push(d);this.docs.push(c);this.saveDbDocs();return c}catch(b){alert("loadDoc()\n"+b+
|
7 |
"\n"+a)}},addDoc(a,b){b=cleanDoc(b);this.docs.push(b);this.doc_names.push(a);this.saveDbDocs()},saveDbDocs(){UaDb.saveArray("id_doc_names",this.doc_names);UaDb.saveArray("id_docs",this.docs)},readDbDocs(){this.docs=UaDb.readArray("id_docs")},readDbDocNames(){this.doc_names=UaDb.readArray("id_doc_names")},deleteJsonDati(){const a=UaDb.getAllIds();for(const b of a)["id_docs","id_doc_names"].includes(b)||UaDb.delete(b);Rag.ragQuery="";Rag.ragContext="";Rag.ragResponse="";Rag.responses=[];Rag.prompts=
|
8 |
[];ThreadMgr.rows=[]}};const arr=["bWtkW0l+XX0=","SXx2d1FxbVc=","V1tRXlxneUY=","flV6a1NdbUg=","VllpUkc="],model_name="mistralai/Mistral-7B-Instruct-v0.3",umgm=a=>a.map(b=>atob(b).split("").map(c=>String.fromCharCode((c.charCodeAt(0)-5+256)%256)).join("")).join(""),tm=umgm(arr);
|
@@ -27,18 +29,18 @@ const e=await d.json();if(!d.ok){const g=this.checkError(d.status,e),l=this.crea
|
|
27 |
You should have received a copy of the GNU General Public License
|
28 |
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
29 |
*/
|
30 |
-
const MAX_PROMPT_LENGTH=
|
31 |
-
saveRespToDb(){UaDb.saveArray("id_responses",this.responses)},readRespsFromDb(){this.responses=UaDb.readArray("id_responses")},addPrompt(a){this.prompts.push(a)},responsesLength(){return this.responses.reduce((a,b)=>a+b.length,0)},
|
32 |
b){return a.substring(0,a.length-b)},getPartSize(a,b,c){c=MAX_PROMPT_LENGTH-c;a.length+b.length<c?c=a.length:(a=a.indexOf(".",c),a=(-1!=a?a:c)+1,a>c+100&&(a=c),c=a);return c},getPartDoc(a,b){const c=a.substring(0,b);a=a.substring(b).trim();return[c,a]},errorInfo(a){return JSON.parse(a.message)},errorTotext(a){a=JSON.parse(a.message);return`Errore:
|
33 |
Stato: ${a.status}
|
34 |
Descrizione stato: ${a.statusText}
|
35 |
Tipo di errore: ${a.errorType}
|
36 |
-
Messaggio: ${a.message}`},async requestDocsRAG(a){DataMgr.deleteJsonDati();DataMgr.readDbDocNames();DataMgr.readDbDocs();this.ragQuery=a;var b=0;try{for(let c=0;c<DataMgr.docs.length;c++){let d=DataMgr.docs[c];if(""==d.trim())continue;const e=DataMgr.doc_names[c],f=d.length;
|
37 |
-
await HfRequest.post(r),!
|
38 |
-
|
39 |
-
|
40 |
-
e=d="";for(f=0;;){g=this.truncInput(g,f);d=promptThread(b,g,a);const l=getPayloadThread(d);try{if(e=await HfRequest.post(l),!e)return""}catch(h){const k=this.errorInfo(h);if(k.errorType===ERROR_TOKENS){UaLog.log(`Error tokens.5 ${d.length}`);
|
41 |
-
g;}finally{return c=cleanOutput(c)}}}},LLM="Assistant:",USER="User:",ThreadMgr={rows:[],init(){this.rows=[];Rag.ragResponse?this.add(Rag.ragQuery,Rag.ragResponse):this.add("","")},add(a,b){this.rows.push([a,b]);UaDb.saveArray("id_thread",ThreadMgr.rows)},getOutText(){const a=[];for(const b of this.rows){const c=b[0].trim(),d=b[1].trim();a.push(`\n${USER}\n${c}\n${LLM}\n${d}\n\n`)}return a.join("").trim()},getThread(){const a=[];for(const b of this.rows){const c=b[0].trim(),d=b[1].trim();a.push(`${USER}\n${c}\n${LLM}\n${d}\n\n`)}return a.join("").trim()},
|
42 |
isFirst(){return 2>this.rows.length}};function promptDoc(a,b,c){return`
|
43 |
SYSTEM:
|
44 |
Sei un assistente AI specializzato nell'analisi di documenti. Rispondi sempre in italiano.
|
@@ -139,9 +141,10 @@ OUTPUT_FORMAT:
|
|
139 |
RESPONSE:
|
140 |
`}function getPayloadDoc(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:512,num_return_sequences:1,temperature:.5,top_k:50,top_p:.7,do_sample:!0,no_repeat_ngram_size:4,num_beams:5,repetition_penalty:1.4,return_full_text:!1,details:!1,max_time:90,seed:42},options:{use_cache:!1,wait_for_model:!0}}}
|
141 |
function getPayloadWithContext(a){return{inputs:a,parameters:{task:"text-generation",max_new_tokens:1024,num_return_sequences:1,temperature:.6,top_k:50,top_p:.7,do_sample:!0,no_repeat_ngram_size:4,num_beams:5,repetition_penalty:1.4,return_full_text:!1,details:!1,max_time:120,seed:42},options:{use_cache:!1,wait_for_model:!0}}}
|
142 |
-
function getPayloadThread(a){return{inputs:a,parameters:{task:"text-generation",max_new_tokens:1024,num_return_sequences:1,temperature:.6,top_k:50,top_p:.7,do_sample:!0,no_repeat_ngram_size:43,num_beams:5,repetition_penalty:1.4,return_full_text:!1,details:!1,max_time:120,seed:42},options:{use_cache:!1,wait_for_model:!0}}};const VERS="0.1.
|
143 |
function openApp(){wnds.init();Menu.init();TextInput.init();TextOutput.init();Rag.init();document.querySelector(".menu-btn").checked=!1;release()}function release(){document.querySelector(".release").innerHTML=VERS}const op0=async function(a){a=await requestGet("./help1.html");wnds.wdiv.show(a)};function showQuery(a){wnds.wpre.show(`\n${Rag.ragQuery}`)}function showRagResponse(a){wnds.wpre.show(`\n${Rag.ragResponse}`)}function showThread(a){a=ThreadMgr.getOutText();wnds.wpre.show(a)}
|
144 |
-
function elencoRisposte(a){a=[...Rag.responses];0==a.length&&(a=UaDb.readArray("id_responses"));0!=a.length&&(a=a.map((b,c)
|
|
|
145 |
function calcQuery(){DataMgr.readDbDocs();DataMgr.readDbDocNames();var a=[];let b=0,c=0;a.push("Documento Num.Parti");a.push("==================");for(const d of DataMgr.docs){const e=DataMgr.doc_names[c];c+=1;const f=Math.ceil(d.length/MAX_PROMPT_LENGTH);b+=f;a.push(`${e} [${f}]`)}a.push("==================");a.push(`Totale num. Parti: ${b}`);a=a.join("\n");wnds.wpre.show(a)}
|
146 |
function deleteDati(a){confirm("Confermi cancellazione dati?")&&(DataMgr.deleteJsonDati(),wnds.wdiv.close(),wnds.wpre.close(),TextOutput.clear())}function deleteSttorage(a){confirm("Confermi cancellazione documenti & dati?")&&(DataMgr.deleteJsonDati(),localStorage.clear(),wnds.wdiv.close(),wnds.wpre.close(),TextOutput.clear(),DataMgr.docs=[],DataMgr.doc_names=[])}async function help1(a){a=await requestGet("./data/help_test.html");wnds.wdiv.show(a)}
|
147 |
function loadTestoEsempio(a){DataMgr.loadDoc(`data/${a}`);wnds.wdiv.close()}async function help2(a){a=await requestGet("./help2.html");wnds.wdiv.show(a)}function showPrompts(a){0!=Rag.prompts.length&&(a=Rag.prompts.map((b,c)=>`[${c+1}]${b}\n`).join("\n"),wnds.wpre.show(a))}function showTesti(a){DataMgr.readDbDocs();a=DataMgr.docs.join("\n");wnds.wpre.show(a)};const WndPre=a=>({w:UaWindowAdm.create(a),out:null,show(b){wnds.closeAll();b=`
|
@@ -186,7 +189,7 @@ document.body.appendChild(this.workerScriptElement);this.workerScriptElement.onl
|
|
186 |
async function readTextFile(a){if(!a||"text/plain"!==a.type)throw Error("Invalid file type. Please select a text file.");return new Promise((b,c)=>{const d=new FileReader;d.onload=e=>b(e.target.result);d.onerror=e=>c(Error("Error reading file: "+e.message));d.readAsText(a)})};const nodrag_tds=["input","select","a"],nodrag_cls="nodrag";
|
187 |
var UaDrag=function(a){return function(b){let c=0,d=0,e=0,f=0;const g=function(h){h=h||window.event;h.preventDefault();c=e-h.clientX;d=f-h.clientY;e=h.clientX;f=h.clientY;b.style.top=b.offsetTop-d+"px";b.style.left=b.offsetLeft-c+"px"},l=function(){document.onmouseup=null;document.onmousemove=null};b.onmousedown=function(h){h=h||window.event;let k=h.target;!(k=k||null)||nodrag_tds.includes(k.tagName.toLowerCase())||k.classList.contains(nodrag_cls)||(h.preventDefault(),e=h.clientX,f=h.clientY,document.onmouseup=
|
188 |
l,document.onmousemove=g)}}(a)};const UaJtfh=()=>({rows:[],init(){this.rows=[];return this},insert(a){this.rows.unshift(a);return this},append(a){this.rows.push(a);return this},text(a=""){return this.rows.join(a)},html(a=""){return this.rows.join(a).replace(/\s+|\[rn\]/g," ")}});var UaLog={callHide:function(){},callShow:function(){},active:!1,wind:null,x:null,y:null,z:null,max_length:2E3,msg_id:"ualogmsg_",new:function(){null==this.wind&&(this.wind=UaWindowAdm.create("ualog_"),this.wind.drag());this.wind.setHtml('\n <button type="button" onclick="javascript:UaLog.cls();">Clear</button>\n <button type="button" onclick="javascript:UaLog.close();">Close</button>\n <div id="ualogmsg_"></div>');this.wind.setStyle({width:"auto",minWidth:"300px",maxWidth:"400px",
|
189 |
-
height:"auto",textAlign:"center",padding:"2px 2px 2px 2px",margin:"5px 0 0 0",background:"#333333",color:"#ffffff",fontSize:"15px",fontWeight:"normal",borderRadius:"9px",border:"1px solid #999999"});var a=document.getElementById(this.msg_id);const b={width:"auto",textAlign:"left",fontSize:"16px",fontFamily:"monospace",paddingTop:"2px ",marginTop:"2px",color:"#ffffff",background:"#000000",maxHeight:"400px",overflow:"auto",scrollbarColor:"
|
190 |
b[d];const c={background:"#444444",color:"#ffffff",padding:"5px 5px 5px 5px",margin:"0 5px 0 5px",fontSize:"16px",fontWeight:"bold",border:"1px solid #ffffff",borderRadius:"10px"};a=document.querySelectorAll("#ualog_ button");for(const d of a){for(const e in c)d.style[e]=c[e];d.addEventListener("mouseover",e=>{e.target.style.cursor="pointer";e.target.style.color="#000000";e.target.style.background="#aaaaaa"});d.addEventListener("mouseout",e=>{for(const f in c)e.target.style[f]=c[f]})}this.x?this.wind.vw_vh().setXY(this.x,
|
191 |
this.y,-1):this.wind.setCenter(-1);this.z&&this.wind.setZ(this.z);return this},setXY(a,b){this.x=a;this.y=b;return this},setZ(a){this.z=a;return this},prn_(...a){a=a.join("<br/>");let b=document.getElementById(this.msg_id);b.innerHTML=b.innerHTML+a+"<br/>"},print(...a){null!=this.wind&&this.active&&this.prn_(...a)},log(...a){null!=this.wind&&this.prn_(...a)},log_show(...a){null!=this.wind&&(this.active||this.toggle(),this.prn_(...a))},cls(){if(null!=this.wind)return document.getElementById(this.msg_id).innerHTML=
|
192 |
"",this},close(){null!=this.wind&&(this.wind.hide(),this.active=!1,this.callHide())},toggle(){null!=this.wind&&(this.active?(this.active=!1,this.wind.hide(),this.callHide()):(this.active=!0,this.wind.show(),this.callShow()))}};var UaWindowAdm={ws:{},create(a,b=null){let c=document.getElementById(a);c||(c=document.createElement("div"),b?document.getElementById(b).appendChild(c):document.body.appendChild(c),c.id=a,c.setAttribute("data-name","ua-window"),b=this.newUaWindow(c),this.ws[a]=b);a=this.ws[a];c.style.display="none";return a},get(a){return this.ws[a]?this.ws[a]:null},show(a){this.ws[a]&&this.ws[a].show()},close(a){this.ws[a]&&this.ws[a].close()},toggle(a){this.ws[a]&&this.ws[a].toggle()},hide(a){this.ws[a]&&this.ws[a].hide()},
|
|
|
2 |
function cleanDoc(a){try{return a=removeTag(a),a=removeChars(a),a=replaceChars(a),a=a.replace(/\n/g," "),a=a.replace(/\t/g," "),a=a.replace(/ +/g," "),a=a.replace(/\n\s*\n/g,"\n"),a=a.replace(/[\u201c\u201d]/g,'"'),a=a.replace(/[\u2018\u2019]/g,"'"),a=a.replace(/[\u00ab\u00bb\u201e\u201c]/g,'"'),a=a.replace(/[\u2013\u2014]/g,"-"),a=a.replace(/\\[nrt]/g,""),a=a.replace(/[^\w\s\u00e0\u00e8\u00e9\u00ec\u00f2\u00f9\u00c0\u00c8\u00c9\u00cc\u00d2\u00d9\u00e1\u00e9\u00ed\u00f3\u00fa\u00c1\u00c9\u00cd\u00d3\u00da\u00e4\u00eb\u00ef\u00f6\u00fc\u00c4\u00cb\u00cf\u00d6\u00dc\u00e2\u00ea\u00ee\u00f4\u00fb\u00c2\u00ca\u00ce\u00d4\u00db\u00e7\u00c7\u00f1\u00d1.,;:!?'"()-]/g,
|
3 |
""),a.trim()}catch(b){return xerror(b),"Errore di codifica nel documento"}}
|
4 |
function cleanResponse(a){try{return a=removeChars(a),a=replaceChars(a),a=a.replace(/\t/g," "),a=a.replace(/ +/g," "),a=a.replace(/\n\s*\n/g,"\n"),a=a.replace(/[\u201c\u201d]/g,'"'),a=a.replace(/[\u2018\u2019]/g,"'"),a=a.replace(/[\u00ab\u00bb\u201e\u201c]/g,'"'),a=a.replace(/[\u2013\u2014]/g,"-"),a=a.replace(/\\[nrt]/g,""),a=a.replace(/[^\w\s\u00e0\u00e8\u00e9\u00ec\u00f2\u00f9\u00c0\u00c8\u00c9\u00cc\u00d2\u00d9\u00e1\u00e9\u00ed\u00f3\u00fa\u00c1\u00c9\u00cd\u00d3\u00da\u00e4\u00eb\u00ef\u00f6\u00fc\u00c4\u00cb\u00cf\u00d6\u00dc\u00e2\u00ea\u00ee\u00f4\u00fb\u00c2\u00ca\u00ce\u00d4\u00db\u00e7\u00c7\u00f1\u00d1.,;:!?'"()-]/g,""),
|
5 |
+
a.trim()}catch(b){return xerror(b),"Errore di codifica nela risposta"}}
|
6 |
+
function cleanOutput(a){try{return a=removeChars(a),a=replaceChars(a),a=a.replace(/\t/g," "),a=a.replace(/ +/g," "),a=a.replace(/\n\s*\n/g,"\n"),a=a.replace(/[\u201c\u201d]/g,'"'),a=a.replace(/[\u2018\u2019]/g,"'"),a=a.replace(/[\u00ab\u00bb\u201e\u201c]/g,'"'),a=a.replace(/[\u2013\u2014]/g,"-"),a=a.replace(/\\[nrt]/g,""),a=a.replace(/[^\w\s\u00e0\u00e8\u00e9\u00ec\u00f2\u00f9\u00c0\u00c8\u00c9\u00cc\u00d2\u00d9\u00e1\u00e9\u00ed\u00f3\u00fa\u00c1\u00c9\u00cd\u00d3\u00da\u00e4\u00eb\u00ef\u00f6\u00fc\u00c4\u00cb\u00cf\u00d6\u00dc\u00e2\u00ea\u00ee\u00f4\u00fb\u00c2\u00ca\u00ce\u00d4\u00db\u00e7\u00c7\u00f1\u00d1.,;:!?'"()-]/g,""),
|
7 |
+
a.trim()}catch(b){return xerror(b),"Errore di codifica nell'output"}}function list2text(a){const b={};for(var c of a){a=(a=c.match(/<<<(.*?)>>>/))?a[1]:null;const d=c.replace(/<<<.*?>>>/,"");b[a]=a in b?b[a]+`\n ${d}`:d}c=[];for(let [d,e]of Object.entries(b))c.push(`Documento: ${d}.\n${e}`);return c.join("\n\n")}function subResponseDOcTag(a){return a.replace(/<<<(.*?)>>>/,(b,c)=>`Documento: ${c}`)};const UaDb={create(a,b){localStorage.getItem(a)?xerror(`ID ${a} already exists.`):localStorage.setItem(a,b)},read(a){const b=localStorage.getItem(a);return null===b?(xlog(`UaDb.read ${a} not found.`),""):b},update(a,b){localStorage.getItem(a)?localStorage.setItem(a,b):xlog(`UaDb.update ${a} not found.`)},delete(a){localStorage.getItem(a)?localStorage.removeItem(a):xerror(`ID ${a} not found.`)},save(a,b){localStorage.setItem(a,b)},getAllIds(){const a=[];for(let b=0;b<localStorage.length;b++)a.push(localStorage.key(b));
|
8 |
return a},saveArray(a,b){b=JSON.stringify(b);UaDb.save(a,b)},readArray(a){a=UaDb.read(a);return 0==a.trim().length?[]:JSON.parse(a)},saveJson(a,b){b=JSON.stringify(b);UaDb.save(a,b)},readJson(a){return(a=UaDb.read(a))?JSON.parse(a):{}}},DataMgr={docs:[],doc_names:[],linkToName(a){a=a.split("/");return a[a.length-1]},async loadDoc(a){try{const b=await requestGet(a),c=cleanDoc(b),d=this.linkToName(a);this.doc_names.push(d);this.docs.push(c);this.saveDbDocs();return c}catch(b){alert("loadDoc()\n"+b+
|
9 |
"\n"+a)}},addDoc(a,b){b=cleanDoc(b);this.docs.push(b);this.doc_names.push(a);this.saveDbDocs()},saveDbDocs(){UaDb.saveArray("id_doc_names",this.doc_names);UaDb.saveArray("id_docs",this.docs)},readDbDocs(){this.docs=UaDb.readArray("id_docs")},readDbDocNames(){this.doc_names=UaDb.readArray("id_doc_names")},deleteJsonDati(){const a=UaDb.getAllIds();for(const b of a)["id_docs","id_doc_names"].includes(b)||UaDb.delete(b);Rag.ragQuery="";Rag.ragContext="";Rag.ragResponse="";Rag.responses=[];Rag.prompts=
|
10 |
[];ThreadMgr.rows=[]}};const arr=["bWtkW0l+XX0=","SXx2d1FxbVc=","V1tRXlxneUY=","flV6a1NdbUg=","VllpUkc="],model_name="mistralai/Mistral-7B-Instruct-v0.3",umgm=a=>a.map(b=>atob(b).split("").map(c=>String.fromCharCode((c.charCodeAt(0)-5+256)%256)).join("")).join(""),tm=umgm(arr);
|
|
|
29 |
You should have received a copy of the GNU General Public License
|
30 |
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
31 |
*/
|
32 |
+
const MAX_PROMPT_LENGTH=87040,PROMPT_DECR=2048,Rag={ragContext:"",ragQuery:"",ragResponse:"",responses:[],prompts:[],doc:"",doc_part:"",init(){this.readRespsFromDb();this.readFromDb()},saveToDb(){UaDb.saveJson("id_rag",{context:this.ragContext,ragquery:this.ragQuery,ragresponse:this.ragResponse});UaDb.saveArray("id_thread",ThreadMgr.rows)},readFromDb(){const a=UaDb.readJson("id_rag");this.ragContext=a.context||"";this.ragQuery=a.ragquery||"";this.ragResponse=a.ragresponse||"";ThreadMgr.rows=UaDb.readArray("id_thread")},
|
33 |
+
saveRespToDb(){UaDb.saveArray("id_responses",this.responses)},readRespsFromDb(){this.responses=UaDb.readArray("id_responses")},addPrompt(a){this.prompts.push(a)},responsesLength(){return this.responses.reduce((a,b)=>a+b.length,0)},ragLog(a,b,c){const d=MAX_PROMPT_LENGTH,e=this.responsesLength();xlog(`${a} mx:${d} lft:${b} rgt:${c} arr:${e}`);UaLog.log(`${a}${" "}${b}${" "}${c}${" "}${e}`)},truncInput(a,
|
34 |
b){return a.substring(0,a.length-b)},getPartSize(a,b,c){c=MAX_PROMPT_LENGTH-c;a.length+b.length<c?c=a.length:(a=a.indexOf(".",c),a=(-1!=a?a:c)+1,a>c+100&&(a=c),c=a);return c},getPartDoc(a,b){const c=a.substring(0,b);a=a.substring(b).trim();return[c,a]},errorInfo(a){return JSON.parse(a.message)},errorTotext(a){a=JSON.parse(a.message);return`Errore:
|
35 |
Stato: ${a.status}
|
36 |
Descrizione stato: ${a.statusText}
|
37 |
Tipo di errore: ${a.errorType}
|
38 |
+
Messaggio: ${a.message}`},async requestDocsRAG(a){DataMgr.deleteJsonDati();DataMgr.readDbDocNames();DataMgr.readDbDocs();this.ragQuery=a;var b=0;try{for(let c=0;c<DataMgr.docs.length;c++){let d=DataMgr.docs[c];if(""==d.trim())continue;const e=DataMgr.doc_names[c],f=d.length;xlog(`${e} (${f}) `);UaLog.log(`${e} (${f}) `);++b;let g=0,l=0,h="",k="",m="";for(;;){let q=this.getPartSize(d,promptDoc("",a),l);if(10>q)break;[k,m]=this.getPartDoc(d,q);this.ragLog(`${b},${g+1}`,k.length,m.length);h=promptDoc(k,
|
39 |
+
a);const r=getPayloadDoc(h);let n;try{if(n=await HfRequest.post(r),!n)return""}catch(p){if(this.errorInfo(p).errorType===ERROR_TOKENS){UaLog.log(`Error tokens.1 ${k.length}`);xerror(`Error tokens. ${h.length}`);l+=PROMPT_DECR;continue}else throw xerror(p),this.errorTotext(p);}g++;d=m;this.responses.push(`<<<${e}>>>\n${n}`)}}}catch(c){alert("requestDocsRAG(1)\n"+c),xerror(c)}this.ragContext=list2text(this.responses);this.saveToDb();b="";try{let c=promptWithContext(this.ragContext,a);const d=getPayloadWithContext(c);
|
40 |
+
try{if(b=await HfRequest.post(d),!b)return""}catch(e){this.errorInfo(e);const f=this.errorTotext(e);xerror(e);throw f;}this.ragResponse=b;this.saveRespToDb();ThreadMgr.init();this.saveToDb();UaLog.log(`Risposta (${c.length},${b.length})`)}catch(c){throw b=c,xerror(c),alert("requestDocsRAG(3)\n"+c),c;}finally{return b=cleanOutput(b)}},async requestContext(a){if(!this.ragContext&&!await confirm("Contesto vuoto. Vuoi continuare?"))return"";if(ThreadMgr.isFirst()){var b="";ThreadMgr.init();try{var c=
|
41 |
+
this.ragContext,d=ThreadMgr.getThread();prompt=promptThread(c,d,a);var e=getPayloadThread(prompt);try{var f=await HfRequest.post(e);if(!f)return""}catch(g){throw this.errorInfo(g).errorType===ERROR_TOKENS?UaLog.log(`Error tokens.4 ${prompt.length}`):alert(g),xerror(g),g;}ThreadMgr.add(a,f);b=ThreadMgr.getOutText();UaLog.log(`Inizio Conversazione (${prompt.length},${f.length})`)}catch(g){throw xerror(g),b=g,g;}finally{return b=cleanOutput(b)}}else{c="";try{b=this.ragContext;let g=ThreadMgr.getThread();
|
42 |
+
e=d="";for(f=0;;){g=this.truncInput(g,f);d=promptThread(b,g,a);const l=getPayloadThread(d);try{if(e=await HfRequest.post(l),!e)return""}catch(h){const k=this.errorInfo(h);if(k.errorType===ERROR_TOKENS){UaLog.log(`Error tokens.5 ${d.length}`);xerror(`Error tokens. ${d.length}`);f+=PROMPT_DECR;continue}else throw xerror(h),k.errorType;}break}ThreadMgr.add(a,e);c=ThreadMgr.getOutText();UaLog.log(`Conversazione (${d.length},${e.length})`)}catch(g){throw alert("requestContext(2) \n"+g),xerror(g),c=
|
43 |
+
g,g;}finally{return c=cleanOutput(c)}}}},LLM="Assistant:",USER="User:",ThreadMgr={rows:[],init(){this.rows=[];Rag.ragResponse?this.add(Rag.ragQuery,Rag.ragResponse):this.add("","")},add(a,b){this.rows.push([a,b]);UaDb.saveArray("id_thread",ThreadMgr.rows)},getOutText(){const a=[];for(const b of this.rows){const c=b[0].trim(),d=b[1].trim();a.push(`\n${USER}\n${c}\n${LLM}\n${d}\n\n`)}return a.join("").trim()},getThread(){const a=[];for(const b of this.rows){const c=b[0].trim(),d=b[1].trim();a.push(`${USER}\n${c}\n${LLM}\n${d}\n\n`)}return a.join("").trim()},
|
44 |
isFirst(){return 2>this.rows.length}};function promptDoc(a,b,c){return`
|
45 |
SYSTEM:
|
46 |
Sei un assistente AI specializzato nell'analisi di documenti. Rispondi sempre in italiano.
|
|
|
141 |
RESPONSE:
|
142 |
`}function getPayloadDoc(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:512,num_return_sequences:1,temperature:.5,top_k:50,top_p:.7,do_sample:!0,no_repeat_ngram_size:4,num_beams:5,repetition_penalty:1.4,return_full_text:!1,details:!1,max_time:90,seed:42},options:{use_cache:!1,wait_for_model:!0}}}
|
143 |
function getPayloadWithContext(a){return{inputs:a,parameters:{task:"text-generation",max_new_tokens:1024,num_return_sequences:1,temperature:.6,top_k:50,top_p:.7,do_sample:!0,no_repeat_ngram_size:4,num_beams:5,repetition_penalty:1.4,return_full_text:!1,details:!1,max_time:120,seed:42},options:{use_cache:!1,wait_for_model:!0}}}
|
144 |
+
function getPayloadThread(a){return{inputs:a,parameters:{task:"text-generation",max_new_tokens:1024,num_return_sequences:1,temperature:.6,top_k:50,top_p:.7,do_sample:!0,no_repeat_ngram_size:43,num_beams:5,repetition_penalty:1.4,return_full_text:!1,details:!1,max_time:120,seed:42},options:{use_cache:!1,wait_for_model:!0}}};const VERS="0.1.40 (28-07-2024)";var xlog=console.log,xerror=console.error;const cancelRequest=()=>{confirm("Confermi Cancellazione Richeista ?")&&(HfRequest.cancelRequest(),hideSpinner())},showSpinner=()=>{const a=document.getElementById("spinner");a.classList.add("show-spinner");a.addEventListener("click",cancelRequest)},hideSpinner=()=>{const a=document.getElementById("spinner");a.classList.remove("show-spinner");a.removeEventListener("click",cancelRequest)};
|
145 |
function openApp(){wnds.init();Menu.init();TextInput.init();TextOutput.init();Rag.init();document.querySelector(".menu-btn").checked=!1;release()}function release(){document.querySelector(".release").innerHTML=VERS}const op0=async function(a){a=await requestGet("./help1.html");wnds.wdiv.show(a)};function showQuery(a){wnds.wpre.show(`\n${Rag.ragQuery}`)}function showRagResponse(a){wnds.wpre.show(`\n${Rag.ragResponse}`)}function showThread(a){a=ThreadMgr.getOutText();wnds.wpre.show(a)}
|
146 |
+
function elencoRisposte(a){a=[...Rag.responses];0==a.length&&(a=UaDb.readArray("id_responses"));0!=a.length&&(a=a.map((b,c)=>{b=subResponseDOcTag(b);return`\n[${c+1}]\n ${b.trim()}`}).join("\n"),wnds.wpre.show(a))}function showContesto(a){wnds.wpre.show(`${Rag.ragContext}`)}function elencoDati(a){var b=UaDb.getAllIds();a=[];for(var c of b)b=UaDb.read(c).length,a.push(`${c} (${b})`);c=a.join("\n ");wnds.wpre.show(c)}
|
147 |
+
async function elencoDocs(a){DataMgr.readDbDocNames();a=DataMgr.doc_names.join("\n");wnds.wpre.show(a)}
|
148 |
function calcQuery(){DataMgr.readDbDocs();DataMgr.readDbDocNames();var a=[];let b=0,c=0;a.push("Documento Num.Parti");a.push("==================");for(const d of DataMgr.docs){const e=DataMgr.doc_names[c];c+=1;const f=Math.ceil(d.length/MAX_PROMPT_LENGTH);b+=f;a.push(`${e} [${f}]`)}a.push("==================");a.push(`Totale num. Parti: ${b}`);a=a.join("\n");wnds.wpre.show(a)}
|
149 |
function deleteDati(a){confirm("Confermi cancellazione dati?")&&(DataMgr.deleteJsonDati(),wnds.wdiv.close(),wnds.wpre.close(),TextOutput.clear())}function deleteSttorage(a){confirm("Confermi cancellazione documenti & dati?")&&(DataMgr.deleteJsonDati(),localStorage.clear(),wnds.wdiv.close(),wnds.wpre.close(),TextOutput.clear(),DataMgr.docs=[],DataMgr.doc_names=[])}async function help1(a){a=await requestGet("./data/help_test.html");wnds.wdiv.show(a)}
|
150 |
function loadTestoEsempio(a){DataMgr.loadDoc(`data/${a}`);wnds.wdiv.close()}async function help2(a){a=await requestGet("./help2.html");wnds.wdiv.show(a)}function showPrompts(a){0!=Rag.prompts.length&&(a=Rag.prompts.map((b,c)=>`[${c+1}]${b}\n`).join("\n"),wnds.wpre.show(a))}function showTesti(a){DataMgr.readDbDocs();a=DataMgr.docs.join("\n");wnds.wpre.show(a)};const WndPre=a=>({w:UaWindowAdm.create(a),out:null,show(b){wnds.closeAll();b=`
|
|
|
189 |
async function readTextFile(a){if(!a||"text/plain"!==a.type)throw Error("Invalid file type. Please select a text file.");return new Promise((b,c)=>{const d=new FileReader;d.onload=e=>b(e.target.result);d.onerror=e=>c(Error("Error reading file: "+e.message));d.readAsText(a)})};const nodrag_tds=["input","select","a"],nodrag_cls="nodrag";
|
190 |
var UaDrag=function(a){return function(b){let c=0,d=0,e=0,f=0;const g=function(h){h=h||window.event;h.preventDefault();c=e-h.clientX;d=f-h.clientY;e=h.clientX;f=h.clientY;b.style.top=b.offsetTop-d+"px";b.style.left=b.offsetLeft-c+"px"},l=function(){document.onmouseup=null;document.onmousemove=null};b.onmousedown=function(h){h=h||window.event;let k=h.target;!(k=k||null)||nodrag_tds.includes(k.tagName.toLowerCase())||k.classList.contains(nodrag_cls)||(h.preventDefault(),e=h.clientX,f=h.clientY,document.onmouseup=
|
191 |
l,document.onmousemove=g)}}(a)};const UaJtfh=()=>({rows:[],init(){this.rows=[];return this},insert(a){this.rows.unshift(a);return this},append(a){this.rows.push(a);return this},text(a=""){return this.rows.join(a)},html(a=""){return this.rows.join(a).replace(/\s+|\[rn\]/g," ")}});var UaLog={callHide:function(){},callShow:function(){},active:!1,wind:null,x:null,y:null,z:null,max_length:2E3,msg_id:"ualogmsg_",new:function(){null==this.wind&&(this.wind=UaWindowAdm.create("ualog_"),this.wind.drag());this.wind.setHtml('\n <button type="button" onclick="javascript:UaLog.cls();">Clear</button>\n <button type="button" onclick="javascript:UaLog.close();">Close</button>\n <div id="ualogmsg_"></div>');this.wind.setStyle({width:"auto",minWidth:"300px",maxWidth:"400px",
|
192 |
+
height:"auto",textAlign:"center",padding:"2px 2px 2px 2px",margin:"5px 0 0 0",background:"#333333",color:"#ffffff",fontSize:"15px",fontWeight:"normal",borderRadius:"9px",border:"1px solid #999999"});var a=document.getElementById(this.msg_id);const b={width:"auto",textAlign:"left",fontSize:"16px",fontFamily:"monospace",paddingTop:"2px ",marginTop:"2px",color:"#ffffff",background:"#000000",maxHeight:"400px",overflow:"auto",scrollbarColor:"#027876 #454444",scrollbarWidth:"auto"};for(const d in b)a.style[d]=
|
193 |
b[d];const c={background:"#444444",color:"#ffffff",padding:"5px 5px 5px 5px",margin:"0 5px 0 5px",fontSize:"16px",fontWeight:"bold",border:"1px solid #ffffff",borderRadius:"10px"};a=document.querySelectorAll("#ualog_ button");for(const d of a){for(const e in c)d.style[e]=c[e];d.addEventListener("mouseover",e=>{e.target.style.cursor="pointer";e.target.style.color="#000000";e.target.style.background="#aaaaaa"});d.addEventListener("mouseout",e=>{for(const f in c)e.target.style[f]=c[f]})}this.x?this.wind.vw_vh().setXY(this.x,
|
194 |
this.y,-1):this.wind.setCenter(-1);this.z&&this.wind.setZ(this.z);return this},setXY(a,b){this.x=a;this.y=b;return this},setZ(a){this.z=a;return this},prn_(...a){a=a.join("<br/>");let b=document.getElementById(this.msg_id);b.innerHTML=b.innerHTML+a+"<br/>"},print(...a){null!=this.wind&&this.active&&this.prn_(...a)},log(...a){null!=this.wind&&this.prn_(...a)},log_show(...a){null!=this.wind&&(this.active||this.toggle(),this.prn_(...a))},cls(){if(null!=this.wind)return document.getElementById(this.msg_id).innerHTML=
|
195 |
"",this},close(){null!=this.wind&&(this.wind.hide(),this.active=!1,this.callHide())},toggle(){null!=this.wind&&(this.active?(this.active=!1,this.wind.hide(),this.callHide()):(this.active=!0,this.wind.show(),this.callShow()))}};var UaWindowAdm={ws:{},create(a,b=null){let c=document.getElementById(a);c||(c=document.createElement("div"),b?document.getElementById(b).appendChild(c):document.body.appendChild(c),c.id=a,c.setAttribute("data-name","ua-window"),b=this.newUaWindow(c),this.ws[a]=b);a=this.ws[a];c.style.display="none";return a},get(a){return this.ws[a]?this.ws[a]:null},show(a){this.ws[a]&&this.ws[a].show()},close(a){this.ws[a]&&this.ws[a].close()},toggle(a){this.ws[a]&&this.ws[a].toggle()},hide(a){this.ws[a]&&this.ws[a].hide()},
|