Files
ST-Amily2-Chat-Optimisation…/core/rag-api.js

2 lines
14 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.
'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)]);}