Upload folder using huggingface_hub
Browse files- static/javascript/rag_mgr.js +7 -2
- static/javascript/rag_prompts.js +42 -36
- static/javascript/rag_rqs.js +1 -1
- static/js/ragrqs.min.js +22 -45
static/javascript/rag_mgr.js
CHANGED
|
@@ -166,6 +166,7 @@ const Rag = {
|
|
| 166 |
ragLog(`${j}) ${ndoc},${npart}`, lft.length, rgt.length, this.answers);
|
| 167 |
prompt = promptDoc(lft, query, docName);
|
| 168 |
const payload = getPayloadDoc(prompt);
|
|
|
|
| 169 |
try {
|
| 170 |
answer = await HfRequest.post(payload, TIMEOUT);
|
| 171 |
if (!answer) return "";
|
|
@@ -178,12 +179,16 @@ const Rag = {
|
|
| 178 |
continue;
|
| 179 |
} else if (ei.errorType === TIMEOUT_ERROR) {
|
| 180 |
UaLog.log(`Error timeout Doc`);
|
| 181 |
-
// wait(5);
|
| 182 |
continue;
|
| 183 |
} else {
|
| 184 |
-
|
|
|
|
|
|
|
| 185 |
}
|
| 186 |
} //end catch
|
|
|
|
|
|
|
|
|
|
| 187 |
npart++;
|
| 188 |
j++;
|
| 189 |
doc = rgt;
|
|
|
|
| 166 |
ragLog(`${j}) ${ndoc},${npart}`, lft.length, rgt.length, this.answers);
|
| 167 |
prompt = promptDoc(lft, query, docName);
|
| 168 |
const payload = getPayloadDoc(prompt);
|
| 169 |
+
// const t0 = performance.now();
|
| 170 |
try {
|
| 171 |
answer = await HfRequest.post(payload, TIMEOUT);
|
| 172 |
if (!answer) return "";
|
|
|
|
| 179 |
continue;
|
| 180 |
} else if (ei.errorType === TIMEOUT_ERROR) {
|
| 181 |
UaLog.log(`Error timeout Doc`);
|
|
|
|
| 182 |
continue;
|
| 183 |
} else {
|
| 184 |
+
UaLog.log(`Error ${err}`);
|
| 185 |
+
answer=`ERROR \n${err}\n`
|
| 186 |
+
// throw err;
|
| 187 |
}
|
| 188 |
} //end catch
|
| 189 |
+
// const t1 = performance.now();
|
| 190 |
+
// const dt = Math.round((t1 - t0) / 1000);
|
| 191 |
+
// UaLog.log(`t: ${dt}`);
|
| 192 |
npart++;
|
| 193 |
j++;
|
| 194 |
doc = rgt;
|
static/javascript/rag_prompts.js
CHANGED
|
@@ -145,84 +145,90 @@
|
|
| 145 |
// RESPONSE:
|
| 146 |
// `;
|
| 147 |
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
/////////////////
|
| 149 |
function promptDoc(testo, domanda, docName) {
|
| 150 |
return `
|
| 151 |
-
|
| 152 |
|
| 153 |
-
|
| 154 |
|
| 155 |
-
|
| 156 |
1. Esamina il testo e identifica le informazioni utili per costruire un contesto relativo alla domanda.
|
| 157 |
2. Per ogni informazione rilevante, fornisci una breve descrizione.
|
| 158 |
3. Elenca i punti chiave pertinenti per ogni informazione.
|
| 159 |
4. Riporta elementi specifici (dati, citazioni, eventi) se utili per il contesto.
|
| 160 |
5. Struttura la risposta secondo il formato di output specificato.
|
| 161 |
|
|
|
|
|
|
|
| 162 |
TESTO DA ANALIZZARE:
|
| 163 |
<<<INIZIO_TESTO>>>
|
| 164 |
${testo}
|
| 165 |
<<<FINE_TESTO>>>
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
Descrizione:
|
| 170 |
-
Punti chiave:
|
| 171 |
-
Elementi specifici:
|
| 172 |
-
|
| 173 |
-
- Informazione 2:
|
| 174 |
-
Descrizione:
|
| 175 |
-
Punti chiave:
|
| 176 |
-
Elementi specifici:
|
| 177 |
-
|
| 178 |
-
(Ripeti il formato per ogni informazione identificata)
|
| 179 |
|
| 180 |
-
|
| 181 |
`;
|
| 182 |
}
|
| 183 |
|
| 184 |
function promptBuildContext(informazioni, domanda = "") {
|
| 185 |
return `
|
| 186 |
-
|
| 187 |
|
| 188 |
-
|
| 189 |
|
| 190 |
-
|
| 191 |
1. Esamina tutte le informazioni fornite.
|
| 192 |
-
2. Seleziona e raggruppa le informazioni
|
| 193 |
3. Per ogni gruppo di informazioni rilevanti, genera una descrizione concisa.
|
| 194 |
4. Elenca i punti chiave essenziali per comprendere il contesto.
|
| 195 |
5. Riporta elementi specifici (dati, citazioni, eventi) se utili.
|
| 196 |
6. Elimina le informazioni ridondanti, mantenendo solo la versione più completa.
|
| 197 |
7. Elabora inferenze logiche basate sulle informazioni, se rilevanti per il contesto.
|
| 198 |
8. Genera una sintesi finale del contesto.
|
| 199 |
-
9. Struttura la risposta secondo il formato di output specificato.
|
| 200 |
|
| 201 |
INFORMAZIONI:
|
| 202 |
<<<INIZIO_INFORMAZIONI>>>
|
| 203 |
${informazioni}
|
| 204 |
<<<FINE_INFORMAZIONI>>>
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
|
| 217 |
-
(Ripeti il formato per ogni gruppo di informazioni
|
| 218 |
|
| 219 |
-
- Inferenze Logiche:
|
| 220 |
|
| 221 |
-
- Sintesi Finale:
|
| 222 |
|
| 223 |
-
RISPOSTA:
|
| 224 |
-
`;
|
| 225 |
-
}
|
| 226 |
|
| 227 |
function promptWithContext(contesto, domanda) {
|
| 228 |
return `
|
|
|
|
| 145 |
// RESPONSE:
|
| 146 |
// `;
|
| 147 |
// }
|
| 148 |
+
// - Informazione 1:
|
| 149 |
+
// Descrizione:
|
| 150 |
+
// Punti chiave:
|
| 151 |
+
// Elementi specifici:
|
| 152 |
+
|
| 153 |
+
// - Informazione 2:
|
| 154 |
+
// Descrizione:
|
| 155 |
+
// Punti chiave:
|
| 156 |
+
// Elementi specifici:
|
| 157 |
+
|
| 158 |
+
// (Ripeti il formato per ogni informazione identificata)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
/////////////////
|
| 162 |
function promptDoc(testo, domanda, docName) {
|
| 163 |
return `
|
| 164 |
+
SYSTEM: Sei un assistente AI specializzato nell'analisi documentale e nell'estrazione mirata di informazioni. Rispondi esclusivamente in italiano.
|
| 165 |
|
| 166 |
+
TASK: Analizza l'estratto del documento "${docName}" e identifica le informazioni rilevanti per rispondere alla domanda: "${domanda}".
|
| 167 |
|
| 168 |
+
INSTRUCTIONS:
|
| 169 |
1. Esamina il testo e identifica le informazioni utili per costruire un contesto relativo alla domanda.
|
| 170 |
2. Per ogni informazione rilevante, fornisci una breve descrizione.
|
| 171 |
3. Elenca i punti chiave pertinenti per ogni informazione.
|
| 172 |
4. Riporta elementi specifici (dati, citazioni, eventi) se utili per il contesto.
|
| 173 |
5. Struttura la risposta secondo il formato di output specificato.
|
| 174 |
|
| 175 |
+
DOMANDA: ${domanda}
|
| 176 |
+
|
| 177 |
TESTO DA ANALIZZARE:
|
| 178 |
<<<INIZIO_TESTO>>>
|
| 179 |
${testo}
|
| 180 |
<<<FINE_TESTO>>>
|
| 181 |
|
| 182 |
+
OUTPUT_FORMAT:
|
| 183 |
+
L'ouput deve essere organizzato sula base delle istruioni. L'output deve essere privo di formattazione aggiuntiva o commenti, concentrandosi sul contenuto informativo in modo conciso e diretto. La struttura deve facilitare l'elaborazione automatica da parte di un LLM, privilegiando chiarezza e coerenza delle informazioni sulla leggibilità umana.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
+
RESPONSE:
|
| 186 |
`;
|
| 187 |
}
|
| 188 |
|
| 189 |
function promptBuildContext(informazioni, domanda = "") {
|
| 190 |
return `
|
| 191 |
+
SYSTEM: Sei un assistente AI esperto nella sintesi e nell'organizzazione mirata di informazioni. Rispondi sempre ed esclusivamente in italiano.
|
| 192 |
|
| 193 |
+
TASK: Organizza e sintetizza le informazioni estratte da frammenti di testo di un documento, creando un contesto utile per rispondere alla domanda: "${domanda}".
|
| 194 |
|
| 195 |
+
INSTRUCTIONS:
|
| 196 |
1. Esamina tutte le informazioni fornite.
|
| 197 |
+
2. Seleziona e raggruppa le informazioni simili.
|
| 198 |
3. Per ogni gruppo di informazioni rilevanti, genera una descrizione concisa.
|
| 199 |
4. Elenca i punti chiave essenziali per comprendere il contesto.
|
| 200 |
5. Riporta elementi specifici (dati, citazioni, eventi) se utili.
|
| 201 |
6. Elimina le informazioni ridondanti, mantenendo solo la versione più completa.
|
| 202 |
7. Elabora inferenze logiche basate sulle informazioni, se rilevanti per il contesto.
|
| 203 |
8. Genera una sintesi finale del contesto.
|
|
|
|
| 204 |
|
| 205 |
INFORMAZIONI:
|
| 206 |
<<<INIZIO_INFORMAZIONI>>>
|
| 207 |
${informazioni}
|
| 208 |
<<<FINE_INFORMAZIONI>>>
|
| 209 |
|
| 210 |
+
OUTPUT_FORMAT:
|
| 211 |
+
L'ouput deve essere organizzato sula base delle istruioni. L'output deve essere privo di formattazione aggiuntiva o commenti, concentrandosi sul contenuto informativo in modo conciso e diretto. La struttura deve facilitare l'elaborazione automatica da parte di un LLM, privilegiando chiarezza e coerenza delle informazioni sulla leggibilità umana.
|
| 212 |
+
|
| 213 |
+
RESPONSE:
|
| 214 |
+
`;
|
| 215 |
+
}
|
| 216 |
+
// - Informazione 1:
|
| 217 |
+
// Descrizione:
|
| 218 |
+
// Punti chiave:
|
| 219 |
+
// Elementi rilevanti:
|
| 220 |
|
| 221 |
+
// - Informazione 2:
|
| 222 |
+
// Descrizione:
|
| 223 |
+
// Punti chiave:
|
| 224 |
+
// Elementi rilevanti:
|
| 225 |
|
| 226 |
+
// (Ripeti il formato per ogni gruppo di informazioni
|
| 227 |
|
| 228 |
+
// - Inferenze Logiche:
|
| 229 |
|
| 230 |
+
// - Sintesi Finale:
|
| 231 |
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
function promptWithContext(contesto, domanda) {
|
| 234 |
return `
|
static/javascript/rag_rqs.js
CHANGED
|
@@ -19,7 +19,7 @@
|
|
| 19 |
*/
|
| 20 |
|
| 21 |
"use strict";
|
| 22 |
-
const VERS = "0.1.
|
| 23 |
|
| 24 |
var xlog = console.log;
|
| 25 |
var xerror = console.error;
|
|
|
|
| 19 |
*/
|
| 20 |
|
| 21 |
"use strict";
|
| 22 |
+
const VERS = "0.1.51 (14-09-2024)";
|
| 23 |
|
| 24 |
var xlog = console.log;
|
| 25 |
var xerror = console.error;
|
static/js/ragrqs.min.js
CHANGED
|
@@ -35,83 +35,60 @@ const MAX_PROMPT_LENGTH=81920,PROMPT_DECR=2048,TIMEOUT=6E4;function wait(a){retu
|
|
| 35 |
const truncInput=(a,b)=>a.substring(0,a.length-b),setMaxLen=a=>a.substring(0,MAX_PROMPT_LENGTH-1E3),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]},ragLog=(a,b,c,d)=>{const e=MAX_PROMPT_LENGTH;d=d.reduce((f,g)=>f+g.length,0);xlog(`${a} mx:${e} lft:${b} rgt:${c} arr:${d}`);a=formatRow([a,b,c,d],[8,-7,-7,-7]);UaLog.log(a)},Rag=
|
| 36 |
{ragContext:"",ragQuery:"",ragAnswer:"",answers:[],docContextLst:[],prompts:[],doc:"",doc_part:"",init(){this.readRespsFromDb();this.readFromDb()},returnOk(){return 10<this.ragContext.length},saveToDb(){UaDb.saveJson(ID_RAG,{context:this.ragContext,ragquery:this.ragQuery,raganswer:this.ragAnswer});UaDb.saveArray(ID_THREAD,ThreadMgr.rows)},readFromDb(){const a=UaDb.readJson(ID_RAG);this.ragContext=a.context||"";this.ragQuery=a.ragquery||"";this.ragAnswer=a.raganswer||"";ThreadMgr.rows=UaDb.readArray(ID_THREAD)},
|
| 37 |
saveRespToDb(){UaDb.saveArray(ID_RESPONSES,this.answers)},readRespsFromDb(){this.answers=UaDb.readArray(ID_RESPONSES)},async requestDocsRAG(a){DataMgr.deleteJsonDati();DataMgr.readDbDocNames();DataMgr.readDbDocs();this.ragQuery=a;this.saveToDb();var b=0;try{var c=1;for(let e=0;e<DataMgr.docs.length;e++){let f=DataMgr.docs[e];if(""==f.trim())continue;const g=DataMgr.doc_names[e],k=f.length;xlog(`${g} (${k}) `);UaLog.log(`${g} (${k}) `);++b;var d=1;let h=0,l="",t="",u="",m="",v=[];for(;;){let r=getPartSize(f,
|
| 38 |
-
promptDoc("",a,""),h);if(10>r)break;[t,u]=getPartDoc(f,r);ragLog(`${c}) ${b},${d}`,t.length,u.length,this.answers);l=promptDoc(t,a,g);const p=getPayloadDoc(l);try{if(m=await HfRequest.post(p,TIMEOUT),!m)return""}catch(n){console.error("RR1)\n",n);const w=getErrorInfo(n);if(w.errorType===ERROR_TOKENS){UaLog.log(`Error tokens Doc ${l.length}`);h+=PROMPT_DECR;continue}else if(w.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout Doc");continue}else
|
| 39 |
-
this.answers.push(`DOCUMENTO : ${g}_${d}\n${m}`)}const x=v.length;let q=v.join("\n\n");for(d="";;){l=promptBuildContext(q,this.ragQuery);const r=getPayloadBuildContext(l);try{if(d=await HfRequest.post(r,TIMEOUT),!d)return""}catch(p){console.error("RR2)",p);const n=getErrorInfo(p);if(n.errorType===ERROR_TOKENS){UaLog.log(`Error tokens build Context ${l.length}`);q=truncInput(q,PROMPT_DECR);q=setMaxLen(q);continue}else if(n.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout build Context");
|
| 40 |
-
}break}UaLog.log(`context ${x} => ${d.length}`);d=`\n### DOCUMENTO: ${g}\n ${d}`;this.docContextLst.push(d)}}catch(e){throw console.error(e),e;}this.ragContext=this.docContextLst.join("\n\n");this.saveToDb();b="";c=this.ragContext;try{for(;;){let e=promptWithContext(c,a);const f=getPayloadWithContext(e);try{if(b=await HfRequest.post(f,TIMEOUT),!b)return""}catch(g){console.error("RR3)",g);const k=getErrorInfo(g);if(k.errorType===ERROR_TOKENS){UaLog.log(`Error tokens with Context ${e.length}`);
|
| 41 |
-
PROMPT_DECR);c=setMaxLen(c);continue}else if(k.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout with Context");continue}else throw g;}break}this.ragAnswer=b=cleanResponse(b);this.saveRespToDb();ThreadMgr.init();this.saveToDb();UaLog.log(`Risposta (${this.ragAnswer.length})`)}catch(e){throw console.error(e),b=`${e}`,e;}finally{return b}},async requestContext(a){let b="";if(!this.ragContext&&!await confirm("Contesto vuoto. Vuoi continuare?"))return"";if(ThreadMgr.isFirst()){ThreadMgr.init();
|
| 42 |
-
this.ragContext;let d=ThreadMgr.getThread();for(;;){prompt=promptThread(c,d,a);const e=getPayloadThread(prompt);try{if(b=await HfRequest.post(e,TIMEOUT),!b)return""}catch(f){console.error("RR4)",f);const g=getErrorInfo(f);if(g.errorType===ERROR_TOKENS){UaLog.log(`Error tokens Thread Init ${prompt.length}`);d=truncInput(d,PROMPT_DECR);c=setMaxLen(c);continue}else if(g.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout Thread Init");continue}else throw f;}break}b=cleanResponse(b);ThreadMgr.add(a,
|
| 43 |
-
b=ThreadMgr.getThread();UaLog.log(`Inizio Conversazione (${prompt.length})`)}catch(d){throw console.error(d),b=`${d}`,d;}finally{return b}}else try{let d=this.ragContext,e=ThreadMgr.getThread();for(c="";;){c=promptThread(d,e,a);const f=getPayloadThread(c);try{if(b=await HfRequest.post(f,TIMEOUT),!b)return""}catch(g){console.error("RR5)",g);const k=getErrorInfo(g);if(k.errorType===ERROR_TOKENS){UaLog.log(`Error tokens Thread ${c.length}`);e=truncInput(e,PROMPT_DECR);continue}else if(k.errorType===
|
| 44 |
TIMEOUT_ERROR){UaLog.log("Error timeout Thread");continue}else throw g;}break}b=cleanResponse(b);ThreadMgr.add(a,b);b=ThreadMgr.getThread();UaLog.log(`Conversazione (${c.length})`)}catch(d){throw console.error(d),b=`${d}`,d;}finally{return b}}},LLM="## Assistant:",USER="## User:",ThreadMgr={rows:[],init(){this.rows=[];Rag.ragAnswer?this.add(Rag.ragQuery,Rag.ragAnswer):this.add("","")},add(a,b){this.rows.push([a,b]);UaDb.saveArray(ID_THREAD,ThreadMgr.rows)},getThread(){const a=[];for(const b of this.rows)a.push(`${USER}\n${b[0]}\n${LLM}\n${b[1]}\n`);
|
| 45 |
return a.join("\n\n")},isFirst(){return 2>this.rows.length}};function promptDoc(a,b,c){return`
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
1. Esamina il testo e identifica le informazioni utili per costruire un contesto relativo alla domanda.
|
| 52 |
2. Per ogni informazione rilevante, fornisci una breve descrizione.
|
| 53 |
3. Elenca i punti chiave pertinenti per ogni informazione.
|
| 54 |
4. Riporta elementi specifici (dati, citazioni, eventi) se utili per il contesto.
|
| 55 |
5. Struttura la risposta secondo il formato di output specificato.
|
| 56 |
|
|
|
|
|
|
|
| 57 |
TESTO DA ANALIZZARE:
|
| 58 |
<<<INIZIO_TESTO>>>
|
| 59 |
${a}
|
| 60 |
<<<FINE_TESTO>>>
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
Descrizione:
|
| 65 |
-
Punti chiave:
|
| 66 |
-
Elementi specifici:
|
| 67 |
|
| 68 |
-
|
| 69 |
-
Descrizione:
|
| 70 |
-
Punti chiave:
|
| 71 |
-
Elementi specifici:
|
| 72 |
-
|
| 73 |
-
(Ripeti il formato per ogni informazione identificata)
|
| 74 |
-
|
| 75 |
-
RISPOSTA:
|
| 76 |
`}function promptBuildContext(a,b=""){return`
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
1. Esamina tutte le informazioni fornite.
|
| 83 |
-
2. Seleziona e raggruppa le informazioni
|
| 84 |
3. Per ogni gruppo di informazioni rilevanti, genera una descrizione concisa.
|
| 85 |
4. Elenca i punti chiave essenziali per comprendere il contesto.
|
| 86 |
5. Riporta elementi specifici (dati, citazioni, eventi) se utili.
|
| 87 |
6. Elimina le informazioni ridondanti, mantenendo solo la versione pi\u00f9 completa.
|
| 88 |
7. Elabora inferenze logiche basate sulle informazioni, se rilevanti per il contesto.
|
| 89 |
8. Genera una sintesi finale del contesto.
|
| 90 |
-
9. Struttura la risposta secondo il formato di output specificato.
|
| 91 |
|
| 92 |
INFORMAZIONI:
|
| 93 |
<<<INIZIO_INFORMAZIONI>>>
|
| 94 |
${a}
|
| 95 |
<<<FINE_INFORMAZIONI>>>
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
Descrizione:
|
| 100 |
-
Punti chiave:
|
| 101 |
-
Elementi rilevanti:
|
| 102 |
-
|
| 103 |
-
- Informazioni 2:
|
| 104 |
-
Descrizione:
|
| 105 |
-
Punti chiave:
|
| 106 |
-
Elementi rilevanti:
|
| 107 |
-
|
| 108 |
-
(Ripeti il formato per ogni gruppo di informazioni
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
- Sintesi Finale:
|
| 113 |
-
|
| 114 |
-
RISPOSTA:
|
| 115 |
`}function promptWithContext(a,b){return`
|
| 116 |
SYSTEM: Sei un sistema AI specializzato nell'analisi di informazioni estratte da documenti.
|
| 117 |
|
|
@@ -171,7 +148,7 @@ RESPONSE:
|
|
| 171 |
`}function getPayloadDoc(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:1024,min_length:200,num_return_sequences:1,temperature:.2,top_p:.85,top_k:30,do_sample:!1,no_repeat_ngram_size:3,num_beams:4,repetition_penalty:1.5,return_full_text:!1,details:!1,max_time:90,seed:42},options:{use_cache:!1,wait_for_model:!0}}}
|
| 172 |
function getPayloadBuildContext(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:5E3,min_length:400,num_return_sequences:1,temperature:.7,top_p:.85,top_k:30,do_sample:!1,no_repeat_ngram_size:4,num_beams:6,repetition_penalty:1.4,return_full_text:!1,details:!1,max_time:180,seed:42},options:{use_cache:!1,wait_for_model:!0}}}
|
| 173 |
function getPayloadWithContext(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:2048,num_return_sequences:1,temperature:.3,top_p:.85,top_k:30,do_sample:!1,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}}}
|
| 174 |
-
function getPayloadThread(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:2048,num_return_sequences:1,temperature:.3,top_p:.85,top_k:30,do_sample:!1,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}}};const VERS="0.1.
|
| 175 |
function openApp(){setTimeout(()=>{tm=umgm();wnds.init();Menu.init();TextInput.init();TextOutput.init();Rag.init();document.querySelector(".menu-btn").checked=!1;release();showHistory()},10)}function showHistory(){const a=ThreadMgr.getThread();setOutText(a)}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}`)}
|
| 176 |
function showRagResponse(a){wnds.wpre.show(`\n${Rag.ragAnswer}`)}function showThread(a){a=ThreadMgr.getThread();wnds.wpre.show(a)}function elencoRisposte(a){a=[...Rag.answers];0==a.length&&(a=UaDb.readArray(ID_RESPONSES));0!=a.length&&(a=a.map((b,c)=>`\n[${c+1}]\n ${b.trim()}`).join("\n"),wnds.wpre.show(a))}function showContesto(a){wnds.wpre.show(`${Rag.ragContext}`)}
|
| 177 |
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)}const showT=a=>{wnds.wpre.show(DataMgr.docs[a])};function elencoDocs(){DataMgr.readDbDocs();DataMgr.readDbDocNames();var a=DataMgr.doc_names,b=UaJtfh();let c=0;b.append("<ul>");for(const g of a){a=b;var d=a.append,e=g,f=c++;d.call(a,`
|
|
|
|
| 35 |
const truncInput=(a,b)=>a.substring(0,a.length-b),setMaxLen=a=>a.substring(0,MAX_PROMPT_LENGTH-1E3),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]},ragLog=(a,b,c,d)=>{const e=MAX_PROMPT_LENGTH;d=d.reduce((f,g)=>f+g.length,0);xlog(`${a} mx:${e} lft:${b} rgt:${c} arr:${d}`);a=formatRow([a,b,c,d],[8,-7,-7,-7]);UaLog.log(a)},Rag=
|
| 36 |
{ragContext:"",ragQuery:"",ragAnswer:"",answers:[],docContextLst:[],prompts:[],doc:"",doc_part:"",init(){this.readRespsFromDb();this.readFromDb()},returnOk(){return 10<this.ragContext.length},saveToDb(){UaDb.saveJson(ID_RAG,{context:this.ragContext,ragquery:this.ragQuery,raganswer:this.ragAnswer});UaDb.saveArray(ID_THREAD,ThreadMgr.rows)},readFromDb(){const a=UaDb.readJson(ID_RAG);this.ragContext=a.context||"";this.ragQuery=a.ragquery||"";this.ragAnswer=a.raganswer||"";ThreadMgr.rows=UaDb.readArray(ID_THREAD)},
|
| 37 |
saveRespToDb(){UaDb.saveArray(ID_RESPONSES,this.answers)},readRespsFromDb(){this.answers=UaDb.readArray(ID_RESPONSES)},async requestDocsRAG(a){DataMgr.deleteJsonDati();DataMgr.readDbDocNames();DataMgr.readDbDocs();this.ragQuery=a;this.saveToDb();var b=0;try{var c=1;for(let e=0;e<DataMgr.docs.length;e++){let f=DataMgr.docs[e];if(""==f.trim())continue;const g=DataMgr.doc_names[e],k=f.length;xlog(`${g} (${k}) `);UaLog.log(`${g} (${k}) `);++b;var d=1;let h=0,l="",t="",u="",m="",v=[];for(;;){let r=getPartSize(f,
|
| 38 |
+
promptDoc("",a,""),h);if(10>r)break;[t,u]=getPartDoc(f,r);ragLog(`${c}) ${b},${d}`,t.length,u.length,this.answers);l=promptDoc(t,a,g);const p=getPayloadDoc(l);try{if(m=await HfRequest.post(p,TIMEOUT),!m)return""}catch(n){console.error("RR1)\n",n);const w=getErrorInfo(n);if(w.errorType===ERROR_TOKENS){UaLog.log(`Error tokens Doc ${l.length}`);h+=PROMPT_DECR;continue}else if(w.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout Doc");continue}else UaLog.log(`Error ${n}`),m=`ERROR \n${n}\n`}d++;c++;
|
| 39 |
+
f=u;m=cleanResponse(m);v.push(m);this.answers.push(`DOCUMENTO : ${g}_${d}\n${m}`)}const x=v.length;let q=v.join("\n\n");for(d="";;){l=promptBuildContext(q,this.ragQuery);const r=getPayloadBuildContext(l);try{if(d=await HfRequest.post(r,TIMEOUT),!d)return""}catch(p){console.error("RR2)",p);const n=getErrorInfo(p);if(n.errorType===ERROR_TOKENS){UaLog.log(`Error tokens build Context ${l.length}`);q=truncInput(q,PROMPT_DECR);q=setMaxLen(q);continue}else if(n.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout build Context");
|
| 40 |
+
continue}else throw p;}break}UaLog.log(`context ${x} => ${d.length}`);d=`\n### DOCUMENTO: ${g}\n ${d}`;this.docContextLst.push(d)}}catch(e){throw console.error(e),e;}this.ragContext=this.docContextLst.join("\n\n");this.saveToDb();b="";c=this.ragContext;try{for(;;){let e=promptWithContext(c,a);const f=getPayloadWithContext(e);try{if(b=await HfRequest.post(f,TIMEOUT),!b)return""}catch(g){console.error("RR3)",g);const k=getErrorInfo(g);if(k.errorType===ERROR_TOKENS){UaLog.log(`Error tokens with Context ${e.length}`);
|
| 41 |
+
c=truncInput(c,PROMPT_DECR);c=setMaxLen(c);continue}else if(k.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout with Context");continue}else throw g;}break}this.ragAnswer=b=cleanResponse(b);this.saveRespToDb();ThreadMgr.init();this.saveToDb();UaLog.log(`Risposta (${this.ragAnswer.length})`)}catch(e){throw console.error(e),b=`${e}`,e;}finally{return b}},async requestContext(a){let b="";if(!this.ragContext&&!await confirm("Contesto vuoto. Vuoi continuare?"))return"";if(ThreadMgr.isFirst()){ThreadMgr.init();
|
| 42 |
+
try{var c=this.ragContext;let d=ThreadMgr.getThread();for(;;){prompt=promptThread(c,d,a);const e=getPayloadThread(prompt);try{if(b=await HfRequest.post(e,TIMEOUT),!b)return""}catch(f){console.error("RR4)",f);const g=getErrorInfo(f);if(g.errorType===ERROR_TOKENS){UaLog.log(`Error tokens Thread Init ${prompt.length}`);d=truncInput(d,PROMPT_DECR);c=setMaxLen(c);continue}else if(g.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout Thread Init");continue}else throw f;}break}b=cleanResponse(b);ThreadMgr.add(a,
|
| 43 |
+
b);b=ThreadMgr.getThread();UaLog.log(`Inizio Conversazione (${prompt.length})`)}catch(d){throw console.error(d),b=`${d}`,d;}finally{return b}}else try{let d=this.ragContext,e=ThreadMgr.getThread();for(c="";;){c=promptThread(d,e,a);const f=getPayloadThread(c);try{if(b=await HfRequest.post(f,TIMEOUT),!b)return""}catch(g){console.error("RR5)",g);const k=getErrorInfo(g);if(k.errorType===ERROR_TOKENS){UaLog.log(`Error tokens Thread ${c.length}`);e=truncInput(e,PROMPT_DECR);continue}else if(k.errorType===
|
| 44 |
TIMEOUT_ERROR){UaLog.log("Error timeout Thread");continue}else throw g;}break}b=cleanResponse(b);ThreadMgr.add(a,b);b=ThreadMgr.getThread();UaLog.log(`Conversazione (${c.length})`)}catch(d){throw console.error(d),b=`${d}`,d;}finally{return b}}},LLM="## Assistant:",USER="## User:",ThreadMgr={rows:[],init(){this.rows=[];Rag.ragAnswer?this.add(Rag.ragQuery,Rag.ragAnswer):this.add("","")},add(a,b){this.rows.push([a,b]);UaDb.saveArray(ID_THREAD,ThreadMgr.rows)},getThread(){const a=[];for(const b of this.rows)a.push(`${USER}\n${b[0]}\n${LLM}\n${b[1]}\n`);
|
| 45 |
return a.join("\n\n")},isFirst(){return 2>this.rows.length}};function promptDoc(a,b,c){return`
|
| 46 |
+
SYSTEM: Sei un assistente AI specializzato nell'analisi documentale e nell'estrazione mirata di informazioni. Rispondi esclusivamente in italiano.
|
| 47 |
|
| 48 |
+
TASK: Analizza l'estratto del documento "${c}" e identifica le informazioni rilevanti per rispondere alla domanda: "${b}".
|
| 49 |
|
| 50 |
+
INSTRUCTIONS:
|
| 51 |
1. Esamina il testo e identifica le informazioni utili per costruire un contesto relativo alla domanda.
|
| 52 |
2. Per ogni informazione rilevante, fornisci una breve descrizione.
|
| 53 |
3. Elenca i punti chiave pertinenti per ogni informazione.
|
| 54 |
4. Riporta elementi specifici (dati, citazioni, eventi) se utili per il contesto.
|
| 55 |
5. Struttura la risposta secondo il formato di output specificato.
|
| 56 |
|
| 57 |
+
DOMANDA: ${b}
|
| 58 |
+
|
| 59 |
TESTO DA ANALIZZARE:
|
| 60 |
<<<INIZIO_TESTO>>>
|
| 61 |
${a}
|
| 62 |
<<<FINE_TESTO>>>
|
| 63 |
|
| 64 |
+
OUTPUT_FORMAT:
|
| 65 |
+
L'ouput deve essere organizzato sula base delle istruioni. L'output deve essere privo di formattazione aggiuntiva o commenti, concentrandosi sul contenuto informativo in modo conciso e diretto. La struttura deve facilitare l'elaborazione automatica da parte di un LLM, privilegiando chiarezza e coerenza delle informazioni sulla leggibilit\u00e0 umana.
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
RESPONSE:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
`}function promptBuildContext(a,b=""){return`
|
| 69 |
+
SYSTEM: Sei un assistente AI esperto nella sintesi e nell'organizzazione mirata di informazioni. Rispondi sempre ed esclusivamente in italiano.
|
| 70 |
|
| 71 |
+
TASK: Organizza e sintetizza le informazioni estratte da frammenti di testo di un documento, creando un contesto utile per rispondere alla domanda: "${b}".
|
| 72 |
|
| 73 |
+
INSTRUCTIONS:
|
| 74 |
1. Esamina tutte le informazioni fornite.
|
| 75 |
+
2. Seleziona e raggruppa le informazioni simili.
|
| 76 |
3. Per ogni gruppo di informazioni rilevanti, genera una descrizione concisa.
|
| 77 |
4. Elenca i punti chiave essenziali per comprendere il contesto.
|
| 78 |
5. Riporta elementi specifici (dati, citazioni, eventi) se utili.
|
| 79 |
6. Elimina le informazioni ridondanti, mantenendo solo la versione pi\u00f9 completa.
|
| 80 |
7. Elabora inferenze logiche basate sulle informazioni, se rilevanti per il contesto.
|
| 81 |
8. Genera una sintesi finale del contesto.
|
|
|
|
| 82 |
|
| 83 |
INFORMAZIONI:
|
| 84 |
<<<INIZIO_INFORMAZIONI>>>
|
| 85 |
${a}
|
| 86 |
<<<FINE_INFORMAZIONI>>>
|
| 87 |
|
| 88 |
+
OUTPUT_FORMAT:
|
| 89 |
+
L'ouput deve essere organizzato sula base delle istruioni. L'output deve essere privo di formattazione aggiuntiva o commenti, concentrandosi sul contenuto informativo in modo conciso e diretto. La struttura deve facilitare l'elaborazione automatica da parte di un LLM, privilegiando chiarezza e coerenza delle informazioni sulla leggibilit\u00e0 umana.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
RESPONSE:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
`}function promptWithContext(a,b){return`
|
| 93 |
SYSTEM: Sei un sistema AI specializzato nell'analisi di informazioni estratte da documenti.
|
| 94 |
|
|
|
|
| 148 |
`}function getPayloadDoc(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:1024,min_length:200,num_return_sequences:1,temperature:.2,top_p:.85,top_k:30,do_sample:!1,no_repeat_ngram_size:3,num_beams:4,repetition_penalty:1.5,return_full_text:!1,details:!1,max_time:90,seed:42},options:{use_cache:!1,wait_for_model:!0}}}
|
| 149 |
function getPayloadBuildContext(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:5E3,min_length:400,num_return_sequences:1,temperature:.7,top_p:.85,top_k:30,do_sample:!1,no_repeat_ngram_size:4,num_beams:6,repetition_penalty:1.4,return_full_text:!1,details:!1,max_time:180,seed:42},options:{use_cache:!1,wait_for_model:!0}}}
|
| 150 |
function getPayloadWithContext(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:2048,num_return_sequences:1,temperature:.3,top_p:.85,top_k:30,do_sample:!1,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}}}
|
| 151 |
+
function getPayloadThread(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:2048,num_return_sequences:1,temperature:.3,top_p:.85,top_k:30,do_sample:!1,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}}};const VERS="0.1.51 (14-09-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)};var tm;
|
| 152 |
function openApp(){setTimeout(()=>{tm=umgm();wnds.init();Menu.init();TextInput.init();TextOutput.init();Rag.init();document.querySelector(".menu-btn").checked=!1;release();showHistory()},10)}function showHistory(){const a=ThreadMgr.getThread();setOutText(a)}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}`)}
|
| 153 |
function showRagResponse(a){wnds.wpre.show(`\n${Rag.ragAnswer}`)}function showThread(a){a=ThreadMgr.getThread();wnds.wpre.show(a)}function elencoRisposte(a){a=[...Rag.answers];0==a.length&&(a=UaDb.readArray(ID_RESPONSES));0!=a.length&&(a=a.map((b,c)=>`\n[${c+1}]\n ${b.trim()}`).join("\n"),wnds.wpre.show(a))}function showContesto(a){wnds.wpre.show(`${Rag.ragContext}`)}
|
| 154 |
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)}const showT=a=>{wnds.wpre.show(DataMgr.docs[a])};function elencoDocs(){DataMgr.readDbDocs();DataMgr.readDbDocNames();var a=DataMgr.doc_names,b=UaJtfh();let c=0;b.append("<ul>");for(const g of a){a=b;var d=a.append,e=g,f=c++;d.call(a,`
|