diff --git a/core/rag-api.js b/core/rag-api.js index 1c84384..47b2def 100644 --- a/core/rag-api.js +++ b/core/rag-api.js @@ -1 +1 @@ -'use strict';const _0x886a26=_0x9ef4;(function(_0x35aea3,_0x51ff9f){const _0x571b57=_0x9ef4,_0x47c399=_0x35aea3();while(!![]){try{const _0x540590=-parseInt(_0x571b57(0xad))/0x1+parseInt(_0x571b57(0xe6))/0x2+parseInt(_0x571b57(0xaf))/0x3*(-parseInt(_0x571b57(0xce))/0x4)+parseInt(_0x571b57(0xa8))/0x5+parseInt(_0x571b57(0xbb))/0x6*(-parseInt(_0x571b57(0xa7))/0x7)+parseInt(_0x571b57(0xbd))/0x8+-parseInt(_0x571b57(0xd4))/0x9*(-parseInt(_0x571b57(0xb4))/0xa);if(_0x540590===_0x51ff9f)break;else _0x47c399['push'](_0x47c399['shift']());}catch(_0x15f931){_0x47c399['push'](_0x47c399['shift']());}}}(_0x54a5,0x1af1d));import{extension_settings}from'/scripts/extensions.js';import{buildGoogleEmbeddingRequest,parseGoogleEmbeddingResponse,buildGoogleEmbeddingApiUrl}from'./utils/googleAdapter.js';function _0x54a5(){const _0x441cfc=['custom','aborted','Rerank模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。','100QiVNqv','sort','api-key','[翰林院]\x20正在从\x20Google\x20API\x20获取模型列表:\x20','Rerank\x20API\x20请求失败\x20(','POST','45657PsfoTs','Rerank\x20API\x20URL\x20未提供。','batchEmbedContents','includes','log','无法确定获取模型的有效路径。','error','models','application/json','[翰林院-Rerank]\x20正在向\x20','local_proxy','[翰林院-API]\x20无法获取设置,API调用可能失败。','?key=','embedContent','Authorization','object','length',')...','344534QzUDOR','/embeddings','Invalid\x20JSON\x20response','\x20发送请求\x20(模式:\x20','name','supportedGenerationMethods','openai','retrieval','json','rerank','[翰林院-Rerank]\x20正在从\x20','endsWith','text','isArray','azure','trim','\x20获取模型列表\x20(模式:\x20','data','hasOwn','\x20获取模型列表\x20(需要认证)...','164773mjHfdj','953735DKIjVJ','[翰林院]\x20正在从\x20','自定义模式下,Rerank\x20API\x20Key\x20未提供。','[翰林院-API]\x20API响应JSON解析失败:','AbortError','139383YaPvaY','status','3165WUZybH','stringify','extensionSettings','parse','/v1/models','190YkRTFi','/v1beta/models?key=','getContext','hanlinyuan-rag-core','GET','/v1/rerank','/rerank','54dVUUXo','Google模型API的响应格式无效:\x20未找到\x20\x27models\x27\x20数组。','228584wufjtj','):\x20',')\x20获取向量。','map','/v1','embedding','神力获取失败\x20','\x20获取模型列表\x20(无需认证)...','string','slice','Bearer\x20','API\x20URL\x20未提供。','push','模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。'];_0x54a5=function(){return _0x441cfc;};return _0x54a5();}const MODULE_NAME=_0x886a26(0xb7),GOOGLE_API_BASE_URL='https://generativelanguage.googleapis.com';function getSettings(){const _0x4ebf43=_0x886a26,_0x3c4acd=SillyTavern[_0x4ebf43(0xb6)]();if(!_0x3c4acd||!_0x3c4acd[_0x4ebf43(0xb1)]||!_0x3c4acd[_0x4ebf43(0xb1)][MODULE_NAME])return console['error'](_0x4ebf43(0xdf)),{'retrieval':{},'rerank':{}};return _0x3c4acd[_0x4ebf43(0xb1)][MODULE_NAME];}function normalizeApiResponse(_0x2cbb7f){const _0x329570=_0x886a26;let _0x17c188=_0x2cbb7f;if(typeof _0x17c188===_0x329570(0xc5))try{_0x17c188=JSON[_0x329570(0xb2)](_0x17c188);}catch(_0x3afbb8){return console[_0x329570(0xda)](_0x329570(0xab),_0x3afbb8),{'error':{'message':_0x329570(0xe8)}};}_0x17c188&&typeof _0x17c188[_0x329570(0xa4)]===_0x329570(0xe3)&&_0x17c188[_0x329570(0xa4)]!==null&&!Array[_0x329570(0xa0)](_0x17c188['data'])&&(Object[_0x329570(0xa5)](_0x17c188[_0x329570(0xa4)],_0x329570(0xa4))&&(_0x17c188=_0x17c188[_0x329570(0xa4)]));if(_0x17c188&&_0x17c188[_0x329570(0xa4)])return{'data':_0x17c188[_0x329570(0xa4)]};if(_0x17c188&&_0x17c188[_0x329570(0xda)])return{'error':_0x17c188[_0x329570(0xda)]};return _0x17c188;}export function getSanitizedBaseUrl(_0x35f3af){const _0xf8e913=_0x886a26;let _0x214206=_0x35f3af[_0xf8e913(0xa2)]();return _0x214206[_0xf8e913(0x9e)]('/')&&(_0x214206=_0x214206['slice'](0x0,-0x1)),_0x214206[_0xf8e913(0x9e)]('/v1')&&(_0x214206=_0x214206[_0xf8e913(0xc6)](0x0,-0x3)),_0x214206[_0xf8e913(0x9e)](_0xf8e913(0xe7))&&(_0x214206=_0x214206['slice'](0x0,-0xb)),_0x214206;}function _0x9ef4(_0x4e879c,_0x58c364){const _0x54a5d2=_0x54a5();return _0x9ef4=function(_0x9ef409,_0x96a077){_0x9ef409=_0x9ef409-0x98;let _0x514f7c=_0x54a5d2[_0x9ef409];return _0x514f7c;},_0x9ef4(_0x4e879c,_0x58c364);}export async function fetchEmbeddingModels(){const _0x4cc4ae=_0x886a26,_0xe7931f=getSettings()[_0x4cc4ae(0x9a)],{apiEndpoint:_0x3805eb,apiKey:_0x4fbc99,customApiUrl:_0x37fe56}=_0xe7931f;if(!_0x4fbc99)throw new Error('API\x20Key\x20未提供。');let _0x333be4,_0xc3936a={},_0x3e03a1;switch(_0x3805eb){case'google_direct':_0x333be4=GOOGLE_API_BASE_URL+_0x4cc4ae(0xb5)+_0x4fbc99,console[_0x4cc4ae(0xd8)](_0x4cc4ae(0xd1)+_0x333be4),_0x3e03a1=_0x2dc84c=>{const _0x2ef934=_0x4cc4ae;if(!_0x2dc84c[_0x2ef934(0xdb)]||!Array['isArray'](_0x2dc84c[_0x2ef934(0xdb)]))throw new Error(_0x2ef934(0xbc));return _0x2dc84c[_0x2ef934(0xdb)]['filter'](_0x4ad8d4=>_0x4ad8d4[_0x2ef934(0x98)]?.[_0x2ef934(0xd7)](_0x2ef934(0xe1))||_0x4ad8d4['supportedGenerationMethods']?.[_0x2ef934(0xd7)](_0x2ef934(0xd6)))[_0x2ef934(0xc0)](_0x55cfff=>_0x55cfff[_0x2ef934(0xea)]['replace']('models/',''))[_0x2ef934(0xcf)]();};break;case'custom':if(!_0x37fe56)throw new Error('API\x20URL\x20未提供。');const _0x2c68f3=getSanitizedBaseUrl(_0x37fe56);_0x333be4=_0x2c68f3+_0x4cc4ae(0xb3),_0xc3936a=getApiHeaders(),console[_0x4cc4ae(0xd8)](_0x4cc4ae(0xa9)+_0x333be4+_0x4cc4ae(0xa6)),_0x3e03a1=_0x2163ba=>{const _0x18c055=_0x4cc4ae;if(!_0x2163ba[_0x18c055(0xa4)]||!Array['isArray'](_0x2163ba[_0x18c055(0xa4)]))throw new Error(_0x18c055(0xca));return _0x2163ba[_0x18c055(0xa4)][_0x18c055(0xc0)](_0x549a69=>_0x549a69['id'])[_0x18c055(0xcf)]();};break;case _0x4cc4ae(0xde):default:if(!_0x37fe56)throw new Error(_0x4cc4ae(0xc8));const _0x5d7ddb=getSanitizedBaseUrl(_0x37fe56);_0x333be4=_0x5d7ddb+'/v1/models',_0xc3936a={'Content-Type':'application/json'},console[_0x4cc4ae(0xd8)](_0x4cc4ae(0xa9)+_0x333be4+_0x4cc4ae(0xc4)),_0x3e03a1=_0x76a1b8=>{const _0x226447=_0x4cc4ae;if(!_0x76a1b8[_0x226447(0xa4)]||!Array['isArray'](_0x76a1b8[_0x226447(0xa4)]))throw new Error('模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。');return _0x76a1b8['data'][_0x226447(0xc0)](_0x1898d0=>_0x1898d0['id'])[_0x226447(0xcf)]();};break;}if(!_0x333be4)throw new Error(_0x4cc4ae(0xd9));const _0x8c882=await fetch(_0x333be4,{'method':_0x4cc4ae(0xb8),'headers':_0xc3936a});if(!_0x8c882['ok']){const _0x33a13a=await _0x8c882[_0x4cc4ae(0x9f)]();throw new Error('获取模型列表失败\x20('+_0x8c882[_0x4cc4ae(0xae)]+'):\x20'+_0x33a13a);}const _0xd3131f=await _0x8c882['json']();return _0x3e03a1(_0xd3131f);}export function getRerankBaseUrl(_0x592868){const _0x2ee520=_0x886a26;let _0xbe03e=_0x592868['trim']();return _0xbe03e[_0x2ee520(0x9e)]('/')&&(_0xbe03e=_0xbe03e[_0x2ee520(0xc6)](0x0,-0x1)),_0xbe03e[_0x2ee520(0x9e)](_0x2ee520(0xc1))&&(_0xbe03e=_0xbe03e[_0x2ee520(0xc6)](0x0,-0x3)),_0xbe03e[_0x2ee520(0x9e)](_0x2ee520(0xba))&&(_0xbe03e=_0xbe03e['slice'](0x0,-0x7)),_0xbe03e;}export async function fetchRerankModels(){const _0x35c823=_0x886a26,_0x42fa2b=getSettings()[_0x35c823(0x9c)],{url:_0x99d6c7,apiKey:_0x389654,apiMode:apiMode='custom'}=_0x42fa2b;if(!_0x99d6c7)throw new Error(_0x35c823(0xd5));if(apiMode===_0x35c823(0xcb)&&!_0x389654)throw new Error(_0x35c823(0xaa));const _0x1da41e=getRerankBaseUrl(_0x99d6c7),_0x183e54=_0x1da41e+'/v1/models',_0x22914c={'Content-Type':_0x35c823(0xdc)};apiMode===_0x35c823(0xcb)&&(_0x22914c[_0x35c823(0xe2)]=_0x35c823(0xc7)+_0x389654);console['log'](_0x35c823(0x9d)+_0x183e54+_0x35c823(0xa3)+apiMode+')...');const _0x436023=await fetch(_0x183e54,{'method':_0x35c823(0xb8),'headers':_0x22914c});if(!_0x436023['ok']){const _0x456787=await _0x436023[_0x35c823(0x9f)]();throw new Error('获取Rerank模型列表失败\x20('+_0x436023[_0x35c823(0xae)]+_0x35c823(0xbe)+_0x456787);}const _0x348115=await _0x436023['json']();if(!_0x348115[_0x35c823(0xa4)]||!Array[_0x35c823(0xa0)](_0x348115['data']))throw new Error(_0x35c823(0xcd));return _0x348115['data']['map'](_0x8946e6=>_0x8946e6['id'])[_0x35c823(0xcf)]();}export async function executeRerank(_0x2c7925,_0x4d31af,_0x5b4560){const _0x134ea8=_0x886a26,{url:_0x271965,apiKey:_0x48cb64,model:_0x34c78d,top_n:_0x5f01d3,apiMode:apiMode=_0x134ea8(0xcb)}=_0x5b4560;if(!_0x271965)throw new Error(_0x134ea8(0xd5));if(apiMode==='custom'&&!_0x48cb64)throw new Error(_0x134ea8(0xaa));const _0x57a292=getRerankBaseUrl(_0x271965),_0x52c650=_0x57a292+_0x134ea8(0xb9),_0x2f51f6={'Content-Type':_0x134ea8(0xdc)};apiMode===_0x134ea8(0xcb)&&(_0x2f51f6[_0x134ea8(0xe2)]='Bearer\x20'+_0x48cb64);const _0x516584=JSON[_0x134ea8(0xb0)]({'query':_0x2c7925,'documents':_0x4d31af,'model':_0x34c78d,'top_n':_0x5f01d3});console[_0x134ea8(0xd8)](_0x134ea8(0xdd)+_0x52c650+_0x134ea8(0xe9)+apiMode+_0x134ea8(0xe5));const _0x4f5c4e=await fetch(_0x52c650,{'method':_0x134ea8(0xd3),'headers':_0x2f51f6,'body':_0x516584});if(!_0x4f5c4e['ok'])throw new Error(_0x134ea8(0xd2)+_0x4f5c4e[_0x134ea8(0xae)]+_0x134ea8(0xbe)+await _0x4f5c4e[_0x134ea8(0x9f)]());return await _0x4f5c4e['json']();}export function getApiEndpointUrl(_0x23fa40=![]){const _0x395acc=_0x886a26,_0x4618c4=getSettings(),{apiEndpoint:_0x246a68,customApiUrl:_0x270b0a}=_0x4618c4[_0x395acc(0x9a)];let _0x4a2a5a;switch(_0x246a68){case _0x395acc(0x99):_0x4a2a5a='https://api.openai.com';break;case'azure':case _0x395acc(0xcb):_0x4a2a5a=_0x270b0a;break;default:_0x4a2a5a='https://api.openai.com';break;}if(_0x23fa40)return _0x4a2a5a;return getSanitizedBaseUrl(_0x4a2a5a)+'/v1/embeddings';}export function getApiHeaders(){const _0x3a5308=_0x886a26,_0x4b3350=getSettings(),_0x4a0a19={'Content-Type':_0x3a5308(0xdc)},{apiKey:_0x53cebe,apiEndpoint:_0x292c1c}=_0x4b3350[_0x3a5308(0x9a)];switch(_0x292c1c){case _0x3a5308(0x99):case _0x3a5308(0xcb):_0x4a0a19[_0x3a5308(0xe2)]='Bearer\x20'+_0x53cebe;break;case _0x3a5308(0xa1):_0x4a0a19[_0x3a5308(0xd0)]=_0x53cebe;break;}return _0x4a0a19;}export async function getEmbeddings(_0x462606,_0x28f1f7=null){const _0x23c304=_0x886a26,_0xe3e4b7=getSettings()[_0x23c304(0x9a)],{apiEndpoint:_0x4131a0,customApiUrl:_0x33eb8a,apiKey:_0x34cb71,embeddingModel:_0x51f384,batchSize:batchSize=0x5}=_0xe3e4b7,_0x34d15c=[];for(let _0xa867c0=0x0;_0xa867c0<_0x462606['length'];_0xa867c0+=batchSize){if(_0x28f1f7?.[_0x23c304(0xcc)])throw new Error(_0x23c304(0xac));const _0x362d62=_0x462606[_0x23c304(0xc6)](_0xa867c0,_0xa867c0+batchSize);let _0x406ac1=[];switch(_0x4131a0){case'google_direct':console[_0x23c304(0xd8)]('[翰林院-API]\x20使用Google直连模式获取向量。');if(!_0x34cb71)throw new Error('Google直连模式需要API\x20Key。');const _0x185a16=buildGoogleEmbeddingApiUrl(GOOGLE_API_BASE_URL,_0x51f384)+_0x23c304(0xe0)+_0x34cb71,_0x35b5d4=buildGoogleEmbeddingRequest(_0x362d62,_0x51f384),_0x4f7795=await fetch(_0x185a16,{'method':'POST','headers':{'Content-Type':_0x23c304(0xdc)},'body':JSON['stringify'](_0x35b5d4),'signal':_0x28f1f7});if(!_0x4f7795['ok']){const _0xe17e9=await _0x4f7795[_0x23c304(0x9f)]();throw new Error('Google\x20API\x20Error:\x20'+_0x4f7795[_0x23c304(0xae)]+'\x20'+_0xe17e9);}const _0x33e925=await _0x4f7795[_0x23c304(0x9b)]();_0x406ac1=parseGoogleEmbeddingResponse(_0x33e925);break;case _0x23c304(0xcb):case _0x23c304(0xde):default:console[_0x23c304(0xd8)]('[翰林院-API]\x20使用前端直连模式\x20('+_0x4131a0+_0x23c304(0xbf));if(!_0x34cb71&&_0x4131a0===_0x23c304(0xcb)){}const _0x5cc824=getApiEndpointUrl(),_0x25bcdd=getApiHeaders(),_0x4074a4=await fetch(_0x5cc824,{'method':_0x23c304(0xd3),'headers':_0x25bcdd,'body':JSON[_0x23c304(0xb0)]({'input':_0x362d62,'model':_0x51f384}),'signal':_0x28f1f7});if(!_0x4074a4['ok']){const _0x144722=await _0x4074a4[_0x23c304(0x9f)]();throw new Error(_0x23c304(0xc3)+_0x4074a4[_0x23c304(0xae)]+':\x20'+_0x144722);}const _0x4ff06b=await _0x4074a4['json']();if(_0x4ff06b[_0x23c304(0xa4)]&&Array[_0x23c304(0xa0)](_0x4ff06b[_0x23c304(0xa4)]))_0x406ac1=_0x4ff06b[_0x23c304(0xa4)][_0x23c304(0xc0)](_0x41221b=>_0x41221b[_0x23c304(0xc2)]);else throw new Error('API返回的向量数据格式不正确。');break;}if(_0x406ac1[_0x23c304(0xe4)]!==_0x362d62['length'])throw new Error('获取到的向量数量与发送的文本数量不匹配。');_0x34d15c[_0x23c304(0xc9)](..._0x406ac1),_0xa867c0+batchSize<_0x462606[_0x23c304(0xe4)]&&await new Promise(_0x4dc93a=>setTimeout(_0x4dc93a,0xc8));}return _0x34d15c;}export async function testApiConnection(){await getEmbeddings(['测试连接']);} +'use strict';function _0x57c4(){const _0x3adb11=['log','/v1/rerank','trim','all','[翰林院]\x20正在从\x20','372NBDMzj','398349IcWtOC','96834ndAHso','includes','[翰林院-Rerank]\x20正在从\x20','getContext','/v1/embeddings','retrieval','error','[翰林院-API]\x20发送到\x20Google\x20API\x20的请求体:','/v1','Rerank\x20API\x20URL\x20未提供。','batchEmbedContents','/rerank','v1beta','[翰林院-API]\x20无法获取设置,API调用可能失败。','91JCNnSD','275AVLViT','/v1/models','text','AbortError','length','获取模型列表失败\x20(','map','Authorization',')\x20模型列表失败:\x20',')\x20获取模型列表:\x20','[翰林院]\x20正在从\x20Google\x20API\x20(','custom','name','\x20获取模型列表\x20(无需认证)...','data','models','?key=','GET','/embeddings','1252540ZgUNrP','\x20发送请求\x20(模式:\x20','[翰林院-API]\x20使用前端直连模式\x20(','parse','supportedGenerationMethods',')...','[翰林院-API]\x20Google\x20API\x20错误响应:\x20','slice','Bearer\x20','Rerank模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。','models/','[翰林院-API]\x20API响应JSON解析失败:','Google\x20API\x20Error:\x20','10118uKsLJT','stringify','aborted','extensionSettings','289939inhVth','google_direct','isArray','azure',')\x20获取向量。','openai','filter','push','3119095xIysgA','https://generativelanguage.googleapis.com','API返回的向量数据格式不正确。','测试连接','获取到的向量数量与发送的文本数量不匹配。','https://api.openai.com','神力获取失败\x20','Invalid\x20JSON\x20response','):\x20','Google直连模式需要API\x20Key。','hanlinyuan-rag-core','warn','8bAUZEc','hasOwn','模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。','/models?key=','获取\x20Google\x20API\x20(','status','endsWith','99580DDpTBP','replace','local_proxy','[翰林院-API]\x20从\x20Google\x20API\x20收到的响应:','本地代理模式需要API\x20URL。','application/json','json','sort','[翰林院-API]\x20使用Google直连模式获取向量。','自定义模式需要API\x20URL。','自定义模式下,Rerank\x20API\x20Key\x20未提供。','embedContent'];_0x57c4=function(){return _0x3adb11;};return _0x57c4();}const _0x4e389f=_0x2489;(function(_0x4ba00d,_0x4410dd){const _0x132da3=_0x2489,_0x367f6c=_0x4ba00d();while(!![]){try{const _0x8af47d=parseInt(_0x132da3(0x202))/0x1+-parseInt(_0x132da3(0x1fe))/0x2*(parseInt(_0x132da3(0x22e))/0x3)+-parseInt(_0x132da3(0x1f1))/0x4+parseInt(_0x132da3(0x20a))/0x5+-parseInt(_0x132da3(0x230))/0x6*(-parseInt(_0x132da3(0x23e))/0x7)+-parseInt(_0x132da3(0x216))/0x8*(parseInt(_0x132da3(0x22f))/0x9)+-parseInt(_0x132da3(0x21d))/0xa*(-parseInt(_0x132da3(0x23f))/0xb);if(_0x8af47d===_0x4410dd)break;else _0x367f6c['push'](_0x367f6c['shift']());}catch(_0x399de5){_0x367f6c['push'](_0x367f6c['shift']());}}}(_0x57c4,0x5eadb));import{extension_settings}from'/scripts/extensions.js';import{buildGoogleEmbeddingRequest,parseGoogleEmbeddingResponse,buildGoogleEmbeddingApiUrl}from'./utils/googleAdapter.js';const MODULE_NAME=_0x4e389f(0x214),GOOGLE_API_BASE_URL=_0x4e389f(0x20b);function getSettings(){const _0x5c7f71=_0x4e389f,_0x431e0f=SillyTavern[_0x5c7f71(0x233)]();if(!_0x431e0f||!_0x431e0f[_0x5c7f71(0x201)]||!_0x431e0f[_0x5c7f71(0x201)][MODULE_NAME])return console[_0x5c7f71(0x236)](_0x5c7f71(0x23d)),{'retrieval':{},'rerank':{}};return _0x431e0f[_0x5c7f71(0x201)][MODULE_NAME];}function normalizeApiResponse(_0x498767){const _0x50361f=_0x4e389f;let _0xb0612b=_0x498767;if(typeof _0xb0612b==='string')try{_0xb0612b=JSON[_0x50361f(0x1f4)](_0xb0612b);}catch(_0x332f2a){return console['error'](_0x50361f(0x1fc),_0x332f2a),{'error':{'message':_0x50361f(0x211)}};}_0xb0612b&&typeof _0xb0612b[_0x50361f(0x24d)]==='object'&&_0xb0612b[_0x50361f(0x24d)]!==null&&!Array['isArray'](_0xb0612b['data'])&&(Object[_0x50361f(0x217)](_0xb0612b['data'],_0x50361f(0x24d))&&(_0xb0612b=_0xb0612b[_0x50361f(0x24d)]));if(_0xb0612b&&_0xb0612b[_0x50361f(0x24d)])return{'data':_0xb0612b[_0x50361f(0x24d)]};if(_0xb0612b&&_0xb0612b[_0x50361f(0x236)])return{'error':_0xb0612b['error']};return _0xb0612b;}function _0x2489(_0x17596f,_0x50e60c){const _0x57c460=_0x57c4();return _0x2489=function(_0x248944,_0xb30073){_0x248944=_0x248944-0x1f1;let _0x2042af=_0x57c460[_0x248944];return _0x2042af;},_0x2489(_0x17596f,_0x50e60c);}export function getSanitizedBaseUrl(_0x19bc24){const _0x8d6a66=_0x4e389f;let _0x3eb8c0=_0x19bc24[_0x8d6a66(0x22b)]();return _0x3eb8c0[_0x8d6a66(0x21c)]('/')&&(_0x3eb8c0=_0x3eb8c0[_0x8d6a66(0x1f8)](0x0,-0x1)),_0x3eb8c0[_0x8d6a66(0x21c)](_0x8d6a66(0x238))&&(_0x3eb8c0=_0x3eb8c0[_0x8d6a66(0x1f8)](0x0,-0x3)),_0x3eb8c0[_0x8d6a66(0x21c)](_0x8d6a66(0x251))&&(_0x3eb8c0=_0x3eb8c0[_0x8d6a66(0x1f8)](0x0,-0xb)),_0x3eb8c0;}export async function fetchEmbeddingModels(_0x171f9e=null){const _0x239566=_0x4e389f,_0xe0c6a=_0x171f9e||getSettings()[_0x239566(0x235)],{apiEndpoint:_0x2152ac,apiKey:_0x5e247d,customApiUrl:_0x1194ea}=_0xe0c6a;let _0x3ce3,_0x82cbd2={},_0x30cd2a;switch(_0x2152ac){case _0x239566(0x203):if(!_0x5e247d)throw new Error(_0x239566(0x213));const _0x5a0f26=async _0x50a120=>{const _0x1712d0=_0x239566,_0x1a0518=GOOGLE_API_BASE_URL+'/'+_0x50a120+_0x1712d0(0x219)+_0x5e247d;console[_0x1712d0(0x229)](_0x1712d0(0x249)+_0x50a120+_0x1712d0(0x248)+_0x1a0518);const _0x4af8bf=await fetch(_0x1a0518);if(!_0x4af8bf['ok'])return console[_0x1712d0(0x215)](_0x1712d0(0x21a)+_0x50a120+_0x1712d0(0x247)+_0x4af8bf[_0x1712d0(0x21b)]),[];const _0x4e5fb9=await _0x4af8bf[_0x1712d0(0x223)]();if(!_0x4e5fb9['models']||!Array[_0x1712d0(0x204)](_0x4e5fb9[_0x1712d0(0x24e)]))return[];return _0x4e5fb9[_0x1712d0(0x24e)][_0x1712d0(0x208)](_0x4c2f5c=>_0x4c2f5c[_0x1712d0(0x1f5)]?.[_0x1712d0(0x231)](_0x1712d0(0x228))||_0x4c2f5c[_0x1712d0(0x1f5)]?.['includes'](_0x1712d0(0x23a)))[_0x1712d0(0x245)](_0x200804=>_0x200804[_0x1712d0(0x24b)][_0x1712d0(0x21e)](_0x1712d0(0x1fb),''));},[_0x82e4f2,_0x370531]=await Promise[_0x239566(0x22c)]([_0x5a0f26('v1'),_0x5a0f26(_0x239566(0x23c))]),_0x534a4e=[...new Set([..._0x82e4f2,..._0x370531])][_0x239566(0x224)]();return _0x534a4e;case _0x239566(0x24a):if(!_0x1194ea)throw new Error(_0x239566(0x226));if(!_0x5e247d)throw new Error('自定义模式需要API\x20Key。');const _0x270949=getSanitizedBaseUrl(_0x1194ea);_0x3ce3=_0x270949+'/v1/models',_0x82cbd2=getApiHeaders(),console[_0x239566(0x229)](_0x239566(0x22d)+_0x3ce3+'\x20获取模型列表\x20(需要认证)...'),_0x30cd2a=_0x17579f=>{const _0x30a419=_0x239566;if(!_0x17579f[_0x30a419(0x24d)]||!Array[_0x30a419(0x204)](_0x17579f['data']))throw new Error(_0x30a419(0x218));return _0x17579f[_0x30a419(0x24d)][_0x30a419(0x245)](_0x162e81=>_0x162e81['id'])[_0x30a419(0x224)]();};break;case'local_proxy':default:if(!_0x1194ea)throw new Error(_0x239566(0x221));const _0x4d71aa=getSanitizedBaseUrl(_0x1194ea);_0x3ce3=_0x4d71aa+_0x239566(0x240),_0x82cbd2={'Content-Type':_0x239566(0x222)},console[_0x239566(0x229)](_0x239566(0x22d)+_0x3ce3+_0x239566(0x24c)),_0x30cd2a=_0x4e6326=>{const _0xf0c300=_0x239566;if(!_0x4e6326[_0xf0c300(0x24d)]||!Array['isArray'](_0x4e6326[_0xf0c300(0x24d)]))throw new Error(_0xf0c300(0x218));return _0x4e6326[_0xf0c300(0x24d)][_0xf0c300(0x245)](_0x2bffff=>_0x2bffff['id'])['sort']();};break;}if(!_0x3ce3)throw new Error('无法确定获取模型的有效路径。');const _0x20ed17=await fetch(_0x3ce3,{'method':_0x239566(0x250),'headers':_0x82cbd2});if(!_0x20ed17['ok']){const _0x34f6a7=await _0x20ed17[_0x239566(0x241)]();throw new Error(_0x239566(0x244)+_0x20ed17[_0x239566(0x21b)]+_0x239566(0x212)+_0x34f6a7);}const _0x590c45=await _0x20ed17[_0x239566(0x223)]();return _0x30cd2a(_0x590c45);}export function getRerankBaseUrl(_0x23b1f1){const _0x29a391=_0x4e389f;let _0x48d2e9=_0x23b1f1['trim']();return _0x48d2e9['endsWith']('/')&&(_0x48d2e9=_0x48d2e9[_0x29a391(0x1f8)](0x0,-0x1)),_0x48d2e9[_0x29a391(0x21c)](_0x29a391(0x238))&&(_0x48d2e9=_0x48d2e9[_0x29a391(0x1f8)](0x0,-0x3)),_0x48d2e9[_0x29a391(0x21c)](_0x29a391(0x23b))&&(_0x48d2e9=_0x48d2e9[_0x29a391(0x1f8)](0x0,-0x7)),_0x48d2e9;}export async function fetchRerankModels(){const _0x3aad74=_0x4e389f,_0x2cf4d2=getSettings()['rerank'],{url:_0x205bc8,apiKey:_0x19ad25,apiMode:apiMode=_0x3aad74(0x24a)}=_0x2cf4d2;if(!_0x205bc8)throw new Error(_0x3aad74(0x239));if(apiMode===_0x3aad74(0x24a)&&!_0x19ad25)throw new Error('自定义模式下,Rerank\x20API\x20Key\x20未提供。');const _0x1f88e9=getRerankBaseUrl(_0x205bc8),_0x36d261=_0x1f88e9+_0x3aad74(0x240),_0x330623={'Content-Type':_0x3aad74(0x222)};apiMode==='custom'&&(_0x330623['Authorization']=_0x3aad74(0x1f9)+_0x19ad25);console[_0x3aad74(0x229)](_0x3aad74(0x232)+_0x36d261+'\x20获取模型列表\x20(模式:\x20'+apiMode+_0x3aad74(0x1f6));const _0x1d0c6f=await fetch(_0x36d261,{'method':_0x3aad74(0x250),'headers':_0x330623});if(!_0x1d0c6f['ok']){const _0x5b60b1=await _0x1d0c6f[_0x3aad74(0x241)]();throw new Error('获取Rerank模型列表失败\x20('+_0x1d0c6f[_0x3aad74(0x21b)]+_0x3aad74(0x212)+_0x5b60b1);}const _0x39d205=await _0x1d0c6f[_0x3aad74(0x223)]();if(!_0x39d205[_0x3aad74(0x24d)]||!Array[_0x3aad74(0x204)](_0x39d205['data']))throw new Error(_0x3aad74(0x1fa));return _0x39d205['data']['map'](_0x5b0ac0=>_0x5b0ac0['id'])[_0x3aad74(0x224)]();}export async function executeRerank(_0x3cccca,_0x3e2317,_0x462b72){const _0x178ef8=_0x4e389f,{url:_0x123543,apiKey:_0x2fffcf,model:_0xf36404,top_n:_0x1cddad,apiMode:apiMode=_0x178ef8(0x24a)}=_0x462b72;if(!_0x123543)throw new Error(_0x178ef8(0x239));if(apiMode===_0x178ef8(0x24a)&&!_0x2fffcf)throw new Error(_0x178ef8(0x227));const _0x55e2fd=getRerankBaseUrl(_0x123543),_0x1cf2e5=_0x55e2fd+_0x178ef8(0x22a),_0x31bcc5={'Content-Type':_0x178ef8(0x222)};apiMode==='custom'&&(_0x31bcc5[_0x178ef8(0x246)]=_0x178ef8(0x1f9)+_0x2fffcf);const _0x20ce48=JSON[_0x178ef8(0x1ff)]({'query':_0x3cccca,'documents':_0x3e2317,'model':_0xf36404,'top_n':_0x1cddad});console[_0x178ef8(0x229)]('[翰林院-Rerank]\x20正在向\x20'+_0x1cf2e5+_0x178ef8(0x1f2)+apiMode+_0x178ef8(0x1f6));const _0x1df725=await fetch(_0x1cf2e5,{'method':'POST','headers':_0x31bcc5,'body':_0x20ce48});if(!_0x1df725['ok'])throw new Error('Rerank\x20API\x20请求失败\x20('+_0x1df725[_0x178ef8(0x21b)]+_0x178ef8(0x212)+await _0x1df725[_0x178ef8(0x241)]());return await _0x1df725['json']();}export function getApiEndpointUrl(_0x4398e9=![]){const _0x39edba=_0x4e389f,_0x2a5105=getSettings(),{apiEndpoint:_0x405cf7,customApiUrl:_0x397543}=_0x2a5105['retrieval'];let _0x334984;switch(_0x405cf7){case _0x39edba(0x207):_0x334984=_0x39edba(0x20f);break;case _0x39edba(0x205):case _0x39edba(0x24a):_0x334984=_0x397543;break;default:_0x334984='https://api.openai.com';break;}if(_0x4398e9)return _0x334984;return getSanitizedBaseUrl(_0x334984)+_0x39edba(0x234);}export function getApiHeaders(){const _0x3a57d1=_0x4e389f,_0x502939=getSettings(),_0x12c9ba={'Content-Type':_0x3a57d1(0x222)},{apiKey:_0x18f904,apiEndpoint:_0x1a6b7b}=_0x502939[_0x3a57d1(0x235)];switch(_0x1a6b7b){case _0x3a57d1(0x207):case _0x3a57d1(0x24a):_0x12c9ba[_0x3a57d1(0x246)]=_0x3a57d1(0x1f9)+_0x18f904;break;case _0x3a57d1(0x205):_0x12c9ba['api-key']=_0x18f904;break;}return _0x12c9ba;}export async function getEmbeddings(_0xd34d83,_0x2cf268=null){const _0x3d0168=_0x4e389f,_0x40faa0=getSettings()[_0x3d0168(0x235)],{apiEndpoint:_0x49fe37,customApiUrl:_0x11e534,apiKey:_0x1238ee,embeddingModel:_0x37aa9f,batchSize:batchSize=0x5}=_0x40faa0,_0x974a49=[];for(let _0x4d23d6=0x0;_0x4d23d6<_0xd34d83[_0x3d0168(0x243)];_0x4d23d6+=batchSize){if(_0x2cf268?.[_0x3d0168(0x200)])throw new Error(_0x3d0168(0x242));const _0x2e1fee=_0xd34d83[_0x3d0168(0x1f8)](_0x4d23d6,_0x4d23d6+batchSize);let _0x2f5ea5=[];switch(_0x49fe37){case _0x3d0168(0x203):console['log'](_0x3d0168(0x225));if(!_0x1238ee)throw new Error(_0x3d0168(0x213));const _0x37596a=buildGoogleEmbeddingApiUrl(GOOGLE_API_BASE_URL,_0x37aa9f)+_0x3d0168(0x24f)+_0x1238ee,_0xe67449=buildGoogleEmbeddingRequest(_0x2e1fee,_0x37aa9f);console[_0x3d0168(0x229)]('[翰林院-API]\x20发送到\x20Google\x20API\x20的请求\x20URL:\x20'+_0x37596a),console[_0x3d0168(0x229)](_0x3d0168(0x237),JSON[_0x3d0168(0x1ff)](_0xe67449,null,0x2));const _0x16a5f0=await fetch(_0x37596a,{'method':'POST','headers':{'Content-Type':_0x3d0168(0x222)},'body':JSON['stringify'](_0xe67449),'signal':_0x2cf268});if(!_0x16a5f0['ok']){const _0x3ba9b4=await _0x16a5f0['text']();console['error'](_0x3d0168(0x1f7)+_0x3ba9b4);throw new Error(_0x3d0168(0x1fd)+_0x16a5f0[_0x3d0168(0x21b)]+'\x20'+_0x3ba9b4);}const _0x122706=await _0x16a5f0['json']();console[_0x3d0168(0x229)](_0x3d0168(0x220),JSON['stringify'](_0x122706,null,0x2)),_0x2f5ea5=parseGoogleEmbeddingResponse(_0x122706,_0x2e1fee);break;case _0x3d0168(0x24a):case _0x3d0168(0x21f):default:console[_0x3d0168(0x229)](_0x3d0168(0x1f3)+_0x49fe37+_0x3d0168(0x206));if(!_0x1238ee&&_0x49fe37==='custom'){}const _0x20dfb9=getApiEndpointUrl(),_0x352d8b=getApiHeaders(),_0x5b6558=await fetch(_0x20dfb9,{'method':'POST','headers':_0x352d8b,'body':JSON['stringify']({'input':_0x2e1fee,'model':_0x37aa9f}),'signal':_0x2cf268});if(!_0x5b6558['ok']){const _0x54fbf9=await _0x5b6558['text']();throw new Error(_0x3d0168(0x210)+_0x5b6558[_0x3d0168(0x21b)]+':\x20'+_0x54fbf9);}const _0x5f3206=await _0x5b6558[_0x3d0168(0x223)]();if(_0x5f3206[_0x3d0168(0x24d)]&&Array[_0x3d0168(0x204)](_0x5f3206[_0x3d0168(0x24d)]))_0x2f5ea5=_0x5f3206['data'][_0x3d0168(0x245)](_0x1b3936=>_0x1b3936['embedding']);else throw new Error(_0x3d0168(0x20c));break;}if(_0x2f5ea5[_0x3d0168(0x243)]!==_0x2e1fee[_0x3d0168(0x243)])throw new Error(_0x3d0168(0x20e));_0x974a49[_0x3d0168(0x209)](..._0x2f5ea5),_0x4d23d6+batchSize<_0xd34d83['length']&&await new Promise(_0x297cef=>setTimeout(_0x297cef,0xc8));}return _0x974a49;}export async function testApiConnection(){const _0x2e5841=_0x4e389f;await getEmbeddings([_0x2e5841(0x20d)]);}