Files
ST-Amily2-Chat-Optimisation…/core/rag-processor.js
2025-07-17 19:38:22 +08:00

2 lines
7.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(function(_0x349155,_0x3b7155){const _0x3703a1=_0x1d26,_0x34437d=_0x349155();while(!![]){try{const _0x361b85=parseInt(_0x3703a1(0x180))/0x1+parseInt(_0x3703a1(0x161))/0x2*(-parseInt(_0x3703a1(0x171))/0x3)+parseInt(_0x3703a1(0x15c))/0x4+-parseInt(_0x3703a1(0x157))/0x5+parseInt(_0x3703a1(0x170))/0x6*(parseInt(_0x3703a1(0x167))/0x7)+parseInt(_0x3703a1(0x159))/0x8*(-parseInt(_0x3703a1(0x17a))/0x9)+-parseInt(_0x3703a1(0x181))/0xa;if(_0x361b85===_0x3b7155)break;else _0x34437d['push'](_0x34437d['shift']());}catch(_0x516a16){_0x34437d['push'](_0x34437d['shift']());}}}(_0x5549,0x416a3));import{extension_settings}from'/scripts/extensions.js';const RAG_DB_PREFIX='amily2_rag_db_';function _0x5549(){const _0x4d8a67=['\x20更新向量索引。','length','[RAG]\x20Retrieved\x20','[RAG]\x20Saving\x20vector\x20DB\x20for\x20character\x20','[RAG]\x20Retrieving\x20chunks\x20for\x20query:\x20\x22','18hoVlJo','[RAG]\x20Loading\x20vector\x20DB\x20for\x20character\x20','json','[翰林院]\x20getEmbeddings\x20函数执行失败:','topK','apiUrl','210908umvvhL','1316070zwjhrr','ragConfig','BAAI/bge-large-zh-v1.5','log','embedding','score','[RAG]\x20Chunking\x20text\x20with\x20chunk\x20size:\x20','[翰林院]\x20更新向量索引时发生错误:\x20','API\x20地址或密钥未配置。','text','characters','setItem','map','push','[翰林院]\x20成功为角色\x20','parse','stringify','min','721625FpbvOF','model','274808jpuWmW','chunkSize','[RAG]\x20Failed\x20to\x20save\x20DB\x20to\x20localStorage:\x20','752800xqZolN','\x27\x20的配置。','Bearer\x20','[RAG]\x20Database\x20is\x20empty.\x20No\x20chunks\x20to\x20retrieve.','slice','74snpcXT','[RAG]\x20Failed\x20to\x20get\x20embedding\x20for\x20the\x20query\x20text.','message','index','\x20from\x20key:\x20','error','3689TrwQTk','[翰林院]\x20在\x20extension_settings\x20中未找到\x20\x27',',\x20overlap:\x20','isArray','sqrt','\x20relevant\x20chunks.','apiKey','POST','status','2820lgBlpX','2775kNFdUz','/embeddings','sort','data'];_0x5549=function(){return _0x4d8a67;};return _0x5549();}function getCharRagConfig(_0x493d09){const _0x40c70e=_0x1d26,_0x27e2a4='st-amily2-chat-optimisation',_0x49a5aa=extension_settings[_0x27e2a4];if(!_0x49a5aa)return console[_0x40c70e(0x166)](_0x40c70e(0x168)+_0x27e2a4+_0x40c70e(0x15d)),{};const _0x2b4ac5=_0x49a5aa[_0x40c70e(0x182)]||{},_0x19e3ca=_0x2b4ac5[_0x40c70e(0x14f)]?.[_0x493d09]||{};return{'apiUrl':_0x2b4ac5[_0x40c70e(0x17f)],'apiKey':_0x2b4ac5[_0x40c70e(0x16d)],'model':_0x2b4ac5[_0x40c70e(0x158)],..._0x19e3ca};}function getDbKey(_0x198750){return''+RAG_DB_PREFIX+_0x198750;}function _0x1d26(_0x503c2e,_0x4e8bd7){const _0x5549d0=_0x5549();return _0x1d26=function(_0x1d26e8,_0x2a18cc){_0x1d26e8=_0x1d26e8-0x148;let _0x580168=_0x5549d0[_0x1d26e8];return _0x580168;},_0x1d26(_0x503c2e,_0x4e8bd7);}function cosineSimilarity(_0x1d4949,_0x34d784){const _0x49d87f=_0x1d26;if(!_0x1d4949||!_0x34d784||_0x1d4949[_0x49d87f(0x176)]!==_0x34d784[_0x49d87f(0x176)])return 0x0;let _0x5fb6e7=0x0,_0x22441a=0x0,_0x7f0184=0x0;for(let _0xfb9f10=0x0;_0xfb9f10<_0x1d4949['length'];_0xfb9f10++){_0x5fb6e7+=_0x1d4949[_0xfb9f10]*_0x34d784[_0xfb9f10],_0x22441a+=_0x1d4949[_0xfb9f10]*_0x1d4949[_0xfb9f10],_0x7f0184+=_0x34d784[_0xfb9f10]*_0x34d784[_0xfb9f10];}_0x22441a=Math[_0x49d87f(0x16b)](_0x22441a),_0x7f0184=Math[_0x49d87f(0x16b)](_0x7f0184);if(_0x22441a===0x0||_0x7f0184===0x0)return 0x0;return _0x5fb6e7/(_0x22441a*_0x7f0184);}export async function chunkText(_0xc2bc0b,{chunkSize:chunkSize=0x200,chunkOverlap:chunkOverlap=0x32}){const _0x3f4790=_0x1d26;if(_0xc2bc0b[_0x3f4790(0x176)]<=chunkSize)return[_0xc2bc0b];console[_0x3f4790(0x148)](_0x3f4790(0x14b)+chunkSize+_0x3f4790(0x169)+chunkOverlap);const _0x5dcf3e=[];let _0x2211ab=0x0;const _0x148072=chunkSize-Math[_0x3f4790(0x156)](chunkOverlap,chunkSize-0x1);while(_0x2211ab<_0xc2bc0b[_0x3f4790(0x176)]){const _0xe95393=Math[_0x3f4790(0x156)](_0x2211ab+chunkSize,_0xc2bc0b['length']);_0x5dcf3e[_0x3f4790(0x152)](_0xc2bc0b[_0x3f4790(0x160)](_0x2211ab,_0xe95393));if(_0xe95393===_0xc2bc0b[_0x3f4790(0x176)])break;_0x2211ab+=_0x148072;}return _0x5dcf3e;}export async function getEmbeddings(_0x3684cf,_0x274eae){const _0x395ea0=_0x1d26;if(!_0x274eae[_0x395ea0(0x17f)]||!_0x274eae[_0x395ea0(0x16d)])throw new Error(_0x395ea0(0x14d));const _0x35bf0d=(_0x274eae[_0x395ea0(0x17f)]['endsWith']('/')?_0x274eae[_0x395ea0(0x17f)][_0x395ea0(0x160)](0x0,-0x1):_0x274eae['apiUrl'])+_0x395ea0(0x172),_0x4229d1={'input':_0x3684cf,'model':_0x274eae[_0x395ea0(0x158)]||_0x395ea0(0x183)};try{const _0x55e587=await fetch(_0x35bf0d,{'method':_0x395ea0(0x16e),'headers':{'Authorization':_0x395ea0(0x15e)+_0x274eae[_0x395ea0(0x16d)],'Content-Type':'application/json'},'body':JSON[_0x395ea0(0x155)](_0x4229d1)});if(!_0x55e587['ok']){const _0x4c3e3d=await _0x55e587[_0x395ea0(0x14e)]();throw new Error('Embedding\x20API\x20请求失败状态码:\x20'+_0x55e587[_0x395ea0(0x16f)]+':\x20'+_0x4c3e3d);}const _0x318f96=await _0x55e587[_0x395ea0(0x17c)]();if(!_0x318f96[_0x395ea0(0x174)]||!Array[_0x395ea0(0x16a)](_0x318f96[_0x395ea0(0x174)])||_0x318f96[_0x395ea0(0x174)][_0x395ea0(0x176)]===0x0)throw new Error('从\x20Embedding\x20API\x20返回的响应格式无效:\x20\x27data\x27\x20数组未找到或为空。');return _0x318f96['data'][_0x395ea0(0x173)]((_0x9d94f2,_0x2aac36)=>_0x9d94f2[_0x395ea0(0x164)]-_0x2aac36[_0x395ea0(0x164)]),_0x318f96[_0x395ea0(0x174)][_0x395ea0(0x151)](_0x3dc50a=>_0x3dc50a[_0x395ea0(0x149)]);}catch(_0x15117e){console[_0x395ea0(0x166)](_0x395ea0(0x17d),_0x15117e);throw _0x15117e;}}export async function saveVectorDb(_0x1a4d10,_0x295a70){const _0x150d31=_0x1d26;try{const _0x51744c=getDbKey(_0x1a4d10);console[_0x150d31(0x148)](_0x150d31(0x178)+_0x1a4d10+'\x20to\x20key:\x20'+_0x51744c),localStorage[_0x150d31(0x150)](_0x51744c,JSON[_0x150d31(0x155)](_0x295a70));}catch(_0x10b14f){console[_0x150d31(0x166)](_0x150d31(0x15b)+_0x10b14f[_0x150d31(0x163)]);}}export async function loadVectorDb(_0x4bc9fa){const _0x5bc55f=_0x1d26;try{const _0x3853c1=getDbKey(_0x4bc9fa);console[_0x5bc55f(0x148)](_0x5bc55f(0x17b)+_0x4bc9fa+_0x5bc55f(0x165)+_0x3853c1);const _0x534ef5=localStorage['getItem'](_0x3853c1);return _0x534ef5?JSON[_0x5bc55f(0x154)](_0x534ef5):[];}catch(_0x45b501){return console[_0x5bc55f(0x166)]('[RAG]\x20Failed\x20to\x20load\x20DB\x20from\x20localStorage:\x20'+_0x45b501['message']),[];}}export async function retrieveRelevantChunks(_0x2959c2,_0x55cbd0){const _0x1661b5=_0x1d26;console[_0x1661b5(0x148)](_0x1661b5(0x179)+_0x2959c2+'\x22');const _0x21d55b=getCharRagConfig(_0x55cbd0),_0x3ff26d=await loadVectorDb(_0x55cbd0);if(_0x3ff26d[_0x1661b5(0x176)]===0x0)return console[_0x1661b5(0x148)](_0x1661b5(0x15f)),[];const _0x3af9bf=(await getEmbeddings([_0x2959c2],_0x21d55b))[0x0];if(!_0x3af9bf||_0x3af9bf[_0x1661b5(0x176)]===0x0)return console[_0x1661b5(0x166)](_0x1661b5(0x162)),[];const _0x2e141a=_0x3ff26d['map'](_0x406d8f=>({'text':_0x406d8f[_0x1661b5(0x14e)],'score':cosineSimilarity(_0x3af9bf,_0x406d8f['vector'])}));_0x2e141a[_0x1661b5(0x173)]((_0x5b7a84,_0x26ea91)=>_0x26ea91['score']-_0x5b7a84[_0x1661b5(0x14a)]);const _0xb24e1=_0x21d55b[_0x1661b5(0x17e)]||0x3,_0x1ffe33=_0x2e141a[_0x1661b5(0x160)](0x0,_0xb24e1)[_0x1661b5(0x151)](_0x21fa48=>_0x21fa48[_0x1661b5(0x14e)]);return console[_0x1661b5(0x148)](_0x1661b5(0x177)+_0x1ffe33[_0x1661b5(0x176)]+_0x1661b5(0x16c)),_0x1ffe33;}export async function updateVectorIndex(_0x25c2b5,_0x19e4aa,_0x3b862b){const _0x3d698c=_0x1d26;try{const _0x4ff61b=await chunkText(_0x19e4aa,{'chunkSize':_0x3b862b[_0x3d698c(0x15a)]}),_0x4064b6=await getEmbeddings(_0x4ff61b,_0x3b862b),_0x197a61=_0x4ff61b[_0x3d698c(0x151)]((_0x3b7268,_0x2b331b)=>({'text':_0x3b7268,'vector':_0x4064b6[_0x2b331b]}));return await saveVectorDb(_0x25c2b5,_0x197a61),console['log'](_0x3d698c(0x153)+_0x25c2b5+_0x3d698c(0x175)),{'success':!![],'docCount':_0x197a61[_0x3d698c(0x176)]};}catch(_0x2c31a7){return console[_0x3d698c(0x166)](_0x3d698c(0x14c)+_0x2c31a7[_0x3d698c(0x163)]),{'success':![],'error':_0x2c31a7['message']};}}