Upload folder using huggingface_hub
Browse files- static/data/help_test.html +7 -7
- static/javascript/rag_cleaner.js +68 -76
- static/javascript/rag_data_mgr.js +9 -3
- static/javascript/rag_http.js +16 -8
- static/javascript/rag_mgr.js +44 -30
- static/javascript/rag_prompts.js +109 -93
- static/javascript/rag_rqs.js +50 -47
- static/javascript/rag_ui.js +2 -11
- static/javascript/rag_upload.js +2 -2
- static/js/ragrqs.min.js +117 -93
- static/less/less.js +3 -3
- static/ragrqs.html +17 -14
- static/ragrqs_.html +13 -11
static/data/help_test.html
CHANGED
@@ -1,18 +1,18 @@
|
|
1 |
<div class="text">
|
2 |
<br>
|
3 |
-
<div><a href="#" onCLick="
|
4 |
<br>
|
5 |
-
<div><a href="#" onCLick="
|
6 |
<br>
|
7 |
-
<div><a href="#" onCLick="
|
8 |
<br>
|
9 |
-
<div><a href="#" onCLick="
|
10 |
<br>
|
11 |
-
<div><a href="#" onCLick="
|
12 |
<br>
|
13 |
-
<div><a href="#" onCLick="
|
14 |
<br>
|
15 |
-
<div><a href="#" onCLick="
|
16 |
|
17 |
<!--
|
18 |
aaron_swartz.txt
|
|
|
1 |
<div class="text">
|
2 |
<br>
|
3 |
+
<div><a href="#" onCLick="loadTestoEsempio('aaron_swartz.txt');">Aaron Swartz</a></div>
|
4 |
<br>
|
5 |
+
<div><a href="#" onCLick="loadTestoEsempio('roberto_busa.txt');">Roberto Busa</a></div>
|
6 |
<br>
|
7 |
+
<div><a href="#" onCLick="loadTestoEsempio('la_cattedrale_e_il_bazaar.txt');">La cattedrale e il Bazar</a></div>
|
8 |
<br>
|
9 |
+
<div><a href="#" onCLick="loadTestoEsempio('aristotele_detective.txt');">Aristotele Detective</a></div>
|
10 |
<br>
|
11 |
+
<div><a href="#" onCLick="loadTestoEsempio('montecristo.txt');">Montecristor</a></div>
|
12 |
<br>
|
13 |
+
<div><a href="#" onCLick="loadTestoEsempio('il_libro_rosso.txt');">Il libro rosso</a></div>
|
14 |
<br>
|
15 |
+
<div><a href="#" onCLick="loadTestoEsempio('lettere_musulmane.txt');">Lettere Mussulmane</a></div>
|
16 |
|
17 |
<!--
|
18 |
aaron_swartz.txt
|
static/javascript/rag_cleaner.js
CHANGED
@@ -1,99 +1,91 @@
|
|
1 |
/** @format */
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
txt = txt.split("\n").map(removeControlCharacters).join("\n");
|
10 |
-
// Converte tabulazioni in spazi
|
11 |
-
txt = txt.replace(/\t/g, " ");
|
12 |
-
// Rimuove spazi multipli
|
13 |
-
txt = txt.replace(/ +/g, " ");
|
14 |
-
// Rimuove spazi all'inizio e alla fine di ogni riga
|
15 |
-
txt = txt
|
16 |
-
.split("\n")
|
17 |
-
.map((line) => line.trim())
|
18 |
-
.join("\n");
|
19 |
-
// Riduce linee vuote multiple a una sola
|
20 |
-
txt = txt.replace(/\n\s*\n/g, "\n\n");
|
21 |
-
// Uniforma i caratteri di quotazione
|
22 |
-
txt = txt.replace(/["""]/g, '"').replace(/['']/g, "'");
|
23 |
-
// Uniforma i trattini
|
24 |
-
txt = txt.replace(/[–—]/g, "-");
|
25 |
-
// Rimuove caratteri di escape visibili
|
26 |
-
txt = txt.replace(/\\[nrt]/g, "");
|
27 |
-
// Preserva caratteri Unicode (lettere e numeri), punteggiatura comune e spazi
|
28 |
-
txt = txt.replace(/[^\p{L}\p{N}\s\.,;:!?'"()\-]/gu, "");
|
29 |
-
// Rimuove spazi prima della punteggiatura
|
30 |
-
txt = txt.replace(/ +([.,;:!?])/g, "$1");
|
31 |
-
// Trim finale
|
32 |
-
return txt.trim();
|
33 |
-
}
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
-
function
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
// // Rimuove le linee di separazione composte da caratteri ripetuti
|
68 |
-
// .replace(/^[=\-_.]+$/gm, '')
|
69 |
-
// // Rimuove i simboli di elenco puntato (* - +) all'inizio delle righe
|
70 |
-
// .replace(/^[*\-+]\s*/gm, '')
|
71 |
-
// // Rimuove le righe vuote multiple, lasciando solo una riga vuota tra i paragrafi
|
72 |
-
// .replace(/\n\s*\n/g, '\n\n')
|
73 |
-
// // Rimuove gli spazi bianchi all'inizio e alla fine del testo
|
74 |
-
// .trim();
|
75 |
-
// }
|
76 |
|
77 |
|
78 |
function cleanOutput(text) {
|
79 |
// Rimuove gli spazi bianchi iniziali e finali
|
80 |
text = text.trim();
|
|
|
81 |
// Sostituisce le sequenze di più di due newline con due newline
|
82 |
text = text.replace(/\n{3,}/g, '\n\n');
|
83 |
-
// text = text.replace(/\n{2,}/g,
|
84 |
// Rimuove gli spazi bianchi extra all'inizio di ogni riga
|
85 |
-
text = text.replace(/^[ \t]+/gm,
|
86 |
// Sostituisce i trattini bassi multipli con una linea orizzontale
|
87 |
// text = text.replace(/_{3,}/g, '\n\n' + '-'.repeat(40) + '\n\n');
|
88 |
// Formatta gli elenchi puntati per una migliore leggibilità
|
89 |
-
text = text.replace(/^(\s*[-*•])(\s*)/gm,
|
90 |
// Formatta gli elenchi numerati per una migliore leggibilità
|
91 |
-
text = text.replace(/^(\s*\d+\.)(\s*)/gm,
|
92 |
// Aggiunge una riga vuota prima e dopo i blocchi di codice
|
93 |
-
text = text.replace(/(```[\s\S]*?```)/g,
|
94 |
// Aggiunge un'andata a capo dopo ogni punto, eccetto quando seguito da newline o fine stringa
|
95 |
-
// text = text.replace(/\.(?!\n|$)/g, '.\n');
|
96 |
// Rimuove gli spazi bianchi extra alla fine di ogni riga
|
97 |
-
text = text.replace(/[ \t]+$/gm,
|
98 |
return text;
|
99 |
-
}
|
|
|
1 |
/** @format */
|
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).filter(ch => !/^C/.test(ch.codePointAt(0))).join('');
|
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 |
+
txt = removeTag(txt);
|
25 |
+
txt = removeChars(txt);
|
26 |
+
txt = replaceChars(txt);
|
27 |
+
txt = txt.replace(/\n/g, " ");
|
28 |
+
txt = txt.replace(/\t/g, " ");
|
29 |
+
txt = txt.replace(/ +/g, ' ');
|
30 |
+
txt = txt.replace(/\n\s*\n/g, '\n');
|
31 |
+
txt = txt.replace(/[“”]/g, '"');
|
32 |
+
txt = txt.replace(/[‘’]/g, "'");
|
33 |
+
txt = txt.replace(/[«»„“]/g, '"');
|
34 |
+
txt = txt.replace(/[–—]/g, '-');
|
35 |
+
txt = txt.replace(/\\[nrt]/g, '');
|
36 |
+
txt = txt.replace(/[^\w\sàèéìòùÀÈÉÌÒÙáéíóúÁÉÍÓÚäëïöüÄËÏÖÜâêîôûÂÊÎÔÛçÇñÑ.,;:!?'"()-]/g, '');
|
37 |
+
return txt.trim();
|
38 |
+
} catch (e) {
|
39 |
+
xerror(e);
|
40 |
+
return "Errore di codifica nel documento";
|
41 |
+
}
|
42 |
+
};
|
43 |
|
44 |
+
function cleanResponse(txt){
|
45 |
+
try {
|
46 |
+
txt = removeChars(txt);
|
47 |
+
txt = replaceChars(txt);
|
48 |
+
txt = txt.replace(/\t/g, " ");
|
49 |
+
txt = txt.replace(/ +/g, ' ');
|
50 |
+
txt = txt.replace(/\n\s*\n/g, '\n');
|
51 |
+
txt = txt.replace(/[“”]/g, '"');
|
52 |
+
txt = txt.replace(/[‘’]/g, "'");
|
53 |
+
txt = txt.replace(/[«»„“]/g, '"');
|
54 |
+
txt = txt.replace(/[–—]/g, '-');
|
55 |
+
txt = txt.replace(/\\[nrt]/g, '');
|
56 |
+
txt = txt.replace(/[^\w\sàèéìòùÀÈÉÌÒÙáéíóúÁÉÍÓÚäëïöüÄËÏÖÜâêîôûÂÊÎÔÛçÇñÑ.,;:!?'"()-]/g, '');
|
57 |
+
return txt.trim();
|
58 |
+
} catch (e) {
|
59 |
+
xerror(e);
|
60 |
+
return "Errore di codifica nela risposta";
|
61 |
+
}
|
62 |
+
};
|
63 |
|
64 |
+
function list2text(lst) {
|
65 |
+
return lst.map(s => `${cleanResponse(s)}`).join('\n\n');
|
66 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
|
69 |
function cleanOutput(text) {
|
70 |
// Rimuove gli spazi bianchi iniziali e finali
|
71 |
text = text.trim();
|
72 |
+
// text = removeLabels(text);// TODO
|
73 |
// Sostituisce le sequenze di più di due newline con due newline
|
74 |
text = text.replace(/\n{3,}/g, '\n\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 |
+
}
|
static/javascript/rag_data_mgr.js
CHANGED
@@ -42,13 +42,13 @@ const UaDb = {
|
|
42 |
return ids;
|
43 |
},
|
44 |
saveArray(id, arr) {
|
45 |
-
const str =
|
46 |
UaDb.save(id, str);
|
47 |
},
|
48 |
readArray(id) {
|
49 |
const str = UaDb.read(id);
|
50 |
if (str.trim().length == 0) return [];
|
51 |
-
const arr
|
52 |
return arr;
|
53 |
},
|
54 |
saveJson(id, js) {
|
@@ -99,11 +99,17 @@ const DataMgr = {
|
|
99 |
readDbDocNames() {
|
100 |
this.doc_names = UaDb.readArray("id_doc_names");
|
101 |
},
|
102 |
-
|
103 |
const ids = UaDb.getAllIds();
|
104 |
for (const id of ids) {
|
105 |
if (["id_docs", "id_doc_names"].includes(id)) continue;
|
106 |
UaDb.delete(id);
|
107 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
},
|
109 |
};
|
|
|
42 |
return ids;
|
43 |
},
|
44 |
saveArray(id, arr) {
|
45 |
+
const str = JSON.stringify(arr);
|
46 |
UaDb.save(id, str);
|
47 |
},
|
48 |
readArray(id) {
|
49 |
const str = UaDb.read(id);
|
50 |
if (str.trim().length == 0) return [];
|
51 |
+
const arr=JSON.parse(str);
|
52 |
return arr;
|
53 |
},
|
54 |
saveJson(id, js) {
|
|
|
99 |
readDbDocNames() {
|
100 |
this.doc_names = UaDb.readArray("id_doc_names");
|
101 |
},
|
102 |
+
deleteJsonDati() {
|
103 |
const ids = UaDb.getAllIds();
|
104 |
for (const id of ids) {
|
105 |
if (["id_docs", "id_doc_names"].includes(id)) continue;
|
106 |
UaDb.delete(id);
|
107 |
}
|
108 |
+
Rag.ragQuery = "";
|
109 |
+
Rag.ragContext = "";
|
110 |
+
Rag.ragResponse = "";
|
111 |
+
Rag.responses = [];
|
112 |
+
Rag.prompts = [];
|
113 |
+
ThreadMgr.rows = [];
|
114 |
},
|
115 |
};
|
static/javascript/rag_http.js
CHANGED
@@ -1,17 +1,25 @@
|
|
1 |
/** @format */
|
2 |
"use strict";
|
3 |
|
|
|
|
|
4 |
const model_name = "mistralai/Mistral-7B-Instruct-v0.3";
|
5 |
// const model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1";
|
6 |
// const model_name = "mistralai/Mixtral-8x22B-Instruct-v0.1"
|
7 |
-
// const model_name = "google/gemma-2-9b-it"
|
8 |
|
9 |
-
|
10 |
-
return
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
const
|
14 |
-
const HF_TOKEN = decodeTokenBase64(encodedToken);
|
15 |
|
16 |
async function requestGet(url) {
|
17 |
try {
|
@@ -28,7 +36,7 @@ async function requestGet(url) {
|
|
28 |
const text = new TextDecoder("utf-8").decode(arrayBuffer);
|
29 |
return text;
|
30 |
} catch (error) {
|
31 |
-
|
32 |
let message;
|
33 |
if (error.name === "AbortError") {
|
34 |
message = "Request was aborted";
|
@@ -60,7 +68,7 @@ const HfRequest = {
|
|
60 |
const response = await fetch(`${this.baseUrl}/${model_name}`, {
|
61 |
method: "POST",
|
62 |
headers: {
|
63 |
-
Authorization: `Bearer ${
|
64 |
"Content-Type": "application/json",
|
65 |
},
|
66 |
body: JSON.stringify(payload),
|
|
|
1 |
/** @format */
|
2 |
"use strict";
|
3 |
|
4 |
+
const arr = ["bWtkVEpGRl8=", "bVdncnlGUEk=", "SFNZa1h1W3U=", "WXxmcW9nf1U=", "dEd+ZnE="];
|
5 |
+
|
6 |
const model_name = "mistralai/Mistral-7B-Instruct-v0.3";
|
7 |
// const model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1";
|
8 |
// const model_name = "mistralai/Mixtral-8x22B-Instruct-v0.1"
|
|
|
9 |
|
10 |
+
const umgm = (arr) => {
|
11 |
+
return arr
|
12 |
+
.map((part) => {
|
13 |
+
const ch = atob(part);
|
14 |
+
return ch
|
15 |
+
.split("")
|
16 |
+
.map((char) => String.fromCharCode((char.charCodeAt(0) - 5 + 256) % 256))
|
17 |
+
.join("");
|
18 |
+
})
|
19 |
+
.join("");
|
20 |
+
};
|
21 |
|
22 |
+
const tm = umgm(arr);
|
|
|
23 |
|
24 |
async function requestGet(url) {
|
25 |
try {
|
|
|
36 |
const text = new TextDecoder("utf-8").decode(arrayBuffer);
|
37 |
return text;
|
38 |
} catch (error) {
|
39 |
+
xerror(error);
|
40 |
let message;
|
41 |
if (error.name === "AbortError") {
|
42 |
message = "Request was aborted";
|
|
|
68 |
const response = await fetch(`${this.baseUrl}/${model_name}`, {
|
69 |
method: "POST",
|
70 |
headers: {
|
71 |
+
Authorization: `Bearer ${tm}`,
|
72 |
"Content-Type": "application/json",
|
73 |
},
|
74 |
body: JSON.stringify(payload),
|
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 |
|
@@ -33,6 +33,10 @@ const Rag = {
|
|
33 |
prompts: [],
|
34 |
doc: "",
|
35 |
doc_part: "",
|
|
|
|
|
|
|
|
|
36 |
saveToDb() {
|
37 |
const js = {
|
38 |
context: this.ragContext,
|
@@ -40,12 +44,14 @@ const Rag = {
|
|
40 |
ragresponse: this.ragResponse,
|
41 |
};
|
42 |
UaDb.saveJson("id_rag", js);
|
|
|
43 |
},
|
44 |
readFromDb() {
|
45 |
const js = UaDb.readJson("id_rag");
|
46 |
this.ragContext = js.context || "";
|
47 |
this.ragQuery = js.ragquery || "";
|
48 |
this.ragResponse = js.ragresponse || "";
|
|
|
49 |
},
|
50 |
saveRespToDb() {
|
51 |
UaDb.saveArray("id_responses", this.responses);
|
@@ -109,7 +115,6 @@ const Rag = {
|
|
109 |
return js;
|
110 |
},
|
111 |
errorTotext(e) {
|
112 |
-
//
|
113 |
const j = JSON.parse(e.message);
|
114 |
return `Errore:
|
115 |
Stato: ${j.status}
|
@@ -120,25 +125,25 @@ const Rag = {
|
|
120 |
|
121 |
// documenti => risposte RAG => context
|
122 |
async requestDocsRAG(query) {
|
123 |
-
|
124 |
-
DataMgr.deleteDati();
|
125 |
DataMgr.readDbDocNames();
|
126 |
DataMgr.readDbDocs();
|
127 |
this.ragQuery = query;
|
128 |
-
this.ragContext = "";
|
129 |
-
this.ragResponse = "";
|
130 |
-
this.responses = [];
|
131 |
-
this.prompts = [];
|
|
|
132 |
let ndoc = 0;
|
133 |
|
134 |
try {
|
135 |
for (let i = 0; i < DataMgr.docs.length; i++) {
|
136 |
let doc = DataMgr.docs[i];
|
137 |
if (doc.trim() == "") continue;
|
138 |
-
const
|
139 |
const doc_entire_len = doc.length;
|
140 |
-
UaLog.log(`${
|
141 |
-
xlog(`${
|
142 |
++ndoc;
|
143 |
let npart = 0;
|
144 |
let partLft = "";
|
@@ -182,7 +187,7 @@ const Rag = {
|
|
182 |
npart++;
|
183 |
doc = partRgt;
|
184 |
if (npart == 1) {
|
185 |
-
text = `
|
186 |
}
|
187 |
this.responses.push(text);
|
188 |
this.log(`${ndoc},${npart}`, partLft.length, partRgt.length);
|
@@ -225,7 +230,7 @@ const Rag = {
|
|
225 |
break;
|
226 |
}
|
227 |
const pl = prompt.length;
|
228 |
-
//
|
229 |
this.ragContext = text;
|
230 |
this.saveToDb();
|
231 |
UaLog.log(`Contesto (${pl})`);
|
@@ -235,19 +240,16 @@ const Rag = {
|
|
235 |
throw error;
|
236 |
}
|
237 |
*/
|
238 |
-
////////////////////////////
|
239 |
-
// ALTENATIVA elenco come coontext
|
240 |
const resps = list2text(this.responses);
|
241 |
this.ragContext = resps;
|
242 |
this.saveToDb();
|
243 |
-
////////////////////////////
|
244 |
|
245 |
// query finale utilizza context
|
246 |
{
|
247 |
let text = "";
|
248 |
try {
|
249 |
let prompt = promptWithContext(this.ragContext, query);
|
250 |
-
const payload =
|
251 |
|
252 |
try {
|
253 |
text = await HfRequest.post(payload);
|
@@ -263,8 +265,9 @@ const Rag = {
|
|
263 |
throw s;
|
264 |
}
|
265 |
}
|
266 |
-
this.ragResponse = text;
|
267 |
this.saveRespToDb();
|
|
|
268 |
this.saveToDb();
|
269 |
const pl = prompt.length;
|
270 |
UaLog.log(`Risposta (${pl},${text.length})`);
|
@@ -274,15 +277,15 @@ const Rag = {
|
|
274 |
alert("requestDocsRAG(3)\n" + error);
|
275 |
throw error;
|
276 |
} finally {
|
277 |
-
text = cleanOutput(text);
|
278 |
return text;
|
279 |
}
|
280 |
}
|
281 |
},
|
282 |
|
283 |
-
//
|
284 |
async requestContext(query) {
|
285 |
-
this.readFromDb();
|
286 |
|
287 |
if (!this.ragContext) {
|
288 |
const ok = await confirm("Contesto vuoto. Vuoi continuare?");
|
@@ -291,10 +294,12 @@ const Rag = {
|
|
291 |
|
292 |
if (ThreadMgr.isFirst()) {
|
293 |
let outText = "";
|
|
|
294 |
try {
|
295 |
let context = this.ragContext;
|
296 |
-
|
297 |
-
|
|
|
298 |
|
299 |
let text;
|
300 |
try {
|
@@ -307,7 +312,7 @@ const Rag = {
|
|
307 |
xerror(err);
|
308 |
throw err;
|
309 |
} else {
|
310 |
-
alert(err)
|
311 |
xerror(err);
|
312 |
throw err;
|
313 |
}
|
@@ -320,7 +325,7 @@ const Rag = {
|
|
320 |
outText = error;
|
321 |
throw error;
|
322 |
} finally {
|
323 |
-
outText = cleanOutput(outText);
|
324 |
return outText;
|
325 |
}
|
326 |
} else {
|
@@ -335,7 +340,7 @@ const Rag = {
|
|
335 |
while (true) {
|
336 |
thread = this.truncInput(thread, decr);
|
337 |
prompt = promptThread(context, thread, query);
|
338 |
-
const payload =
|
339 |
|
340 |
try {
|
341 |
text = await HfRequest.post(payload);
|
@@ -363,28 +368,37 @@ const Rag = {
|
|
363 |
outText = error;
|
364 |
throw error;
|
365 |
} finally {
|
366 |
-
outText = cleanOutput(outText);
|
367 |
return outText;
|
368 |
}
|
369 |
}
|
370 |
},
|
371 |
};
|
372 |
|
|
|
|
|
|
|
373 |
const ThreadMgr = {
|
374 |
rows: [],
|
375 |
init() {
|
376 |
this.rows = [];
|
|
|
|
|
|
|
|
|
|
|
377 |
},
|
378 |
add(query, resp) {
|
379 |
const row = [query, resp];
|
380 |
this.rows.push(row);
|
|
|
381 |
},
|
382 |
getOutText() {
|
383 |
const rows = [];
|
384 |
for (const ua of this.rows) {
|
385 |
const u = ua[0].trim();
|
386 |
const a = ua[1].trim();
|
387 |
-
rows.push(
|
388 |
}
|
389 |
let text = rows.join("").trim();
|
390 |
return text;
|
@@ -394,12 +408,12 @@ const ThreadMgr = {
|
|
394 |
for (const ua of this.rows) {
|
395 |
const u = ua[0].trim();
|
396 |
const a = ua[1].trim();
|
397 |
-
rows.push(
|
398 |
}
|
399 |
const text = rows.join("").trim();
|
400 |
return text;
|
401 |
},
|
402 |
isFirst() {
|
403 |
-
return this.rows.length
|
404 |
},
|
405 |
};
|
|
|
18 |
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
19 |
*/
|
20 |
"use strict";
|
21 |
+
const MAX_PROMPT_LENGTH = 1024 * 80;
|
22 |
// decremento dopo errore per tokens eccessivi
|
23 |
const PROMPT_DECR = 1024 * 2;
|
24 |
|
|
|
33 |
prompts: [],
|
34 |
doc: "",
|
35 |
doc_part: "",
|
36 |
+
init() {
|
37 |
+
this.readRespsFromDb();
|
38 |
+
this.readFromDb();
|
39 |
+
},
|
40 |
saveToDb() {
|
41 |
const js = {
|
42 |
context: this.ragContext,
|
|
|
44 |
ragresponse: this.ragResponse,
|
45 |
};
|
46 |
UaDb.saveJson("id_rag", js);
|
47 |
+
UaDb.saveArray("id_thread", ThreadMgr.rows);
|
48 |
},
|
49 |
readFromDb() {
|
50 |
const js = UaDb.readJson("id_rag");
|
51 |
this.ragContext = js.context || "";
|
52 |
this.ragQuery = js.ragquery || "";
|
53 |
this.ragResponse = js.ragresponse || "";
|
54 |
+
ThreadMgr.rows = UaDb.readArray("id_thread");
|
55 |
},
|
56 |
saveRespToDb() {
|
57 |
UaDb.saveArray("id_responses", this.responses);
|
|
|
115 |
return js;
|
116 |
},
|
117 |
errorTotext(e) {
|
|
|
118 |
const j = JSON.parse(e.message);
|
119 |
return `Errore:
|
120 |
Stato: ${j.status}
|
|
|
125 |
|
126 |
// documenti => risposte RAG => context
|
127 |
async requestDocsRAG(query) {
|
128 |
+
DataMgr.deleteJsonDati();
|
|
|
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 {
|
140 |
for (let i = 0; i < DataMgr.docs.length; i++) {
|
141 |
let doc = DataMgr.docs[i];
|
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 = "";
|
|
|
187 |
npart++;
|
188 |
doc = partRgt;
|
189 |
if (npart == 1) {
|
190 |
+
text = `Documento:${doc_name}\n${text}`;
|
191 |
}
|
192 |
this.responses.push(text);
|
193 |
this.log(`${ndoc},${npart}`, partLft.length, partRgt.length);
|
|
|
230 |
break;
|
231 |
}
|
232 |
const pl = prompt.length;
|
233 |
+
// text = cleanContext(text);
|
234 |
this.ragContext = text;
|
235 |
this.saveToDb();
|
236 |
UaLog.log(`Contesto (${pl})`);
|
|
|
240 |
throw error;
|
241 |
}
|
242 |
*/
|
|
|
|
|
243 |
const resps = list2text(this.responses);
|
244 |
this.ragContext = resps;
|
245 |
this.saveToDb();
|
|
|
246 |
|
247 |
// query finale utilizza context
|
248 |
{
|
249 |
let text = "";
|
250 |
try {
|
251 |
let prompt = promptWithContext(this.ragContext, query);
|
252 |
+
const payload = getPayloadWithContext(prompt);
|
253 |
|
254 |
try {
|
255 |
text = await HfRequest.post(payload);
|
|
|
265 |
throw s;
|
266 |
}
|
267 |
}
|
268 |
+
this.ragResponse = text;
|
269 |
this.saveRespToDb();
|
270 |
+
ThreadMgr.init();
|
271 |
this.saveToDb();
|
272 |
const pl = prompt.length;
|
273 |
UaLog.log(`Risposta (${pl},${text.length})`);
|
|
|
277 |
alert("requestDocsRAG(3)\n" + error);
|
278 |
throw error;
|
279 |
} finally {
|
280 |
+
text = cleanOutput(text);
|
281 |
return text;
|
282 |
}
|
283 |
}
|
284 |
},
|
285 |
|
286 |
+
// thread
|
287 |
async requestContext(query) {
|
288 |
+
// this.readFromDb(); XXX
|
289 |
|
290 |
if (!this.ragContext) {
|
291 |
const ok = await confirm("Contesto vuoto. Vuoi continuare?");
|
|
|
294 |
|
295 |
if (ThreadMgr.isFirst()) {
|
296 |
let outText = "";
|
297 |
+
ThreadMgr.init(); //AAA
|
298 |
try {
|
299 |
let context = this.ragContext;
|
300 |
+
const thread = ThreadMgr.getThread();
|
301 |
+
prompt = promptThread(context, thread, query);
|
302 |
+
const payload = getPayloadThread(prompt);
|
303 |
|
304 |
let text;
|
305 |
try {
|
|
|
312 |
xerror(err);
|
313 |
throw err;
|
314 |
} else {
|
315 |
+
alert(err);
|
316 |
xerror(err);
|
317 |
throw err;
|
318 |
}
|
|
|
325 |
outText = error;
|
326 |
throw error;
|
327 |
} finally {
|
328 |
+
outText = cleanOutput(outText);
|
329 |
return outText;
|
330 |
}
|
331 |
} else {
|
|
|
340 |
while (true) {
|
341 |
thread = this.truncInput(thread, decr);
|
342 |
prompt = promptThread(context, thread, query);
|
343 |
+
const payload = getPayloadThread(prompt);
|
344 |
|
345 |
try {
|
346 |
text = await HfRequest.post(payload);
|
|
|
368 |
outText = error;
|
369 |
throw error;
|
370 |
} finally {
|
371 |
+
outText = cleanOutput(outText);
|
372 |
return outText;
|
373 |
}
|
374 |
}
|
375 |
},
|
376 |
};
|
377 |
|
378 |
+
const LLM = "Assistant:";
|
379 |
+
const USER = "User:";
|
380 |
+
|
381 |
const ThreadMgr = {
|
382 |
rows: [],
|
383 |
init() {
|
384 |
this.rows = [];
|
385 |
+
if (!!Rag.ragResponse) {
|
386 |
+
this.add(Rag.ragQuery, Rag.ragResponse);
|
387 |
+
} else {
|
388 |
+
this.add("", "");
|
389 |
+
}
|
390 |
},
|
391 |
add(query, resp) {
|
392 |
const row = [query, resp];
|
393 |
this.rows.push(row);
|
394 |
+
UaDb.saveArray("id_thread", ThreadMgr.rows);
|
395 |
},
|
396 |
getOutText() {
|
397 |
const rows = [];
|
398 |
for (const ua of this.rows) {
|
399 |
const u = ua[0].trim();
|
400 |
const a = ua[1].trim();
|
401 |
+
rows.push(`\n${USER}\n${u}\n${LLM}\n${a}\n\n`);
|
402 |
}
|
403 |
let text = rows.join("").trim();
|
404 |
return text;
|
|
|
408 |
for (const ua of this.rows) {
|
409 |
const u = ua[0].trim();
|
410 |
const a = ua[1].trim();
|
411 |
+
rows.push(`${USER}\n${u}\n${LLM}\n${a}\n\n`);
|
412 |
}
|
413 |
const text = rows.join("").trim();
|
414 |
return text;
|
415 |
},
|
416 |
isFirst() {
|
417 |
+
return this.rows.length < 2;
|
418 |
},
|
419 |
};
|
static/javascript/rag_prompts.js
CHANGED
@@ -1,103 +1,120 @@
|
|
|
|
|
|
1 |
"use strict";
|
2 |
|
3 |
-
function promptDoc(documento, domanda) {
|
4 |
return `
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
${documento}
|
|
|
13 |
|
14 |
-
|
15 |
${domanda}
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
Elenco delle informazioni identificate, incluse eventuali inferenze
|
26 |
-
`;
|
27 |
-
}
|
28 |
-
|
29 |
-
function promptToContext(risposte) {
|
30 |
-
return `
|
31 |
-
[[RUOLO]]
|
32 |
-
Esperto di analisi semantica delle informazioni.
|
33 |
-
|
34 |
-
[[OBIETTIVO]]
|
35 |
-
Unificare le informazioni semanticamente equivalenti da vari documenti.
|
36 |
-
|
37 |
-
[[ELENCO_RISPOSTE]]
|
38 |
-
${risposte}
|
39 |
-
|
40 |
-
[[ISTRUZIONI]]
|
41 |
-
1. Analizza le informazioni dell'elenco risposte.
|
42 |
-
2. Per ogni documento raggruppa le informazioni sulla base del loro significato.
|
43 |
-
3. Presenta le informazioni in modo chiaro e coerente.
|
44 |
-
|
45 |
-
[[OUTPUT]]
|
46 |
-
Elenco delle informazioni mantenendo la divisione per documento.
|
47 |
`;
|
48 |
}
|
49 |
|
50 |
function promptWithContext(contesto, domanda) {
|
51 |
return `
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
${contesto}
|
|
|
57 |
|
58 |
-
|
59 |
${domanda}
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
|
69 |
-
L'output è destinato ad essere letto da un lettore italiano.
|
70 |
`;
|
71 |
}
|
72 |
|
73 |
-
function promptThread(contesto, conversazione,
|
74 |
return `
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
${contesto}
|
|
|
80 |
|
81 |
-
|
82 |
${conversazione}
|
|
|
83 |
|
84 |
-
|
85 |
-
${
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
5. Adatta la risposta alla domanda attuale, anche se si discosta dal tema principale della conversazione.
|
93 |
-
6. Usa citazioni o riferimenti solo se necessario per chiarire la descrizione.
|
94 |
|
95 |
-
|
96 |
-
L'output è destinato ad essere letto da un lettore italiano.
|
97 |
`;
|
98 |
}
|
99 |
|
100 |
-
///////////////////////////////////}
|
101 |
/*
|
102 |
|
103 |
1. max_new_tokens: 512
|
@@ -151,20 +168,21 @@ Suggerimenti per l'ottimizzazione:
|
|
151 |
4. Per risposte più diverse, potresti aumentare temperature a 0.8 e top_p a 0.9.
|
152 |
5. Se noti ancora troppe ripetizioni, considera di aumentare repetition_penalty a 1.3 o 1.4.
|
153 |
*/
|
154 |
-
|
155 |
-
|
|
|
156 |
inputs: prompt,
|
157 |
parameters: {
|
158 |
task: "text2text-generation",
|
159 |
max_new_tokens: 512,
|
160 |
num_return_sequences: 1,
|
161 |
-
temperature: 0.
|
162 |
top_k: 50,
|
163 |
-
top_p: 0.
|
164 |
do_sample: true,
|
165 |
no_repeat_ngram_size: 4,
|
166 |
-
num_beams:
|
167 |
-
repetition_penalty: 1.
|
168 |
return_full_text: false,
|
169 |
details: false,
|
170 |
max_time: 90.0,
|
@@ -176,22 +194,22 @@ const getPayloadDoc = (prompt) => {
|
|
176 |
},
|
177 |
};
|
178 |
return payload;
|
179 |
-
}
|
180 |
|
181 |
-
|
182 |
-
|
183 |
inputs: prompt,
|
184 |
parameters: {
|
185 |
-
task: "
|
186 |
max_new_tokens: 1024,
|
187 |
num_return_sequences: 1,
|
188 |
-
temperature: 0.
|
189 |
top_k: 50,
|
190 |
-
top_p: 0.
|
191 |
do_sample: true,
|
192 |
-
no_repeat_ngram_size:
|
193 |
num_beams: 5,
|
194 |
-
repetition_penalty: 1.
|
195 |
return_full_text: false,
|
196 |
details: false,
|
197 |
max_time: 120.0,
|
@@ -203,21 +221,20 @@ const getPlayloadContext = (prompt) => {
|
|
203 |
},
|
204 |
};
|
205 |
return payload;
|
206 |
-
}
|
207 |
|
208 |
-
|
209 |
-
const
|
210 |
-
let payload = {
|
211 |
inputs: prompt,
|
212 |
parameters: {
|
213 |
task: "text-generation",
|
214 |
-
max_new_tokens:
|
215 |
num_return_sequences: 1,
|
216 |
-
temperature: 0.
|
217 |
top_k: 50,
|
218 |
-
top_p: 0.
|
219 |
do_sample: true,
|
220 |
-
no_repeat_ngram_size:
|
221 |
num_beams: 5,
|
222 |
repetition_penalty: 1.4,
|
223 |
return_full_text: false,
|
@@ -231,5 +248,4 @@ const getPayloadQuery = (prompt) => {
|
|
231 |
},
|
232 |
};
|
233 |
return payload;
|
234 |
-
}
|
235 |
-
|
|
|
1 |
+
/** @format */
|
2 |
+
|
3 |
"use strict";
|
4 |
|
5 |
+
function promptDoc(documento, domanda, docName) {
|
6 |
return `
|
7 |
+
SYSTEM:
|
8 |
+
Sei un assistente AI specializzato nell'analisi di documenti. Rispondi sempre in italiano.
|
9 |
+
|
10 |
+
TASK:
|
11 |
+
Estrarre informazioni dal documento fornito in relazione a una domanda specifica.
|
12 |
+
|
13 |
+
INSTRUCTIONS:
|
14 |
+
1. Analizza attentamente il documento.
|
15 |
+
2. Identifica le informazioni pertinenti alla domanda.
|
16 |
+
3. Estrai i concetti chiave e fai inferenze ragionevoli.
|
17 |
+
4. Organizza le informazioni in modo logico.
|
18 |
+
5. Prepara una risposta completa ma concisa.
|
19 |
+
6. Struttura la risposta in paragrafi logici.
|
20 |
+
7. Includi un'introduzione breve, sviluppa l'analisi, presenta le inferenze e concludi con una sintesi.
|
21 |
+
8. Cita il documento solo se assolutamente necessario per chiarire informazioni cruciali.
|
22 |
+
9. Mantieni un tono oggettivo e uno stile fluido e coerente.
|
23 |
+
|
24 |
+
DOCUMENT:
|
25 |
+
<<<BEGIN_DOCUMENT>>>
|
26 |
${documento}
|
27 |
+
<<<END_DOCUMENT>>>
|
28 |
|
29 |
+
QUESTION:
|
30 |
${domanda}
|
31 |
|
32 |
+
OUTPUT_FORMAT:
|
33 |
+
- Inizia la tua risposta direttamente con il contenuto, senza etichette o introduzioni.
|
34 |
+
- Fornisci la tua risposta come testo continuo diviso in paragrafi.
|
35 |
+
- Non usare elenchi puntati o numerati.
|
36 |
+
- Evita qualsiasi tipo di etichetta o marcatori speciali.
|
37 |
+
- Mantieni un tono oggettivo e uno stile fluido e coerente.
|
38 |
|
39 |
+
RESPONSE:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
`;
|
41 |
}
|
42 |
|
43 |
function promptWithContext(contesto, domanda) {
|
44 |
return `
|
45 |
+
SYSTEM:
|
46 |
+
Sei un assistente AI specializzato nell'analisi e nell'elaborazione di informazioni contestuali..Rispondi sempre in italiano.
|
47 |
+
|
48 |
+
TASK:
|
49 |
+
Elaborare la risposta alla domanda sulla base del contesto fornito.
|
50 |
+
|
51 |
+
INSTRUCTIONS:
|
52 |
+
1. Analizza attentamente il contesto fornito.
|
53 |
+
2. Identifica le informazioni pertinenti alla domanda.
|
54 |
+
3. Estrai i concetti chiave e fai inferenze ragionevoli.
|
55 |
+
4. Organizza le informazioni in modo logico.
|
56 |
+
5. Prepara una risposta completa ma concisa.
|
57 |
+
6. Struttura la risposta in paragrafi logici.
|
58 |
+
7. Includi un'introduzione breve, sviluppa l'analisi, presenta le inferenze e concludi con una sintesi.
|
59 |
+
8. Cita il documento solo se assolutamente necessario per chiarire informazioni cruciali.
|
60 |
+
9. Mantieni un tono oggettivo e uno stile fluido e coerente.
|
61 |
+
|
62 |
+
CONTEXT:
|
63 |
+
<<<BEGIN_CONTEXT>>>
|
64 |
${contesto}
|
65 |
+
<<<END_CONTEXT>>>
|
66 |
|
67 |
+
QUESTION:
|
68 |
${domanda}
|
69 |
|
70 |
+
OUTPUT_FORMAT:
|
71 |
+
- Inizia la tua risposta direttamente con il contenuto, senza etichette o introduzioni.
|
72 |
+
- Fornisci la tua risposta come testo continuo diviso in paragrafi.
|
73 |
+
- Non usare elenchi puntati o numerati.
|
74 |
+
- Evita qualsiasi tipo di etichetta o marcatori speciali.
|
75 |
+
- Mantieni un tono oggettivo e uno stile fluido e coerente.
|
76 |
|
77 |
+
RESPONSE:
|
|
|
78 |
`;
|
79 |
}
|
80 |
|
81 |
+
function promptThread(contesto, conversazione, richiesta) {
|
82 |
return `
|
83 |
+
SYSTEM:
|
84 |
+
Sei un assistente AI versatile progettato per gestire conversazioni dinamiche e adattarti a varie richieste. Rispondi in italiano.
|
85 |
+
|
86 |
+
INSTRUCTIONS:
|
87 |
+
1. Analizza il contesto, la conversazione precedente e la richiesta attuale.
|
88 |
+
2. Interpreta l'intento dell'utente senza limitarti a categorie predefinite.
|
89 |
+
3. Adatta la tua risposta in base all'intento percepito, sia esso una domanda, una richiesta di azione, un'istruzione specifica o altro.
|
90 |
+
4. Mantieni la coerenza con il contesto della conversazione.
|
91 |
+
5. Adatta tono e stile in base all'interazione, rimanendo professionale ma amichevole.
|
92 |
+
6. Fai riferimento a informazioni precedenti quando pertinente.
|
93 |
+
7. Se l'intento non è chiaro, chiedi gentilmente chiarimenti.
|
94 |
+
8. Sii flessibile: se la richiesta implica un formato o un'azione specifica, adattati di conseguenza.
|
95 |
+
|
96 |
+
CONTEXT:
|
97 |
+
<<<BEGIN_CONTEXT>>>
|
98 |
${contesto}
|
99 |
+
<<<END_CONTEXT>>>
|
100 |
|
101 |
+
<<<BEGIN_CONVERSATION>>>
|
102 |
${conversazione}
|
103 |
+
<<<END_CONVERSATION>>>
|
104 |
|
105 |
+
REQUEST:
|
106 |
+
${richiesta}
|
107 |
|
108 |
+
OUTPUT_FORMAT:
|
109 |
+
- Inizia la risposta direttamente con il contenuto, senza etichette o introduzioni.
|
110 |
+
- Adatta il formato della risposta in base alla richiesta dell'utente.
|
111 |
+
- Se non viene specificato un formato particolare, fornisci la risposta come testo continuo diviso in paragrafi.
|
112 |
+
- Mantieni un tono oggettivo e uno stile fluido e coerente, a meno che non sia richiesto diversamente.
|
|
|
|
|
113 |
|
114 |
+
RESPONSE:
|
|
|
115 |
`;
|
116 |
}
|
117 |
|
|
|
118 |
/*
|
119 |
|
120 |
1. max_new_tokens: 512
|
|
|
168 |
4. Per risposte più diverse, potresti aumentare temperature a 0.8 e top_p a 0.9.
|
169 |
5. Se noti ancora troppe ripetizioni, considera di aumentare repetition_penalty a 1.3 o 1.4.
|
170 |
*/
|
171 |
+
|
172 |
+
function getPayloadDoc(prompt) {
|
173 |
+
const payload = {
|
174 |
inputs: prompt,
|
175 |
parameters: {
|
176 |
task: "text2text-generation",
|
177 |
max_new_tokens: 512,
|
178 |
num_return_sequences: 1,
|
179 |
+
temperature: 0.5,
|
180 |
top_k: 50,
|
181 |
+
top_p: 0.7,
|
182 |
do_sample: true,
|
183 |
no_repeat_ngram_size: 4,
|
184 |
+
num_beams: 5,
|
185 |
+
repetition_penalty: 1.4,
|
186 |
return_full_text: false,
|
187 |
details: false,
|
188 |
max_time: 90.0,
|
|
|
194 |
},
|
195 |
};
|
196 |
return payload;
|
197 |
+
}
|
198 |
|
199 |
+
function getPayloadWithContext(prompt) {
|
200 |
+
const payload = {
|
201 |
inputs: prompt,
|
202 |
parameters: {
|
203 |
+
task: "text-generation",
|
204 |
max_new_tokens: 1024,
|
205 |
num_return_sequences: 1,
|
206 |
+
temperature: 0.6,
|
207 |
top_k: 50,
|
208 |
+
top_p: 0.7,
|
209 |
do_sample: true,
|
210 |
+
no_repeat_ngram_size: 4,
|
211 |
num_beams: 5,
|
212 |
+
repetition_penalty: 1.4,
|
213 |
return_full_text: false,
|
214 |
details: false,
|
215 |
max_time: 120.0,
|
|
|
221 |
},
|
222 |
};
|
223 |
return payload;
|
224 |
+
}
|
225 |
|
226 |
+
function getPayloadThread(prompt) {
|
227 |
+
const payload = {
|
|
|
228 |
inputs: prompt,
|
229 |
parameters: {
|
230 |
task: "text-generation",
|
231 |
+
max_new_tokens: 1024,
|
232 |
num_return_sequences: 1,
|
233 |
+
temperature: 0.6,
|
234 |
top_k: 50,
|
235 |
+
top_p: 0.7,
|
236 |
do_sample: true,
|
237 |
+
no_repeat_ngram_size: 43,
|
238 |
num_beams: 5,
|
239 |
repetition_penalty: 1.4,
|
240 |
return_full_text: false,
|
|
|
248 |
},
|
249 |
};
|
250 |
return payload;
|
251 |
+
}
|
|
static/javascript/rag_rqs.js
CHANGED
@@ -19,13 +19,10 @@
|
|
19 |
*/
|
20 |
|
21 |
"use strict";
|
22 |
-
const VERS = "0.1.
|
23 |
|
24 |
var xlog = console.log;
|
25 |
var xerror = console.error;
|
26 |
-
//////////////////////
|
27 |
-
|
28 |
-
/////////////////////////
|
29 |
// let T0 = {};
|
30 |
|
31 |
// var start_performance = function (k) {
|
@@ -69,12 +66,13 @@ const hideSpinner = () => {
|
|
69 |
spinner.removeEventListener("click", cancelRequest);
|
70 |
};
|
71 |
|
72 |
-
|
73 |
function openApp() {
|
74 |
wnds.init();
|
75 |
Menu.init();
|
76 |
TextInput.init();
|
77 |
TextOutput.init();
|
|
|
78 |
document.querySelector(".menu-btn").checked = false;
|
79 |
release();
|
80 |
}
|
@@ -83,7 +81,6 @@ function release() {
|
|
83 |
document.querySelector(".release").innerHTML = VERS;
|
84 |
}
|
85 |
|
86 |
-
/////////////////////////
|
87 |
// README
|
88 |
const op0 = async function (e) {
|
89 |
const text = await requestGet("./help1.html");
|
@@ -91,22 +88,29 @@ const op0 = async function (e) {
|
|
91 |
};
|
92 |
|
93 |
//query iniziale
|
94 |
-
function
|
95 |
-
Rag.readFromDb();
|
96 |
const txt = `\n${Rag.ragQuery}`;
|
97 |
wnds.wpre.show(txt);
|
98 |
// Menu.close();
|
99 |
}
|
100 |
|
101 |
-
//
|
102 |
function showRagResponse(e) {
|
103 |
-
Rag.readFromDb();
|
104 |
const txt = `\n${Rag.ragResponse}`;
|
105 |
wnds.wpre.show(txt);
|
106 |
}
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
//elenco risposte
|
109 |
-
function
|
110 |
let rs = [...Rag.responses];
|
111 |
if (rs.length == 0) {
|
112 |
rs = UaDb.readArray("id_responses");
|
@@ -118,19 +122,17 @@ function op2(e) {
|
|
118 |
})
|
119 |
.join("\n");
|
120 |
wnds.wpre.show(text);
|
121 |
-
// Menu.close();
|
122 |
}
|
123 |
|
124 |
//contesto
|
125 |
-
function
|
126 |
-
Rag.readFromDb();
|
127 |
const txt = `${Rag.ragContext}`;
|
128 |
wnds.wpre.show(txt);
|
129 |
-
// Menu.close();
|
130 |
}
|
131 |
|
132 |
//elemco dati
|
133 |
-
function
|
134 |
const ids = UaDb.getAllIds();
|
135 |
const arr = [];
|
136 |
for (const id of ids) {
|
@@ -142,8 +144,8 @@ function op4(e) {
|
|
142 |
wnds.wpre.show(msg);
|
143 |
}
|
144 |
|
145 |
-
//elenco documenti
|
146 |
-
async function
|
147 |
DataMgr.readDbDocNames();
|
148 |
const arr = DataMgr.doc_names;
|
149 |
const s = arr.join("\n");
|
@@ -177,27 +179,23 @@ function calcQuery() {
|
|
177 |
wnds.wpre.show(s);
|
178 |
}
|
179 |
|
180 |
-
//
|
181 |
-
function
|
182 |
const ok = confirm("Confermi cancellazione dati?");
|
183 |
if (ok) {
|
184 |
-
DataMgr.
|
185 |
-
Rag.ragContext = "";
|
186 |
-
Rag.ragQuery = "";
|
187 |
-
Rag.responses = [];
|
188 |
wnds.wdiv.close();
|
189 |
wnds.wpre.close();
|
190 |
TextOutput.clear();
|
191 |
}
|
192 |
}
|
|
|
193 |
//cancella localstrage
|
194 |
-
function
|
195 |
const ok = confirm("Confermi cancellazione documenti & dati?");
|
196 |
if (ok) {
|
|
|
197 |
localStorage.clear();
|
198 |
-
Rag.ragContext = "";
|
199 |
-
Rag.ragQuery = "";
|
200 |
-
Rag.responses = [];
|
201 |
wnds.wdiv.close();
|
202 |
wnds.wpre.close();
|
203 |
TextOutput.clear();
|
@@ -205,10 +203,31 @@ function op8(e) {
|
|
205 |
DataMgr.doc_names = [];
|
206 |
}
|
207 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
////////////////////////////////
|
209 |
// Solo Sviluppo
|
|
|
210 |
//prompts
|
211 |
-
function
|
212 |
if (Rag.prompts.length == 0) return;
|
213 |
const text = Rag.prompts
|
214 |
.map((x, i) => {
|
@@ -217,27 +236,11 @@ function op9(e) {
|
|
217 |
.join("\n");
|
218 |
wnds.wpre.show(text);
|
219 |
}
|
|
|
220 |
//testi
|
221 |
-
function
|
222 |
DataMgr.readDbDocs();
|
223 |
const arr = DataMgr.docs;
|
224 |
const s = arr.join("\n");
|
225 |
wnds.wpre.show(s);
|
226 |
}
|
227 |
-
///////////////////////////
|
228 |
-
async function help1(e) {
|
229 |
-
const text = await requestGet("./data/help_test.html");
|
230 |
-
wnds.wdiv.show(text);
|
231 |
-
}
|
232 |
-
|
233 |
-
//lanciata dalla pagina diegli sempi
|
234 |
-
function esempio(name) {
|
235 |
-
const link = `data/${name}`;
|
236 |
-
DataMgr.loadDoc(link);
|
237 |
-
wnds.wdiv.close();
|
238 |
-
}
|
239 |
-
|
240 |
-
async function help2(e) {
|
241 |
-
const text = await requestGet("./help2.html");
|
242 |
-
wnds.wdiv.show(text);
|
243 |
-
}
|
|
|
19 |
*/
|
20 |
|
21 |
"use strict";
|
22 |
+
const VERS = "0.1.36 (22-07-2024)";
|
23 |
|
24 |
var xlog = console.log;
|
25 |
var xerror = console.error;
|
|
|
|
|
|
|
26 |
// let T0 = {};
|
27 |
|
28 |
// var start_performance = function (k) {
|
|
|
66 |
spinner.removeEventListener("click", cancelRequest);
|
67 |
};
|
68 |
|
69 |
+
// INIZIO
|
70 |
function openApp() {
|
71 |
wnds.init();
|
72 |
Menu.init();
|
73 |
TextInput.init();
|
74 |
TextOutput.init();
|
75 |
+
Rag.init(); //AAA
|
76 |
document.querySelector(".menu-btn").checked = false;
|
77 |
release();
|
78 |
}
|
|
|
81 |
document.querySelector(".release").innerHTML = VERS;
|
82 |
}
|
83 |
|
|
|
84 |
// README
|
85 |
const op0 = async function (e) {
|
86 |
const text = await requestGet("./help1.html");
|
|
|
88 |
};
|
89 |
|
90 |
//query iniziale
|
91 |
+
function showQuery(e) {
|
92 |
+
// Rag.readFromDb(); XXX
|
93 |
const txt = `\n${Rag.ragQuery}`;
|
94 |
wnds.wpre.show(txt);
|
95 |
// Menu.close();
|
96 |
}
|
97 |
|
98 |
+
//Risposta Rsg
|
99 |
function showRagResponse(e) {
|
100 |
+
// Rag.readFromDb(); XXX
|
101 |
const txt = `\n${Rag.ragResponse}`;
|
102 |
wnds.wpre.show(txt);
|
103 |
}
|
104 |
|
105 |
+
//conversazione
|
106 |
+
function showThread(e) {
|
107 |
+
// Rag.readFromDb(); //XXX
|
108 |
+
const txt = ThreadMgr.getOutText();
|
109 |
+
wnds.wpre.show(txt);
|
110 |
+
}
|
111 |
+
|
112 |
//elenco risposte
|
113 |
+
function elencoRisposte(e) {
|
114 |
let rs = [...Rag.responses];
|
115 |
if (rs.length == 0) {
|
116 |
rs = UaDb.readArray("id_responses");
|
|
|
122 |
})
|
123 |
.join("\n");
|
124 |
wnds.wpre.show(text);
|
|
|
125 |
}
|
126 |
|
127 |
//contesto
|
128 |
+
function showContesto(e) {
|
129 |
+
// Rag.readFromDb();XXX
|
130 |
const txt = `${Rag.ragContext}`;
|
131 |
wnds.wpre.show(txt);
|
|
|
132 |
}
|
133 |
|
134 |
//elemco dati
|
135 |
+
function elencoDati(e) {
|
136 |
const ids = UaDb.getAllIds();
|
137 |
const arr = [];
|
138 |
for (const id of ids) {
|
|
|
144 |
wnds.wpre.show(msg);
|
145 |
}
|
146 |
|
147 |
+
//elenco documenti in memoria
|
148 |
+
async function elencoDocs(e) {
|
149 |
DataMgr.readDbDocNames();
|
150 |
const arr = DataMgr.doc_names;
|
151 |
const s = arr.join("\n");
|
|
|
179 |
wnds.wpre.show(s);
|
180 |
}
|
181 |
|
182 |
+
//cancella dati
|
183 |
+
function deleteDati(e) {
|
184 |
const ok = confirm("Confermi cancellazione dati?");
|
185 |
if (ok) {
|
186 |
+
DataMgr.deleteJsonDati();
|
|
|
|
|
|
|
187 |
wnds.wdiv.close();
|
188 |
wnds.wpre.close();
|
189 |
TextOutput.clear();
|
190 |
}
|
191 |
}
|
192 |
+
|
193 |
//cancella localstrage
|
194 |
+
function deleteSttorage(e) {
|
195 |
const ok = confirm("Confermi cancellazione documenti & dati?");
|
196 |
if (ok) {
|
197 |
+
DataMgr.deleteJsonDati();
|
198 |
localStorage.clear();
|
|
|
|
|
|
|
199 |
wnds.wdiv.close();
|
200 |
wnds.wpre.close();
|
201 |
TextOutput.clear();
|
|
|
203 |
DataMgr.doc_names = [];
|
204 |
}
|
205 |
}
|
206 |
+
|
207 |
+
//documenti di esempio nella dir ./data
|
208 |
+
async function help1(e) {
|
209 |
+
const text = await requestGet("./data/help_test.html");
|
210 |
+
wnds.wdiv.show(text);
|
211 |
+
}
|
212 |
+
|
213 |
+
//lettura files di esempio invocata dalla pagina ./data/help_test.html
|
214 |
+
function loadTestoEsempio(name) {
|
215 |
+
const link = `data/${name}`;
|
216 |
+
DataMgr.loadDoc(link);
|
217 |
+
wnds.wdiv.close();
|
218 |
+
}
|
219 |
+
|
220 |
+
//domande di esempio
|
221 |
+
async function help2(e) {
|
222 |
+
const text = await requestGet("./help2.html");
|
223 |
+
wnds.wdiv.show(text);
|
224 |
+
}
|
225 |
+
|
226 |
////////////////////////////////
|
227 |
// Solo Sviluppo
|
228 |
+
|
229 |
//prompts
|
230 |
+
function showPrompts(e) {
|
231 |
if (Rag.prompts.length == 0) return;
|
232 |
const text = Rag.prompts
|
233 |
.map((x, i) => {
|
|
|
236 |
.join("\n");
|
237 |
wnds.wpre.show(text);
|
238 |
}
|
239 |
+
|
240 |
//testi
|
241 |
+
function showTesti(e) {
|
242 |
DataMgr.readDbDocs();
|
243 |
const arr = DataMgr.docs;
|
244 |
const s = arr.join("\n");
|
245 |
wnds.wpre.show(s);
|
246 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static/javascript/rag_ui.js
CHANGED
@@ -247,7 +247,7 @@ const TextInput = {
|
|
247 |
alert("Ricorda di scrivere la Query ");
|
248 |
return;
|
249 |
}
|
250 |
-
Rag.readFromDb();
|
251 |
if (!!Rag.ragContext) {
|
252 |
const ok = confirm("Vuoi iniziare una nuova elabrazione ?");
|
253 |
if (!ok) return "";
|
@@ -300,19 +300,10 @@ const TextInput = {
|
|
300 |
if (!ok) return;
|
301 |
this.inp.value = "";
|
302 |
setOutText("");
|
303 |
-
ThreadMgr.init();
|
304 |
},
|
305 |
};
|
306 |
|
307 |
-
//XXX const setOutText = (txt) => {
|
308 |
-
// let out = document.getElementById("id-text-out");
|
309 |
-
// const ht = `<div class="div-text">${txt}</div>`;
|
310 |
-
// out.innerHTML=ht;
|
311 |
-
// const pre = out.querySelector(".div-text");
|
312 |
-
// pre.scrollTop = pre.scrollHeight;
|
313 |
-
// };
|
314 |
-
|
315 |
-
|
316 |
const setOutText = (txt) => {
|
317 |
let out = document.getElementById("id-text-out");
|
318 |
const h = `<pre class="pre-text"></pre>`;
|
|
|
247 |
alert("Ricorda di scrivere la Query ");
|
248 |
return;
|
249 |
}
|
250 |
+
// Rag.readFromDb();XXX
|
251 |
if (!!Rag.ragContext) {
|
252 |
const ok = confirm("Vuoi iniziare una nuova elabrazione ?");
|
253 |
if (!ok) return "";
|
|
|
300 |
if (!ok) return;
|
301 |
this.inp.value = "";
|
302 |
setOutText("");
|
303 |
+
ThreadMgr.init(); //AAA
|
304 |
},
|
305 |
};
|
306 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
const setOutText = (txt) => {
|
308 |
let out = document.getElementById("id-text-out");
|
309 |
const h = `<pre class="pre-text"></pre>`;
|
static/javascript/rag_upload.js
CHANGED
@@ -60,7 +60,7 @@ const RagUpload = {
|
|
60 |
const msg = `<br><br> ${fileName}<br><br>caricato e salvato nella memoria locale`;
|
61 |
result.innerHTML = msg;
|
62 |
} catch (error) {
|
63 |
-
|
64 |
alert("Errore durante l'estrazione del testo dal file.");
|
65 |
} finally {
|
66 |
hideSpinner();
|
@@ -129,7 +129,7 @@ const RagUpload = {
|
|
129 |
msgs.push(msg);
|
130 |
}
|
131 |
} catch (error) {
|
132 |
-
|
133 |
alert("Errore durante l'estrazione del testo dal file.");
|
134 |
} finally {
|
135 |
hideSpinner();
|
|
|
60 |
const msg = `<br><br> ${fileName}<br><br>caricato e salvato nella memoria locale`;
|
61 |
result.innerHTML = msg;
|
62 |
} catch (error) {
|
63 |
+
xerror("Error:", error);
|
64 |
alert("Errore durante l'estrazione del testo dal file.");
|
65 |
} finally {
|
66 |
hideSpinner();
|
|
|
129 |
msgs.push(msg);
|
130 |
}
|
131 |
} catch (error) {
|
132 |
+
xerror("Error:", error);
|
133 |
alert("Errore durante l'estrazione del testo dal file.");
|
134 |
} finally {
|
135 |
hideSpinner();
|
static/js/ragrqs.min.js
CHANGED
@@ -1,12 +1,15 @@
|
|
1 |
-
|
2 |
-
function
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
"
|
|
|
|
|
|
|
10 |
|
11 |
rag_rqs
|
12 |
Copyright (C) 2024 [Il tuo nome]
|
@@ -24,106 +27,127 @@ throw c;}},cancelRequest(){this.controller&&(this.isCancelled=!0,this.controller
|
|
24 |
You should have received a copy of the GNU General Public License
|
25 |
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
26 |
*/
|
27 |
-
const MAX_PROMPT_LENGTH=
|
28 |
-
addPrompt(a){this.prompts.push(a)},responsesLength(){return this.responses.reduce((a,b)=>a+b.length,0)},log(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,
|
29 |
-
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:
|
30 |
Stato: ${a.status}
|
31 |
Descrizione stato: ${a.statusText}
|
32 |
Tipo di errore: ${a.errorType}
|
33 |
-
Messaggio: ${a.message}`},async requestDocsRAG(a){
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
d="";
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
${a}
|
|
|
47 |
|
48 |
-
|
49 |
${b}
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
|
58 |
-
|
59 |
-
Elenco delle informazioni identificate, incluse eventuali inferenze
|
60 |
-
`}function promptToContext(a){return`
|
61 |
-
[[RUOLO]]
|
62 |
-
Esperto di analisi semantica delle informazioni.
|
63 |
-
|
64 |
-
[[OBIETTIVO]]
|
65 |
-
Unificare le informazioni semanticamente equivalenti da vari documenti.
|
66 |
-
|
67 |
-
[[ELENCO_RISPOSTE]]
|
68 |
-
${a}
|
69 |
-
|
70 |
-
[[ISTRUZIONI]]
|
71 |
-
1. Analizza le informazioni dell'elenco risposte.
|
72 |
-
2. Per ogni documento raggruppa le informazioni sulla base del loro significato.
|
73 |
-
3. Presenta le informazioni in modo chiaro e coerente.
|
74 |
-
|
75 |
-
[[OUTPUT]]
|
76 |
-
Elenco delle informazioni mantenendo la divisione per documento.
|
77 |
`}function promptWithContext(a,b){return`
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
${a}
|
|
|
83 |
|
84 |
-
|
85 |
${b}
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
|
94 |
-
|
95 |
-
L'output \u00e8 destinato ad essere letto da un lettore italiano.
|
96 |
`}function promptThread(a,b,c){return`
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
${a}
|
|
|
102 |
|
103 |
-
|
104 |
${b}
|
|
|
105 |
|
106 |
-
|
107 |
${c}
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
`}
|
120 |
-
|
121 |
-
repetition_penalty:1.1,return_full_text:!1,details:!1,max_time:120,seed:42},options:{use_cache:!1,wait_for_model:!0}}),getPayloadQuery=a=>({inputs:a,parameters:{task:"text-generation",max_new_tokens:512,num_return_sequences:1,temperature:.7,top_k:50,top_p:.85,do_sample:!0,no_repeat_ngram_size:3,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.33 (16-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)};
|
122 |
-
function openApp(){wnds.init();Menu.init();TextInput.init();TextOutput.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 op1(a){Rag.readFromDb();wnds.wpre.show(`\n${Rag.ragQuery}`)}function showRagResponse(a){Rag.readFromDb();wnds.wpre.show(`\n${Rag.ragResponse}`)}
|
123 |
-
function op2(a){a=[...Rag.responses];0==a.length&&(a=UaDb.readArray("id_responses"));0!=a.length&&(a=a.map((b,c)=>`[${c+1}]\n ${b.trim()}`).join("\n"),wnds.wpre.show(a))}function op3(a){Rag.readFromDb();wnds.wpre.show(`${Rag.ragContext}`)}function op4(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)}async function op5(a){DataMgr.readDbDocNames();a=DataMgr.doc_names.join("\n");wnds.wpre.show(a)}
|
124 |
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)}
|
125 |
-
function
|
126 |
-
function
|
127 |
<div class="window-text">
|
128 |
<div class="btn-wrapper">
|
129 |
<button class="btn-copy" title="Copia">
|
@@ -150,21 +174,21 @@ function op9(a){0!=Rag.prompts.length&&(a=Rag.prompts.map((b,c)=>`[${c+1}]${b}\n
|
|
150 |
`;this.w.drag();this.w.setZ(12);this.w.vw_vh().setXY(16.5,10,-1);this.w.setHtml(b);this.w.show();this.w.getElement().querySelector(".btn-copy").addEventListener("click",()=>this.copy())},close(){this.w.close()},async copy(){const b=this.w.getElement().querySelector(".div-text").textContent;try{await navigator.clipboard.writeText(b)}catch(c){xerror("Errore ",c)}}}),wnds={wdiv:null,wpre:null,wout:null,init(){this.wdiv=WndDiv("id_w0");this.wpre=WndPre("id_w1");this.wout=WndPre("id_out")},closeAll(){UaWindowAdm.close("id_w0");
|
151 |
UaWindowAdm.close("id_w1");UaWindowAdm.close("id_out")}},Menu={init(){const a=document.querySelector("#id-menu-btn");a.addEventListener("change",()=>{document.querySelector("body").classList.toggle("menu-open",a.checked)});const b=document.getElementById("id_log");UaLog.callHide=()=>{b.classList.contains("active")&&b.classList.remove("active")};UaLog.callShow=()=>{b.classList.contains("active")||b.classList.add("active")};UaLog.setXY(64,13).setZ(111).new();UaLog.log_show("Buon Lavoro")},close(){const a=
|
152 |
document.querySelector("#menu-toggle");document.querySelector("body").classList.remove("menu-open",a.checked);document.querySelector(".menu-btn").checked=!1},async help(){const a=await requestGet("./help0.html");wnds.wdiv.show(a)},upload(){RagUpload.open()},uploadDir(){RagUpload.openDir()},async load(){alert("load")},log(){UaLog.toggle()}},TextInput={wnd:null,init(){this.inp=document.querySelector(".text-input");document.addEventListener("keydown",a=>{document.activeElement!==this.inp&&("F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Control Alt Shift Meta CapsLock Escape PrintScreen ScrollLock Pause Insert Delete Home End PageUp PageDown ArrowLeft ArrowRight ArrowUp ArrowDown".split(" ").includes(a.key)||
|
153 |
-
a.ctrlKey||a.metaKey||a.preventDefault())});document.querySelector(".clear-button").addEventListener("click",()=>{this.inp.value="";this.inp.focus()});document.querySelector(".send-input").addEventListener("click",()=>this.send());document.querySelector(".send2-input").addEventListener("click",()=>this.send2());document.querySelector(".clear-input").addEventListener("click",()=>this.clear())},handleEnter(a){},async send(){if(this.inp.value){
|
154 |
showSpinner();setOutText("");var a=this.inp.value.trim();try{const b=await Rag.requestDocsRAG(a);setOutText(b);this.inp.value="";UaLog.close()}catch(b){a=`send\n${b}`,xerror(a),alert(a),setOutText(a)}hideSpinner()}else alert("Ricorda di scrivere la Query ")},async send2(){if(this.inp.value){showSpinner();ThreadMgr.isFirst()&&setOutText("");var a=this.inp.value.trim();try{const b=await Rag.requestContext(a);if(""==b){hideSpinner();return}setOutText(b);this.inp.value=""}catch(b){a=`send2\n${b}`,xerror(a),
|
155 |
alert(a),setOutText(a)}hideSpinner()}else alert("Ricorda di scrivere la Query ")},clear(){confirm("Confermi cancellazione conversazione? ")&&(this.inp.value="",setOutText(""),ThreadMgr.init())}},setOutText=a=>{var b=document.getElementById("id-text-out");b.innerHTML='<pre class="pre-text"></pre>';b=b.querySelector(".pre-text");b.textContent=a;b.scrollTop=b.scrollHeight};
|
156 |
TextOutput={init(){this.copyBtn=document.querySelector(".copy-output");this.copyBtn.addEventListener("click",()=>this.copy());document.querySelector(".clear-output").addEventListener("click",()=>this.clear());document.querySelector(".wnd-output").addEventListener("click",()=>this.openWnd())},openWnd(){const a=document.getElementById("id-text-out").textContent;wnds.wout.show(a)},async copy(){const a=document.getElementById("id-text-out").querySelector("pre");if(a){var b=a.textContent;a.classList.add("copied");
|
157 |
this.copyBtn.classList.add("copied");try{await navigator.clipboard.writeText(b)}catch(c){xerror("Errore ",c)}setTimeout(()=>{this.copyBtn.classList.remove("copied");a.classList.remove("copied")},5E3)}},clear(){document.getElementById("id-text-out").textContent=""}};const RagUpload={open(){const a=UaWindowAdm.create("id_upload");a.drag();a.setZ(12);a.vw_vh().setXY(16.5,10,-1);a.setHtml('\n <div class="window-text">\n <div class="btn-wrapper">\n <button class="btn-close" title="chiudi" onclick="UaWindowAdm.closeThis(this)">X</button>\n </div> \n <div class="upload"> \n <h4>Upload file Text / PDF</h4> \n <form id="uploadForm">\n <input class="file" type="file" id="id_fileupload" > \n <button type="button" onclick="RagUpload.upload();">Upload and Convert</button>\n </form>\n <div id="result" class="result"></div>\n </div>\n </div>\n ');
|
158 |
a.show()},async upload(){const a=document.getElementById("id_fileupload").files[0];if(a){var b=a.name;if(DataMgr.doc_names.includes(b))alert("Il file \u00e8 gi\u00e0 in archivio");else{var c=document.getElementById("result"),d=a.name.split(".").pop().toLowerCase();showSpinner();try{let e;if("pdf"===d){const f=new PdfHandler;await f.loadPdfJs();e=await f.extractTextFromPDF(a);f.cleanup()}else if("txt"===d)e=await readTextFile(a);else{alert("Formato file non supportato.");return}DataMgr.addDoc(b,e);
|
159 |
-
c.innerHTML=`<br><br> ${b}<br><br>caricato e salvato nella memoria locale`}catch(e){
|
160 |
-
a.show()},async uploadDir(){var a=document.getElementById("id_fileupload").files;if(a&&0!=a.length){var b=[];showSpinner();try{for(const c of a){const d=c.name;UaLog.log_show(d);if(DataMgr.doc_names.includes(d)){UaLog.log_shw(d+" : \u00e8 gi\u00e0 in archivio");continue}const e=c.name.split(".").pop().toLowerCase();let f;if("pdf"===e){const
|
161 |
-
return}DataMgr.addDoc(d,f);b.push(`${d}`)}}catch(c){
|
162 |
class PdfHandler{constructor(){this.workerScriptElement=this.scriptElement=this.pdfjsLib=null}async loadPdfJs(){window.pdfjsLib||(this.scriptElement=document.createElement("script"),this.scriptElement.src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.min.js",document.body.appendChild(this.scriptElement),await new Promise(a=>{this.scriptElement.onload=()=>{this.workerScriptElement=document.createElement("script");this.workerScriptElement.src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js";
|
163 |
document.body.appendChild(this.workerScriptElement);this.workerScriptElement.onload=a}}));this.pdfjsLib=window["pdfjs-dist/build/pdf"];this.pdfjsLib.GlobalWorkerOptions.workerSrc="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js"}async extractTextFromPDF(a){a=await a.arrayBuffer();a=await this.pdfjsLib.getDocument({data:a}).promise;let b="";for(let c=1;c<=a.numPages;c++){const d=(await (await a.getPage(c)).getTextContent()).items.map(e=>e.str).join(" ");b+=d+"\n"}return b}cleanup(){this.scriptElement&&
|
164 |
(document.body.removeChild(this.scriptElement),this.scriptElement=null);this.workerScriptElement&&(document.body.removeChild(this.workerScriptElement),this.workerScriptElement=null);this.pdfjsLib=null;window.gc&&window.gc()}}
|
165 |
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";
|
166 |
-
var UaDrag=function(a){return function(b){let c=0,d=0,e=0,f=0;const
|
167 |
-
l,document.onmousemove=
|
168 |
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:"inherit",background:"#000000",maxHeight:"600px",overflow:"auto",scrollbarColor:"#ab3205 #ffffff",scrollbarWidth:"auto"};for(const d in b)a.style[d]=
|
169 |
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,
|
170 |
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){var b=a.join("<br/>");a=document.getElementById(this.msg_id);b=a.innerHTML+b+"<br/>";b.length>this.max_length&&(b=b.slice(-this.max_length));a.innerHTML=b},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!=
|
|
|
1 |
+
const removeChars=a=>{a=a.replace(/[\u00AD\u200B\u200C\u200D\u2060\uFEFF]/g,"");return a=Array.from(a).filter(b=>!/^C/.test(b.codePointAt(0))).join("")},replaceChars=a=>{a=a.replace(/[\u00A0\u2000-\u200A\u202F\u205F\u3000\t\r\f\v]/g," ");a=a.replace(/ +/g," ");return a.trim()},removeTag=a=>a=a.replace(/<<<|>>>|<<|>>/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"}}function list2text(a){return a.map(b=>`${cleanResponse(b)}`).join("\n\n")}function cleanOutput(a){a=a.trim();a=a.replace(/\n{3,}/g,"\n\n");a=a.replace(/^[ \t]+/gm,"");a=a.replace(/^(\s*[-*\u2022])(\s*)/gm,"\n$1 ");a=a.replace(/^(\s*\d+\.)(\s*)/gm,"\n$1 ");a=a.replace(/(```[\s\S]*?```)/g,"\n\n$1\n\n");return a=a.replace(/[ \t]+$/gm,"")};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));
|
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=["bWtkVEpGRl8=","bVdncnlGUEk=","SFNZa1h1W3U=","WXxmcW9nf1U=","dEd+ZnE="],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);
|
9 |
+
async function requestGet(a){try{var b=await fetch(a,{method:"GET",headers:{"Content-Type":"text/plain;charset=UTF-8"}});if(!b.ok)throw Error(`HTTP error! status: ${b.status}`);const c=await b.arrayBuffer();return(new TextDecoder("utf-8")).decode(c)}catch(c){throw xerror(c),b="AbortError"===c.name?"Request was aborted":c.message.includes("HTTP error! status")?c.message:"An error occurred",alert(`requestGet()\nurl: ${a}\n${b}`),c;}}
|
10 |
+
const ERROR_TOKENS="ERROR_TOKENS",TIMEOUT_ERROR="TIMEOUT_ERROR",HfRequest={controller:null,isCancelled:!1,baseUrl:"https://api-inference.huggingface.co/models",async post(a,b=3E4){this.isCancelled=!1;this.controller=new AbortController;try{const c=setTimeout(()=>{this.controller.abort()},b),d=await fetch(`${this.baseUrl}/${model_name}`,{method:"POST",headers:{Authorization:`Bearer ${tm}`,"Content-Type":"application/json"},body:JSON.stringify(a),signal:this.controller.signal});clearTimeout(c);if(this.isCancelled)return null;
|
11 |
+
const e=await d.json();if(!d.ok){const g=this.checkError(d.status,e),l=this.createErrorInfo(d.status,d.statusText,e.error,g);throw Error(l);}const f=this.isInvalidResponse(e);if("string"===typeof f){const g=this.createErrorInfo(d.status,d.statusText,e.error,f);throw Error(g);}return e[0].generated_text.trim()}catch(c){if(this.isCancelled)return null;if("AbortError"===c.name)throw a=this.createErrorInfo(0,"Timeout","La richiesta \u00e8 scaduta",TIMEOUT_ERROR),Error(a);throw c;}},cancelRequest(){this.controller&&
|
12 |
+
(this.isCancelled=!0,this.controller.abort())},checkError(a,b){return 500<=a?"SERVER_ERROR":400<=a&&500>a?"object"===typeof b&&"validation"===b.error_type?b.error.includes("tokens")?ERROR_TOKENS:"ERROR_VALIDATION":"CLIENT_ERROR":"UNKNOWN_ERROR"},isInvalidResponse(a){return a&&Array.isArray(a)&&0!==a.length?a[0].generated_text?null:"MISSING_GENERATED_TEXT":"INVALID_DATA"},createErrorInfo(a,b,c,d){return JSON.stringify({status:a,statusText:b,errorType:d,message:c||"Errore sconosciuto"})}};/*
|
13 |
|
14 |
rag_rqs
|
15 |
Copyright (C) 2024 [Il tuo nome]
|
|
|
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=81920,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")},
|
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)},log(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,
|
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;UaLog.log(`${e} (${f}) `);xlog(`${e} (${f}) `);++b;let g=0,l="",h="",k=0,n="";for(;;){let q=this.getPartSize(d,promptDoc("",a),k);if(10>q)break;[l,h]=this.getPartDoc(d,q);n=promptDoc(l,a);const r=getPayloadDoc(n);let m;try{if(m=
|
37 |
+
await HfRequest.post(r),!m)return""}catch(p){if(this.errorInfo(p).errorType===ERROR_TOKENS){UaLog.log(`Error tokens.1 ${n.length}`);xlog(`Error tokens. ${n.length}`);k+=PROMPT_DECR;continue}else throw xerror(p),this.errorTotext(p);}g++;d=h;1==g&&(m=`Documento:${e}\n${m}`);this.responses.push(m);this.log(`${b},${g}`,l.length,h.length)}}}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=
|
38 |
+
getPayloadWithContext(c);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();
|
39 |
+
try{var c=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();
|
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}`);xlog(`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=g,
|
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.
|
45 |
+
|
46 |
+
TASK:
|
47 |
+
Estrarre informazioni dal documento fornito in relazione a una domanda specifica.
|
48 |
+
|
49 |
+
INSTRUCTIONS:
|
50 |
+
1. Analizza attentamente il documento.
|
51 |
+
2. Identifica le informazioni pertinenti alla domanda.
|
52 |
+
3. Estrai i concetti chiave e fai inferenze ragionevoli.
|
53 |
+
4. Organizza le informazioni in modo logico.
|
54 |
+
5. Prepara una risposta completa ma concisa.
|
55 |
+
6. Struttura la risposta in paragrafi logici.
|
56 |
+
7. Includi un'introduzione breve, sviluppa l'analisi, presenta le inferenze e concludi con una sintesi.
|
57 |
+
8. Cita il documento solo se assolutamente necessario per chiarire informazioni cruciali.
|
58 |
+
9. Mantieni un tono oggettivo e uno stile fluido e coerente.
|
59 |
+
|
60 |
+
DOCUMENT:
|
61 |
+
<<<BEGIN_DOCUMENT>>>
|
62 |
${a}
|
63 |
+
<<<END_DOCUMENT>>>
|
64 |
|
65 |
+
QUESTION:
|
66 |
${b}
|
67 |
|
68 |
+
OUTPUT_FORMAT:
|
69 |
+
- Inizia la tua risposta direttamente con il contenuto, senza etichette o introduzioni.
|
70 |
+
- Fornisci la tua risposta come testo continuo diviso in paragrafi.
|
71 |
+
- Non usare elenchi puntati o numerati.
|
72 |
+
- Evita qualsiasi tipo di etichetta o marcatori speciali.
|
73 |
+
- Mantieni un tono oggettivo e uno stile fluido e coerente.
|
74 |
|
75 |
+
RESPONSE:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
`}function promptWithContext(a,b){return`
|
77 |
+
SYSTEM:
|
78 |
+
Sei un assistente AI specializzato nell'analisi e nell'elaborazione di informazioni contestuali..Rispondi sempre in italiano.
|
79 |
+
|
80 |
+
TASK:
|
81 |
+
Elaborare la risposta alla domanda sulla base del contesto fornito.
|
82 |
+
|
83 |
+
INSTRUCTIONS:
|
84 |
+
1. Analizza attentamente il contesto fornito.
|
85 |
+
2. Identifica le informazioni pertinenti alla domanda.
|
86 |
+
3. Estrai i concetti chiave e fai inferenze ragionevoli.
|
87 |
+
4. Organizza le informazioni in modo logico.
|
88 |
+
5. Prepara una risposta completa ma concisa.
|
89 |
+
6. Struttura la risposta in paragrafi logici.
|
90 |
+
7. Includi un'introduzione breve, sviluppa l'analisi, presenta le inferenze e concludi con una sintesi.
|
91 |
+
8. Cita il documento solo se assolutamente necessario per chiarire informazioni cruciali.
|
92 |
+
9. Mantieni un tono oggettivo e uno stile fluido e coerente.
|
93 |
+
|
94 |
+
CONTEXT:
|
95 |
+
<<<BEGIN_CONTEXT>>>
|
96 |
${a}
|
97 |
+
<<<END_CONTEXT>>>
|
98 |
|
99 |
+
QUESTION:
|
100 |
${b}
|
101 |
|
102 |
+
OUTPUT_FORMAT:
|
103 |
+
- Inizia la tua risposta direttamente con il contenuto, senza etichette o introduzioni.
|
104 |
+
- Fornisci la tua risposta come testo continuo diviso in paragrafi.
|
105 |
+
- Non usare elenchi puntati o numerati.
|
106 |
+
- Evita qualsiasi tipo di etichetta o marcatori speciali.
|
107 |
+
- Mantieni un tono oggettivo e uno stile fluido e coerente.
|
108 |
|
109 |
+
RESPONSE:
|
|
|
110 |
`}function promptThread(a,b,c){return`
|
111 |
+
SYSTEM:
|
112 |
+
Sei un assistente AI versatile progettato per gestire conversazioni dinamiche e adattarti a varie richieste. Rispondi in italiano.
|
113 |
+
|
114 |
+
INSTRUCTIONS:
|
115 |
+
1. Analizza il contesto, la conversazione precedente e la richiesta attuale.
|
116 |
+
2. Interpreta l'intento dell'utente senza limitarti a categorie predefinite.
|
117 |
+
3. Adatta la tua risposta in base all'intento percepito, sia esso una domanda, una richiesta di azione, un'istruzione specifica o altro.
|
118 |
+
4. Mantieni la coerenza con il contesto della conversazione.
|
119 |
+
5. Adatta tono e stile in base all'interazione, rimanendo professionale ma amichevole.
|
120 |
+
6. Fai riferimento a informazioni precedenti quando pertinente.
|
121 |
+
7. Se l'intento non \u00e8 chiaro, chiedi gentilmente chiarimenti.
|
122 |
+
8. Sii flessibile: se la richiesta implica un formato o un'azione specifica, adattati di conseguenza.
|
123 |
+
|
124 |
+
CONTEXT:
|
125 |
+
<<<BEGIN_CONTEXT>>>
|
126 |
${a}
|
127 |
+
<<<END_CONTEXT>>>
|
128 |
|
129 |
+
<<<BEGIN_CONVERSATION>>>
|
130 |
${b}
|
131 |
+
<<<END_CONVERSATION>>>
|
132 |
|
133 |
+
REQUEST:
|
134 |
${c}
|
135 |
|
136 |
+
OUTPUT_FORMAT:
|
137 |
+
- Inizia la risposta direttamente con il contenuto, senza etichette o introduzioni.
|
138 |
+
- Adatta il formato della risposta in base alla richiesta dell'utente.
|
139 |
+
- Se non viene specificato un formato particolare, fornisci la risposta come testo continuo diviso in paragrafi.
|
140 |
+
- Mantieni un tono oggettivo e uno stile fluido e coerente, a meno che non sia richiesto diversamente.
|
141 |
+
|
142 |
+
RESPONSE:
|
143 |
+
`}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}}}
|
144 |
+
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}}}
|
145 |
+
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.36 (22-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)};
|
146 |
+
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)}
|
147 |
+
function elencoRisposte(a){a=[...Rag.responses];0==a.length&&(a=UaDb.readArray("id_responses"));0!=a.length&&(a=a.map((b,c)=>`[${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)}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=`
|
151 |
<div class="window-text">
|
152 |
<div class="btn-wrapper">
|
153 |
<button class="btn-copy" title="Copia">
|
|
|
174 |
`;this.w.drag();this.w.setZ(12);this.w.vw_vh().setXY(16.5,10,-1);this.w.setHtml(b);this.w.show();this.w.getElement().querySelector(".btn-copy").addEventListener("click",()=>this.copy())},close(){this.w.close()},async copy(){const b=this.w.getElement().querySelector(".div-text").textContent;try{await navigator.clipboard.writeText(b)}catch(c){xerror("Errore ",c)}}}),wnds={wdiv:null,wpre:null,wout:null,init(){this.wdiv=WndDiv("id_w0");this.wpre=WndPre("id_w1");this.wout=WndPre("id_out")},closeAll(){UaWindowAdm.close("id_w0");
|
175 |
UaWindowAdm.close("id_w1");UaWindowAdm.close("id_out")}},Menu={init(){const a=document.querySelector("#id-menu-btn");a.addEventListener("change",()=>{document.querySelector("body").classList.toggle("menu-open",a.checked)});const b=document.getElementById("id_log");UaLog.callHide=()=>{b.classList.contains("active")&&b.classList.remove("active")};UaLog.callShow=()=>{b.classList.contains("active")||b.classList.add("active")};UaLog.setXY(64,13).setZ(111).new();UaLog.log_show("Buon Lavoro")},close(){const a=
|
176 |
document.querySelector("#menu-toggle");document.querySelector("body").classList.remove("menu-open",a.checked);document.querySelector(".menu-btn").checked=!1},async help(){const a=await requestGet("./help0.html");wnds.wdiv.show(a)},upload(){RagUpload.open()},uploadDir(){RagUpload.openDir()},async load(){alert("load")},log(){UaLog.toggle()}},TextInput={wnd:null,init(){this.inp=document.querySelector(".text-input");document.addEventListener("keydown",a=>{document.activeElement!==this.inp&&("F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Control Alt Shift Meta CapsLock Escape PrintScreen ScrollLock Pause Insert Delete Home End PageUp PageDown ArrowLeft ArrowRight ArrowUp ArrowDown".split(" ").includes(a.key)||
|
177 |
+
a.ctrlKey||a.metaKey||a.preventDefault())});document.querySelector(".clear-button").addEventListener("click",()=>{this.inp.value="";this.inp.focus()});document.querySelector(".send-input").addEventListener("click",()=>this.send());document.querySelector(".send2-input").addEventListener("click",()=>this.send2());document.querySelector(".clear-input").addEventListener("click",()=>this.clear())},handleEnter(a){},async send(){if(this.inp.value){if(Rag.ragContext&&!confirm("Vuoi iniziare una nuova elabrazione ?"))return"";
|
178 |
showSpinner();setOutText("");var a=this.inp.value.trim();try{const b=await Rag.requestDocsRAG(a);setOutText(b);this.inp.value="";UaLog.close()}catch(b){a=`send\n${b}`,xerror(a),alert(a),setOutText(a)}hideSpinner()}else alert("Ricorda di scrivere la Query ")},async send2(){if(this.inp.value){showSpinner();ThreadMgr.isFirst()&&setOutText("");var a=this.inp.value.trim();try{const b=await Rag.requestContext(a);if(""==b){hideSpinner();return}setOutText(b);this.inp.value=""}catch(b){a=`send2\n${b}`,xerror(a),
|
179 |
alert(a),setOutText(a)}hideSpinner()}else alert("Ricorda di scrivere la Query ")},clear(){confirm("Confermi cancellazione conversazione? ")&&(this.inp.value="",setOutText(""),ThreadMgr.init())}},setOutText=a=>{var b=document.getElementById("id-text-out");b.innerHTML='<pre class="pre-text"></pre>';b=b.querySelector(".pre-text");b.textContent=a;b.scrollTop=b.scrollHeight};
|
180 |
TextOutput={init(){this.copyBtn=document.querySelector(".copy-output");this.copyBtn.addEventListener("click",()=>this.copy());document.querySelector(".clear-output").addEventListener("click",()=>this.clear());document.querySelector(".wnd-output").addEventListener("click",()=>this.openWnd())},openWnd(){const a=document.getElementById("id-text-out").textContent;wnds.wout.show(a)},async copy(){const a=document.getElementById("id-text-out").querySelector("pre");if(a){var b=a.textContent;a.classList.add("copied");
|
181 |
this.copyBtn.classList.add("copied");try{await navigator.clipboard.writeText(b)}catch(c){xerror("Errore ",c)}setTimeout(()=>{this.copyBtn.classList.remove("copied");a.classList.remove("copied")},5E3)}},clear(){document.getElementById("id-text-out").textContent=""}};const RagUpload={open(){const a=UaWindowAdm.create("id_upload");a.drag();a.setZ(12);a.vw_vh().setXY(16.5,10,-1);a.setHtml('\n <div class="window-text">\n <div class="btn-wrapper">\n <button class="btn-close" title="chiudi" onclick="UaWindowAdm.closeThis(this)">X</button>\n </div> \n <div class="upload"> \n <h4>Upload file Text / PDF</h4> \n <form id="uploadForm">\n <input class="file" type="file" id="id_fileupload" > \n <button type="button" onclick="RagUpload.upload();">Upload and Convert</button>\n </form>\n <div id="result" class="result"></div>\n </div>\n </div>\n ');
|
182 |
a.show()},async upload(){const a=document.getElementById("id_fileupload").files[0];if(a){var b=a.name;if(DataMgr.doc_names.includes(b))alert("Il file \u00e8 gi\u00e0 in archivio");else{var c=document.getElementById("result"),d=a.name.split(".").pop().toLowerCase();showSpinner();try{let e;if("pdf"===d){const f=new PdfHandler;await f.loadPdfJs();e=await f.extractTextFromPDF(a);f.cleanup()}else if("txt"===d)e=await readTextFile(a);else{alert("Formato file non supportato.");return}DataMgr.addDoc(b,e);
|
183 |
+
c.innerHTML=`<br><br> ${b}<br><br>caricato e salvato nella memoria locale`}catch(e){xerror("Error:",e),alert("Errore durante l'estrazione del testo dal file.")}finally{hideSpinner()}}}else alert("Nessun file selezionato.")},openDir(){const a=UaWindowAdm.create("id_upload");a.drag();a.setZ(12);a.vw_vh().setXY(16.5,10,-1);a.setHtml('\n <div class="window-text">\n <div class="btn-wrapper">\n <button class="btn-close" title="chiudi" onclick="UaWindowAdm.closeThis(this)">X</button>\n </div> \n <div class="upload"> \n <h4>Upload files Text / PDF</h4> \n <form id="uploadForm">\n <input id="id_fileupload" class="file" type="file" webkitdirectory mozdirectory msdirectory odirectory directory multiple />\n <button type="button" onclick="RagUpload.uploadDir();">Upload and Convert</button>\n </form>\n <div class="result" id="result"></div>\n </div>\n </div>\n ');
|
184 |
+
a.show()},async uploadDir(){var a=document.getElementById("id_fileupload").files;if(a&&0!=a.length){var b=[];showSpinner();try{for(const c of a){const d=c.name;UaLog.log_show(d);if(DataMgr.doc_names.includes(d)){UaLog.log_shw(d+" : \u00e8 gi\u00e0 in archivio");continue}const e=c.name.split(".").pop().toLowerCase();let f;if("pdf"===e){const g=new PdfHandler;await g.loadPdfJs();f=await g.extractTextFromPDF(c);g.cleanup()}else if("txt"===e)f=await readTextFile(c);else{alert("Formato file non supportato.");
|
185 |
+
return}DataMgr.addDoc(d,f);b.push(`${d}`)}}catch(c){xerror("Error:",c),alert("Errore durante l'estrazione del testo dal file.")}finally{hideSpinner()}a=document.getElementById("result");b=b.join("<br>");a.innerHTML=b}else alert("Nessun file selezionato.")}};
|
186 |
class PdfHandler{constructor(){this.workerScriptElement=this.scriptElement=this.pdfjsLib=null}async loadPdfJs(){window.pdfjsLib||(this.scriptElement=document.createElement("script"),this.scriptElement.src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.min.js",document.body.appendChild(this.scriptElement),await new Promise(a=>{this.scriptElement.onload=()=>{this.workerScriptElement=document.createElement("script");this.workerScriptElement.src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js";
|
187 |
document.body.appendChild(this.workerScriptElement);this.workerScriptElement.onload=a}}));this.pdfjsLib=window["pdfjs-dist/build/pdf"];this.pdfjsLib.GlobalWorkerOptions.workerSrc="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js"}async extractTextFromPDF(a){a=await a.arrayBuffer();a=await this.pdfjsLib.getDocument({data:a}).promise;let b="";for(let c=1;c<=a.numPages;c++){const d=(await (await a.getPage(c)).getTextContent()).items.map(e=>e.str).join(" ");b+=d+"\n"}return b}cleanup(){this.scriptElement&&
|
188 |
(document.body.removeChild(this.scriptElement),this.scriptElement=null);this.workerScriptElement&&(document.body.removeChild(this.workerScriptElement),this.workerScriptElement=null);this.pdfjsLib=null;window.gc&&window.gc()}}
|
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:"inherit",background:"#000000",maxHeight:"600px",overflow:"auto",scrollbarColor:"#ab3205 #ffffff",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){var b=a.join("<br/>");a=document.getElementById(this.msg_id);b=a.innerHTML+b+"<br/>";b.length>this.max_length&&(b=b.slice(-this.max_length));a.innerHTML=b},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!=
|
static/less/less.js
CHANGED
@@ -7957,11 +7957,11 @@
|
|
7957 |
}
|
7958 |
});
|
7959 |
|
7960 |
-
var QueryInParens = function (op, l, m,
|
7961 |
this.op = op.trim();
|
7962 |
this.lvalue = l;
|
7963 |
this.mvalue = m;
|
7964 |
-
this.
|
7965 |
this.rvalue = r;
|
7966 |
this._index = i;
|
7967 |
};
|
@@ -7987,7 +7987,7 @@
|
|
7987 |
output.add(' ' + this.op + ' ');
|
7988 |
this.mvalue.genCSS(context, output);
|
7989 |
if (this.rvalue) {
|
7990 |
-
output.add(' ' + this.
|
7991 |
this.rvalue.genCSS(context, output);
|
7992 |
}
|
7993 |
},
|
|
|
7957 |
}
|
7958 |
});
|
7959 |
|
7960 |
+
var QueryInParens = function (op, l, m, elencoRisposte, r, i) {
|
7961 |
this.op = op.trim();
|
7962 |
this.lvalue = l;
|
7963 |
this.mvalue = m;
|
7964 |
+
this.elencoRisposte = elencoRisposte ? elencoRisposte.trim() : null;
|
7965 |
this.rvalue = r;
|
7966 |
this._index = i;
|
7967 |
};
|
|
|
7987 |
output.add(' ' + this.op + ' ');
|
7988 |
this.mvalue.genCSS(context, output);
|
7989 |
if (this.rvalue) {
|
7990 |
+
output.add(' ' + this.elencoRisposte + ' ');
|
7991 |
this.rvalue.genCSS(context, output);
|
7992 |
}
|
7993 |
},
|
static/ragrqs.html
CHANGED
@@ -4,12 +4,13 @@
|
|
4 |
<head>
|
5 |
<meta charset="utf-8" />
|
6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
7 |
-
<meta name="description" content="Author:
|
8 |
<title>RagRqs</title>
|
9 |
|
10 |
<link rel="stylesheet" href="css/ragrqs.min.css" />
|
11 |
<script src="js/ragrqs.min.js"></script>
|
12 |
|
|
|
13 |
<body>
|
14 |
<div id="spinner" class="spinner">
|
15 |
<div class="pulse-icon"></div>
|
@@ -45,23 +46,25 @@
|
|
45 |
<div class="menu-box">
|
46 |
<ul>
|
47 |
<li><a href="#" onCLick="op0(this);">README</a></li>
|
48 |
-
<li><a href="#" onCLick="
|
49 |
-
<li><a href="#" onCLick="showRagResponse(
|
50 |
-
<li><a href="#" onCLick="
|
51 |
-
|
52 |
-
<li><a href="#" onCLick="
|
53 |
-
<li><a href="#" onCLick="
|
54 |
-
<li><a href="#" onCLick="
|
55 |
-
<li><a
|
|
|
|
|
56 |
<li>-</li>
|
57 |
-
<li><a class="help" href="#" onCLick="
|
58 |
<li>-</li>
|
59 |
<li><a href="#" onCLick="help1(this);">Esempi di documenti</a></li>
|
60 |
<li><a href="#" onCLick="help2(this);">Esempi di domande</a></li>
|
61 |
-
|
62 |
-
<li><a href="#" onCLick="
|
63 |
-
<li><a href="#" onCLick="
|
64 |
-
|
65 |
</ul>
|
66 |
</div>
|
67 |
</div>
|
|
|
4 |
<head>
|
5 |
<meta charset="utf-8" />
|
6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
7 |
+
<meta name="description" content="Author:G.Materni" />
|
8 |
<title>RagRqs</title>
|
9 |
|
10 |
<link rel="stylesheet" href="css/ragrqs.min.css" />
|
11 |
<script src="js/ragrqs.min.js"></script>
|
12 |
|
13 |
+
|
14 |
<body>
|
15 |
<div id="spinner" class="spinner">
|
16 |
<div class="pulse-icon"></div>
|
|
|
46 |
<div class="menu-box">
|
47 |
<ul>
|
48 |
<li><a href="#" onCLick="op0(this);">README</a></li>
|
49 |
+
<li><a href="#" onCLick="showQuery();">Domanda</a></li>
|
50 |
+
<li><a href="#" onCLick="showRagResponse();">Risposta</a></li>
|
51 |
+
<li><a href="#" onCLick="showThread();">Conversazione</a></li>
|
52 |
+
|
53 |
+
<li><a href="#" onCLick="elencoRisposte(this);">Elenco Risposte</a></li>
|
54 |
+
<li><a href="#" onCLick="showContesto(this);">Contesto RAG</a></li>
|
55 |
+
<li><a href="#" onCLick="elencoDati(this);">Dati archviati</a></li>
|
56 |
+
<li><a href="#" onCLick="elencoDocs(this);">Elenco Documenti</a></li>
|
57 |
+
<li><a href="#" onCLick="calcQuery();">Numero Richieste</a></li>
|
58 |
+
<li><a class="delete" href="#" onCLick="deleteDati(this);">Cancella Dati</a></li>
|
59 |
<li>-</li>
|
60 |
+
<li><a class="help" href="#" onCLick="deleteSttorage(this);">Cancella Documenti</a></li>
|
61 |
<li>-</li>
|
62 |
<li><a href="#" onCLick="help1(this);">Esempi di documenti</a></li>
|
63 |
<li><a href="#" onCLick="help2(this);">Esempi di domande</a></li>
|
64 |
+
|
65 |
+
<!-- <li><a href="#" onCLick="showPrompts(this);">prompts</a></li>
|
66 |
+
<li><a href="#" onCLick="showTesti(this);">Testo Documenti</a></li> -->
|
67 |
+
|
68 |
</ul>
|
69 |
</div>
|
70 |
</div>
|
static/ragrqs_.html
CHANGED
@@ -63,22 +63,24 @@
|
|
63 |
<div class="menu-box">
|
64 |
<ul>
|
65 |
<li><a href="#" onCLick="op0(this);">README</a></li>
|
66 |
-
<li><a href="#" onCLick="
|
67 |
-
<li><a href="#" onCLick="showRagResponse(
|
68 |
-
<li><a href="#" onCLick="
|
69 |
-
|
70 |
-
<li><a href="#" onCLick="
|
71 |
-
<li><a href="#" onCLick="
|
72 |
-
<li><a href="#" onCLick="
|
73 |
-
<li><a
|
|
|
|
|
74 |
<li>-</li>
|
75 |
-
<li><a class="help" href="#" onCLick="
|
76 |
<li>-</li>
|
77 |
<li><a href="#" onCLick="help1(this);">Esempi di documenti</a></li>
|
78 |
<li><a href="#" onCLick="help2(this);">Esempi di domande</a></li>
|
79 |
|
80 |
-
<li><a href="#" onCLick="
|
81 |
-
<li><a href="#" onCLick="
|
82 |
|
83 |
</ul>
|
84 |
</div>
|
|
|
63 |
<div class="menu-box">
|
64 |
<ul>
|
65 |
<li><a href="#" onCLick="op0(this);">README</a></li>
|
66 |
+
<li><a href="#" onCLick="showQuery();">Domanda</a></li>
|
67 |
+
<li><a href="#" onCLick="showRagResponse();">Risposta</a></li>
|
68 |
+
<li><a href="#" onCLick="showThread();">Conversazione</a></li>
|
69 |
+
|
70 |
+
<li><a href="#" onCLick="elencoRisposte(this);">Elenco Risposte</a></li>
|
71 |
+
<li><a href="#" onCLick="showContesto(this);">Contesto RAG</a></li>
|
72 |
+
<li><a href="#" onCLick="elencoDati(this);">Dati archviati</a></li>
|
73 |
+
<li><a href="#" onCLick="elencoDocs(this);">Elenco Documenti</a></li>
|
74 |
+
<li><a href="#" onCLick="calcQuery();">Numero Richieste</a></li>
|
75 |
+
<li><a class="delete" href="#" onCLick="deleteDati(this);">Cancella Dati</a></li>
|
76 |
<li>-</li>
|
77 |
+
<li><a class="help" href="#" onCLick="deleteSttorage(this);">Cancella Documenti</a></li>
|
78 |
<li>-</li>
|
79 |
<li><a href="#" onCLick="help1(this);">Esempi di documenti</a></li>
|
80 |
<li><a href="#" onCLick="help2(this);">Esempi di domande</a></li>
|
81 |
|
82 |
+
<li><a href="#" onCLick="showPrompts(this);">prompts</a></li>
|
83 |
+
<li><a href="#" onCLick="showTesti(this);">Testo Documenti</a></li>
|
84 |
|
85 |
</ul>
|
86 |
</div>
|