@easbot/local-model-sdk 0.3.3 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +20 -96
- package/dist/index.d.cts +47 -104
- package/dist/index.d.ts +47 -104
- package/dist/index.mjs +20 -96
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
'use strict';var
|
|
2
|
-
`,r=e.lastIndexOf(n);if(r!==-1){let
|
|
1
|
+
'use strict';var G=require('os'),D=require('path'),b=require('fs'),utils=require('@easbot/utils'),ai=require('ai');function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var G__namespace=/*#__PURE__*/_interopNamespace(G);var D__namespace=/*#__PURE__*/_interopNamespace(D);var b__namespace=/*#__PURE__*/_interopNamespace(b);var ee=Object.defineProperty;var te=(p,e,t)=>e in p?ee(p,e,{enumerable:true,configurable:true,writable:true,value:t}):p[e]=t;var g=(p,e,t)=>te(p,typeof e!="symbol"?e+"":e,t);var M=(f=>(f.MODEL_NOT_FOUND="MODEL_NOT_FOUND",f.MODEL_LOAD_ERROR="MODEL_LOAD_ERROR",f.MODEL_NOT_INITIALIZED="MODEL_NOT_INITIALIZED",f.GENERATION_ERROR="GENERATION_ERROR",f.EMBEDDING_ERROR="EMBEDDING_ERROR",f.INVALID_INPUT="INVALID_INPUT",f.UNSUPPORTED_FEATURE="UNSUPPORTED_FEATURE",f.CACHE_ERROR="CACHE_ERROR",f.PROCESSING_ERROR="PROCESSING_ERROR",f.ABORTED="ABORTED",f.OUT_OF_MEMORY="OUT_OF_MEMORY",f.UNKNOWN_ERROR="UNKNOWN_ERROR",f))(M||{});var L={"qwen3-0.6b":{modelId:"qwen3-0.6b",huggingFaceId:"onnx-community/Qwen3-0.6B-ONNX",contextLength:32768,device:"cpu",dtype:"q4f16",defaultSettings:{temperature:0,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}},"qwen3.5-0.8b":{modelId:"qwen3.5-0.8b",huggingFaceId:"onnx-community/Qwen3.5-0.8B-ONNX",contextLength:32768,dtype:"q4",defaultSettings:{temperature:.7,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}},"gemma-3-1b-it":{modelId:"gemma-3-1b-it",huggingFaceId:"onnx-community/gemma-3-1b-it-ONNX",contextLength:32768,dtype:"q4",defaultSettings:{temperature:.7,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}}},k={"bge-small-zh-v1.5":{modelId:"bge-small-zh-v1.5",huggingFaceId:"Xenova/bge-small-zh-v1.5",dimensions:512,maxEmbeddingsPerCall:32},"bge-base-zh-v1.5":{modelId:"bge-base-zh-v1.5",huggingFaceId:"Xenova/bge-base-zh-v1.5",dimensions:768,maxEmbeddingsPerCall:32,dtype:"q4"},"bge-large-zh-v1.5":{modelId:"bge-large-zh-v1.5",huggingFaceId:"Xenova/bge-large-zh-v1.5",dimensions:1024,maxEmbeddingsPerCall:32,dtype:"q4"},"all-MiniLM-L6-v2":{modelId:"all-MiniLM-L6-v2",huggingFaceId:"sentence-transformers/all-MiniLM-L6-v2",dimensions:384,maxEmbeddingsPerCall:32,dtype:"q4"}};var u=class p extends Error{constructor(t,n,r){super(t);g(this,"code",n);g(this,"cause",r);this.name="LocalModelError",Error.captureStackTrace&&Error.captureStackTrace(this,p);}},T=class{static modelLoadError(e){return new u(`\u6A21\u578B\u52A0\u8F7D\u5931\u8D25: ${e.message}`,"MODEL_LOAD_ERROR",e)}static generationError(e){return new u(`\u751F\u6210\u5931\u8D25: ${e.message}`,"GENERATION_ERROR",e)}static embeddingError(e){return new u(`\u5D4C\u5165\u5931\u8D25: ${e.message}`,"EMBEDDING_ERROR",e)}static validationError(e){return new u(e,"INVALID_INPUT")}static abortError(){return new u("\u64CD\u4F5C\u5DF2\u4E2D\u6B62","ABORTED")}static unsupportedFeature(e){return new u(`\u4E0D\u652F\u6301\u7684\u529F\u80FD: ${e}`,"UNSUPPORTED_FEATURE")}};var ne=D__namespace.join(G__namespace.homedir(),".cache","easbot","models"),I=class{constructor(e){g(this,"cacheDir");this.cacheDir=e.cacheDir||ne;}getCachePath(e){return D__namespace.join(this.cacheDir,e)}isCached(e){try{let t=this.getCachePath(e);return b__namespace.existsSync(t)&&b__namespace.statSync(t).isDirectory()}catch{return false}}async clear(e){try{if(e){let t=this.getCachePath(e);b__namespace.existsSync(t)&&await this.removeDirectory(t);}else b__namespace.existsSync(this.cacheDir)&&await this.removeDirectory(this.cacheDir);}catch(t){throw new u(`\u6E05\u7406\u7F13\u5B58\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,"CACHE_ERROR",t instanceof Error?t:void 0)}}async getCacheSize(e){try{let t=e?this.getCachePath(e):this.cacheDir;return b__namespace.existsSync(t)?await this.getDirectorySize(t):0}catch(t){throw new u(`\u83B7\u53D6\u7F13\u5B58\u5927\u5C0F\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,"CACHE_ERROR",t instanceof Error?t:void 0)}}async getDirectorySize(e){let t=0,n=b__namespace.readdirSync(e);for(let r of n){let a=D__namespace.join(e,r),o=b__namespace.statSync(a);o.isDirectory()?t+=await this.getDirectorySize(a):t+=o.size;}return t}async removeDirectory(e){if(!b__namespace.existsSync(e))return;let t=b__namespace.readdirSync(e);for(let n of t){let r=D__namespace.join(e,n);b__namespace.statSync(r).isDirectory()?await this.removeDirectory(r):b__namespace.unlinkSync(r);}b__namespace.rmdirSync(e);}ensureCacheDir(){try{b__namespace.existsSync(this.cacheDir)||b__namespace.mkdirSync(this.cacheDir,{recursive:!0});}catch(e){throw new u(`\u521B\u5EFA\u7F13\u5B58\u76EE\u5F55\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`,"CACHE_ERROR",e instanceof Error?e:void 0)}}};var h=utils.Log.create({service:"transformers-engine"}),P=null;async function oe(){if(!P){P=(await import('@huggingface/transformers')).env;let e=D__namespace.join(G__namespace.homedir(),".cache","easbot","models");process.env.HF_HOME||(process.env.HF_HOME=e),P.allowLocalModels=true,P.allowRemoteModels=true,P.useBrowserCache=false,P.useFSCache=true;}return P}var V=null;async function z(){return V||(V=(await import('@huggingface/transformers')).pipeline),V}var F=class{constructor(e){g(this,"pipeline",null);g(this,"tokenizer",null);g(this,"isInitialized",false);g(this,"config");g(this,"modelCache");this.config=e,this.modelCache=new I({cacheDir:e.cacheDir});}async configureEnvironment(){let e=await oe();this.modelCache.ensureCacheDir(),process.env.HF_HOME||(process.env.HF_HOME=this.config.cacheDir),e.cacheDir=this.config.cacheDir,e.allowLocalModels=true,e.allowRemoteModels=true,e.useBrowserCache=false,e.useFSCache=true,e.backends?.onnx&&(e.backends.onnx.logLevel="error"),this.config.verbose&&h.debug("\u73AF\u5883\u914D\u7F6E\u5B8C\u6210",{cacheDir:this.config.cacheDir,hfHome:process.env.HF_HOME,modelId:this.config.modelId,modelType:this.config.modelType});}async initialize(){if(this.isInitialized){this.config.verbose&&h.debug("\u6A21\u578B\u5DF2\u521D\u59CB\u5316\uFF0C\u8DF3\u8FC7\u91CD\u590D\u52A0\u8F7D");return}await this.configureEnvironment();try{this.config.verbose&&h.debug("\u5F00\u59CB\u521D\u59CB\u5316\u6A21\u578B");let e,t,n;if(this.config.modelType==="language"){let o=L[this.config.modelId];if(!o)throw new u(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${this.config.modelId}`,"MODEL_NOT_FOUND");e=o.huggingFaceId,t=o.dtype,n=o.device;}else {let o=k[this.config.modelId];if(!o)throw new u(`\u4E0D\u652F\u6301\u7684 Embedding Model ID: ${this.config.modelId}`,"MODEL_NOT_FOUND");e=o.huggingFaceId,t=o.dtype;}this.config.verbose&&(h.debug("\u52A0\u8F7D\u6A21\u578B",{huggingFaceId:e,dtype:t||"auto"}),this.modelCache.isCached(e)?h.debug("\u4F7F\u7528\u7F13\u5B58\u7684\u6A21\u578B"):h.debug("\u9996\u6B21\u4E0B\u8F7D\u6A21\u578B\uFF0C\u53EF\u80FD\u9700\u8981\u4E00\u4E9B\u65F6\u95F4"));let r={cache_dir:this.config.cacheDir,local_files_only:!0};if(t&&(r.dtype=t),n&&(r.device=n),!this.modelCache.isCached(e))throw this.config.verbose&&h.warn("\u672C\u5730\u7F13\u5B58\u4E0D\u5B58\u5728",{huggingFaceId:e,cacheDir:this.config.cacheDir}),new u(`\u6A21\u578B ${e} \u672A\u627E\u5230\u672C\u5730\u7F13\u5B58\u3002\u8BF7\u5148\u4E0B\u8F7D\u6A21\u578B\u5230: ${this.config.cacheDir}`,"MODEL_NOT_FOUND");if(this.config.modelType==="language"){let o=this.config.pipelineType||"text-generation";this.config.verbose&&h.debug("\u52A0\u8F7D\u8BED\u8A00\u6A21\u578B pipeline",{pipelineType:o,huggingFaceId:e});let l=await z();this.pipeline=await l(o,e,r);}else {let o=await z();this.pipeline=await o("feature-extraction",e,r);}this.tokenizer=this.pipeline.tokenizer,this.isInitialized=!0,this.config.verbose&&h.debug("\u6A21\u578B\u521D\u59CB\u5316\u6210\u529F",{modelId:this.config.modelId});}catch(e){throw this.config.verbose&&h.error("\u6A21\u578B\u521D\u59CB\u5316\u5931\u8D25",{error:e}),T.modelLoadError(e instanceof Error?e:new Error(String(e)))}}extractAssistantResponse(e,t){let n=`assistant
|
|
2
|
+
`,r=e.lastIndexOf(n);if(r!==-1){let s=e.slice(r+n.length);return s=s.replace(/<\|im_end\|>$/g,""),s.trim()}let a=`
|
|
3
3
|
assistant
|
|
4
|
-
`,a=e.lastIndexOf(o);if(a!==-1)return e.slice(a+o.length).trim();let g=`assistant
|
|
5
|
-
`,i=e.lastIndexOf(g);return i!==-1?e.slice(i+g.length).trim():(e.startsWith(t)?e.slice(t.length):e).trim()}async generate(e,t){let r=L[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]},o={temperature:t.temperature??r.temperature,topP:t.topP??r.topP,topK:t.topK??r.topK,maxTokens:t.maxTokens??r.maxTokens,repetitionPenalty:t.repetitionPenalty??r.repetitionPenalty,stopSequences:t.stopSequences??r.stopSequences,abortSignal:t.abortSignal};if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw T.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u751F\u6210\u6587\u672C");let a=await this.countTokens([e]),g=await this.pipeline(e,{max_new_tokens:o.maxTokens,temperature:o.temperature,top_p:o.topP,top_k:o.topK,repetition_penalty:o.repetitionPenalty,do_sample:o.temperature>0});if(t.abortSignal?.aborted)throw T.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=await this.countTokens([m]);return this.config.verbose&&y.debug("\u751F\u6210\u5B8C\u6210",{completionTokens:u}),{content:m,finishReason:B(!0,!1),usage:{promptTokens:a,completionTokens:u,totalTokens:a+u}}}catch(a){throw this.config.verbose&&y.error("\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:T.generationError(a instanceof Error?a:new Error(String(a)))}}async generateStream(e,t,n){let o=L[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]};if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw T.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u6D41\u5F0F\u751F\u6210\u6587\u672C\uFF08\u6A21\u62DF\u6D41\u5F0F\u8F93\u51FA\uFF09");let{TextStreamer:a}=await import('@huggingface/transformers'),g=await this.pipeline(e,{max_new_tokens:t.maxTokens??o.maxTokens,temperature:t.temperature??o.temperature,top_p:t.topP??o.topP,top_k:t.topK??o.topK,repetition_penalty:t.repetitionPenalty??o.repetitionPenalty,do_sample:(t.temperature??o.temperature)>0});if(t.abortSignal?.aborted)throw T.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=m.split(/(\s+)/);for(let l of u){if(t.abortSignal?.aborted)throw T.abortError();l.length>0&&n({type:"text-delta",delta:l});}n({type:"text-end"});let p=await this.countTokens([e]),d=await this.countTokens([m]);n({type:"finish",finishReason:B(!0,!1),usage:{promptTokens:p,completionTokens:d,totalTokens:p+d}}),this.config.verbose&&y.debug("\u6D41\u5F0F\u751F\u6210\u5B8C\u6210",{promptTokens:p,completionTokens:d,totalTokens:p+d});}catch(a){throw this.config.verbose&&y.error("\u6D41\u5F0F\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:(n({type:"finish",finishReason:B(false,true,"generation_error"),usage:{promptTokens:0,completionTokens:0,totalTokens:0}}),T.generationError(a instanceof Error?a:new Error(String(a))))}}async embed(e,t){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="embedding")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u5D4C\u5165\u63A8\u7406","UNSUPPORTED_FEATURE");if(t?.aborted)throw T.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u63A8\u7406",{textCount:e.length});let n=await this.pipeline(e,{pooling:"mean",normalize:!0});if(t?.aborted)throw T.abortError();let r=[];if(n.dims&&n.dims.length===2){let[o,a]=n.dims,g=Array.from(n.data);for(let i=0;i<o;i++){let m=i*a,u=m+a;r.push(g.slice(m,u));}}else r.push(Array.from(n.data));return this.config.verbose&&y.debug("\u63A8\u7406\u5B8C\u6210",{embeddingCount:r.length}),r}catch(n){throw this.config.verbose&&y.error("\u63A8\u7406\u5931\u8D25",{error:n}),n instanceof s&&n.code==="ABORTED"?n:T.embeddingError(n instanceof Error?n:new Error(String(n)))}}async countTokens(e){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");try{let t=0;for(let n of e){let r=await this.tokenizer(n);t+=r.input_ids.data.length;}return t}catch(t){return this.config.verbose&&y.error("\u8BA1\u7B97 token \u5931\u8D25",{error:t}),e.reduce((n,r)=>n+Math.ceil(r.length/4),0)}}async dispose(){this.config.verbose&&y.debug("\u5378\u8F7D\u6A21\u578B"),this.pipeline=null,this.tokenizer=null,this.isInitialized=false,this.config.verbose&&y.debug("\u6A21\u578B\u5DF2\u5378\u8F7D");}isReady(){return this.isInitialized}};var E=class{static validateTexts(e){if(!Array.isArray(e))throw new s("\u8F93\u5165\u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u8F93\u5165\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(typeof n!="string")throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u4E0D\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`,"INVALID_INPUT");if(n.trim().length===0)throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}static validateMessages(e){if(!Array.isArray(e))throw new s("\u6D88\u606F\u5FC5\u987B\u662F\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u6D88\u606F\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(!n||typeof n!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!n.role)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 role \u5B57\u6BB5`,"INVALID_INPUT");if(!["system","user","assistant"].includes(n.role))throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 role "${n.role}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 system\u3001user\u3001assistant`,"UNSUPPORTED_FEATURE");if(!n.content)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 content \u5B57\u6BB5`,"INVALID_INPUT");if(typeof n.content=="string"){if(n.content.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}else if(Array.isArray(n.content)){if(n.content.length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u6570\u7EC4\u4E3A\u7A7A`,"INVALID_INPUT");for(let r=0;r<n.content.length;r++){let o=n.content[r];if(!o||typeof o!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!o.type)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11 type \u5B57\u6BB5`,"INVALID_INPUT");if(o.type!=="text"&&o.type!=="reasoning")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7C7B\u578B "${o.type}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 text \u548C reasoning`,"UNSUPPORTED_FEATURE");if(!("text"in o)||typeof o.text!="string")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11\u6709\u6548\u7684 text \u5B57\u6BB5`,"INVALID_INPUT");if(o.text.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7684 text \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}else throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6216\u6570\u7EC4`,"INVALID_INPUT")}}static validateGenerateOptions(e){if(e.temperature!==void 0&&(typeof e.temperature!="number"||e.temperature<0||e.temperature>2))throw new s("temperature \u5FC5\u987B\u662F 0 \u5230 2 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topP!==void 0&&(typeof e.topP!="number"||e.topP<0||e.topP>1))throw new s("topP \u5FC5\u987B\u662F 0 \u5230 1 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topK!==void 0&&(typeof e.topK!="number"||e.topK<1||!Number.isInteger(e.topK)))throw new s("topK \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.maxTokens!==void 0&&(typeof e.maxTokens!="number"||e.maxTokens<1||!Number.isInteger(e.maxTokens)))throw new s("maxTokens \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.repetitionPenalty!==void 0&&(typeof e.repetitionPenalty!="number"||e.repetitionPenalty<0))throw new s("repetitionPenalty \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 0 \u7684\u6570\u5B57","INVALID_INPUT")}};var G=class{static createReadableStream(e,t){return new ReadableStream({async start(n){if(t?.aborted){n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let r=false,o=()=>{r=true,n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));};t?.addEventListener("abort",o);try{if(await e(a=>{if(!r)try{n.enqueue(a),a.isFinished&&(n.close(),t?.removeEventListener("abort",o));}catch(g){n.error(g),t?.removeEventListener("abort",o);}}),!r)try{n.enqueue({textDelta:"",isFinished:!0,finishReason:"stop"}),n.close();}catch{}}catch(a){r||n.error(a instanceof s?a:new s(`\u751F\u6210\u5931\u8D25: ${a instanceof Error?a.message:String(a)}`,"GENERATION_ERROR",a instanceof Error?a:void 0));}finally{t?.removeEventListener("abort",o);}},cancel(){}})}static async*streamToAsyncIterator(e){let t=e.getReader();try{for(;;){let{done:n,value:r}=await t.read();if(n)break;yield r;}}finally{t.releaseLock();}}};function ae(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var v=class{constructor(e,t,n){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"defaultObjectGenerationMode","json");c(this,"supportedUrls",{});c(this,"engine");c(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=L[t],!this.modelConfig)throw new s(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async generate(e){return this.doGenerate(e)}async stream(e){return this.doStream(e)}convertToQwenFormat(e){let t=[];for(let n of e){let r="";switch(typeof n.content=="string"?r=n.content:Array.isArray(n.content)&&(r=n.content.filter(o=>o.type==="text").map(o=>"text"in o?o.text:"").join(`
|
|
6
|
-
`)
|
|
7
|
-
${r}
|
|
8
|
-
${r}
|
|
9
|
-
${r}
|
|
4
|
+
`,o=e.lastIndexOf(a);return o!==-1?e.slice(o+a.length).trim():(e.startsWith(t)?e.slice(t.length):e).trim()}async generate(e,t){let r=L[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]},a={temperature:t.temperature??r.temperature,topP:t.topP??r.topP,topK:t.topK??r.topK,maxTokens:t.maxTokens??r.maxTokens,repetitionPenalty:t.repetitionPenalty??r.repetitionPenalty,stopSequences:t.stopSequences??r.stopSequences,abortSignal:t.abortSignal};if(this.config.modelType!=="language")throw new u("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw T.abortError();try{this.config.verbose&&h.debug("\u5F00\u59CB\u751F\u6210\u6587\u672C");let o=await this.countTokens([e]),l=await this.pipeline(e,{max_new_tokens:a.maxTokens,temperature:a.temperature,top_p:a.topP,top_k:a.topK,repetition_penalty:a.repetitionPenalty,do_sample:a.temperature>0});if(t.abortSignal?.aborted)throw T.abortError();let s=l[0]?.generated_text||"",i=this.extractAssistantResponse(s,e),d=await this.countTokens([i]);return this.config.verbose&&h.debug("\u751F\u6210\u5B8C\u6210",{completionTokens:d}),{content:i,finishReason:{unified:"stop",raw:void 0},usage:{inputTokens:{total:o,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:d,text:d,reasoning:void 0}}}}catch(o){throw this.config.verbose&&h.error("\u751F\u6210\u5931\u8D25",{error:o}),o instanceof u&&o.code==="ABORTED"?o:T.generationError(o instanceof Error?o:new Error(String(o)))}}async generateStream(e,t,n){let a=L[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]};if(!this.isInitialized)throw new u("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="language")throw new u("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw T.abortError();try{this.config.verbose&&h.debug("\u5F00\u59CB\u6D41\u5F0F\u751F\u6210\u6587\u672C\uFF08\u6A21\u62DF\u6D41\u5F0F\u8F93\u51FA\uFF09");let o=await this.pipeline(e,{max_new_tokens:t.maxTokens??a.maxTokens,temperature:t.temperature??a.temperature,top_p:t.topP??a.topP,top_k:t.topK??a.topK,repetition_penalty:t.repetitionPenalty??a.repetitionPenalty,do_sample:(t.temperature??a.temperature)>0});if(t.abortSignal?.aborted)throw T.abortError();let l=o[0]?.generated_text||"",s=this.extractAssistantResponse(l,e),i=s.split(/(\s+)/);for(let S of i){if(t.abortSignal?.aborted)throw T.abortError();S.length>0&&n({type:"text-delta",delta:S});}n({type:"text-end"});let d=await this.countTokens([e]),m=await this.countTokens([s]);n({type:"finish",finishReason:{unified:"stop",raw:void 0},usage:{inputTokens:{total:d,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:m,text:m,reasoning:void 0}}}),this.config.verbose&&h.debug("\u6D41\u5F0F\u751F\u6210\u5B8C\u6210",{promptTokens:d,completionTokens:m});}catch(o){throw this.config.verbose&&h.error("\u6D41\u5F0F\u751F\u6210\u5931\u8D25",{error:o}),o instanceof u&&o.code==="ABORTED"?o:(n({type:"finish",finishReason:{unified:"error",raw:"generation_error"},usage:{inputTokens:{total:0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:0,text:0,reasoning:void 0}}}),T.generationError(o instanceof Error?o:new Error(String(o))))}}async embed(e,t){if(!this.isInitialized)throw new u("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="embedding")throw new u("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u5D4C\u5165\u63A8\u7406","UNSUPPORTED_FEATURE");if(t?.aborted)throw T.abortError();try{this.config.verbose&&h.debug("\u5F00\u59CB\u63A8\u7406",{textCount:e.length});let n=await this.pipeline(e,{pooling:"mean",normalize:!0});if(t?.aborted)throw T.abortError();let r=[];if(n.dims&&n.dims.length===2){let[a,o]=n.dims,l=Array.from(n.data);for(let s=0;s<a;s++){let i=s*o,d=i+o;r.push(l.slice(i,d));}}else r.push(Array.from(n.data));return this.config.verbose&&h.debug("\u63A8\u7406\u5B8C\u6210",{embeddingCount:r.length}),r}catch(n){throw this.config.verbose&&h.error("\u63A8\u7406\u5931\u8D25",{error:n}),n instanceof u&&n.code==="ABORTED"?n:T.embeddingError(n instanceof Error?n:new Error(String(n)))}}async countTokens(e){if(!this.isInitialized)throw new u("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");try{let t=0;for(let n of e){let r=await this.tokenizer(n);t+=r.input_ids.data.length;}return t}catch(t){return this.config.verbose&&h.error("\u8BA1\u7B97 token \u5931\u8D25",{error:t}),e.reduce((n,r)=>n+Math.ceil(r.length/4),0)}}async dispose(){this.config.verbose&&h.debug("\u5378\u8F7D\u6A21\u578B"),this.pipeline=null,this.tokenizer=null,this.isInitialized=false,this.config.verbose&&h.debug("\u6A21\u578B\u5DF2\u5378\u8F7D");}isReady(){return this.isInitialized}};var E=class{static validateTexts(e){if(!Array.isArray(e))throw new u("\u8F93\u5165\u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new u("\u8F93\u5165\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(typeof n!="string")throw new u(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u4E0D\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`,"INVALID_INPUT");if(n.trim().length===0)throw new u(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}static validateMessages(e){if(!Array.isArray(e))throw new u("\u6D88\u606F\u5FC5\u987B\u662F\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new u("\u6D88\u606F\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(!n||typeof n!="object")throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!n.role)throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 role \u5B57\u6BB5`,"INVALID_INPUT");let r=["system","user","assistant","tool"];if(!r.includes(n.role))throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 role "${n.role}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 ${r.join("\u3001")}`,"UNSUPPORTED_FEATURE");if(!n.content)throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 content \u5B57\u6BB5`,"INVALID_INPUT");if(typeof n.content=="string"){if(n.content.trim().length===0)throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}else if(Array.isArray(n.content)){if(n.content.length===0)throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u6570\u7EC4\u4E3A\u7A7A`,"INVALID_INPUT");for(let a=0;a<n.content.length;a++){let o=n.content[a];if(!o||typeof o!="object")throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${a} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!o.type)throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${a} \u7F3A\u5C11 type \u5B57\u6BB5`,"INVALID_INPUT");let l=["text","file","reasoning","tool-call","tool-result","tool-approval-response"];if(!l.includes(o.type))throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${a} \u7C7B\u578B "${o.type}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 ${l.join("\u3001")}`,"UNSUPPORTED_FEATURE")}}else throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6216\u6570\u7EC4`,"INVALID_INPUT")}}static validateGenerateOptions(e){if(e.temperature!==void 0&&(typeof e.temperature!="number"||e.temperature<0||e.temperature>2))throw new u("temperature \u5FC5\u987B\u662F 0 \u5230 2 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topP!==void 0&&(typeof e.topP!="number"||e.topP<0||e.topP>1))throw new u("topP \u5FC5\u987B\u662F 0 \u5230 1 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topK!==void 0&&(typeof e.topK!="number"||e.topK<1||!Number.isInteger(e.topK)))throw new u("topK \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.maxTokens!==void 0&&(typeof e.maxTokens!="number"||e.maxTokens<1||!Number.isInteger(e.maxTokens)))throw new u("maxTokens \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.repetitionPenalty!==void 0&&(typeof e.repetitionPenalty!="number"||e.repetitionPenalty<0))throw new u("repetitionPenalty \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 0 \u7684\u6570\u5B57","INVALID_INPUT")}};var C=class{static createReadableStream(e,t){return new ReadableStream({async start(n){if(t?.aborted){n.error(new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let r=false,a=()=>{r=true;try{n.error(new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));}catch{}};t?.addEventListener("abort",a);try{if(await(async l=>{let s=l.enqueue.bind(l);l.enqueue=i=>{if(!r)try{s(i);}catch{}},await e(l);})(n),!r)try{n.close();}catch{}}catch(o){if(!r)try{n.error(o instanceof u?o:new u(`\u751F\u6210\u5931\u8D25: ${o instanceof Error?o.message:String(o)}`,"GENERATION_ERROR",o instanceof Error?o:void 0));}catch{}}finally{t?.removeEventListener("abort",a);}},cancel(){}})}static async*streamToAsyncIterator(e){let t=e.getReader();try{for(;;){let{done:n,value:r}=await t.read();if(n)break;yield r;}}finally{t.releaseLock();}}};function H(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var O=class{constructor(e,t,n){g(this,"specificationVersion","v3");g(this,"provider");g(this,"modelId");g(this,"defaultObjectGenerationMode","json");g(this,"supportedUrls",{});g(this,"engine");g(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=L[t],!this.modelConfig)throw new u(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async doGenerate(e){E.validateMessages(e.prompt);let t=this.convertToChatMLFormat(e.prompt),n={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxOutputTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};E.validateGenerateOptions(n),this.engine.isReady()||await this.engine.initialize();let r=await this.engine.generate(t,n);return {content:[{type:"text",text:r.content||""}],finishReason:r.finishReason,usage:r.usage,warnings:[],request:{body:t}}}async doStream(e){E.validateMessages(e.prompt);let t=this.convertToChatMLFormat(e.prompt),n={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxOutputTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};E.validateGenerateOptions(n),this.engine.isReady()||await this.engine.initialize();let r=[];return {stream:C.createReadableStream(async o=>{o.enqueue({type:"stream-start",warnings:r});let l=H();o.enqueue({type:"response-metadata",id:l,modelId:this.modelId,timestamp:new Date});let s=null,i=false;try{await this.engine.generateStream(t,n,d=>{if(d.type==="text-delta"){let m=d.delta||"";if(m.length===0)return;s||(s=H(),o.enqueue({type:"text-start",id:s})),o.enqueue({type:"text-delta",id:s,delta:m});}else d.type==="text-end"?s&&(o.enqueue({type:"text-end",id:s}),s=null):d.type==="finish"&&(s&&(o.enqueue({type:"text-end",id:s}),s=null),o.enqueue({type:"finish",finishReason:d.finishReason??{unified:"stop",raw:void 0},usage:d.usage??{inputTokens:{total:void 0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:void 0,text:void 0,reasoning:void 0}}}),i=!0);});}catch(d){o.enqueue({type:"error",error:d instanceof Error?d:new Error(String(d))}),s&&o.enqueue({type:"text-end",id:s}),i||o.enqueue({type:"finish",finishReason:{unified:"error",raw:void 0},usage:{inputTokens:{total:void 0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:void 0,text:void 0,reasoning:void 0}}});}},e.abortSignal),request:{body:t}}}convertToChatMLFormat(e){let t=[];for(let n of e){let r="";switch(typeof n.content=="string"?r=n.content:Array.isArray(n.content)&&(r=n.content.filter(a=>a.type==="text").map(a=>"text"in a?a.text:"").join(`
|
|
5
|
+
`)),n.role){case "system":t.push(`system
|
|
6
|
+
${r}`);break;case "user":t.push(`user
|
|
7
|
+
${r}`);break;case "assistant":t.push(`assistant
|
|
8
|
+
${r}`);break;default:t.push(`${String(n.role)}
|
|
9
|
+
${r}`);break}}return t.push(`assistant
|
|
10
10
|
`),t.join(`
|
|
11
|
-
`)}
|
|
11
|
+
`)}};var N=class p{static buildEnhancedPrompt(e,t){let n=[];n.push(p.buildSystemPrompt(t));for(let r of e){let a=p.formatMessage(r);a&&n.push(a);}return n.push(`<|im_start|>assistant
|
|
12
12
|
`),n.join(`
|
|
13
13
|
`)}static buildSystemPrompt(e){return e&&e.length>0?`<|im_start|>system
|
|
14
14
|
Environment: ipython
|
|
15
15
|
|
|
16
16
|
You have access to the following functions:
|
|
17
17
|
|
|
18
|
-
${
|
|
18
|
+
${p.formatToolsDefinition(e)}
|
|
19
19
|
|
|
20
20
|
FUNCTION CALLING INSTRUCTIONS:
|
|
21
21
|
|
|
@@ -103,94 +103,18 @@ CRITICAL OUTPUT RULES:
|
|
|
103
103
|
4. Do NOT include any part of the conversation format in your output
|
|
104
104
|
<|im_end|>`}static formatToolsDefinition(e){return e.map(t=>{let n=t.inputSchema;return JSON.stringify({name:t.name,description:t.description||"No description",parameters:n},null,2)}).join(`
|
|
105
105
|
|
|
106
|
-
`)}static formatMessage(e){let t="";if(typeof e.content=="string")t=e.content;else if(Array.isArray(e.content)){let n=[];for(let r of e.content)if(r.type==="text")n.push("text"in r?r.text:"");else if(r.type==="tool-call"){let
|
|
106
|
+
`)}static formatMessage(e){let t="";if(typeof e.content=="string")t=e.content;else if(Array.isArray(e.content)){let n=[];for(let r of e.content)if(r.type==="text")n.push("text"in r?r.text:"");else if(r.type==="tool-call"){let a={name:r.toolName,parameters:r.args||{}};n.push(JSON.stringify(a));}else if(r.type==="tool-result"){let a=r.result;n.push(typeof a=="string"?a:JSON.stringify(a));}t=n.join(`
|
|
107
107
|
`);}switch(e.role){case "system":return null;case "user":return `<|im_start|>user
|
|
108
108
|
${t}<|im_end|>`;case "assistant":return `<|im_start|>assistant
|
|
109
109
|
${t}<|im_end|>`;case "tool":return `<|im_start|>tool
|
|
110
|
-
${t}<|im_end|>`;default:{let n=e.role;throw new s(`\u4E0D\u652F\u6301\u7684\u6D88\u606F\u89D2\u8272: ${n}`,"UNSUPPORTED_FEATURE")}}}};var O=class{static processThinkTags(e,t){let n={...e};for(;n.contentBuffer.length>0;)if(n.isInThinkTag){let r=n.contentBuffer.indexOf("</think>");if(r===-1){n.contentBuffer.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:n.contentBuffer}),n.contentBuffer="";break}if(r>0){let o=n.contentBuffer.substring(0,r);o.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:o});}t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false,n.isInThinkTag=false,n.contentBuffer=n.contentBuffer.substring(r+8);}else {let r=n.contentBuffer.indexOf("<think>");if(r===-1){n.contentBuffer.length>0&&(n.textStarted||(n.textStarted=true,t.enqueue({type:"text-start",id:"txt-0"})),t.enqueue({type:"text-delta",id:"txt-0",delta:n.contentBuffer})),n.contentBuffer="";break}if(r>0){let o=n.contentBuffer.substring(0,r);n.textStarted||(n.textStarted=true,t.enqueue({type:"text-start",id:"txt-0"})),o.length>0&&t.enqueue({type:"text-delta",id:"txt-0",delta:o}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false;}n.isInThinkTag=true,n.contentBuffer=n.contentBuffer.substring(r+7),n.reasoningStarted||(n.reasoningStarted=true,t.enqueue({type:"reasoning-start",id:"reasoning-0"}));}return n}static finishThinkParsing(e,t){let n={...e};return n.contentBuffer.length>0?n.isInThinkTag?(n.contentBuffer.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:n.contentBuffer}),n.reasoningStarted&&(t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false)):(n.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),n.textStarted=true),n.contentBuffer.length>0&&t.enqueue({type:"text-delta",id:"txt-0",delta:n.contentBuffer}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false):(n.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false),n.reasoningStarted&&!n.isInThinkTag&&(t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false)),n}static removeThinkTags(e){return e.replace(/<think>[\s\S]*?<\/think>/g,"").trim()}};function ie(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var C=class f{static extractToolCalls(e){let t=[],n=e,r=e.split(`
|
|
111
|
-
`),o=[];for(let a of r){let g=a.trim();if(!g){o.push(a);continue}try{let i=JSON.parse(g);if(i.name&&typeof i.name=="string"&&i.parameters&&typeof i.parameters=="object"){t.push({name:i.name,parameters:JSON.stringify(i.parameters)});continue}}catch{}o.push(a);}return n=o.join(`
|
|
112
|
-
`).trim(),{toolCalls:t,remainingText:n}}static processToolCalls(e,t,n){let r={...e},o=r.contentBuffer.split(`
|
|
113
|
-
`),a=[];for(let g=0;g<o.length;g++){let i=o[g];if(!i)continue;let m=i.trim();if(!m){a.push(i);continue}let u=false;try{let p=JSON.parse(m);if(p.name&&typeof p.name=="string"&&p.parameters&&typeof p.parameters=="object"){if(n&&n.length>0&&!n.some(w=>w.name===p.name)){a.push(i);continue}if(u=!0,a.length>0){let h=a.join(`
|
|
114
|
-
`);h.trim()&&(r.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),r.textStarted=!0),t.enqueue({type:"text-delta",id:"txt-0",delta:h+`
|
|
115
|
-
`})),a.length=0;}r.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),r.textStarted=!1);let d=ie(),l=JSON.stringify(p.parameters);t.enqueue({type:"tool-input-start",id:d,toolName:p.name}),t.enqueue({type:"tool-input-delta",id:d,delta:l}),t.enqueue({type:"tool-input-end",id:d}),t.enqueue({type:"tool-call",toolCallId:d,toolName:p.name,input:l}),r.completedToolCalls.push({id:d,name:p.name,parameters:l});}}catch{}u||a.push(i);}return r.contentBuffer=a.join(`
|
|
116
|
-
`),r}static finishToolParsing(e,t){let n={...e};return n.contentBuffer.trim().length>0?(n.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),n.textStarted=true),t.enqueue({type:"text-delta",id:"txt-0",delta:n.contentBuffer}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false):n.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false),n}static removeToolCalls(e){let{remainingText:t}=f.extractToolCalls(e);return t}};function X(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var U=class{constructor(e,t,n){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"defaultObjectGenerationMode","json");c(this,"supportedUrls",{});c(this,"supportsToolCalling","experimental");c(this,"supportsThinking","experimental");c(this,"engine");c(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=L[t],!this.modelConfig)throw new s(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async generate(e){return this.doGenerate(e)}async stream(e){return this.doStream(e)}async doGenerate(e){E.validateMessages(e.prompt);let t=e.tools?.filter(d=>"inputSchema"in d),n=_.buildEnhancedPrompt(e.prompt,t),r={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};E.validateGenerateOptions(r),this.engine.isReady()||await this.engine.initialize();let o=await this.engine.generate(n,r),a=O.removeThinkTags(o.content||""),{toolCalls:g,remainingText:i}=C.extractToolCalls(a),m=[];i.trim().length>0&&m.push({type:"text",text:i});for(let d of g)m.push({type:"tool-call",toolCallId:X(),toolName:d.name,input:d.parameters});let u=g.length>0?"tool-calls":o.finishReason,p=[];return {content:m,finishReason:u,usage:{inputTokens:{total:o.usage.promptTokens,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:o.usage.completionTokens,text:void 0,reasoning:void 0}},warnings:p,rawCall:{rawPrompt:n,rawSettings:r}}}async doStream(e){let t=X();E.validateMessages(e.prompt);let n=e.tools?.filter(l=>"inputSchema"in l),r=_.buildEnhancedPrompt(e.prompt,n),o={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};E.validateGenerateOptions(o),this.engine.isReady()||await this.engine.initialize();let a=null,g=null,i=l=>{a&&a.enqueue(l),g&&g.enqueue(l);},m=async()=>{try{let l={isInThinkTag:!1,reasoningStarted:!1,textStarted:!1,contentBuffer:""},h={contentBuffer:"",textStarted:!1,completedToolCalls:[]};i({type:"response-metadata",id:t,modelId:this.modelId,timestamp:new Date}),await this.engine.generateStream(r,o,w=>{if(w.type==="text-delta"){let x=w.delta||"";if(x.length===0)return;l.contentBuffer+=x;let R=O.processThinkTags(l,{enqueue:i});l.isInThinkTag=R.isInThinkTag,l.reasoningStarted=R.reasoningStarted,l.textStarted=R.textStarted,h.contentBuffer+=R.contentBuffer,l.contentBuffer="";let N=C.processToolCalls(h,{enqueue:i},n);h.contentBuffer=N.contentBuffer,h.textStarted=N.textStarted,h.completedToolCalls=N.completedToolCalls;}else if(w.type==="text-end"){let x=O.finishThinkParsing(l,{enqueue:i});l.textStarted=x.textStarted,l.reasoningStarted=x.reasoningStarted;let R=C.finishToolParsing(h,{enqueue:i});h.textStarted=R.textStarted;}else if(w.type==="finish"){let x=O.finishThinkParsing(l,{enqueue:i});l.textStarted=x.textStarted,l.reasoningStarted=x.reasoningStarted;let R=C.finishToolParsing(h,{enqueue:i});h.textStarted=R.textStarted;let N=h.completedToolCalls.length>0;i({type:"finish",finishReason:N?"tool-calls":w.finishReason,usage:w.usage?{inputTokens:{total:w.usage.promptTokens||0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:w.usage.completionTokens||0,text:void 0,reasoning:void 0}}:{inputTokens:{total:0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:0,text:void 0,reasoning:void 0}}});}});}catch(l){i({type:"error",error:l instanceof Error?l.message:String(l)}),i({type:"finish",finishReason:"error",usage:{inputTokens:{total:0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:0,text:void 0,reasoning:void 0}}});}finally{a&&a.close(),g&&g.close();}},u=new ReadableStream({start(l){a=l;}}),p=new ReadableStream({start(l){g=l;}});m();async function*d(l){let h=l.getReader();try{for(;;){let{done:w,value:x}=await h.read();if(w)break;yield x;}}finally{h.releaseLock();}}return {stream:u,fullStream:d(p),request:{body:r},response:{headers:{}}}}};var z=class{constructor(e){c(this,"config");this.config=e;}createBatches(e){let t=[],n=this.config.batchSize;for(let r=0;r<e.length;r+=n)t.push(e.slice(r,r+n));return t}async processBatches(e,t,n){if(n?.aborted)throw new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");let r=this.createBatches(e),o=[];for(let a=0;a<r.length;a++){if(n?.aborted)throw new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");let g=r[a];if(g){try{let i=await t(g);o.push(...i);}catch(i){throw i instanceof s&&i.code==="ABORTED"?i:new s(`\u6279\u6B21 ${a+1}/${r.length} \u5904\u7406\u5931\u8D25: ${i instanceof Error?i.message:String(i)}`,"PROCESSING_ERROR",i instanceof Error?i:void 0)}a<r.length-1&&this.config.delayBetweenBatches&&this.config.delayBetweenBatches>0&&await this.delay(this.config.delayBetweenBatches,n);}}return o}async delay(e,t){return new Promise((n,r)=>{if(t?.aborted){r(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let o=setTimeout(()=>{t?.removeEventListener("abort",a),n();},e),a=()=>{clearTimeout(o),r(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));};t?.addEventListener("abort",a);})}};var $=class{constructor(e,t,n){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"maxEmbeddingsPerCall");c(this,"supportsParallelCalls",false);c(this,"engine");c(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=P[t],!this.modelConfig)throw new s(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND");this.maxEmbeddingsPerCall=this.modelConfig.maxEmbeddingsPerCall;}async doEmbed(e){let{values:t,abortSignal:n}=e;if(E.validateTexts(t),n?.aborted)throw new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");this.engine.isReady()||await this.engine.initialize();let o=await new z({batchSize:this.maxEmbeddingsPerCall,delayBetweenBatches:0}).processBatches(t,async g=>{if(n?.aborted)throw new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");return await this.engine.embed(g,n)},n),a=await this.engine.countTokens(t);return {embeddings:o,usage:{tokens:a},warnings:[]}}};var q=class{constructor(e,t,n,r={}){c(this,"specificationVersion","v3");c(this,"provider","local");c(this,"modelId");c(this,"languageModel");c(this,"options");c(this,"log",utils.Log.create({service:"local-model-sdk",module:"reranking-model"}));this.modelId=e,this.languageModel=new v("local",e,n),this.options={llm:this.languageModel,totalScore:r.totalScore??1,useEnhancedPrompt:true,promptLanguage:r.promptLanguage??"en"},this.log.debug("LocalRerankingModel created",{modelId:e,totalScore:this.options.totalScore,useEnhancedPrompt:this.options.useEnhancedPrompt,promptLanguage:this.options.promptLanguage});}async doRerank(e){let{query:t,documents:n,topN:r}=e,o;Array.isArray(n)?o=n:n.type==="text"?o=n.values.map((i,m)=>({content:i,id:`doc_${m+1}`})):o=n.values;let a=r??o.length;if(o.length===0)return {ranking:[],warnings:[]};try{let i=this.buildRerankPrompt(t,o),u=(await this.languageModel.doGenerate({prompt:[{role:"user",content:[{type:"text",text:i}]}],maxOutputTokens:2048,temperature:0,topP:1,topK:1})).content.find(d=>d.type==="text")?.text||"",p=this.parseRerankResult(u,o,a);return {ranking:p.results.map(d=>({index:d.index,relevanceScore:d.score,document:d.document})),warnings:p.warnings.map(d=>({type:"other",message:d})),response:{timestamp:new Date,modelId:this.modelId}}}catch(i){return this.log.error("Reranking failed",{error:i.message}),{ranking:o.map((u,p)=>({index:p,relevanceScore:this.options.totalScore*(1-p/o.length),document:u})),warnings:[{type:"other",message:`Reranking failed, using original order: ${i.message}`}],response:{timestamp:new Date,modelId:this.modelId}}}}buildRerankPrompt(e,t){let n=t.map((r,o)=>{let a=r.id||`doc_${o+1}`,g=r.content.slice(0,300);return `${o+1}. [ID: ${a}] ${g}`}).join(`
|
|
110
|
+
${t}<|im_end|>`;default:{let n=e.role;throw new u(`\u4E0D\u652F\u6301\u7684\u6D88\u606F\u89D2\u8272: ${n}`,"UNSUPPORTED_FEATURE")}}}};var x=class{static processThinkTags(e,t){let n={...e};for(;n.contentBuffer.length>0;)if(n.isInThinkTag){let r=n.contentBuffer.indexOf("</think>");if(r===-1){n.contentBuffer.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:n.contentBuffer}),n.contentBuffer="";break}if(r>0){let a=n.contentBuffer.substring(0,r);a.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:a});}t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false,n.isInThinkTag=false,n.contentBuffer=n.contentBuffer.substring(r+8);}else {let r=n.contentBuffer.indexOf("<think>");if(r===-1)break;if(r>0){let a=n.contentBuffer.substring(0,r);n.textStarted||(n.textStarted=true,t.enqueue({type:"text-start",id:"txt-0"})),a.length>0&&t.enqueue({type:"text-delta",id:"txt-0",delta:a}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false;}n.isInThinkTag=true,n.contentBuffer=n.contentBuffer.substring(r+7),n.reasoningStarted||(n.reasoningStarted=true,t.enqueue({type:"reasoning-start",id:"reasoning-0"}));}return n}static finishThinkParsing(e,t){let n={...e};return n.contentBuffer.length>0?n.isInThinkTag?(n.contentBuffer.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:n.contentBuffer}),n.reasoningStarted&&(t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false)):(n.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),n.textStarted=true),n.contentBuffer.length>0&&t.enqueue({type:"text-delta",id:"txt-0",delta:n.contentBuffer}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false):(n.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false),n.reasoningStarted&&!n.isInThinkTag&&(t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false)),n}static removeThinkTags(e){return e.replace(/<think>[\s\S]*?<\/think>/g,"").trim()}};function ae(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var W=/\{\s*"name"\s*:\s*"([^"]+)"\s*,\s*"parameters"\s*:\s*(\{[\s\S]*?\})\s*\}/g;function J(p){let e=[];W.lastIndex=0;let t;for(;(t=W.exec(p))!==null;){let n=t[1]||"",r=t[2]||"";try{let a=JSON.parse(r);a&&typeof a=="object"&&!Array.isArray(a)&&e.push({name:n,parameters:JSON.stringify(a),index:t.index,length:t[0].length});}catch{}}return {toolCalls:e}}var R=class p{static extractToolCalls(e){let t=J(e),n=[],r=[],a=0;for(let o of t.toolCalls)o.index>a&&r.push(e.substring(a,o.index)),n.push({name:o.name,parameters:o.parameters}),a=o.index+o.length;return a<e.length&&r.push(e.substring(a)),{toolCalls:n,remainingText:r.join("").trim()}}static processToolCalls(e,t,n){let r={...e},a=J(r.contentBuffer);if(a.toolCalls.length===0)return r;let o=[],l=0;for(let i of a.toolCalls){let d=r.contentBuffer.substring(l,i.index);if(d.length>0){o.length=0,o.push(d);let f=d;f.trim()&&(r.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),r.textStarted=true),t.enqueue({type:"text-delta",id:"txt-0",delta:f}),true);}if(n&&n.length>0&&!n.some(c=>c.name===i.name)){o.push(r.contentBuffer.substring(i.index,i.index+i.length)),l=i.index+i.length;continue}r.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),r.textStarted=false);let m=ae();t.enqueue({type:"tool-input-start",id:m,toolName:i.name}),t.enqueue({type:"tool-input-delta",id:m,delta:i.parameters}),t.enqueue({type:"tool-input-end",id:m}),t.enqueue({type:"tool-call",toolCallId:m,toolName:i.name,input:i.parameters}),r.completedToolCalls.push({id:m,name:i.name,parameters:i.parameters}),l=i.index+i.length;}return l<r.contentBuffer.length&&o.push(r.contentBuffer.substring(l)),r.contentBuffer=o.join(""),r}static finishToolParsing(e,t){let n={...e};return n.contentBuffer.trim().length>0?(n.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),n.textStarted=true),t.enqueue({type:"text-delta",id:"txt-0",delta:n.contentBuffer}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false):n.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false),n}static removeToolCalls(e){let{remainingText:t}=p.extractToolCalls(e);return t}};function Y(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var A=class{constructor(e,t,n){g(this,"specificationVersion","v3");g(this,"provider");g(this,"modelId");g(this,"defaultObjectGenerationMode","json");g(this,"supportedUrls",{});g(this,"engine");g(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=L[t],!this.modelConfig)throw new u(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async doGenerate(e){E.validateMessages(e.prompt);let t=(e.tools??[]).filter(m=>"inputSchema"in m),n=N.buildEnhancedPrompt(e.prompt,t),r={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxOutputTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};E.validateGenerateOptions(r),this.engine.isReady()||await this.engine.initialize();let a=await this.engine.generate(n,r),o=x.removeThinkTags(a.content||""),{toolCalls:l,remainingText:s}=R.extractToolCalls(o),i=[];s.trim().length>0&&i.push({type:"text",text:s});for(let m of l)i.push({type:"tool-call",toolCallId:Y(),toolName:m.name,input:m.parameters});let d=l.length>0?{unified:"tool-calls",raw:void 0}:a.finishReason;return {content:i,finishReason:d,usage:a.usage,warnings:[],request:{body:n}}}async doStream(e){E.validateMessages(e.prompt);let t=(e.tools??[]).filter(l=>"inputSchema"in l),n=N.buildEnhancedPrompt(e.prompt,t),r={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxOutputTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};E.validateGenerateOptions(r),this.engine.isReady()||await this.engine.initialize();let a=[];return {stream:C.createReadableStream(async l=>{l.enqueue({type:"stream-start",warnings:a});let s=Y();l.enqueue({type:"response-metadata",id:s,modelId:this.modelId,timestamp:new Date});let i={isInThinkTag:false,reasoningStarted:false,textStarted:false,contentBuffer:""},d={contentBuffer:"",textStarted:false,completedToolCalls:[]},m=false;try{await this.engine.generateStream(n,r,f=>{if(f.type==="text-delta"){let c=f.delta||"";if(c.length===0)return;i.contentBuffer+=c;let S=x.processThinkTags(i,l);i.isInThinkTag=S.isInThinkTag,i.reasoningStarted=S.reasoningStarted,i.textStarted=S.textStarted,d.contentBuffer+=S.contentBuffer,i.contentBuffer="";let v=R.processToolCalls(d,l,t);d.contentBuffer=v.contentBuffer,d.textStarted=v.textStarted,d.completedToolCalls=v.completedToolCalls;}else if(f.type==="text-end"){let c=x.finishThinkParsing(i,l);i.textStarted=c.textStarted,i.reasoningStarted=c.reasoningStarted;let S=R.finishToolParsing(d,l);d.textStarted=S.textStarted,m=!0;}else if(f.type==="finish"){if(!m){let v=x.finishThinkParsing(i,l);i.textStarted=v.textStarted,i.reasoningStarted=v.reasoningStarted;let Q=R.finishToolParsing(d,l);d.textStarted=Q.textStarted,m=!0;}let S=d.completedToolCalls.length>0?{unified:"tool-calls",raw:void 0}:f.finishReason??{unified:"stop",raw:void 0};l.enqueue({type:"finish",finishReason:S,usage:f.usage??{inputTokens:{total:void 0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:void 0,text:void 0,reasoning:void 0}}});}});}catch(f){if(l.enqueue({type:"error",error:f instanceof Error?f:new Error(String(f))}),!m){let c=x.finishThinkParsing(i,l);i.textStarted=c.textStarted,i.reasoningStarted=c.reasoningStarted;let S=R.finishToolParsing(d,l);d.textStarted=S.textStarted;}l.enqueue({type:"finish",finishReason:{unified:"error",raw:void 0},usage:{inputTokens:{total:0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:0,text:void 0,reasoning:void 0}}});}},e.abortSignal),request:{body:n}}}};var $=class{constructor(e){g(this,"config");this.config=e;}createBatches(e){let t=[],n=this.config.batchSize;for(let r=0;r<e.length;r+=n)t.push(e.slice(r,r+n));return t}async processBatches(e,t,n){if(n?.aborted)throw new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");let r=this.createBatches(e),a=[];for(let o=0;o<r.length;o++){if(n?.aborted)throw new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");let l=r[o];if(l){try{let s=await t(l);a.push(...s);}catch(s){throw s instanceof u&&s.code==="ABORTED"?s:new u(`\u6279\u6B21 ${o+1}/${r.length} \u5904\u7406\u5931\u8D25: ${s instanceof Error?s.message:String(s)}`,"PROCESSING_ERROR",s instanceof Error?s:void 0)}o<r.length-1&&this.config.delayBetweenBatches&&this.config.delayBetweenBatches>0&&await this.delay(this.config.delayBetweenBatches,n);}}return a}async delay(e,t){return new Promise((n,r)=>{if(t?.aborted){r(new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let a=setTimeout(()=>{t?.removeEventListener("abort",o),n();},e),o=()=>{clearTimeout(a),r(new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));};t?.addEventListener("abort",o);})}};var _=class{constructor(e,t,n){g(this,"specificationVersion","v3");g(this,"provider");g(this,"modelId");g(this,"maxEmbeddingsPerCall");g(this,"supportsParallelCalls",false);g(this,"engine");g(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=k[t],!this.modelConfig)throw new u(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND");this.maxEmbeddingsPerCall=this.modelConfig.maxEmbeddingsPerCall;}async doEmbed(e){let{values:t,abortSignal:n}=e;if(E.validateTexts(t),n?.aborted)throw new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");this.engine.isReady()||await this.engine.initialize();let a=await new $({batchSize:this.maxEmbeddingsPerCall,delayBetweenBatches:0}).processBatches(t,async l=>{if(n?.aborted)throw new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");return await this.engine.embed(l,n)},n),o=await this.engine.countTokens(t);return {embeddings:a,usage:{tokens:o},warnings:[]}}};var U=class{constructor(e,t,n,r={}){g(this,"specificationVersion","v3");g(this,"provider","local");g(this,"modelId");g(this,"languageModel");g(this,"options");g(this,"log",utils.Log.create({service:"local-model-sdk",module:"reranking-model"}));this.modelId=e,this.languageModel=new O("local",e,n),this.options={llm:this.languageModel,totalScore:r.totalScore??1,useEnhancedPrompt:true,promptLanguage:r.promptLanguage??"en"},this.log.debug("LocalRerankingModel created",{modelId:e,totalScore:this.options.totalScore,useEnhancedPrompt:this.options.useEnhancedPrompt,promptLanguage:this.options.promptLanguage});}async doRerank(e){let{query:t,documents:n,topN:r,abortSignal:a}=e,o=n.type==="text"?n.values.map((s,i)=>({content:s,id:`doc_${i+1}`})):n.values.map((s,i)=>{let d=s;return {content:typeof d=="string"?d:JSON.stringify(d),id:typeof d?.id=="string"?d.id:`doc_${i+1}`}}),l=r??o.length;if(o.length===0)return {ranking:[],warnings:[]};try{let s=this.buildRerankPrompt(t,o),d=(await this.languageModel.doGenerate({prompt:[{role:"user",content:[{type:"text",text:s}]}],maxOutputTokens:2048,temperature:0,topP:1,topK:1,abortSignal:a})).content.find(c=>c.type==="text"),m=d&&d.type==="text"?d.text:"",f=this.parseRerankResult(m,o,l);return {ranking:f.results.map(c=>({index:c.index,relevanceScore:c.score})),warnings:f.warnings.map(c=>({type:"other",message:c})),response:{timestamp:new Date,modelId:this.modelId}}}catch(s){return this.log.error("Reranking failed",{error:s.message}),{ranking:o.map((d,m)=>({index:m,relevanceScore:o.length>1?this.options.totalScore*(1-.9*m/(o.length-1)):this.options.totalScore})),warnings:[{type:"other",message:`Reranking failed, using original order: ${s.message}`}],response:{timestamp:new Date,modelId:this.modelId}}}}buildRerankPrompt(e,t){let n=t.map((r,a)=>{let o=r.id||`doc_${a+1}`,l=r.content.slice(0,300);return `${a+1}. [ID: ${o}] ${l}`}).join(`
|
|
117
111
|
|
|
118
|
-
`);if(this.options.useEnhancedPrompt)if(this.options.promptLanguage==="en"){
|
|
112
|
+
`);if(this.options.useEnhancedPrompt)if(this.options.promptLanguage==="en"){this.options.totalScore===1?"1.0":String(this.options.totalScore);return ["You are a professional information retrieval reranking expert. Please rank the following documents according to the query.","",`User query: ${e}`,"","Documents to rank:",n,"","Please rank the documents according to the following requirements:","1. Carefully analyze the user's query intent and key information needs","2. Evaluate each document's relevance to the query","3. Consider the document's completeness, accuracy, and usefulness","4. Return ONLY the document numbers (1, 2, 3, ...) in order from MOST relevant to LEAST relevant, separated by commas","5. Most relevant documents MUST be at the front","6. Every document must appear exactly once in your output","","IMPORTANT:","- You MUST actually reorder the documents based on relevance to the query","- Do NOT output the original order unless it happens to be the correct relevance order","- Output format: just the numbers separated by commas, nothing else","","Example output format: 2,5,1,4,3 (where document 2 is most relevant to the query)"].join(`
|
|
113
|
+
`)}else {this.options.totalScore===1?"1.0":String(this.options.totalScore);return ["\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u7684\u4FE1\u606F\u68C0\u7D22\u91CD\u6392\u5E8F\u4E13\u5BB6\u3002\u8BF7\u6839\u636E\u7528\u6237\u67E5\u8BE2\u5BF9\u6587\u6863\u8FDB\u884C\u76F8\u5173\u6027\u6392\u5E8F\u3002","",`\u7528\u6237\u67E5\u8BE2\uFF1A${e}`,"","\u5F85\u6392\u5E8F\u6587\u6863\uFF1A",n,"","\u8BF7\u6309\u7167\u4EE5\u4E0B\u8981\u6C42\u8FDB\u884C\u6392\u5E8F\uFF1A","1. \u4ED4\u7EC6\u5206\u6790\u7528\u6237\u67E5\u8BE2\u7684\u610F\u56FE\u548C\u5173\u952E\u4FE1\u606F\u9700\u6C42","2. \u8BC4\u4F30\u6BCF\u4E2A\u6587\u6863\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027","3. \u8003\u8651\u6587\u6863\u7684\u5B8C\u6574\u6027\u3001\u51C6\u786E\u6027\u548C\u5B9E\u7528\u6027","4. \u53EA\u8F93\u51FA\u6587\u6863\u7F16\u53F7\uFF081, 2, 3, ...\uFF09\uFF0C\u6309\u201C\u6700\u76F8\u5173\u201D\u5230\u201C\u6700\u4E0D\u76F8\u5173\u201D\u987A\u5E8F\u6392\u5217\uFF0C\u7528\u9017\u53F7\u5206\u9694","5. \u6700\u76F8\u5173\u7684\u6587\u6863\u5FC5\u987B\u6392\u5728\u6700\u524D\u9762","6. \u6BCF\u4E2A\u6587\u6863\u5FC5\u987B\u51FA\u73B0\u4E14\u4EC5\u51FA\u73B0\u4E00\u6B21","","\u91CD\u8981\u63D0\u793A\uFF1A","- \u5FC5\u987B\u6839\u636E\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027\u5B9E\u9645\u91CD\u65B0\u6392\u5E8F\u6587\u6863","- \u9664\u975E\u539F\u59CB\u987A\u5E8F\u6070\u597D\u5C31\u662F\u6B63\u786E\u7684\u76F8\u5173\u6027\u6392\u5E8F\uFF0C\u5426\u5219\u4E0D\u8981\u8F93\u51FA\u539F\u59CB\u987A\u5E8F","- \u8F93\u51FA\u683C\u5F0F\uFF1A\u4EC5\u5305\u542B\u7528\u9017\u53F7\u5206\u9694\u7684\u7F16\u53F7\uFF0C\u4E0D\u8981\u6709\u5176\u4ED6\u5185\u5BB9","","\u793A\u4F8B\u8F93\u51FA\u683C\u5F0F\uFF1A2,5,1,4,3\uFF08\u5176\u4E2D\u6587\u6863 2 \u4E0E\u67E5\u8BE2\u6700\u76F8\u5173\uFF09"].join(`
|
|
114
|
+
`)}else return [`\u6839\u636E\u67E5\u8BE2 "${e}" \u5BF9\u4EE5\u4E0B\u7ED3\u679C\u76F8\u5173\u6027\u8FDB\u884C\u6392\u5E8F\uFF0C\u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF08\u7528\u9017\u53F7\u5206\u9694\uFF09\uFF1A`,"",n,"","\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u6700\u524D\u9762\uFF09"].join(`
|
|
115
|
+
`)}parseRerankResult(e,t,n){let r=[];try{let o=e.replaceAll(/<thinking>[\s\S]*?<\/thinking>/gi,"").replaceAll(/<think>[\s\S]*?<\/think>/gi,"").replaceAll(/<answer>[\s\S]*?<\/answer>/gi,"").trim().match(/\d+/g)?.map(c=>Number.parseInt(c,10)-1).filter(c=>c>=0&&c<t.length)||[],l=[],s=new Set;for(let c of o)s.has(c)||(s.add(c),l.push(c));let i=[],d=new Set;for(let c of l)c>=0&&c<t.length&&!d.has(c)&&(d.add(c),i.push({index:c,score:l.length>1?this.options.totalScore*(1-.9*i.length/(l.length-1)):this.options.totalScore}));let m=new Set(l);for(let c=0;c<t.length;c++)if(!m.has(c)){let S=this.options.totalScore*.1;i.push({index:c,score:S});}let f=n?i.slice(0,n):i;return this.log.debug("Parsed rerank results",{extractedNumbers:o,uniqueIndices:l,resultCount:f.length,topK:n}),{results:f,warnings:r}}catch(a){return this.log.error("Failed to parse rerank result",{error:a.message,text:e}),{results:t.map((l,s)=>({index:s,score:t.length>1?this.options.totalScore*(1-.9*s/(t.length-1)):this.options.totalScore})),warnings:[`Failed to parse rerank result: ${a.message}`]}}}};var q=class{constructor(e,t,n={}){g(this,"specificationVersion","v3");g(this,"provider");g(this,"modelId");g(this,"languageModel");g(this,"options");g(this,"log",utils.Log.create({service:"local-model-sdk",module:"llm-reranking-model"}));this.modelId=e,this.languageModel=t,this.provider=t.provider||"external",this.options={llm:t,totalScore:n.totalScore??1,useEnhancedPrompt:n.useEnhancedPrompt??false,promptLanguage:n.promptLanguage??"en",modelOptions:n.modelOptions??{temperature:.1,maxTokens:3e4}},this.log.debug("LLMRerankingModel created",{modelId:e,provider:this.provider,totalScore:this.options.totalScore,useEnhancedPrompt:this.options.useEnhancedPrompt,promptLanguage:this.options.promptLanguage});}async doRerank(e){let{query:t,documents:n,topN:r,abortSignal:a}=e,o=n.type==="text"?n.values.map((s,i)=>({content:s,id:`doc_${i+1}`})):n.values.map((s,i)=>{let d=s;return {content:typeof d=="string"?d:JSON.stringify(d),id:typeof d?.id=="string"?d.id:`doc_${i+1}`}}),l=r??o.length;if(o.length===0)return {ranking:[],warnings:[]};try{let s=this.buildRerankPrompt(t,o),i="",d=null;for(let f=0;f<2;f++)try{if(i=await(await ai.streamText({model:this.languageModel,prompt:s,maxOutputTokens:this.options.modelOptions.maxTokens,temperature:this.options.modelOptions.temperature,abortSignal:a})).text,this.log.debug("LLM raw output",{textContent:i,textLength:i?.length,attempt:f}),i&&i.trim().length>0)break;d=new Error("LLM returned empty response");}catch(c){d=c,this.log.warn(`LLM attempt ${f+1} failed: ${d.message}`);}if(!i||i.trim().length===0)throw this.log.warn("LLM returned empty response, using fallback"),d||new Error("LLM returned empty response");let m=this.parseRerankResult(i,o,l);return {ranking:m.results.map(f=>({index:f.index,relevanceScore:f.score})),warnings:m.warnings.map(f=>({type:"other",message:f})),response:{timestamp:new Date,modelId:this.modelId}}}catch(s){return this.log.error("Reranking failed",{error:s.message}),{ranking:o.map((d,m)=>({index:m,relevanceScore:o.length>1?this.options.totalScore*(1-.9*m/(o.length-1)):this.options.totalScore})),warnings:[{type:"other",message:`Reranking failed, using original order: ${s.message}`}],response:{timestamp:new Date,modelId:this.modelId}}}}buildRerankPrompt(e,t){let n=t.map((r,a)=>{let o=r.id||`doc_${a+1}`,l=r.content.slice(0,300);return `${a+1}. [ID: ${o}] ${l}`}).join(`
|
|
119
116
|
|
|
120
|
-
User query: ${e}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
${n}
|
|
124
|
-
|
|
125
|
-
Please rank the documents according to the following requirements:
|
|
126
|
-
1. Carefully analyze the user's query intent and key information needs
|
|
127
|
-
2. Evaluate each document's relevance to the query
|
|
128
|
-
3. Consider the document's completeness, accuracy, and usefulness
|
|
129
|
-
4. Return the ranked document numbers separated by commas
|
|
130
|
-
5. Most relevant documents should be at the front
|
|
131
|
-
6. Use a scoring scale from ${r}
|
|
132
|
-
|
|
133
|
-
Example output format: 2,5,1,4,3
|
|
134
|
-
|
|
135
|
-
Please only output the ranked document numbers separated by commas, do not include the actual scores.`}else {let r=this.options.totalScore===1?"1.0":`${this.options.totalScore}`;return `\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u7684\u4FE1\u606F\u68C0\u7D22\u91CD\u6392\u5E8F\u4E13\u5BB6\u3002\u8BF7\u6839\u636E\u7528\u6237\u67E5\u8BE2\u5BF9\u6587\u6863\u8FDB\u884C\u76F8\u5173\u6027\u6392\u5E8F\u3002
|
|
136
|
-
|
|
137
|
-
\u7528\u6237\u67E5\u8BE2\uFF1A${e}
|
|
138
|
-
|
|
139
|
-
\u5F85\u6392\u5E8F\u6587\u6863\uFF1A
|
|
140
|
-
${n}
|
|
141
|
-
|
|
142
|
-
\u8BF7\u6309\u7167\u4EE5\u4E0B\u8981\u6C42\u8FDB\u884C\u6392\u5E8F\uFF1A
|
|
143
|
-
1. \u4ED4\u7EC6\u5206\u6790\u7528\u6237\u67E5\u8BE2\u7684\u610F\u56FE\u548C\u5173\u952E\u4FE1\u606F\u9700\u6C42
|
|
144
|
-
2. \u8BC4\u4F30\u6BCF\u4E2A\u6587\u6863\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027
|
|
145
|
-
3. \u8003\u8651\u6587\u6863\u7684\u5B8C\u6574\u6027\u3001\u51C6\u786E\u6027\u548C\u5B9E\u7528\u6027
|
|
146
|
-
4. \u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u6587\u6863\u7F16\u53F7\uFF0C\u7528\u9017\u53F7\u5206\u9694
|
|
147
|
-
5. \u6700\u76F8\u5173\u7684\u6587\u6863\u6392\u5728\u6700\u524D\u9762
|
|
148
|
-
6. \u8BF7\u4F7F\u7528 ${r} \u5206\u503C\u8FDB\u884C\u8BC4\u5206
|
|
149
|
-
|
|
150
|
-
\u793A\u4F8B\u8F93\u51FA\u683C\u5F0F\uFF1A2,5,1,4,3
|
|
151
|
-
|
|
152
|
-
\u8BF7\u53EA\u8F93\u51FA\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF0C\u4E0D\u8981\u5305\u542B\u5176\u4ED6\u89E3\u91CA\u3002`}else return `\u6839\u636E\u67E5\u8BE2 "${e}" \u5BF9\u4EE5\u4E0B\u7ED3\u679C\u76F8\u5173\u6027\u8FDB\u884C\u6392\u5E8F\uFF0C\u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF08\u7528\u9017\u53F7\u5206\u9694\uFF09\uFF1A
|
|
153
|
-
|
|
154
|
-
${n}
|
|
155
|
-
|
|
156
|
-
\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u524D\u9762\uFF09`}parseRerankResult(e,t,n){let r=[];try{let a=e.replaceAll(/<thinking>[\s\S]*?<\/thinking>/gi,"").replaceAll(/<think>[\s\S]*?<\/think>/gi,"").replaceAll(/<answer>[\s\S]*?<\/answer>/gi,"").trim().match(/\d+/g)?.map(l=>parseInt(l,10)-1).filter(l=>l>=0&&l<t.length)||[],g=[],i=new Set;for(let l of a)i.has(l)||(i.add(l),g.push(l));let m=[],u=new Set;for(let l of g)if(l!==void 0&&l>=0&&l<t.length&&!u.has(l)){u.add(l);let h=t[l];h&&m.push({index:l,score:this.options.totalScore*(1-m.length/g.length),document:h});}let p=new Set(g);for(let l=0;l<t.length;l++)if(!p.has(l)){let h=this.options.totalScore*.1*(1-l/t.length);m.push({index:l,score:h,document:t[l]});}let d=n?m.slice(0,n):m;return this.log.debug("Parsed rerank results",{extractedNumbers:a,uniqueIndices:g,resultCount:d.length,topK:n}),{results:d,warnings:r}}catch(o){return this.log.error("Failed to parse rerank result",{error:o.message,text:e}),{results:t.map((g,i)=>({index:i,score:this.options.totalScore*(1-i/t.length),document:g})),warnings:[`Failed to parse rerank result: ${o.message}`]}}}};var F=class{constructor(e,t,n={}){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"languageModel");c(this,"options");c(this,"log",utils.Log.create({service:"local-model-sdk",module:"llm-reranking-model"}));this.modelId=e,this.languageModel=t,this.provider=t.provider||"external",this.options={llm:t,totalScore:n.totalScore??1,useEnhancedPrompt:n.useEnhancedPrompt??false,promptLanguage:n.promptLanguage??"en",modelOptions:n.modelOptions??{temperature:.1,maxTokens:3e4}},this.log.debug("LLMRerankingModel created",{modelId:e,provider:this.provider,totalScore:this.options.totalScore,useEnhancedPrompt:this.options.useEnhancedPrompt,promptLanguage:this.options.promptLanguage});}async doRerank(e){let{query:t,documents:n,topN:r,abortSignal:o}=e,a;Array.isArray(n)?a=n:n.type==="text"?a=n.values.map((m,u)=>({content:m,id:`doc_${u+1}`})):a=n.values;let g=r??a.length;if(a.length===0)return {ranking:[],warnings:[]};try{let m=this.buildRerankPrompt(t,a),u="",p=null;for(let l=0;l<2;l++)try{if(u=await(await ai.streamText({model:this.languageModel,prompt:m,maxOutputTokens:this.options.modelOptions.maxTokens,temperature:this.options.modelOptions.temperature,abortSignal:o})).text,this.log.debug("LLM raw output",{textContent:u,textLength:u?.length,attempt:l}),u&&u.trim().length>0)break;p=new Error("LLM returned empty response");}catch(h){p=h,this.log.warn(`LLM attempt ${l+1} failed: ${p.message}`);}if(!u||u.trim().length===0)throw this.log.warn("LLM returned empty response, using fallback"),p||new Error("LLM returned empty response");let d=this.parseRerankResult(u,a,g);return {ranking:d.results.map(l=>({index:l.index,relevanceScore:l.score,document:l.document})),warnings:d.warnings.map(l=>({type:"other",message:l})),response:{timestamp:new Date,modelId:this.modelId}}}catch(m){return this.log.error("Reranking failed",{error:m.message}),{ranking:a.map((p,d)=>({index:d,relevanceScore:this.options.totalScore*(1-d/a.length),document:p})),warnings:[{type:"other",message:`Reranking failed, using original order: ${m.message}`}],response:{timestamp:new Date,modelId:this.modelId}}}}extractTextContent(e){if(typeof e=="string")return e;if(e.text!==void 0)return e.text;if(e.content&&Array.isArray(e.content)){let t=e.content.find(n=>n.type==="text");if(t?.text)return t.text}return String(e)}buildRerankPrompt(e,t){let n=t.map((r,o)=>{let a=r.id||`doc_${o+1}`,g=r.content.slice(0,300);return `${o+1}. [ID: ${a}] ${g}`}).join(`
|
|
157
|
-
|
|
158
|
-
`);if(this.options.useEnhancedPrompt)if(this.options.promptLanguage==="en"){let r=this.options.totalScore===1?"1.0":`${this.options.totalScore}`;return `You are a professional information retrieval reranking expert. Please rank the following documents according to the query.
|
|
159
|
-
|
|
160
|
-
User query: ${e}
|
|
161
|
-
|
|
162
|
-
Documents to rank:
|
|
163
|
-
${n}
|
|
164
|
-
|
|
165
|
-
Please rank the documents according to the following requirements:
|
|
166
|
-
1. Carefully analyze the user's query intent and key information needs
|
|
167
|
-
2. Evaluate each document's relevance to the query
|
|
168
|
-
3. Consider the document's completeness, accuracy, and usefulness
|
|
169
|
-
4. Return the ranked document numbers separated by commas
|
|
170
|
-
5. Most relevant documents should be at the front
|
|
171
|
-
6. Use a scoring scale from ${r}
|
|
172
|
-
|
|
173
|
-
Example output format: 2,5,1,4,3
|
|
174
|
-
|
|
175
|
-
Please only output the ranked document numbers separated by commas, do not include the actual scores.`}else {let r=this.options.totalScore===1?"1.0":`${this.options.totalScore}`;return `\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u7684\u4FE1\u606F\u68C0\u7D22\u91CD\u6392\u5E8F\u4E13\u5BB6\u3002\u8BF7\u6839\u636E\u7528\u6237\u67E5\u8BE2\u5BF9\u6587\u6863\u8FDB\u884C\u76F8\u5173\u6027\u6392\u5E8F\u3002
|
|
176
|
-
|
|
177
|
-
\u7528\u6237\u67E5\u8BE2\uFF1A${e}
|
|
178
|
-
|
|
179
|
-
\u5F85\u6392\u5E8F\u6587\u6863\uFF1A
|
|
180
|
-
${n}
|
|
181
|
-
|
|
182
|
-
\u8BF7\u6309\u7167\u4EE5\u4E0B\u8981\u6C42\u8FDB\u884C\u6392\u5E8F\uFF1A
|
|
183
|
-
1. \u4ED4\u7EC6\u5206\u6790\u7528\u6237\u67E5\u8BE2\u7684\u610F\u56FE\u548C\u5173\u952E\u4FE1\u606F\u9700\u6C42
|
|
184
|
-
2. \u8BC4\u4F30\u6BCF\u4E2A\u6587\u6863\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027
|
|
185
|
-
3. \u8003\u8651\u6587\u6863\u7684\u5B8C\u6574\u6027\u3001\u51C6\u786E\u6027\u548C\u5B9E\u7528\u6027
|
|
186
|
-
4. \u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u6587\u6863\u7F16\u53F7\uFF0C\u7528\u9017\u53F7\u5206\u9694
|
|
187
|
-
5. \u6700\u76F8\u5173\u7684\u6587\u6863\u6392\u5728\u6700\u524D\u9762
|
|
188
|
-
6. \u8BF7\u4F7F\u7528 ${r} \u5206\u503C\u8FDB\u884C\u8BC4\u5206
|
|
189
|
-
|
|
190
|
-
\u793A\u4F8B\u8F93\u51FA\u683C\u5F0F\uFF1A2,5,1,4,3
|
|
191
|
-
|
|
192
|
-
\u8BF7\u53EA\u8F93\u51FA\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF0C\u4E0D\u8981\u5305\u542B\u5176\u4ED6\u89E3\u91CA\u3002`}else return `\u6839\u636E\u67E5\u8BE2 "${e}" \u5BF9\u4EE5\u4E0B\u7ED3\u679C\u76F8\u5173\u6027\u8FDB\u884C\u6392\u5E8F\uFF0C\u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF08\u7528\u9017\u53F7\u5206\u9694\uFF09\uFF1A
|
|
193
|
-
|
|
194
|
-
${n}
|
|
195
|
-
|
|
196
|
-
\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u524D\u9762\uFF09`}parseRerankResult(e,t,n){let r=[];try{let o=e.match(/\d+/g)?.map(d=>parseInt(d,10)-1).filter(d=>d>=0&&d<t.length)||[],a=[],g=new Set;for(let d of o)g.has(d)||(g.add(d),a.push(d));let i=[],m=new Set;for(let d of a)if(d!==void 0&&d>=0&&d<t.length&&!m.has(d)){m.add(d);let l=t[d];l&&i.push({index:d,score:this.options.totalScore*(1-i.length/a.length),document:l});}let u=new Set(a);for(let d=0;d<t.length;d++)if(!u.has(d)){let l=this.options.totalScore*.1*(1-d/t.length);i.push({index:d,score:l,document:t[d]});}let p=n?i.slice(0,n):i;return this.log.debug("Parsed rerank results",{extractedNumbers:o,uniqueIndices:a,resultCount:p.length,topK:n}),{results:p,warnings:r}}catch(o){return this.log.error("Failed to parse rerank result",{error:o.message,text:e}),{results:t.map((g,i)=>({index:i,score:this.options.totalScore*(1-i/t.length),document:g})),warnings:[`Failed to parse rerank result: ${o.message}`]}}}};var k=utils.Log.create({service:"local-model-provider"}),ce={cacheDir:A__namespace.default.join(process.env.EASBOT_CACHE_PATH??`${A__namespace.default.join(H__namespace.homedir(),".cache","easbot")}`,"models"),verbose:false,autoUnloadTimeout:300*1e3,maxCachedModels:3,memoryWarningThreshold:1.5*1024*1024*1024,useEnhancedModel:true},K=class{constructor(e){c(this,"config");c(this,"modelCache");c(this,"engines",new Map);c(this,"modelInfo",new Map);this.config={...ce,...e},this.modelCache=new D({cacheDir:this.config.cacheDir});}languageModel(e,t){if(!L[e])throw new s(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return t?.useEnhancedModel??this.config.useEnhancedModel?(this.config.verbose&&k.debug("\u521B\u5EFA\u589E\u5F3A\u8BED\u8A00\u6A21\u578B",{modelId:e}),new U("local",e,n)):(this.config.verbose&&k.debug("\u521B\u5EFA\u57FA\u7840\u8BED\u8A00\u6A21\u578B",{modelId:e}),new v("local",e,n))}embeddingModel(e){if(!P[e])throw new s(`\u4E0D\u652F\u6301\u7684 Embedding Model ID: ${e}`,"MODEL_NOT_FOUND");let t=this.getOrCreateEngine(e,"embedding");return new $("local",e,t)}chat(e,t){return this.languageModel(e,t)}rerankingModel(e,t){if(this.config.verbose&&k.debug("\u521B\u5EFA\u91CD\u6392\u5E8F\u6A21\u578B",{modelId:e,hasExternalLlm:!!t?.llm}),t?.llm)return new F(e,t.llm,t);if(!L[e])throw new s(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return new q(e,this.config,n,t)}getOrCreateEngine(e,t){let n=`${t}:${e}`;if(this.engines.has(n)){let o=this.modelInfo.get(n);return o&&(o.lastUsed=Date.now(),o.useCount++),this.config.verbose&&k.debug("\u590D\u7528\u5DF2\u52A0\u8F7D\u7684\u6A21\u578B",{engineKey:n}),this.engines.get(n)}this.engines.size>=this.config.maxCachedModels&&this.evictLeastRecentlyUsed(),this.config.verbose&&k.debug("\u521B\u5EFA\u65B0\u5F15\u64CE",{engineKey:n});let r=new V({modelId:e,modelType:t,cacheDir:this.config.cacheDir,verbose:this.config.verbose});return this.engines.set(n,r),this.modelInfo.set(n,{modelId:e,modelType:t,isLoaded:false,lastUsed:Date.now(),useCount:1}),r}evictLeastRecentlyUsed(){let e=null,t=Number.POSITIVE_INFINITY;for(let[n,r]of this.modelInfo.entries())r.lastUsed<t&&(t=r.lastUsed,e=n);if(e){this.config.verbose&&k.debug("\u6DD8\u6C70\u6A21\u578B",{engineKey:e});let n=this.engines.get(e);n&&n.dispose().catch(r=>{k.error("\u5378\u8F7D\u6A21\u578B\u5931\u8D25",{error:r});}),this.engines.delete(e),this.modelInfo.delete(e);}}getLoadedModels(){return Array.from(this.modelInfo.values())}async dispose(){this.config.verbose&&k.debug("\u5378\u8F7D\u6240\u6709\u6A21\u578B");let e=Array.from(this.engines.values()).map(t=>t.dispose());await Promise.all(e),this.engines.clear(),this.modelInfo.clear(),this.config.verbose&&k.debug("\u6240\u6709\u6A21\u578B\u5DF2\u5378\u8F7D");}};function ue(f){let e=new K(f);return Object.assign((n,r)=>e.languageModel(n,r),{languageModel:(n,r)=>e.languageModel(n,r),chat:(n,r)=>e.chat(n,r),embeddingModel:n=>e.embeddingModel(n),rerankingModel:(n,r)=>e.rerankingModel(n,r)})}var me="0.1.0",pe="@easbot/local-model-sdk",fe="Complete local model SDK for EASBot - Transformers.js + ONNX implementation";exports.ErrorCode=M;exports.ErrorHandler=T;exports.LLMRerankingModel=F;exports.LocalEmbeddingModel=$;exports.LocalEnhancedLanguageModel=U;exports.LocalLanguageModel=v;exports.LocalModelError=s;exports.LocalModelProvider=K;exports.LocalRerankingModel=q;exports.SDK_DESCRIPTION=fe;exports.SDK_NAME=pe;exports.SUPPORTED_EMBEDDING_MODELS=P;exports.SUPPORTED_LANGUAGE_MODELS=L;exports.VERSION=me;exports.createLocal=ue;
|
|
117
|
+
`);if(this.options.useEnhancedPrompt)if(this.options.promptLanguage==="en"){this.options.totalScore===1?"1.0":String(this.options.totalScore);return ["You are a professional information retrieval reranking expert. Please rank the following documents according to the query.","",`User query: ${e}`,"","Documents to rank:",n,"","Please rank the documents according to the following requirements:","1. Carefully analyze the user's query intent and key information needs","2. Evaluate each document's relevance to the query","3. Consider the document's completeness, accuracy, and usefulness","4. Return ONLY the document numbers (1, 2, 3, ...) in order from MOST relevant to LEAST relevant, separated by commas","5. Most relevant documents MUST be at the front","6. Every document must appear exactly once in your output","","IMPORTANT:","- You MUST actually reorder the documents based on relevance to the query","- Do NOT output the original order unless it happens to be the correct relevance order","- Output format: just the numbers separated by commas, nothing else","","Example output format: 2,5,1,4,3 (where document 2 is most relevant to the query)"].join(`
|
|
118
|
+
`)}else {this.options.totalScore===1?"1.0":String(this.options.totalScore);return ["\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u7684\u4FE1\u606F\u68C0\u7D22\u91CD\u6392\u5E8F\u4E13\u5BB6\u3002\u8BF7\u6839\u636E\u7528\u6237\u67E5\u8BE2\u5BF9\u6587\u6863\u8FDB\u884C\u76F8\u5173\u6027\u6392\u5E8F\u3002","",`\u7528\u6237\u67E5\u8BE2\uFF1A${e}`,"","\u5F85\u6392\u5E8F\u6587\u6863\uFF1A",n,"","\u8BF7\u6309\u7167\u4EE5\u4E0B\u8981\u6C42\u8FDB\u884C\u6392\u5E8F\uFF1A","1. \u4ED4\u7EC6\u5206\u6790\u7528\u6237\u67E5\u8BE2\u7684\u610F\u56FE\u548C\u5173\u952E\u4FE1\u606F\u9700\u6C42","2. \u8BC4\u4F30\u6BCF\u4E2A\u6587\u6863\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027","3. \u8003\u8651\u6587\u6863\u7684\u5B8C\u6574\u6027\u3001\u51C6\u786E\u6027\u548C\u5B9E\u7528\u6027","4. \u53EA\u8F93\u51FA\u6587\u6863\u7F16\u53F7\uFF081, 2, 3, ...\uFF09\uFF0C\u6309\u201C\u6700\u76F8\u5173\u201D\u5230\u201C\u6700\u4E0D\u76F8\u5173\u201D\u987A\u5E8F\u6392\u5217\uFF0C\u7528\u9017\u53F7\u5206\u9694","5. \u6700\u76F8\u5173\u7684\u6587\u6863\u5FC5\u987B\u6392\u5728\u6700\u524D\u9762","6. \u6BCF\u4E2A\u6587\u6863\u5FC5\u987B\u51FA\u73B0\u4E14\u4EC5\u51FA\u73B0\u4E00\u6B21","","\u91CD\u8981\u63D0\u793A\uFF1A","- \u5FC5\u987B\u6839\u636E\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027\u5B9E\u9645\u91CD\u65B0\u6392\u5E8F\u6587\u6863","- \u9664\u975E\u539F\u59CB\u987A\u5E8F\u6070\u597D\u5C31\u662F\u6B63\u786E\u7684\u76F8\u5173\u6027\u6392\u5E8F\uFF0C\u5426\u5219\u4E0D\u8981\u8F93\u51FA\u539F\u59CB\u987A\u5E8F","- \u8F93\u51FA\u683C\u5F0F\uFF1A\u4EC5\u5305\u542B\u7528\u9017\u53F7\u5206\u9694\u7684\u7F16\u53F7\uFF0C\u4E0D\u8981\u6709\u5176\u4ED6\u5185\u5BB9","","\u793A\u4F8B\u8F93\u51FA\u683C\u5F0F\uFF1A2,5,1,4,3\uFF08\u5176\u4E2D\u6587\u6863 2 \u4E0E\u67E5\u8BE2\u6700\u76F8\u5173\uFF09"].join(`
|
|
119
|
+
`)}else return [`\u6839\u636E\u67E5\u8BE2 "${e}" \u5BF9\u4EE5\u4E0B\u7ED3\u679C\u76F8\u5173\u6027\u8FDB\u884C\u6392\u5E8F\uFF0C\u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF08\u7528\u9017\u53F7\u5206\u9694\uFF09\uFF1A`,"",n,"","\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u6700\u524D\u9762\uFF09"].join(`
|
|
120
|
+
`)}parseRerankResult(e,t,n){let r=[];try{let o=e.replaceAll(/<think>[\s\S]*?<\/think>/gi,"").replaceAll(/<thinking>[\s\S]*?<\/thinking>/gi,"").replaceAll(/<answer>[\s\S]*?<\/answer>/gi,"").trim().match(/\d+/g)?.map(c=>Number.parseInt(c,10)-1).filter(c=>c>=0&&c<t.length)||[],l=[],s=new Set;for(let c of o)s.has(c)||(s.add(c),l.push(c));let i=[],d=new Set;for(let c of l)c>=0&&c<t.length&&!d.has(c)&&(d.add(c),i.push({index:c,score:l.length>1?this.options.totalScore*(1-.9*i.length/(l.length-1)):this.options.totalScore}));let m=new Set(l);for(let c=0;c<t.length;c++)m.has(c)||i.push({index:c,score:this.options.totalScore*.1});let f=n?i.slice(0,n):i;return this.log.debug("Parsed rerank results",{extractedNumbers:o,uniqueIndices:l,resultCount:f.length,topK:n}),{results:f,warnings:r}}catch(a){return this.log.error("Failed to parse rerank result",{error:a.message,text:e}),{results:t.map((l,s)=>({index:s,score:t.length>1?this.options.totalScore*(1-.9*s/(t.length-1)):this.options.totalScore})),warnings:[`Failed to parse rerank result: ${a.message}`]}}}};var w=utils.Log.create({service:"local-model-provider"}),de={cacheDir:D__namespace.default.join(process.env.EASBOT_CACHE_PATH??`${D__namespace.default.join(G__namespace.homedir(),".cache","easbot")}`,"models"),verbose:false,autoUnloadTimeout:300*1e3,maxCachedModels:3,memoryWarningThreshold:1.5*1024*1024*1024,useEnhancedModel:true},B=class{constructor(e){g(this,"config");g(this,"modelCache");g(this,"engines",new Map);g(this,"modelInfo",new Map);this.config={...de,...e},this.modelCache=new I({cacheDir:this.config.cacheDir});}languageModel(e,t){if(!L[e])throw new u(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return t?.useEnhancedModel??this.config.useEnhancedModel?(this.config.verbose&&w.debug("\u521B\u5EFA\u589E\u5F3A\u8BED\u8A00\u6A21\u578B",{modelId:e}),new A("local",e,n)):(this.config.verbose&&w.debug("\u521B\u5EFA\u57FA\u7840\u8BED\u8A00\u6A21\u578B",{modelId:e}),new O("local",e,n))}embeddingModel(e){if(!k[e])throw new u(`\u4E0D\u652F\u6301\u7684 Embedding Model ID: ${e}`,"MODEL_NOT_FOUND");let t=this.getOrCreateEngine(e,"embedding");return new _("local",e,t)}chat(e,t){return this.languageModel(e,t)}rerankingModel(e,t){if(this.config.verbose&&w.debug("\u521B\u5EFA\u91CD\u6392\u5E8F\u6A21\u578B",{modelId:e,hasExternalLlm:!!t?.llm}),t?.llm)return new q(e,t.llm,t);if(!L[e])throw new u(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return new U(e,this.config,n,t)}getOrCreateEngine(e,t){let n=`${t}:${e}`;if(this.engines.has(n)){let a=this.modelInfo.get(n);return a&&(a.lastUsed=Date.now(),a.useCount++),this.config.verbose&&w.debug("\u590D\u7528\u5DF2\u52A0\u8F7D\u7684\u6A21\u578B",{engineKey:n}),this.engines.get(n)}this.engines.size>=this.config.maxCachedModels&&this.evictLeastRecentlyUsed(),this.config.verbose&&w.debug("\u521B\u5EFA\u65B0\u5F15\u64CE",{engineKey:n});let r=new F({modelId:e,modelType:t,cacheDir:this.config.cacheDir,verbose:this.config.verbose});return this.engines.set(n,r),this.modelInfo.set(n,{modelId:e,modelType:t,isLoaded:false,lastUsed:Date.now(),useCount:1}),r}evictLeastRecentlyUsed(){let e=null,t=Number.POSITIVE_INFINITY;for(let[n,r]of this.modelInfo.entries())r.lastUsed<t&&(t=r.lastUsed,e=n);if(e){this.config.verbose&&w.debug("\u6DD8\u6C70\u6A21\u578B",{engineKey:e});let n=this.engines.get(e);n&&n.dispose().catch(r=>{w.error("\u5378\u8F7D\u6A21\u578B\u5931\u8D25",{error:r});}),this.engines.delete(e),this.modelInfo.delete(e);}}getLoadedModels(){return Array.from(this.modelInfo.values())}async dispose(){this.config.verbose&&w.debug("\u5378\u8F7D\u6240\u6709\u6A21\u578B");let e=Array.from(this.engines.values()).map(t=>t.dispose());await Promise.all(e),this.engines.clear(),this.modelInfo.clear(),this.config.verbose&&w.debug("\u6240\u6709\u6A21\u578B\u5DF2\u5378\u8F7D");}};function ce(p){let e=new B(p);return Object.assign((n,r)=>e.languageModel(n,r),{languageModel:(n,r)=>e.languageModel(n,r),chat:(n,r)=>e.chat(n,r),embeddingModel:n=>e.embeddingModel(n),rerankingModel:(n,r)=>e.rerankingModel(n,r)})}var ge="0.1.0",fe="@easbot/local-model-sdk",me="Complete local model SDK for EASBot - Transformers.js + ONNX implementation";exports.ErrorCode=M;exports.ErrorHandler=T;exports.LLMRerankingModel=q;exports.LocalEmbeddingModel=_;exports.LocalEnhancedLanguageModel=A;exports.LocalLanguageModel=O;exports.LocalModelError=u;exports.LocalModelProvider=B;exports.LocalRerankingModel=U;exports.SDK_DESCRIPTION=me;exports.SDK_NAME=fe;exports.SUPPORTED_EMBEDDING_MODELS=k;exports.SUPPORTED_LANGUAGE_MODELS=L;exports.VERSION=ge;exports.createLocal=ce;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { LanguageModelV3StreamPart, LanguageModelV3GenerateResult, LanguageModelV3, LanguageModelV3FinishReason, LanguageModelV3CallOptions, EmbeddingModelV3, EmbeddingModelV3CallOptions, EmbeddingModelV3Result, RerankingModelV3 } from '@ai-sdk/provider';
|
|
1
|
+
import { LanguageModelV3StreamPart, LanguageModelV3GenerateResult, LanguageModelV3, LanguageModelV3FinishReason, EmbeddingModelV3, RerankingModelV3, LanguageModelV3CallOptions, LanguageModelV3StreamResult, EmbeddingModelV3CallOptions, EmbeddingModelV3Result, RerankingModelV3CallOptions, SharedV3ProviderMetadata, SharedV3Warning, SharedV3Headers } from '@ai-sdk/provider';
|
|
3
2
|
import { PipelineType } from '@huggingface/transformers';
|
|
4
3
|
export { PipelineType } from '@huggingface/transformers';
|
|
5
4
|
|
|
@@ -71,9 +70,17 @@ interface LocalGenerateResult {
|
|
|
71
70
|
content: string;
|
|
72
71
|
finishReason: LanguageModelV3FinishReason;
|
|
73
72
|
usage: {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
inputTokens: {
|
|
74
|
+
total: number | undefined;
|
|
75
|
+
noCache: number | undefined;
|
|
76
|
+
cacheRead: number | undefined;
|
|
77
|
+
cacheWrite: number | undefined;
|
|
78
|
+
};
|
|
79
|
+
outputTokens: {
|
|
80
|
+
total: number | undefined;
|
|
81
|
+
text: number | undefined;
|
|
82
|
+
reasoning: number | undefined;
|
|
83
|
+
};
|
|
77
84
|
};
|
|
78
85
|
}
|
|
79
86
|
interface LocalGenerateChunk {
|
|
@@ -81,9 +88,17 @@ interface LocalGenerateChunk {
|
|
|
81
88
|
delta?: string;
|
|
82
89
|
finishReason?: LanguageModelV3FinishReason;
|
|
83
90
|
usage?: {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
91
|
+
inputTokens: {
|
|
92
|
+
total: number | undefined;
|
|
93
|
+
noCache: number | undefined;
|
|
94
|
+
cacheRead: number | undefined;
|
|
95
|
+
cacheWrite: number | undefined;
|
|
96
|
+
};
|
|
97
|
+
outputTokens: {
|
|
98
|
+
total: number | undefined;
|
|
99
|
+
text: number | undefined;
|
|
100
|
+
reasoning: number | undefined;
|
|
101
|
+
};
|
|
87
102
|
};
|
|
88
103
|
}
|
|
89
104
|
type GenerateResult = LanguageModelV3GenerateResult;
|
|
@@ -112,29 +127,29 @@ declare class LocalModelProvider {
|
|
|
112
127
|
constructor(config?: LocalModelProviderConfig);
|
|
113
128
|
languageModel(modelId: string, options?: {
|
|
114
129
|
useEnhancedModel?: boolean;
|
|
115
|
-
}):
|
|
116
|
-
embeddingModel(modelId: string):
|
|
130
|
+
}): LanguageModelV3;
|
|
131
|
+
embeddingModel(modelId: string): EmbeddingModelV3;
|
|
117
132
|
chat(modelId: string, options?: {
|
|
118
133
|
useEnhancedModel?: boolean;
|
|
119
|
-
}):
|
|
120
|
-
rerankingModel(modelId: string, options?: RerankingModelOptions):
|
|
134
|
+
}): LanguageModelV3;
|
|
135
|
+
rerankingModel(modelId: string, options?: RerankingModelOptions): RerankingModelV3;
|
|
121
136
|
private getOrCreateEngine;
|
|
122
137
|
private evictLeastRecentlyUsed;
|
|
123
138
|
getLoadedModels(): ModelInfo[];
|
|
124
139
|
dispose(): Promise<void>;
|
|
125
140
|
}
|
|
126
|
-
interface LocalProvider
|
|
141
|
+
interface LocalProvider {
|
|
127
142
|
(modelId: string, options?: {
|
|
128
143
|
useEnhancedModel?: boolean;
|
|
129
|
-
}):
|
|
144
|
+
}): LanguageModelV3;
|
|
130
145
|
languageModel(modelId: string, options?: {
|
|
131
146
|
useEnhancedModel?: boolean;
|
|
132
|
-
}):
|
|
147
|
+
}): LanguageModelV3;
|
|
133
148
|
chat(modelId: string, options?: {
|
|
134
149
|
useEnhancedModel?: boolean;
|
|
135
|
-
}):
|
|
136
|
-
embeddingModel(modelId: string):
|
|
137
|
-
rerankingModel(modelId: string, options?: RerankingModelOptions):
|
|
150
|
+
}): LanguageModelV3;
|
|
151
|
+
embeddingModel(modelId: string): EmbeddingModelV3;
|
|
152
|
+
rerankingModel(modelId: string, options?: RerankingModelOptions): RerankingModelV3;
|
|
138
153
|
}
|
|
139
154
|
declare function createLocal(config?: LocalModelProviderConfig): LocalProvider;
|
|
140
155
|
|
|
@@ -165,20 +180,9 @@ declare class LocalLanguageModel implements LanguageModelV3 {
|
|
|
165
180
|
private readonly engine;
|
|
166
181
|
private readonly modelConfig;
|
|
167
182
|
constructor(provider: string, modelId: string, engine: TransformersEngine);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
fullStream: AsyncIterable<LanguageModelV3StreamPart>;
|
|
172
|
-
request: {
|
|
173
|
-
body: string;
|
|
174
|
-
};
|
|
175
|
-
response: {
|
|
176
|
-
headers?: Record<string, string>;
|
|
177
|
-
};
|
|
178
|
-
}>;
|
|
179
|
-
private convertToQwenFormat;
|
|
180
|
-
doGenerate(options: LanguageModelV3CallOptions): Promise<any>;
|
|
181
|
-
doStream(options: LanguageModelV3CallOptions): Promise<any>;
|
|
183
|
+
doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
|
|
184
|
+
doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult>;
|
|
185
|
+
private convertToChatMLFormat;
|
|
182
186
|
}
|
|
183
187
|
|
|
184
188
|
declare class LocalEnhancedLanguageModel implements LanguageModelV3 {
|
|
@@ -187,33 +191,11 @@ declare class LocalEnhancedLanguageModel implements LanguageModelV3 {
|
|
|
187
191
|
readonly modelId: string;
|
|
188
192
|
readonly defaultObjectGenerationMode: "json";
|
|
189
193
|
readonly supportedUrls: Record<string, RegExp[]>;
|
|
190
|
-
readonly supportsToolCalling: "experimental";
|
|
191
|
-
readonly supportsThinking: "experimental";
|
|
192
194
|
private readonly engine;
|
|
193
195
|
private readonly modelConfig;
|
|
194
196
|
constructor(provider: string, modelId: string, engine: TransformersEngine);
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
stream: ReadableStream<LanguageModelV3StreamPart>;
|
|
198
|
-
fullStream: AsyncIterable<LanguageModelV3StreamPart>;
|
|
199
|
-
request: {
|
|
200
|
-
body: string;
|
|
201
|
-
};
|
|
202
|
-
response: {
|
|
203
|
-
headers?: Record<string, string>;
|
|
204
|
-
};
|
|
205
|
-
}>;
|
|
206
|
-
doGenerate(options: LanguageModelV3CallOptions): Promise<any>;
|
|
207
|
-
doStream(options: LanguageModelV3CallOptions): Promise<{
|
|
208
|
-
stream: ReadableStream<LanguageModelV3StreamPart>;
|
|
209
|
-
fullStream: AsyncIterable<LanguageModelV3StreamPart>;
|
|
210
|
-
request: {
|
|
211
|
-
body: string;
|
|
212
|
-
};
|
|
213
|
-
response: {
|
|
214
|
-
headers?: Record<string, string>;
|
|
215
|
-
};
|
|
216
|
-
}>;
|
|
197
|
+
doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
|
|
198
|
+
doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult>;
|
|
217
199
|
}
|
|
218
200
|
|
|
219
201
|
declare class LocalEmbeddingModel implements EmbeddingModelV3 {
|
|
@@ -235,38 +217,19 @@ declare class LocalRerankingModel implements RerankingModelV3 {
|
|
|
235
217
|
private languageModel;
|
|
236
218
|
private options;
|
|
237
219
|
private log;
|
|
238
|
-
constructor(modelId: string,
|
|
239
|
-
doRerank(options: {
|
|
240
|
-
query: string;
|
|
241
|
-
documents: {
|
|
242
|
-
type: 'text';
|
|
243
|
-
values: string[];
|
|
244
|
-
} | {
|
|
245
|
-
type: 'object';
|
|
246
|
-
values: Array<{
|
|
247
|
-
content: string;
|
|
248
|
-
id?: string;
|
|
249
|
-
}>;
|
|
250
|
-
};
|
|
251
|
-
topN?: number;
|
|
252
|
-
abortSignal?: AbortSignal;
|
|
253
|
-
providerOptions?: any;
|
|
254
|
-
headers?: Record<string, string>;
|
|
255
|
-
}): Promise<{
|
|
220
|
+
constructor(modelId: string, _config: unknown, engine: TransformersEngine, options?: RerankingModelOptions);
|
|
221
|
+
doRerank(options: RerankingModelV3CallOptions): Promise<{
|
|
256
222
|
ranking: Array<{
|
|
257
223
|
index: number;
|
|
258
224
|
relevanceScore: number;
|
|
259
225
|
}>;
|
|
260
|
-
providerMetadata?:
|
|
261
|
-
warnings?: Array<
|
|
262
|
-
type: 'other';
|
|
263
|
-
message: string;
|
|
264
|
-
}>;
|
|
226
|
+
providerMetadata?: SharedV3ProviderMetadata;
|
|
227
|
+
warnings?: Array<SharedV3Warning>;
|
|
265
228
|
response?: {
|
|
266
229
|
id?: string;
|
|
267
230
|
timestamp?: Date;
|
|
268
231
|
modelId?: string;
|
|
269
|
-
headers?:
|
|
232
|
+
headers?: SharedV3Headers;
|
|
270
233
|
body?: unknown;
|
|
271
234
|
};
|
|
272
235
|
}>;
|
|
@@ -288,41 +251,21 @@ declare class LLMRerankingModel implements RerankingModelV3 {
|
|
|
288
251
|
private readonly options;
|
|
289
252
|
private readonly log;
|
|
290
253
|
constructor(modelId: string, languageModel: LanguageModelV3, options?: LLMRerankingModelOptions);
|
|
291
|
-
doRerank(options: {
|
|
292
|
-
query: string;
|
|
293
|
-
documents: {
|
|
294
|
-
type: 'text';
|
|
295
|
-
values: string[];
|
|
296
|
-
} | {
|
|
297
|
-
type: 'object';
|
|
298
|
-
values: Array<{
|
|
299
|
-
content: string;
|
|
300
|
-
id?: string;
|
|
301
|
-
}>;
|
|
302
|
-
};
|
|
303
|
-
topN?: number;
|
|
304
|
-
abortSignal?: AbortSignal;
|
|
305
|
-
providerOptions?: any;
|
|
306
|
-
headers?: Record<string, string>;
|
|
307
|
-
}): Promise<{
|
|
254
|
+
doRerank(options: RerankingModelV3CallOptions): Promise<{
|
|
308
255
|
ranking: Array<{
|
|
309
256
|
index: number;
|
|
310
257
|
relevanceScore: number;
|
|
311
258
|
}>;
|
|
312
|
-
providerMetadata?:
|
|
313
|
-
warnings?: Array<
|
|
314
|
-
type: 'other';
|
|
315
|
-
message: string;
|
|
316
|
-
}>;
|
|
259
|
+
providerMetadata?: SharedV3ProviderMetadata;
|
|
260
|
+
warnings?: Array<SharedV3Warning>;
|
|
317
261
|
response?: {
|
|
318
262
|
id?: string;
|
|
319
263
|
timestamp?: Date;
|
|
320
264
|
modelId?: string;
|
|
321
|
-
headers?:
|
|
265
|
+
headers?: SharedV3Headers;
|
|
322
266
|
body?: unknown;
|
|
323
267
|
};
|
|
324
268
|
}>;
|
|
325
|
-
private extractTextContent;
|
|
326
269
|
private buildRerankPrompt;
|
|
327
270
|
private parseRerankResult;
|
|
328
271
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { LanguageModelV3StreamPart, LanguageModelV3GenerateResult, LanguageModelV3, LanguageModelV3FinishReason, LanguageModelV3CallOptions, EmbeddingModelV3, EmbeddingModelV3CallOptions, EmbeddingModelV3Result, RerankingModelV3 } from '@ai-sdk/provider';
|
|
1
|
+
import { LanguageModelV3StreamPart, LanguageModelV3GenerateResult, LanguageModelV3, LanguageModelV3FinishReason, EmbeddingModelV3, RerankingModelV3, LanguageModelV3CallOptions, LanguageModelV3StreamResult, EmbeddingModelV3CallOptions, EmbeddingModelV3Result, RerankingModelV3CallOptions, SharedV3ProviderMetadata, SharedV3Warning, SharedV3Headers } from '@ai-sdk/provider';
|
|
3
2
|
import { PipelineType } from '@huggingface/transformers';
|
|
4
3
|
export { PipelineType } from '@huggingface/transformers';
|
|
5
4
|
|
|
@@ -71,9 +70,17 @@ interface LocalGenerateResult {
|
|
|
71
70
|
content: string;
|
|
72
71
|
finishReason: LanguageModelV3FinishReason;
|
|
73
72
|
usage: {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
inputTokens: {
|
|
74
|
+
total: number | undefined;
|
|
75
|
+
noCache: number | undefined;
|
|
76
|
+
cacheRead: number | undefined;
|
|
77
|
+
cacheWrite: number | undefined;
|
|
78
|
+
};
|
|
79
|
+
outputTokens: {
|
|
80
|
+
total: number | undefined;
|
|
81
|
+
text: number | undefined;
|
|
82
|
+
reasoning: number | undefined;
|
|
83
|
+
};
|
|
77
84
|
};
|
|
78
85
|
}
|
|
79
86
|
interface LocalGenerateChunk {
|
|
@@ -81,9 +88,17 @@ interface LocalGenerateChunk {
|
|
|
81
88
|
delta?: string;
|
|
82
89
|
finishReason?: LanguageModelV3FinishReason;
|
|
83
90
|
usage?: {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
91
|
+
inputTokens: {
|
|
92
|
+
total: number | undefined;
|
|
93
|
+
noCache: number | undefined;
|
|
94
|
+
cacheRead: number | undefined;
|
|
95
|
+
cacheWrite: number | undefined;
|
|
96
|
+
};
|
|
97
|
+
outputTokens: {
|
|
98
|
+
total: number | undefined;
|
|
99
|
+
text: number | undefined;
|
|
100
|
+
reasoning: number | undefined;
|
|
101
|
+
};
|
|
87
102
|
};
|
|
88
103
|
}
|
|
89
104
|
type GenerateResult = LanguageModelV3GenerateResult;
|
|
@@ -112,29 +127,29 @@ declare class LocalModelProvider {
|
|
|
112
127
|
constructor(config?: LocalModelProviderConfig);
|
|
113
128
|
languageModel(modelId: string, options?: {
|
|
114
129
|
useEnhancedModel?: boolean;
|
|
115
|
-
}):
|
|
116
|
-
embeddingModel(modelId: string):
|
|
130
|
+
}): LanguageModelV3;
|
|
131
|
+
embeddingModel(modelId: string): EmbeddingModelV3;
|
|
117
132
|
chat(modelId: string, options?: {
|
|
118
133
|
useEnhancedModel?: boolean;
|
|
119
|
-
}):
|
|
120
|
-
rerankingModel(modelId: string, options?: RerankingModelOptions):
|
|
134
|
+
}): LanguageModelV3;
|
|
135
|
+
rerankingModel(modelId: string, options?: RerankingModelOptions): RerankingModelV3;
|
|
121
136
|
private getOrCreateEngine;
|
|
122
137
|
private evictLeastRecentlyUsed;
|
|
123
138
|
getLoadedModels(): ModelInfo[];
|
|
124
139
|
dispose(): Promise<void>;
|
|
125
140
|
}
|
|
126
|
-
interface LocalProvider
|
|
141
|
+
interface LocalProvider {
|
|
127
142
|
(modelId: string, options?: {
|
|
128
143
|
useEnhancedModel?: boolean;
|
|
129
|
-
}):
|
|
144
|
+
}): LanguageModelV3;
|
|
130
145
|
languageModel(modelId: string, options?: {
|
|
131
146
|
useEnhancedModel?: boolean;
|
|
132
|
-
}):
|
|
147
|
+
}): LanguageModelV3;
|
|
133
148
|
chat(modelId: string, options?: {
|
|
134
149
|
useEnhancedModel?: boolean;
|
|
135
|
-
}):
|
|
136
|
-
embeddingModel(modelId: string):
|
|
137
|
-
rerankingModel(modelId: string, options?: RerankingModelOptions):
|
|
150
|
+
}): LanguageModelV3;
|
|
151
|
+
embeddingModel(modelId: string): EmbeddingModelV3;
|
|
152
|
+
rerankingModel(modelId: string, options?: RerankingModelOptions): RerankingModelV3;
|
|
138
153
|
}
|
|
139
154
|
declare function createLocal(config?: LocalModelProviderConfig): LocalProvider;
|
|
140
155
|
|
|
@@ -165,20 +180,9 @@ declare class LocalLanguageModel implements LanguageModelV3 {
|
|
|
165
180
|
private readonly engine;
|
|
166
181
|
private readonly modelConfig;
|
|
167
182
|
constructor(provider: string, modelId: string, engine: TransformersEngine);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
fullStream: AsyncIterable<LanguageModelV3StreamPart>;
|
|
172
|
-
request: {
|
|
173
|
-
body: string;
|
|
174
|
-
};
|
|
175
|
-
response: {
|
|
176
|
-
headers?: Record<string, string>;
|
|
177
|
-
};
|
|
178
|
-
}>;
|
|
179
|
-
private convertToQwenFormat;
|
|
180
|
-
doGenerate(options: LanguageModelV3CallOptions): Promise<any>;
|
|
181
|
-
doStream(options: LanguageModelV3CallOptions): Promise<any>;
|
|
183
|
+
doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
|
|
184
|
+
doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult>;
|
|
185
|
+
private convertToChatMLFormat;
|
|
182
186
|
}
|
|
183
187
|
|
|
184
188
|
declare class LocalEnhancedLanguageModel implements LanguageModelV3 {
|
|
@@ -187,33 +191,11 @@ declare class LocalEnhancedLanguageModel implements LanguageModelV3 {
|
|
|
187
191
|
readonly modelId: string;
|
|
188
192
|
readonly defaultObjectGenerationMode: "json";
|
|
189
193
|
readonly supportedUrls: Record<string, RegExp[]>;
|
|
190
|
-
readonly supportsToolCalling: "experimental";
|
|
191
|
-
readonly supportsThinking: "experimental";
|
|
192
194
|
private readonly engine;
|
|
193
195
|
private readonly modelConfig;
|
|
194
196
|
constructor(provider: string, modelId: string, engine: TransformersEngine);
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
stream: ReadableStream<LanguageModelV3StreamPart>;
|
|
198
|
-
fullStream: AsyncIterable<LanguageModelV3StreamPart>;
|
|
199
|
-
request: {
|
|
200
|
-
body: string;
|
|
201
|
-
};
|
|
202
|
-
response: {
|
|
203
|
-
headers?: Record<string, string>;
|
|
204
|
-
};
|
|
205
|
-
}>;
|
|
206
|
-
doGenerate(options: LanguageModelV3CallOptions): Promise<any>;
|
|
207
|
-
doStream(options: LanguageModelV3CallOptions): Promise<{
|
|
208
|
-
stream: ReadableStream<LanguageModelV3StreamPart>;
|
|
209
|
-
fullStream: AsyncIterable<LanguageModelV3StreamPart>;
|
|
210
|
-
request: {
|
|
211
|
-
body: string;
|
|
212
|
-
};
|
|
213
|
-
response: {
|
|
214
|
-
headers?: Record<string, string>;
|
|
215
|
-
};
|
|
216
|
-
}>;
|
|
197
|
+
doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
|
|
198
|
+
doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult>;
|
|
217
199
|
}
|
|
218
200
|
|
|
219
201
|
declare class LocalEmbeddingModel implements EmbeddingModelV3 {
|
|
@@ -235,38 +217,19 @@ declare class LocalRerankingModel implements RerankingModelV3 {
|
|
|
235
217
|
private languageModel;
|
|
236
218
|
private options;
|
|
237
219
|
private log;
|
|
238
|
-
constructor(modelId: string,
|
|
239
|
-
doRerank(options: {
|
|
240
|
-
query: string;
|
|
241
|
-
documents: {
|
|
242
|
-
type: 'text';
|
|
243
|
-
values: string[];
|
|
244
|
-
} | {
|
|
245
|
-
type: 'object';
|
|
246
|
-
values: Array<{
|
|
247
|
-
content: string;
|
|
248
|
-
id?: string;
|
|
249
|
-
}>;
|
|
250
|
-
};
|
|
251
|
-
topN?: number;
|
|
252
|
-
abortSignal?: AbortSignal;
|
|
253
|
-
providerOptions?: any;
|
|
254
|
-
headers?: Record<string, string>;
|
|
255
|
-
}): Promise<{
|
|
220
|
+
constructor(modelId: string, _config: unknown, engine: TransformersEngine, options?: RerankingModelOptions);
|
|
221
|
+
doRerank(options: RerankingModelV3CallOptions): Promise<{
|
|
256
222
|
ranking: Array<{
|
|
257
223
|
index: number;
|
|
258
224
|
relevanceScore: number;
|
|
259
225
|
}>;
|
|
260
|
-
providerMetadata?:
|
|
261
|
-
warnings?: Array<
|
|
262
|
-
type: 'other';
|
|
263
|
-
message: string;
|
|
264
|
-
}>;
|
|
226
|
+
providerMetadata?: SharedV3ProviderMetadata;
|
|
227
|
+
warnings?: Array<SharedV3Warning>;
|
|
265
228
|
response?: {
|
|
266
229
|
id?: string;
|
|
267
230
|
timestamp?: Date;
|
|
268
231
|
modelId?: string;
|
|
269
|
-
headers?:
|
|
232
|
+
headers?: SharedV3Headers;
|
|
270
233
|
body?: unknown;
|
|
271
234
|
};
|
|
272
235
|
}>;
|
|
@@ -288,41 +251,21 @@ declare class LLMRerankingModel implements RerankingModelV3 {
|
|
|
288
251
|
private readonly options;
|
|
289
252
|
private readonly log;
|
|
290
253
|
constructor(modelId: string, languageModel: LanguageModelV3, options?: LLMRerankingModelOptions);
|
|
291
|
-
doRerank(options: {
|
|
292
|
-
query: string;
|
|
293
|
-
documents: {
|
|
294
|
-
type: 'text';
|
|
295
|
-
values: string[];
|
|
296
|
-
} | {
|
|
297
|
-
type: 'object';
|
|
298
|
-
values: Array<{
|
|
299
|
-
content: string;
|
|
300
|
-
id?: string;
|
|
301
|
-
}>;
|
|
302
|
-
};
|
|
303
|
-
topN?: number;
|
|
304
|
-
abortSignal?: AbortSignal;
|
|
305
|
-
providerOptions?: any;
|
|
306
|
-
headers?: Record<string, string>;
|
|
307
|
-
}): Promise<{
|
|
254
|
+
doRerank(options: RerankingModelV3CallOptions): Promise<{
|
|
308
255
|
ranking: Array<{
|
|
309
256
|
index: number;
|
|
310
257
|
relevanceScore: number;
|
|
311
258
|
}>;
|
|
312
|
-
providerMetadata?:
|
|
313
|
-
warnings?: Array<
|
|
314
|
-
type: 'other';
|
|
315
|
-
message: string;
|
|
316
|
-
}>;
|
|
259
|
+
providerMetadata?: SharedV3ProviderMetadata;
|
|
260
|
+
warnings?: Array<SharedV3Warning>;
|
|
317
261
|
response?: {
|
|
318
262
|
id?: string;
|
|
319
263
|
timestamp?: Date;
|
|
320
264
|
modelId?: string;
|
|
321
|
-
headers?:
|
|
265
|
+
headers?: SharedV3Headers;
|
|
322
266
|
body?: unknown;
|
|
323
267
|
};
|
|
324
268
|
}>;
|
|
325
|
-
private extractTextContent;
|
|
326
269
|
private buildRerankPrompt;
|
|
327
270
|
private parseRerankResult;
|
|
328
271
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import*as
|
|
2
|
-
`,r=e.lastIndexOf(n);if(r!==-1){let
|
|
1
|
+
import*as z from'os';import*as N from'path';import N__default from'path';import*as b from'fs';import {Log}from'@easbot/utils';import {streamText}from'ai';var te=Object.defineProperty;var ne=(p,e,t)=>e in p?te(p,e,{enumerable:true,configurable:true,writable:true,value:t}):p[e]=t;var g=(p,e,t)=>ne(p,typeof e!="symbol"?e+"":e,t);var L=(f=>(f.MODEL_NOT_FOUND="MODEL_NOT_FOUND",f.MODEL_LOAD_ERROR="MODEL_LOAD_ERROR",f.MODEL_NOT_INITIALIZED="MODEL_NOT_INITIALIZED",f.GENERATION_ERROR="GENERATION_ERROR",f.EMBEDDING_ERROR="EMBEDDING_ERROR",f.INVALID_INPUT="INVALID_INPUT",f.UNSUPPORTED_FEATURE="UNSUPPORTED_FEATURE",f.CACHE_ERROR="CACHE_ERROR",f.PROCESSING_ERROR="PROCESSING_ERROR",f.ABORTED="ABORTED",f.OUT_OF_MEMORY="OUT_OF_MEMORY",f.UNKNOWN_ERROR="UNKNOWN_ERROR",f))(L||{});var w={"qwen3-0.6b":{modelId:"qwen3-0.6b",huggingFaceId:"onnx-community/Qwen3-0.6B-ONNX",contextLength:32768,device:"cpu",dtype:"q4f16",defaultSettings:{temperature:0,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}},"qwen3.5-0.8b":{modelId:"qwen3.5-0.8b",huggingFaceId:"onnx-community/Qwen3.5-0.8B-ONNX",contextLength:32768,dtype:"q4",defaultSettings:{temperature:.7,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}},"gemma-3-1b-it":{modelId:"gemma-3-1b-it",huggingFaceId:"onnx-community/gemma-3-1b-it-ONNX",contextLength:32768,dtype:"q4",defaultSettings:{temperature:.7,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}}},P={"bge-small-zh-v1.5":{modelId:"bge-small-zh-v1.5",huggingFaceId:"Xenova/bge-small-zh-v1.5",dimensions:512,maxEmbeddingsPerCall:32},"bge-base-zh-v1.5":{modelId:"bge-base-zh-v1.5",huggingFaceId:"Xenova/bge-base-zh-v1.5",dimensions:768,maxEmbeddingsPerCall:32,dtype:"q4"},"bge-large-zh-v1.5":{modelId:"bge-large-zh-v1.5",huggingFaceId:"Xenova/bge-large-zh-v1.5",dimensions:1024,maxEmbeddingsPerCall:32,dtype:"q4"},"all-MiniLM-L6-v2":{modelId:"all-MiniLM-L6-v2",huggingFaceId:"sentence-transformers/all-MiniLM-L6-v2",dimensions:384,maxEmbeddingsPerCall:32,dtype:"q4"}};var u=class p extends Error{constructor(t,n,r){super(t);g(this,"code",n);g(this,"cause",r);this.name="LocalModelError",Error.captureStackTrace&&Error.captureStackTrace(this,p);}},E=class{static modelLoadError(e){return new u(`\u6A21\u578B\u52A0\u8F7D\u5931\u8D25: ${e.message}`,"MODEL_LOAD_ERROR",e)}static generationError(e){return new u(`\u751F\u6210\u5931\u8D25: ${e.message}`,"GENERATION_ERROR",e)}static embeddingError(e){return new u(`\u5D4C\u5165\u5931\u8D25: ${e.message}`,"EMBEDDING_ERROR",e)}static validationError(e){return new u(e,"INVALID_INPUT")}static abortError(){return new u("\u64CD\u4F5C\u5DF2\u4E2D\u6B62","ABORTED")}static unsupportedFeature(e){return new u(`\u4E0D\u652F\u6301\u7684\u529F\u80FD: ${e}`,"UNSUPPORTED_FEATURE")}};var re=N.join(z.homedir(),".cache","easbot","models"),C=class{constructor(e){g(this,"cacheDir");this.cacheDir=e.cacheDir||re;}getCachePath(e){return N.join(this.cacheDir,e)}isCached(e){try{let t=this.getCachePath(e);return b.existsSync(t)&&b.statSync(t).isDirectory()}catch{return false}}async clear(e){try{if(e){let t=this.getCachePath(e);b.existsSync(t)&&await this.removeDirectory(t);}else b.existsSync(this.cacheDir)&&await this.removeDirectory(this.cacheDir);}catch(t){throw new u(`\u6E05\u7406\u7F13\u5B58\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,"CACHE_ERROR",t instanceof Error?t:void 0)}}async getCacheSize(e){try{let t=e?this.getCachePath(e):this.cacheDir;return b.existsSync(t)?await this.getDirectorySize(t):0}catch(t){throw new u(`\u83B7\u53D6\u7F13\u5B58\u5927\u5C0F\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,"CACHE_ERROR",t instanceof Error?t:void 0)}}async getDirectorySize(e){let t=0,n=b.readdirSync(e);for(let r of n){let a=N.join(e,r),o=b.statSync(a);o.isDirectory()?t+=await this.getDirectorySize(a):t+=o.size;}return t}async removeDirectory(e){if(!b.existsSync(e))return;let t=b.readdirSync(e);for(let n of t){let r=N.join(e,n);b.statSync(r).isDirectory()?await this.removeDirectory(r):b.unlinkSync(r);}b.rmdirSync(e);}ensureCacheDir(){try{b.existsSync(this.cacheDir)||b.mkdirSync(this.cacheDir,{recursive:!0});}catch(e){throw new u(`\u521B\u5EFA\u7F13\u5B58\u76EE\u5F55\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`,"CACHE_ERROR",e instanceof Error?e:void 0)}}};var h=Log.create({service:"transformers-engine"}),O=null;async function ae(){if(!O){O=(await import('@huggingface/transformers')).env;let e=N.join(z.homedir(),".cache","easbot","models");process.env.HF_HOME||(process.env.HF_HOME=e),O.allowLocalModels=true,O.allowRemoteModels=true,O.useBrowserCache=false,O.useFSCache=true;}return O}var G=null;async function j(){return G||(G=(await import('@huggingface/transformers')).pipeline),G}var $=class{constructor(e){g(this,"pipeline",null);g(this,"tokenizer",null);g(this,"isInitialized",false);g(this,"config");g(this,"modelCache");this.config=e,this.modelCache=new C({cacheDir:e.cacheDir});}async configureEnvironment(){let e=await ae();this.modelCache.ensureCacheDir(),process.env.HF_HOME||(process.env.HF_HOME=this.config.cacheDir),e.cacheDir=this.config.cacheDir,e.allowLocalModels=true,e.allowRemoteModels=true,e.useBrowserCache=false,e.useFSCache=true,e.backends?.onnx&&(e.backends.onnx.logLevel="error"),this.config.verbose&&h.debug("\u73AF\u5883\u914D\u7F6E\u5B8C\u6210",{cacheDir:this.config.cacheDir,hfHome:process.env.HF_HOME,modelId:this.config.modelId,modelType:this.config.modelType});}async initialize(){if(this.isInitialized){this.config.verbose&&h.debug("\u6A21\u578B\u5DF2\u521D\u59CB\u5316\uFF0C\u8DF3\u8FC7\u91CD\u590D\u52A0\u8F7D");return}await this.configureEnvironment();try{this.config.verbose&&h.debug("\u5F00\u59CB\u521D\u59CB\u5316\u6A21\u578B");let e,t,n;if(this.config.modelType==="language"){let o=w[this.config.modelId];if(!o)throw new u(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${this.config.modelId}`,"MODEL_NOT_FOUND");e=o.huggingFaceId,t=o.dtype,n=o.device;}else {let o=P[this.config.modelId];if(!o)throw new u(`\u4E0D\u652F\u6301\u7684 Embedding Model ID: ${this.config.modelId}`,"MODEL_NOT_FOUND");e=o.huggingFaceId,t=o.dtype;}this.config.verbose&&(h.debug("\u52A0\u8F7D\u6A21\u578B",{huggingFaceId:e,dtype:t||"auto"}),this.modelCache.isCached(e)?h.debug("\u4F7F\u7528\u7F13\u5B58\u7684\u6A21\u578B"):h.debug("\u9996\u6B21\u4E0B\u8F7D\u6A21\u578B\uFF0C\u53EF\u80FD\u9700\u8981\u4E00\u4E9B\u65F6\u95F4"));let r={cache_dir:this.config.cacheDir,local_files_only:!0};if(t&&(r.dtype=t),n&&(r.device=n),!this.modelCache.isCached(e))throw this.config.verbose&&h.warn("\u672C\u5730\u7F13\u5B58\u4E0D\u5B58\u5728",{huggingFaceId:e,cacheDir:this.config.cacheDir}),new u(`\u6A21\u578B ${e} \u672A\u627E\u5230\u672C\u5730\u7F13\u5B58\u3002\u8BF7\u5148\u4E0B\u8F7D\u6A21\u578B\u5230: ${this.config.cacheDir}`,"MODEL_NOT_FOUND");if(this.config.modelType==="language"){let o=this.config.pipelineType||"text-generation";this.config.verbose&&h.debug("\u52A0\u8F7D\u8BED\u8A00\u6A21\u578B pipeline",{pipelineType:o,huggingFaceId:e});let l=await j();this.pipeline=await l(o,e,r);}else {let o=await j();this.pipeline=await o("feature-extraction",e,r);}this.tokenizer=this.pipeline.tokenizer,this.isInitialized=!0,this.config.verbose&&h.debug("\u6A21\u578B\u521D\u59CB\u5316\u6210\u529F",{modelId:this.config.modelId});}catch(e){throw this.config.verbose&&h.error("\u6A21\u578B\u521D\u59CB\u5316\u5931\u8D25",{error:e}),E.modelLoadError(e instanceof Error?e:new Error(String(e)))}}extractAssistantResponse(e,t){let n=`assistant
|
|
2
|
+
`,r=e.lastIndexOf(n);if(r!==-1){let s=e.slice(r+n.length);return s=s.replace(/<\|im_end\|>$/g,""),s.trim()}let a=`
|
|
3
3
|
assistant
|
|
4
|
-
`,a=e.lastIndexOf(o);if(a!==-1)return e.slice(a+o.length).trim();let g=`assistant
|
|
5
|
-
`,i=e.lastIndexOf(g);return i!==-1?e.slice(i+g.length).trim():(e.startsWith(t)?e.slice(t.length):e).trim()}async generate(e,t){let r=x[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]},o={temperature:t.temperature??r.temperature,topP:t.topP??r.topP,topK:t.topK??r.topK,maxTokens:t.maxTokens??r.maxTokens,repetitionPenalty:t.repetitionPenalty??r.repetitionPenalty,stopSequences:t.stopSequences??r.stopSequences,abortSignal:t.abortSignal};if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw E.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u751F\u6210\u6587\u672C");let a=await this.countTokens([e]),g=await this.pipeline(e,{max_new_tokens:o.maxTokens,temperature:o.temperature,top_p:o.topP,top_k:o.topK,repetition_penalty:o.repetitionPenalty,do_sample:o.temperature>0});if(t.abortSignal?.aborted)throw E.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=await this.countTokens([m]);return this.config.verbose&&y.debug("\u751F\u6210\u5B8C\u6210",{completionTokens:u}),{content:m,finishReason:V(!0,!1),usage:{promptTokens:a,completionTokens:u,totalTokens:a+u}}}catch(a){throw this.config.verbose&&y.error("\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:E.generationError(a instanceof Error?a:new Error(String(a)))}}async generateStream(e,t,n){let o=x[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]};if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw E.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u6D41\u5F0F\u751F\u6210\u6587\u672C\uFF08\u6A21\u62DF\u6D41\u5F0F\u8F93\u51FA\uFF09");let{TextStreamer:a}=await import('@huggingface/transformers'),g=await this.pipeline(e,{max_new_tokens:t.maxTokens??o.maxTokens,temperature:t.temperature??o.temperature,top_p:t.topP??o.topP,top_k:t.topK??o.topK,repetition_penalty:t.repetitionPenalty??o.repetitionPenalty,do_sample:(t.temperature??o.temperature)>0});if(t.abortSignal?.aborted)throw E.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=m.split(/(\s+)/);for(let l of u){if(t.abortSignal?.aborted)throw E.abortError();l.length>0&&n({type:"text-delta",delta:l});}n({type:"text-end"});let p=await this.countTokens([e]),d=await this.countTokens([m]);n({type:"finish",finishReason:V(!0,!1),usage:{promptTokens:p,completionTokens:d,totalTokens:p+d}}),this.config.verbose&&y.debug("\u6D41\u5F0F\u751F\u6210\u5B8C\u6210",{promptTokens:p,completionTokens:d,totalTokens:p+d});}catch(a){throw this.config.verbose&&y.error("\u6D41\u5F0F\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:(n({type:"finish",finishReason:V(false,true,"generation_error"),usage:{promptTokens:0,completionTokens:0,totalTokens:0}}),E.generationError(a instanceof Error?a:new Error(String(a))))}}async embed(e,t){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="embedding")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u5D4C\u5165\u63A8\u7406","UNSUPPORTED_FEATURE");if(t?.aborted)throw E.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u63A8\u7406",{textCount:e.length});let n=await this.pipeline(e,{pooling:"mean",normalize:!0});if(t?.aborted)throw E.abortError();let r=[];if(n.dims&&n.dims.length===2){let[o,a]=n.dims,g=Array.from(n.data);for(let i=0;i<o;i++){let m=i*a,u=m+a;r.push(g.slice(m,u));}}else r.push(Array.from(n.data));return this.config.verbose&&y.debug("\u63A8\u7406\u5B8C\u6210",{embeddingCount:r.length}),r}catch(n){throw this.config.verbose&&y.error("\u63A8\u7406\u5931\u8D25",{error:n}),n instanceof s&&n.code==="ABORTED"?n:E.embeddingError(n instanceof Error?n:new Error(String(n)))}}async countTokens(e){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");try{let t=0;for(let n of e){let r=await this.tokenizer(n);t+=r.input_ids.data.length;}return t}catch(t){return this.config.verbose&&y.error("\u8BA1\u7B97 token \u5931\u8D25",{error:t}),e.reduce((n,r)=>n+Math.ceil(r.length/4),0)}}async dispose(){this.config.verbose&&y.debug("\u5378\u8F7D\u6A21\u578B"),this.pipeline=null,this.tokenizer=null,this.isInitialized=false,this.config.verbose&&y.debug("\u6A21\u578B\u5DF2\u5378\u8F7D");}isReady(){return this.isInitialized}};var M=class{static validateTexts(e){if(!Array.isArray(e))throw new s("\u8F93\u5165\u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u8F93\u5165\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(typeof n!="string")throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u4E0D\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`,"INVALID_INPUT");if(n.trim().length===0)throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}static validateMessages(e){if(!Array.isArray(e))throw new s("\u6D88\u606F\u5FC5\u987B\u662F\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u6D88\u606F\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(!n||typeof n!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!n.role)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 role \u5B57\u6BB5`,"INVALID_INPUT");if(!["system","user","assistant"].includes(n.role))throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 role "${n.role}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 system\u3001user\u3001assistant`,"UNSUPPORTED_FEATURE");if(!n.content)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 content \u5B57\u6BB5`,"INVALID_INPUT");if(typeof n.content=="string"){if(n.content.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}else if(Array.isArray(n.content)){if(n.content.length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u6570\u7EC4\u4E3A\u7A7A`,"INVALID_INPUT");for(let r=0;r<n.content.length;r++){let o=n.content[r];if(!o||typeof o!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!o.type)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11 type \u5B57\u6BB5`,"INVALID_INPUT");if(o.type!=="text"&&o.type!=="reasoning")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7C7B\u578B "${o.type}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 text \u548C reasoning`,"UNSUPPORTED_FEATURE");if(!("text"in o)||typeof o.text!="string")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11\u6709\u6548\u7684 text \u5B57\u6BB5`,"INVALID_INPUT");if(o.text.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7684 text \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}else throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6216\u6570\u7EC4`,"INVALID_INPUT")}}static validateGenerateOptions(e){if(e.temperature!==void 0&&(typeof e.temperature!="number"||e.temperature<0||e.temperature>2))throw new s("temperature \u5FC5\u987B\u662F 0 \u5230 2 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topP!==void 0&&(typeof e.topP!="number"||e.topP<0||e.topP>1))throw new s("topP \u5FC5\u987B\u662F 0 \u5230 1 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topK!==void 0&&(typeof e.topK!="number"||e.topK<1||!Number.isInteger(e.topK)))throw new s("topK \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.maxTokens!==void 0&&(typeof e.maxTokens!="number"||e.maxTokens<1||!Number.isInteger(e.maxTokens)))throw new s("maxTokens \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.repetitionPenalty!==void 0&&(typeof e.repetitionPenalty!="number"||e.repetitionPenalty<0))throw new s("repetitionPenalty \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 0 \u7684\u6570\u5B57","INVALID_INPUT")}};var z=class{static createReadableStream(e,t){return new ReadableStream({async start(n){if(t?.aborted){n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let r=false,o=()=>{r=true,n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));};t?.addEventListener("abort",o);try{if(await e(a=>{if(!r)try{n.enqueue(a),a.isFinished&&(n.close(),t?.removeEventListener("abort",o));}catch(g){n.error(g),t?.removeEventListener("abort",o);}}),!r)try{n.enqueue({textDelta:"",isFinished:!0,finishReason:"stop"}),n.close();}catch{}}catch(a){r||n.error(a instanceof s?a:new s(`\u751F\u6210\u5931\u8D25: ${a instanceof Error?a.message:String(a)}`,"GENERATION_ERROR",a instanceof Error?a:void 0));}finally{t?.removeEventListener("abort",o);}},cancel(){}})}static async*streamToAsyncIterator(e){let t=e.getReader();try{for(;;){let{done:n,value:r}=await t.read();if(n)break;yield r;}}finally{t.releaseLock();}}};function ie(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var O=class{constructor(e,t,n){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"defaultObjectGenerationMode","json");c(this,"supportedUrls",{});c(this,"engine");c(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=x[t],!this.modelConfig)throw new s(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async generate(e){return this.doGenerate(e)}async stream(e){return this.doStream(e)}convertToQwenFormat(e){let t=[];for(let n of e){let r="";switch(typeof n.content=="string"?r=n.content:Array.isArray(n.content)&&(r=n.content.filter(o=>o.type==="text").map(o=>"text"in o?o.text:"").join(`
|
|
6
|
-
`)
|
|
7
|
-
${r}
|
|
8
|
-
${r}
|
|
9
|
-
${r}
|
|
4
|
+
`,o=e.lastIndexOf(a);return o!==-1?e.slice(o+a.length).trim():(e.startsWith(t)?e.slice(t.length):e).trim()}async generate(e,t){let r=w[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]},a={temperature:t.temperature??r.temperature,topP:t.topP??r.topP,topK:t.topK??r.topK,maxTokens:t.maxTokens??r.maxTokens,repetitionPenalty:t.repetitionPenalty??r.repetitionPenalty,stopSequences:t.stopSequences??r.stopSequences,abortSignal:t.abortSignal};if(this.config.modelType!=="language")throw new u("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw E.abortError();try{this.config.verbose&&h.debug("\u5F00\u59CB\u751F\u6210\u6587\u672C");let o=await this.countTokens([e]),l=await this.pipeline(e,{max_new_tokens:a.maxTokens,temperature:a.temperature,top_p:a.topP,top_k:a.topK,repetition_penalty:a.repetitionPenalty,do_sample:a.temperature>0});if(t.abortSignal?.aborted)throw E.abortError();let s=l[0]?.generated_text||"",i=this.extractAssistantResponse(s,e),d=await this.countTokens([i]);return this.config.verbose&&h.debug("\u751F\u6210\u5B8C\u6210",{completionTokens:d}),{content:i,finishReason:{unified:"stop",raw:void 0},usage:{inputTokens:{total:o,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:d,text:d,reasoning:void 0}}}}catch(o){throw this.config.verbose&&h.error("\u751F\u6210\u5931\u8D25",{error:o}),o instanceof u&&o.code==="ABORTED"?o:E.generationError(o instanceof Error?o:new Error(String(o)))}}async generateStream(e,t,n){let a=w[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]};if(!this.isInitialized)throw new u("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="language")throw new u("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw E.abortError();try{this.config.verbose&&h.debug("\u5F00\u59CB\u6D41\u5F0F\u751F\u6210\u6587\u672C\uFF08\u6A21\u62DF\u6D41\u5F0F\u8F93\u51FA\uFF09");let o=await this.pipeline(e,{max_new_tokens:t.maxTokens??a.maxTokens,temperature:t.temperature??a.temperature,top_p:t.topP??a.topP,top_k:t.topK??a.topK,repetition_penalty:t.repetitionPenalty??a.repetitionPenalty,do_sample:(t.temperature??a.temperature)>0});if(t.abortSignal?.aborted)throw E.abortError();let l=o[0]?.generated_text||"",s=this.extractAssistantResponse(l,e),i=s.split(/(\s+)/);for(let T of i){if(t.abortSignal?.aborted)throw E.abortError();T.length>0&&n({type:"text-delta",delta:T});}n({type:"text-end"});let d=await this.countTokens([e]),m=await this.countTokens([s]);n({type:"finish",finishReason:{unified:"stop",raw:void 0},usage:{inputTokens:{total:d,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:m,text:m,reasoning:void 0}}}),this.config.verbose&&h.debug("\u6D41\u5F0F\u751F\u6210\u5B8C\u6210",{promptTokens:d,completionTokens:m});}catch(o){throw this.config.verbose&&h.error("\u6D41\u5F0F\u751F\u6210\u5931\u8D25",{error:o}),o instanceof u&&o.code==="ABORTED"?o:(n({type:"finish",finishReason:{unified:"error",raw:"generation_error"},usage:{inputTokens:{total:0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:0,text:0,reasoning:void 0}}}),E.generationError(o instanceof Error?o:new Error(String(o))))}}async embed(e,t){if(!this.isInitialized)throw new u("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="embedding")throw new u("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u5D4C\u5165\u63A8\u7406","UNSUPPORTED_FEATURE");if(t?.aborted)throw E.abortError();try{this.config.verbose&&h.debug("\u5F00\u59CB\u63A8\u7406",{textCount:e.length});let n=await this.pipeline(e,{pooling:"mean",normalize:!0});if(t?.aborted)throw E.abortError();let r=[];if(n.dims&&n.dims.length===2){let[a,o]=n.dims,l=Array.from(n.data);for(let s=0;s<a;s++){let i=s*o,d=i+o;r.push(l.slice(i,d));}}else r.push(Array.from(n.data));return this.config.verbose&&h.debug("\u63A8\u7406\u5B8C\u6210",{embeddingCount:r.length}),r}catch(n){throw this.config.verbose&&h.error("\u63A8\u7406\u5931\u8D25",{error:n}),n instanceof u&&n.code==="ABORTED"?n:E.embeddingError(n instanceof Error?n:new Error(String(n)))}}async countTokens(e){if(!this.isInitialized)throw new u("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");try{let t=0;for(let n of e){let r=await this.tokenizer(n);t+=r.input_ids.data.length;}return t}catch(t){return this.config.verbose&&h.error("\u8BA1\u7B97 token \u5931\u8D25",{error:t}),e.reduce((n,r)=>n+Math.ceil(r.length/4),0)}}async dispose(){this.config.verbose&&h.debug("\u5378\u8F7D\u6A21\u578B"),this.pipeline=null,this.tokenizer=null,this.isInitialized=false,this.config.verbose&&h.debug("\u6A21\u578B\u5DF2\u5378\u8F7D");}isReady(){return this.isInitialized}};var M=class{static validateTexts(e){if(!Array.isArray(e))throw new u("\u8F93\u5165\u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new u("\u8F93\u5165\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(typeof n!="string")throw new u(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u4E0D\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`,"INVALID_INPUT");if(n.trim().length===0)throw new u(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}static validateMessages(e){if(!Array.isArray(e))throw new u("\u6D88\u606F\u5FC5\u987B\u662F\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new u("\u6D88\u606F\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(!n||typeof n!="object")throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!n.role)throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 role \u5B57\u6BB5`,"INVALID_INPUT");let r=["system","user","assistant","tool"];if(!r.includes(n.role))throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 role "${n.role}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 ${r.join("\u3001")}`,"UNSUPPORTED_FEATURE");if(!n.content)throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 content \u5B57\u6BB5`,"INVALID_INPUT");if(typeof n.content=="string"){if(n.content.trim().length===0)throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}else if(Array.isArray(n.content)){if(n.content.length===0)throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u6570\u7EC4\u4E3A\u7A7A`,"INVALID_INPUT");for(let a=0;a<n.content.length;a++){let o=n.content[a];if(!o||typeof o!="object")throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${a} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!o.type)throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${a} \u7F3A\u5C11 type \u5B57\u6BB5`,"INVALID_INPUT");let l=["text","file","reasoning","tool-call","tool-result","tool-approval-response"];if(!l.includes(o.type))throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${a} \u7C7B\u578B "${o.type}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 ${l.join("\u3001")}`,"UNSUPPORTED_FEATURE")}}else throw new u(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6216\u6570\u7EC4`,"INVALID_INPUT")}}static validateGenerateOptions(e){if(e.temperature!==void 0&&(typeof e.temperature!="number"||e.temperature<0||e.temperature>2))throw new u("temperature \u5FC5\u987B\u662F 0 \u5230 2 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topP!==void 0&&(typeof e.topP!="number"||e.topP<0||e.topP>1))throw new u("topP \u5FC5\u987B\u662F 0 \u5230 1 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topK!==void 0&&(typeof e.topK!="number"||e.topK<1||!Number.isInteger(e.topK)))throw new u("topK \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.maxTokens!==void 0&&(typeof e.maxTokens!="number"||e.maxTokens<1||!Number.isInteger(e.maxTokens)))throw new u("maxTokens \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.repetitionPenalty!==void 0&&(typeof e.repetitionPenalty!="number"||e.repetitionPenalty<0))throw new u("repetitionPenalty \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 0 \u7684\u6570\u5B57","INVALID_INPUT")}};var D=class{static createReadableStream(e,t){return new ReadableStream({async start(n){if(t?.aborted){n.error(new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let r=false,a=()=>{r=true;try{n.error(new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));}catch{}};t?.addEventListener("abort",a);try{if(await(async l=>{let s=l.enqueue.bind(l);l.enqueue=i=>{if(!r)try{s(i);}catch{}},await e(l);})(n),!r)try{n.close();}catch{}}catch(o){if(!r)try{n.error(o instanceof u?o:new u(`\u751F\u6210\u5931\u8D25: ${o instanceof Error?o.message:String(o)}`,"GENERATION_ERROR",o instanceof Error?o:void 0));}catch{}}finally{t?.removeEventListener("abort",a);}},cancel(){}})}static async*streamToAsyncIterator(e){let t=e.getReader();try{for(;;){let{done:n,value:r}=await t.read();if(n)break;yield r;}}finally{t.releaseLock();}}};function W(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var v=class{constructor(e,t,n){g(this,"specificationVersion","v3");g(this,"provider");g(this,"modelId");g(this,"defaultObjectGenerationMode","json");g(this,"supportedUrls",{});g(this,"engine");g(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=w[t],!this.modelConfig)throw new u(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async doGenerate(e){M.validateMessages(e.prompt);let t=this.convertToChatMLFormat(e.prompt),n={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxOutputTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};M.validateGenerateOptions(n),this.engine.isReady()||await this.engine.initialize();let r=await this.engine.generate(t,n);return {content:[{type:"text",text:r.content||""}],finishReason:r.finishReason,usage:r.usage,warnings:[],request:{body:t}}}async doStream(e){M.validateMessages(e.prompt);let t=this.convertToChatMLFormat(e.prompt),n={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxOutputTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};M.validateGenerateOptions(n),this.engine.isReady()||await this.engine.initialize();let r=[];return {stream:D.createReadableStream(async o=>{o.enqueue({type:"stream-start",warnings:r});let l=W();o.enqueue({type:"response-metadata",id:l,modelId:this.modelId,timestamp:new Date});let s=null,i=false;try{await this.engine.generateStream(t,n,d=>{if(d.type==="text-delta"){let m=d.delta||"";if(m.length===0)return;s||(s=W(),o.enqueue({type:"text-start",id:s})),o.enqueue({type:"text-delta",id:s,delta:m});}else d.type==="text-end"?s&&(o.enqueue({type:"text-end",id:s}),s=null):d.type==="finish"&&(s&&(o.enqueue({type:"text-end",id:s}),s=null),o.enqueue({type:"finish",finishReason:d.finishReason??{unified:"stop",raw:void 0},usage:d.usage??{inputTokens:{total:void 0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:void 0,text:void 0,reasoning:void 0}}}),i=!0);});}catch(d){o.enqueue({type:"error",error:d instanceof Error?d:new Error(String(d))}),s&&o.enqueue({type:"text-end",id:s}),i||o.enqueue({type:"finish",finishReason:{unified:"error",raw:void 0},usage:{inputTokens:{total:void 0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:void 0,text:void 0,reasoning:void 0}}});}},e.abortSignal),request:{body:t}}}convertToChatMLFormat(e){let t=[];for(let n of e){let r="";switch(typeof n.content=="string"?r=n.content:Array.isArray(n.content)&&(r=n.content.filter(a=>a.type==="text").map(a=>"text"in a?a.text:"").join(`
|
|
5
|
+
`)),n.role){case "system":t.push(`system
|
|
6
|
+
${r}`);break;case "user":t.push(`user
|
|
7
|
+
${r}`);break;case "assistant":t.push(`assistant
|
|
8
|
+
${r}`);break;default:t.push(`${String(n.role)}
|
|
9
|
+
${r}`);break}}return t.push(`assistant
|
|
10
10
|
`),t.join(`
|
|
11
|
-
`)}
|
|
11
|
+
`)}};var A=class p{static buildEnhancedPrompt(e,t){let n=[];n.push(p.buildSystemPrompt(t));for(let r of e){let a=p.formatMessage(r);a&&n.push(a);}return n.push(`<|im_start|>assistant
|
|
12
12
|
`),n.join(`
|
|
13
13
|
`)}static buildSystemPrompt(e){return e&&e.length>0?`<|im_start|>system
|
|
14
14
|
Environment: ipython
|
|
15
15
|
|
|
16
16
|
You have access to the following functions:
|
|
17
17
|
|
|
18
|
-
${
|
|
18
|
+
${p.formatToolsDefinition(e)}
|
|
19
19
|
|
|
20
20
|
FUNCTION CALLING INSTRUCTIONS:
|
|
21
21
|
|
|
@@ -103,94 +103,18 @@ CRITICAL OUTPUT RULES:
|
|
|
103
103
|
4. Do NOT include any part of the conversation format in your output
|
|
104
104
|
<|im_end|>`}static formatToolsDefinition(e){return e.map(t=>{let n=t.inputSchema;return JSON.stringify({name:t.name,description:t.description||"No description",parameters:n},null,2)}).join(`
|
|
105
105
|
|
|
106
|
-
`)}static formatMessage(e){let t="";if(typeof e.content=="string")t=e.content;else if(Array.isArray(e.content)){let n=[];for(let r of e.content)if(r.type==="text")n.push("text"in r?r.text:"");else if(r.type==="tool-call"){let
|
|
106
|
+
`)}static formatMessage(e){let t="";if(typeof e.content=="string")t=e.content;else if(Array.isArray(e.content)){let n=[];for(let r of e.content)if(r.type==="text")n.push("text"in r?r.text:"");else if(r.type==="tool-call"){let a={name:r.toolName,parameters:r.args||{}};n.push(JSON.stringify(a));}else if(r.type==="tool-result"){let a=r.result;n.push(typeof a=="string"?a:JSON.stringify(a));}t=n.join(`
|
|
107
107
|
`);}switch(e.role){case "system":return null;case "user":return `<|im_start|>user
|
|
108
108
|
${t}<|im_end|>`;case "assistant":return `<|im_start|>assistant
|
|
109
109
|
${t}<|im_end|>`;case "tool":return `<|im_start|>tool
|
|
110
|
-
${t}<|im_end|>`;default:{let n=e.role;throw new s(`\u4E0D\u652F\u6301\u7684\u6D88\u606F\u89D2\u8272: ${n}`,"UNSUPPORTED_FEATURE")}}}};var C=class{static processThinkTags(e,t){let n={...e};for(;n.contentBuffer.length>0;)if(n.isInThinkTag){let r=n.contentBuffer.indexOf("</think>");if(r===-1){n.contentBuffer.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:n.contentBuffer}),n.contentBuffer="";break}if(r>0){let o=n.contentBuffer.substring(0,r);o.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:o});}t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false,n.isInThinkTag=false,n.contentBuffer=n.contentBuffer.substring(r+8);}else {let r=n.contentBuffer.indexOf("<think>");if(r===-1){n.contentBuffer.length>0&&(n.textStarted||(n.textStarted=true,t.enqueue({type:"text-start",id:"txt-0"})),t.enqueue({type:"text-delta",id:"txt-0",delta:n.contentBuffer})),n.contentBuffer="";break}if(r>0){let o=n.contentBuffer.substring(0,r);n.textStarted||(n.textStarted=true,t.enqueue({type:"text-start",id:"txt-0"})),o.length>0&&t.enqueue({type:"text-delta",id:"txt-0",delta:o}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false;}n.isInThinkTag=true,n.contentBuffer=n.contentBuffer.substring(r+7),n.reasoningStarted||(n.reasoningStarted=true,t.enqueue({type:"reasoning-start",id:"reasoning-0"}));}return n}static finishThinkParsing(e,t){let n={...e};return n.contentBuffer.length>0?n.isInThinkTag?(n.contentBuffer.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:n.contentBuffer}),n.reasoningStarted&&(t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false)):(n.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),n.textStarted=true),n.contentBuffer.length>0&&t.enqueue({type:"text-delta",id:"txt-0",delta:n.contentBuffer}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false):(n.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false),n.reasoningStarted&&!n.isInThinkTag&&(t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false)),n}static removeThinkTags(e){return e.replace(/<think>[\s\S]*?<\/think>/g,"").trim()}};function se(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var D=class f{static extractToolCalls(e){let t=[],n=e,r=e.split(`
|
|
111
|
-
`),o=[];for(let a of r){let g=a.trim();if(!g){o.push(a);continue}try{let i=JSON.parse(g);if(i.name&&typeof i.name=="string"&&i.parameters&&typeof i.parameters=="object"){t.push({name:i.name,parameters:JSON.stringify(i.parameters)});continue}}catch{}o.push(a);}return n=o.join(`
|
|
112
|
-
`).trim(),{toolCalls:t,remainingText:n}}static processToolCalls(e,t,n){let r={...e},o=r.contentBuffer.split(`
|
|
113
|
-
`),a=[];for(let g=0;g<o.length;g++){let i=o[g];if(!i)continue;let m=i.trim();if(!m){a.push(i);continue}let u=false;try{let p=JSON.parse(m);if(p.name&&typeof p.name=="string"&&p.parameters&&typeof p.parameters=="object"){if(n&&n.length>0&&!n.some(L=>L.name===p.name)){a.push(i);continue}if(u=!0,a.length>0){let h=a.join(`
|
|
114
|
-
`);h.trim()&&(r.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),r.textStarted=!0),t.enqueue({type:"text-delta",id:"txt-0",delta:h+`
|
|
115
|
-
`})),a.length=0;}r.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),r.textStarted=!1);let d=se(),l=JSON.stringify(p.parameters);t.enqueue({type:"tool-input-start",id:d,toolName:p.name}),t.enqueue({type:"tool-input-delta",id:d,delta:l}),t.enqueue({type:"tool-input-end",id:d}),t.enqueue({type:"tool-call",toolCallId:d,toolName:p.name,input:l}),r.completedToolCalls.push({id:d,name:p.name,parameters:l});}}catch{}u||a.push(i);}return r.contentBuffer=a.join(`
|
|
116
|
-
`),r}static finishToolParsing(e,t){let n={...e};return n.contentBuffer.trim().length>0?(n.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),n.textStarted=true),t.enqueue({type:"text-delta",id:"txt-0",delta:n.contentBuffer}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false):n.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false),n}static removeToolCalls(e){let{remainingText:t}=f.extractToolCalls(e);return t}};function Q(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var $=class{constructor(e,t,n){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"defaultObjectGenerationMode","json");c(this,"supportedUrls",{});c(this,"supportsToolCalling","experimental");c(this,"supportsThinking","experimental");c(this,"engine");c(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=x[t],!this.modelConfig)throw new s(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async generate(e){return this.doGenerate(e)}async stream(e){return this.doStream(e)}async doGenerate(e){M.validateMessages(e.prompt);let t=e.tools?.filter(d=>"inputSchema"in d),n=U.buildEnhancedPrompt(e.prompt,t),r={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};M.validateGenerateOptions(r),this.engine.isReady()||await this.engine.initialize();let o=await this.engine.generate(n,r),a=C.removeThinkTags(o.content||""),{toolCalls:g,remainingText:i}=D.extractToolCalls(a),m=[];i.trim().length>0&&m.push({type:"text",text:i});for(let d of g)m.push({type:"tool-call",toolCallId:Q(),toolName:d.name,input:d.parameters});let u=g.length>0?"tool-calls":o.finishReason,p=[];return {content:m,finishReason:u,usage:{inputTokens:{total:o.usage.promptTokens,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:o.usage.completionTokens,text:void 0,reasoning:void 0}},warnings:p,rawCall:{rawPrompt:n,rawSettings:r}}}async doStream(e){let t=Q();M.validateMessages(e.prompt);let n=e.tools?.filter(l=>"inputSchema"in l),r=U.buildEnhancedPrompt(e.prompt,n),o={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};M.validateGenerateOptions(o),this.engine.isReady()||await this.engine.initialize();let a=null,g=null,i=l=>{a&&a.enqueue(l),g&&g.enqueue(l);},m=async()=>{try{let l={isInThinkTag:!1,reasoningStarted:!1,textStarted:!1,contentBuffer:""},h={contentBuffer:"",textStarted:!1,completedToolCalls:[]};i({type:"response-metadata",id:t,modelId:this.modelId,timestamp:new Date}),await this.engine.generateStream(r,o,L=>{if(L.type==="text-delta"){let k=L.delta||"";if(k.length===0)return;l.contentBuffer+=k;let P=C.processThinkTags(l,{enqueue:i});l.isInThinkTag=P.isInThinkTag,l.reasoningStarted=P.reasoningStarted,l.textStarted=P.textStarted,h.contentBuffer+=P.contentBuffer,l.contentBuffer="";let A=D.processToolCalls(h,{enqueue:i},n);h.contentBuffer=A.contentBuffer,h.textStarted=A.textStarted,h.completedToolCalls=A.completedToolCalls;}else if(L.type==="text-end"){let k=C.finishThinkParsing(l,{enqueue:i});l.textStarted=k.textStarted,l.reasoningStarted=k.reasoningStarted;let P=D.finishToolParsing(h,{enqueue:i});h.textStarted=P.textStarted;}else if(L.type==="finish"){let k=C.finishThinkParsing(l,{enqueue:i});l.textStarted=k.textStarted,l.reasoningStarted=k.reasoningStarted;let P=D.finishToolParsing(h,{enqueue:i});h.textStarted=P.textStarted;let A=h.completedToolCalls.length>0;i({type:"finish",finishReason:A?"tool-calls":L.finishReason,usage:L.usage?{inputTokens:{total:L.usage.promptTokens||0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:L.usage.completionTokens||0,text:void 0,reasoning:void 0}}:{inputTokens:{total:0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:0,text:void 0,reasoning:void 0}}});}});}catch(l){i({type:"error",error:l instanceof Error?l.message:String(l)}),i({type:"finish",finishReason:"error",usage:{inputTokens:{total:0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:0,text:void 0,reasoning:void 0}}});}finally{a&&a.close(),g&&g.close();}},u=new ReadableStream({start(l){a=l;}}),p=new ReadableStream({start(l){g=l;}});m();async function*d(l){let h=l.getReader();try{for(;;){let{done:L,value:k}=await h.read();if(L)break;yield k;}}finally{h.releaseLock();}}return {stream:u,fullStream:d(p),request:{body:r},response:{headers:{}}}}};var K=class{constructor(e){c(this,"config");this.config=e;}createBatches(e){let t=[],n=this.config.batchSize;for(let r=0;r<e.length;r+=n)t.push(e.slice(r,r+n));return t}async processBatches(e,t,n){if(n?.aborted)throw new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");let r=this.createBatches(e),o=[];for(let a=0;a<r.length;a++){if(n?.aborted)throw new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");let g=r[a];if(g){try{let i=await t(g);o.push(...i);}catch(i){throw i instanceof s&&i.code==="ABORTED"?i:new s(`\u6279\u6B21 ${a+1}/${r.length} \u5904\u7406\u5931\u8D25: ${i instanceof Error?i.message:String(i)}`,"PROCESSING_ERROR",i instanceof Error?i:void 0)}a<r.length-1&&this.config.delayBetweenBatches&&this.config.delayBetweenBatches>0&&await this.delay(this.config.delayBetweenBatches,n);}}return o}async delay(e,t){return new Promise((n,r)=>{if(t?.aborted){r(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let o=setTimeout(()=>{t?.removeEventListener("abort",a),n();},e),a=()=>{clearTimeout(o),r(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));};t?.addEventListener("abort",a);})}};var q=class{constructor(e,t,n){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"maxEmbeddingsPerCall");c(this,"supportsParallelCalls",false);c(this,"engine");c(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=I[t],!this.modelConfig)throw new s(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND");this.maxEmbeddingsPerCall=this.modelConfig.maxEmbeddingsPerCall;}async doEmbed(e){let{values:t,abortSignal:n}=e;if(M.validateTexts(t),n?.aborted)throw new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");this.engine.isReady()||await this.engine.initialize();let o=await new K({batchSize:this.maxEmbeddingsPerCall,delayBetweenBatches:0}).processBatches(t,async g=>{if(n?.aborted)throw new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");return await this.engine.embed(g,n)},n),a=await this.engine.countTokens(t);return {embeddings:o,usage:{tokens:a},warnings:[]}}};var F=class{constructor(e,t,n,r={}){c(this,"specificationVersion","v3");c(this,"provider","local");c(this,"modelId");c(this,"languageModel");c(this,"options");c(this,"log",Log.create({service:"local-model-sdk",module:"reranking-model"}));this.modelId=e,this.languageModel=new O("local",e,n),this.options={llm:this.languageModel,totalScore:r.totalScore??1,useEnhancedPrompt:true,promptLanguage:r.promptLanguage??"en"},this.log.debug("LocalRerankingModel created",{modelId:e,totalScore:this.options.totalScore,useEnhancedPrompt:this.options.useEnhancedPrompt,promptLanguage:this.options.promptLanguage});}async doRerank(e){let{query:t,documents:n,topN:r}=e,o;Array.isArray(n)?o=n:n.type==="text"?o=n.values.map((i,m)=>({content:i,id:`doc_${m+1}`})):o=n.values;let a=r??o.length;if(o.length===0)return {ranking:[],warnings:[]};try{let i=this.buildRerankPrompt(t,o),u=(await this.languageModel.doGenerate({prompt:[{role:"user",content:[{type:"text",text:i}]}],maxOutputTokens:2048,temperature:0,topP:1,topK:1})).content.find(d=>d.type==="text")?.text||"",p=this.parseRerankResult(u,o,a);return {ranking:p.results.map(d=>({index:d.index,relevanceScore:d.score,document:d.document})),warnings:p.warnings.map(d=>({type:"other",message:d})),response:{timestamp:new Date,modelId:this.modelId}}}catch(i){return this.log.error("Reranking failed",{error:i.message}),{ranking:o.map((u,p)=>({index:p,relevanceScore:this.options.totalScore*(1-p/o.length),document:u})),warnings:[{type:"other",message:`Reranking failed, using original order: ${i.message}`}],response:{timestamp:new Date,modelId:this.modelId}}}}buildRerankPrompt(e,t){let n=t.map((r,o)=>{let a=r.id||`doc_${o+1}`,g=r.content.slice(0,300);return `${o+1}. [ID: ${a}] ${g}`}).join(`
|
|
110
|
+
${t}<|im_end|>`;default:{let n=e.role;throw new u(`\u4E0D\u652F\u6301\u7684\u6D88\u606F\u89D2\u8272: ${n}`,"UNSUPPORTED_FEATURE")}}}};var R=class{static processThinkTags(e,t){let n={...e};for(;n.contentBuffer.length>0;)if(n.isInThinkTag){let r=n.contentBuffer.indexOf("</think>");if(r===-1){n.contentBuffer.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:n.contentBuffer}),n.contentBuffer="";break}if(r>0){let a=n.contentBuffer.substring(0,r);a.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:a});}t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false,n.isInThinkTag=false,n.contentBuffer=n.contentBuffer.substring(r+8);}else {let r=n.contentBuffer.indexOf("<think>");if(r===-1)break;if(r>0){let a=n.contentBuffer.substring(0,r);n.textStarted||(n.textStarted=true,t.enqueue({type:"text-start",id:"txt-0"})),a.length>0&&t.enqueue({type:"text-delta",id:"txt-0",delta:a}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false;}n.isInThinkTag=true,n.contentBuffer=n.contentBuffer.substring(r+7),n.reasoningStarted||(n.reasoningStarted=true,t.enqueue({type:"reasoning-start",id:"reasoning-0"}));}return n}static finishThinkParsing(e,t){let n={...e};return n.contentBuffer.length>0?n.isInThinkTag?(n.contentBuffer.length>0&&t.enqueue({type:"reasoning-delta",id:"reasoning-0",delta:n.contentBuffer}),n.reasoningStarted&&(t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false)):(n.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),n.textStarted=true),n.contentBuffer.length>0&&t.enqueue({type:"text-delta",id:"txt-0",delta:n.contentBuffer}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false):(n.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false),n.reasoningStarted&&!n.isInThinkTag&&(t.enqueue({type:"reasoning-end",id:"reasoning-0"}),n.reasoningStarted=false)),n}static removeThinkTags(e){return e.replace(/<think>[\s\S]*?<\/think>/g,"").trim()}};function ie(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var J=/\{\s*"name"\s*:\s*"([^"]+)"\s*,\s*"parameters"\s*:\s*(\{[\s\S]*?\})\s*\}/g;function Y(p){let e=[];J.lastIndex=0;let t;for(;(t=J.exec(p))!==null;){let n=t[1]||"",r=t[2]||"";try{let a=JSON.parse(r);a&&typeof a=="object"&&!Array.isArray(a)&&e.push({name:n,parameters:JSON.stringify(a),index:t.index,length:t[0].length});}catch{}}return {toolCalls:e}}var k=class p{static extractToolCalls(e){let t=Y(e),n=[],r=[],a=0;for(let o of t.toolCalls)o.index>a&&r.push(e.substring(a,o.index)),n.push({name:o.name,parameters:o.parameters}),a=o.index+o.length;return a<e.length&&r.push(e.substring(a)),{toolCalls:n,remainingText:r.join("").trim()}}static processToolCalls(e,t,n){let r={...e},a=Y(r.contentBuffer);if(a.toolCalls.length===0)return r;let o=[],l=0;for(let i of a.toolCalls){let d=r.contentBuffer.substring(l,i.index);if(d.length>0){o.length=0,o.push(d);let f=d;f.trim()&&(r.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),r.textStarted=true),t.enqueue({type:"text-delta",id:"txt-0",delta:f}),true);}if(n&&n.length>0&&!n.some(c=>c.name===i.name)){o.push(r.contentBuffer.substring(i.index,i.index+i.length)),l=i.index+i.length;continue}r.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),r.textStarted=false);let m=ie();t.enqueue({type:"tool-input-start",id:m,toolName:i.name}),t.enqueue({type:"tool-input-delta",id:m,delta:i.parameters}),t.enqueue({type:"tool-input-end",id:m}),t.enqueue({type:"tool-call",toolCallId:m,toolName:i.name,input:i.parameters}),r.completedToolCalls.push({id:m,name:i.name,parameters:i.parameters}),l=i.index+i.length;}return l<r.contentBuffer.length&&o.push(r.contentBuffer.substring(l)),r.contentBuffer=o.join(""),r}static finishToolParsing(e,t){let n={...e};return n.contentBuffer.trim().length>0?(n.textStarted||(t.enqueue({type:"text-start",id:"txt-0"}),n.textStarted=true),t.enqueue({type:"text-delta",id:"txt-0",delta:n.contentBuffer}),t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false):n.textStarted&&(t.enqueue({type:"text-end",id:"txt-0"}),n.textStarted=false),n}static removeToolCalls(e){let{remainingText:t}=p.extractToolCalls(e);return t}};function X(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var _=class{constructor(e,t,n){g(this,"specificationVersion","v3");g(this,"provider");g(this,"modelId");g(this,"defaultObjectGenerationMode","json");g(this,"supportedUrls",{});g(this,"engine");g(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=w[t],!this.modelConfig)throw new u(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async doGenerate(e){M.validateMessages(e.prompt);let t=(e.tools??[]).filter(m=>"inputSchema"in m),n=A.buildEnhancedPrompt(e.prompt,t),r={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxOutputTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};M.validateGenerateOptions(r),this.engine.isReady()||await this.engine.initialize();let a=await this.engine.generate(n,r),o=R.removeThinkTags(a.content||""),{toolCalls:l,remainingText:s}=k.extractToolCalls(o),i=[];s.trim().length>0&&i.push({type:"text",text:s});for(let m of l)i.push({type:"tool-call",toolCallId:X(),toolName:m.name,input:m.parameters});let d=l.length>0?{unified:"tool-calls",raw:void 0}:a.finishReason;return {content:i,finishReason:d,usage:a.usage,warnings:[],request:{body:n}}}async doStream(e){M.validateMessages(e.prompt);let t=(e.tools??[]).filter(l=>"inputSchema"in l),n=A.buildEnhancedPrompt(e.prompt,t),r={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxOutputTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};M.validateGenerateOptions(r),this.engine.isReady()||await this.engine.initialize();let a=[];return {stream:D.createReadableStream(async l=>{l.enqueue({type:"stream-start",warnings:a});let s=X();l.enqueue({type:"response-metadata",id:s,modelId:this.modelId,timestamp:new Date});let i={isInThinkTag:false,reasoningStarted:false,textStarted:false,contentBuffer:""},d={contentBuffer:"",textStarted:false,completedToolCalls:[]},m=false;try{await this.engine.generateStream(n,r,f=>{if(f.type==="text-delta"){let c=f.delta||"";if(c.length===0)return;i.contentBuffer+=c;let T=R.processThinkTags(i,l);i.isInThinkTag=T.isInThinkTag,i.reasoningStarted=T.reasoningStarted,i.textStarted=T.textStarted,d.contentBuffer+=T.contentBuffer,i.contentBuffer="";let I=k.processToolCalls(d,l,t);d.contentBuffer=I.contentBuffer,d.textStarted=I.textStarted,d.completedToolCalls=I.completedToolCalls;}else if(f.type==="text-end"){let c=R.finishThinkParsing(i,l);i.textStarted=c.textStarted,i.reasoningStarted=c.reasoningStarted;let T=k.finishToolParsing(d,l);d.textStarted=T.textStarted,m=!0;}else if(f.type==="finish"){if(!m){let I=R.finishThinkParsing(i,l);i.textStarted=I.textStarted,i.reasoningStarted=I.reasoningStarted;let ee=k.finishToolParsing(d,l);d.textStarted=ee.textStarted,m=!0;}let T=d.completedToolCalls.length>0?{unified:"tool-calls",raw:void 0}:f.finishReason??{unified:"stop",raw:void 0};l.enqueue({type:"finish",finishReason:T,usage:f.usage??{inputTokens:{total:void 0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:void 0,text:void 0,reasoning:void 0}}});}});}catch(f){if(l.enqueue({type:"error",error:f instanceof Error?f:new Error(String(f))}),!m){let c=R.finishThinkParsing(i,l);i.textStarted=c.textStarted,i.reasoningStarted=c.reasoningStarted;let T=k.finishToolParsing(d,l);d.textStarted=T.textStarted;}l.enqueue({type:"finish",finishReason:{unified:"error",raw:void 0},usage:{inputTokens:{total:0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:0,text:void 0,reasoning:void 0}}});}},e.abortSignal),request:{body:n}}}};var B=class{constructor(e){g(this,"config");this.config=e;}createBatches(e){let t=[],n=this.config.batchSize;for(let r=0;r<e.length;r+=n)t.push(e.slice(r,r+n));return t}async processBatches(e,t,n){if(n?.aborted)throw new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");let r=this.createBatches(e),a=[];for(let o=0;o<r.length;o++){if(n?.aborted)throw new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");let l=r[o];if(l){try{let s=await t(l);a.push(...s);}catch(s){throw s instanceof u&&s.code==="ABORTED"?s:new u(`\u6279\u6B21 ${o+1}/${r.length} \u5904\u7406\u5931\u8D25: ${s instanceof Error?s.message:String(s)}`,"PROCESSING_ERROR",s instanceof Error?s:void 0)}o<r.length-1&&this.config.delayBetweenBatches&&this.config.delayBetweenBatches>0&&await this.delay(this.config.delayBetweenBatches,n);}}return a}async delay(e,t){return new Promise((n,r)=>{if(t?.aborted){r(new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let a=setTimeout(()=>{t?.removeEventListener("abort",o),n();},e),o=()=>{clearTimeout(a),r(new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));};t?.addEventListener("abort",o);})}};var U=class{constructor(e,t,n){g(this,"specificationVersion","v3");g(this,"provider");g(this,"modelId");g(this,"maxEmbeddingsPerCall");g(this,"supportsParallelCalls",false);g(this,"engine");g(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=P[t],!this.modelConfig)throw new u(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND");this.maxEmbeddingsPerCall=this.modelConfig.maxEmbeddingsPerCall;}async doEmbed(e){let{values:t,abortSignal:n}=e;if(M.validateTexts(t),n?.aborted)throw new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");this.engine.isReady()||await this.engine.initialize();let a=await new B({batchSize:this.maxEmbeddingsPerCall,delayBetweenBatches:0}).processBatches(t,async l=>{if(n?.aborted)throw new u("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED");return await this.engine.embed(l,n)},n),o=await this.engine.countTokens(t);return {embeddings:a,usage:{tokens:o},warnings:[]}}};var q=class{constructor(e,t,n,r={}){g(this,"specificationVersion","v3");g(this,"provider","local");g(this,"modelId");g(this,"languageModel");g(this,"options");g(this,"log",Log.create({service:"local-model-sdk",module:"reranking-model"}));this.modelId=e,this.languageModel=new v("local",e,n),this.options={llm:this.languageModel,totalScore:r.totalScore??1,useEnhancedPrompt:true,promptLanguage:r.promptLanguage??"en"},this.log.debug("LocalRerankingModel created",{modelId:e,totalScore:this.options.totalScore,useEnhancedPrompt:this.options.useEnhancedPrompt,promptLanguage:this.options.promptLanguage});}async doRerank(e){let{query:t,documents:n,topN:r,abortSignal:a}=e,o=n.type==="text"?n.values.map((s,i)=>({content:s,id:`doc_${i+1}`})):n.values.map((s,i)=>{let d=s;return {content:typeof d=="string"?d:JSON.stringify(d),id:typeof d?.id=="string"?d.id:`doc_${i+1}`}}),l=r??o.length;if(o.length===0)return {ranking:[],warnings:[]};try{let s=this.buildRerankPrompt(t,o),d=(await this.languageModel.doGenerate({prompt:[{role:"user",content:[{type:"text",text:s}]}],maxOutputTokens:2048,temperature:0,topP:1,topK:1,abortSignal:a})).content.find(c=>c.type==="text"),m=d&&d.type==="text"?d.text:"",f=this.parseRerankResult(m,o,l);return {ranking:f.results.map(c=>({index:c.index,relevanceScore:c.score})),warnings:f.warnings.map(c=>({type:"other",message:c})),response:{timestamp:new Date,modelId:this.modelId}}}catch(s){return this.log.error("Reranking failed",{error:s.message}),{ranking:o.map((d,m)=>({index:m,relevanceScore:o.length>1?this.options.totalScore*(1-.9*m/(o.length-1)):this.options.totalScore})),warnings:[{type:"other",message:`Reranking failed, using original order: ${s.message}`}],response:{timestamp:new Date,modelId:this.modelId}}}}buildRerankPrompt(e,t){let n=t.map((r,a)=>{let o=r.id||`doc_${a+1}`,l=r.content.slice(0,300);return `${a+1}. [ID: ${o}] ${l}`}).join(`
|
|
117
111
|
|
|
118
|
-
`);if(this.options.useEnhancedPrompt)if(this.options.promptLanguage==="en"){
|
|
112
|
+
`);if(this.options.useEnhancedPrompt)if(this.options.promptLanguage==="en"){this.options.totalScore===1?"1.0":String(this.options.totalScore);return ["You are a professional information retrieval reranking expert. Please rank the following documents according to the query.","",`User query: ${e}`,"","Documents to rank:",n,"","Please rank the documents according to the following requirements:","1. Carefully analyze the user's query intent and key information needs","2. Evaluate each document's relevance to the query","3. Consider the document's completeness, accuracy, and usefulness","4. Return ONLY the document numbers (1, 2, 3, ...) in order from MOST relevant to LEAST relevant, separated by commas","5. Most relevant documents MUST be at the front","6. Every document must appear exactly once in your output","","IMPORTANT:","- You MUST actually reorder the documents based on relevance to the query","- Do NOT output the original order unless it happens to be the correct relevance order","- Output format: just the numbers separated by commas, nothing else","","Example output format: 2,5,1,4,3 (where document 2 is most relevant to the query)"].join(`
|
|
113
|
+
`)}else {this.options.totalScore===1?"1.0":String(this.options.totalScore);return ["\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u7684\u4FE1\u606F\u68C0\u7D22\u91CD\u6392\u5E8F\u4E13\u5BB6\u3002\u8BF7\u6839\u636E\u7528\u6237\u67E5\u8BE2\u5BF9\u6587\u6863\u8FDB\u884C\u76F8\u5173\u6027\u6392\u5E8F\u3002","",`\u7528\u6237\u67E5\u8BE2\uFF1A${e}`,"","\u5F85\u6392\u5E8F\u6587\u6863\uFF1A",n,"","\u8BF7\u6309\u7167\u4EE5\u4E0B\u8981\u6C42\u8FDB\u884C\u6392\u5E8F\uFF1A","1. \u4ED4\u7EC6\u5206\u6790\u7528\u6237\u67E5\u8BE2\u7684\u610F\u56FE\u548C\u5173\u952E\u4FE1\u606F\u9700\u6C42","2. \u8BC4\u4F30\u6BCF\u4E2A\u6587\u6863\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027","3. \u8003\u8651\u6587\u6863\u7684\u5B8C\u6574\u6027\u3001\u51C6\u786E\u6027\u548C\u5B9E\u7528\u6027","4. \u53EA\u8F93\u51FA\u6587\u6863\u7F16\u53F7\uFF081, 2, 3, ...\uFF09\uFF0C\u6309\u201C\u6700\u76F8\u5173\u201D\u5230\u201C\u6700\u4E0D\u76F8\u5173\u201D\u987A\u5E8F\u6392\u5217\uFF0C\u7528\u9017\u53F7\u5206\u9694","5. \u6700\u76F8\u5173\u7684\u6587\u6863\u5FC5\u987B\u6392\u5728\u6700\u524D\u9762","6. \u6BCF\u4E2A\u6587\u6863\u5FC5\u987B\u51FA\u73B0\u4E14\u4EC5\u51FA\u73B0\u4E00\u6B21","","\u91CD\u8981\u63D0\u793A\uFF1A","- \u5FC5\u987B\u6839\u636E\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027\u5B9E\u9645\u91CD\u65B0\u6392\u5E8F\u6587\u6863","- \u9664\u975E\u539F\u59CB\u987A\u5E8F\u6070\u597D\u5C31\u662F\u6B63\u786E\u7684\u76F8\u5173\u6027\u6392\u5E8F\uFF0C\u5426\u5219\u4E0D\u8981\u8F93\u51FA\u539F\u59CB\u987A\u5E8F","- \u8F93\u51FA\u683C\u5F0F\uFF1A\u4EC5\u5305\u542B\u7528\u9017\u53F7\u5206\u9694\u7684\u7F16\u53F7\uFF0C\u4E0D\u8981\u6709\u5176\u4ED6\u5185\u5BB9","","\u793A\u4F8B\u8F93\u51FA\u683C\u5F0F\uFF1A2,5,1,4,3\uFF08\u5176\u4E2D\u6587\u6863 2 \u4E0E\u67E5\u8BE2\u6700\u76F8\u5173\uFF09"].join(`
|
|
114
|
+
`)}else return [`\u6839\u636E\u67E5\u8BE2 "${e}" \u5BF9\u4EE5\u4E0B\u7ED3\u679C\u76F8\u5173\u6027\u8FDB\u884C\u6392\u5E8F\uFF0C\u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF08\u7528\u9017\u53F7\u5206\u9694\uFF09\uFF1A`,"",n,"","\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u6700\u524D\u9762\uFF09"].join(`
|
|
115
|
+
`)}parseRerankResult(e,t,n){let r=[];try{let o=e.replaceAll(/<thinking>[\s\S]*?<\/thinking>/gi,"").replaceAll(/<think>[\s\S]*?<\/think>/gi,"").replaceAll(/<answer>[\s\S]*?<\/answer>/gi,"").trim().match(/\d+/g)?.map(c=>Number.parseInt(c,10)-1).filter(c=>c>=0&&c<t.length)||[],l=[],s=new Set;for(let c of o)s.has(c)||(s.add(c),l.push(c));let i=[],d=new Set;for(let c of l)c>=0&&c<t.length&&!d.has(c)&&(d.add(c),i.push({index:c,score:l.length>1?this.options.totalScore*(1-.9*i.length/(l.length-1)):this.options.totalScore}));let m=new Set(l);for(let c=0;c<t.length;c++)if(!m.has(c)){let T=this.options.totalScore*.1;i.push({index:c,score:T});}let f=n?i.slice(0,n):i;return this.log.debug("Parsed rerank results",{extractedNumbers:o,uniqueIndices:l,resultCount:f.length,topK:n}),{results:f,warnings:r}}catch(a){return this.log.error("Failed to parse rerank result",{error:a.message,text:e}),{results:t.map((l,s)=>({index:s,score:t.length>1?this.options.totalScore*(1-.9*s/(t.length-1)):this.options.totalScore})),warnings:[`Failed to parse rerank result: ${a.message}`]}}}};var F=class{constructor(e,t,n={}){g(this,"specificationVersion","v3");g(this,"provider");g(this,"modelId");g(this,"languageModel");g(this,"options");g(this,"log",Log.create({service:"local-model-sdk",module:"llm-reranking-model"}));this.modelId=e,this.languageModel=t,this.provider=t.provider||"external",this.options={llm:t,totalScore:n.totalScore??1,useEnhancedPrompt:n.useEnhancedPrompt??false,promptLanguage:n.promptLanguage??"en",modelOptions:n.modelOptions??{temperature:.1,maxTokens:3e4}},this.log.debug("LLMRerankingModel created",{modelId:e,provider:this.provider,totalScore:this.options.totalScore,useEnhancedPrompt:this.options.useEnhancedPrompt,promptLanguage:this.options.promptLanguage});}async doRerank(e){let{query:t,documents:n,topN:r,abortSignal:a}=e,o=n.type==="text"?n.values.map((s,i)=>({content:s,id:`doc_${i+1}`})):n.values.map((s,i)=>{let d=s;return {content:typeof d=="string"?d:JSON.stringify(d),id:typeof d?.id=="string"?d.id:`doc_${i+1}`}}),l=r??o.length;if(o.length===0)return {ranking:[],warnings:[]};try{let s=this.buildRerankPrompt(t,o),i="",d=null;for(let f=0;f<2;f++)try{if(i=await(await streamText({model:this.languageModel,prompt:s,maxOutputTokens:this.options.modelOptions.maxTokens,temperature:this.options.modelOptions.temperature,abortSignal:a})).text,this.log.debug("LLM raw output",{textContent:i,textLength:i?.length,attempt:f}),i&&i.trim().length>0)break;d=new Error("LLM returned empty response");}catch(c){d=c,this.log.warn(`LLM attempt ${f+1} failed: ${d.message}`);}if(!i||i.trim().length===0)throw this.log.warn("LLM returned empty response, using fallback"),d||new Error("LLM returned empty response");let m=this.parseRerankResult(i,o,l);return {ranking:m.results.map(f=>({index:f.index,relevanceScore:f.score})),warnings:m.warnings.map(f=>({type:"other",message:f})),response:{timestamp:new Date,modelId:this.modelId}}}catch(s){return this.log.error("Reranking failed",{error:s.message}),{ranking:o.map((d,m)=>({index:m,relevanceScore:o.length>1?this.options.totalScore*(1-.9*m/(o.length-1)):this.options.totalScore})),warnings:[{type:"other",message:`Reranking failed, using original order: ${s.message}`}],response:{timestamp:new Date,modelId:this.modelId}}}}buildRerankPrompt(e,t){let n=t.map((r,a)=>{let o=r.id||`doc_${a+1}`,l=r.content.slice(0,300);return `${a+1}. [ID: ${o}] ${l}`}).join(`
|
|
119
116
|
|
|
120
|
-
User query: ${e}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
${n}
|
|
124
|
-
|
|
125
|
-
Please rank the documents according to the following requirements:
|
|
126
|
-
1. Carefully analyze the user's query intent and key information needs
|
|
127
|
-
2. Evaluate each document's relevance to the query
|
|
128
|
-
3. Consider the document's completeness, accuracy, and usefulness
|
|
129
|
-
4. Return the ranked document numbers separated by commas
|
|
130
|
-
5. Most relevant documents should be at the front
|
|
131
|
-
6. Use a scoring scale from ${r}
|
|
132
|
-
|
|
133
|
-
Example output format: 2,5,1,4,3
|
|
134
|
-
|
|
135
|
-
Please only output the ranked document numbers separated by commas, do not include the actual scores.`}else {let r=this.options.totalScore===1?"1.0":`${this.options.totalScore}`;return `\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u7684\u4FE1\u606F\u68C0\u7D22\u91CD\u6392\u5E8F\u4E13\u5BB6\u3002\u8BF7\u6839\u636E\u7528\u6237\u67E5\u8BE2\u5BF9\u6587\u6863\u8FDB\u884C\u76F8\u5173\u6027\u6392\u5E8F\u3002
|
|
136
|
-
|
|
137
|
-
\u7528\u6237\u67E5\u8BE2\uFF1A${e}
|
|
138
|
-
|
|
139
|
-
\u5F85\u6392\u5E8F\u6587\u6863\uFF1A
|
|
140
|
-
${n}
|
|
141
|
-
|
|
142
|
-
\u8BF7\u6309\u7167\u4EE5\u4E0B\u8981\u6C42\u8FDB\u884C\u6392\u5E8F\uFF1A
|
|
143
|
-
1. \u4ED4\u7EC6\u5206\u6790\u7528\u6237\u67E5\u8BE2\u7684\u610F\u56FE\u548C\u5173\u952E\u4FE1\u606F\u9700\u6C42
|
|
144
|
-
2. \u8BC4\u4F30\u6BCF\u4E2A\u6587\u6863\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027
|
|
145
|
-
3. \u8003\u8651\u6587\u6863\u7684\u5B8C\u6574\u6027\u3001\u51C6\u786E\u6027\u548C\u5B9E\u7528\u6027
|
|
146
|
-
4. \u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u6587\u6863\u7F16\u53F7\uFF0C\u7528\u9017\u53F7\u5206\u9694
|
|
147
|
-
5. \u6700\u76F8\u5173\u7684\u6587\u6863\u6392\u5728\u6700\u524D\u9762
|
|
148
|
-
6. \u8BF7\u4F7F\u7528 ${r} \u5206\u503C\u8FDB\u884C\u8BC4\u5206
|
|
149
|
-
|
|
150
|
-
\u793A\u4F8B\u8F93\u51FA\u683C\u5F0F\uFF1A2,5,1,4,3
|
|
151
|
-
|
|
152
|
-
\u8BF7\u53EA\u8F93\u51FA\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF0C\u4E0D\u8981\u5305\u542B\u5176\u4ED6\u89E3\u91CA\u3002`}else return `\u6839\u636E\u67E5\u8BE2 "${e}" \u5BF9\u4EE5\u4E0B\u7ED3\u679C\u76F8\u5173\u6027\u8FDB\u884C\u6392\u5E8F\uFF0C\u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF08\u7528\u9017\u53F7\u5206\u9694\uFF09\uFF1A
|
|
153
|
-
|
|
154
|
-
${n}
|
|
155
|
-
|
|
156
|
-
\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u524D\u9762\uFF09`}parseRerankResult(e,t,n){let r=[];try{let a=e.replaceAll(/<thinking>[\s\S]*?<\/thinking>/gi,"").replaceAll(/<think>[\s\S]*?<\/think>/gi,"").replaceAll(/<answer>[\s\S]*?<\/answer>/gi,"").trim().match(/\d+/g)?.map(l=>parseInt(l,10)-1).filter(l=>l>=0&&l<t.length)||[],g=[],i=new Set;for(let l of a)i.has(l)||(i.add(l),g.push(l));let m=[],u=new Set;for(let l of g)if(l!==void 0&&l>=0&&l<t.length&&!u.has(l)){u.add(l);let h=t[l];h&&m.push({index:l,score:this.options.totalScore*(1-m.length/g.length),document:h});}let p=new Set(g);for(let l=0;l<t.length;l++)if(!p.has(l)){let h=this.options.totalScore*.1*(1-l/t.length);m.push({index:l,score:h,document:t[l]});}let d=n?m.slice(0,n):m;return this.log.debug("Parsed rerank results",{extractedNumbers:a,uniqueIndices:g,resultCount:d.length,topK:n}),{results:d,warnings:r}}catch(o){return this.log.error("Failed to parse rerank result",{error:o.message,text:e}),{results:t.map((g,i)=>({index:i,score:this.options.totalScore*(1-i/t.length),document:g})),warnings:[`Failed to parse rerank result: ${o.message}`]}}}};var B=class{constructor(e,t,n={}){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"languageModel");c(this,"options");c(this,"log",Log.create({service:"local-model-sdk",module:"llm-reranking-model"}));this.modelId=e,this.languageModel=t,this.provider=t.provider||"external",this.options={llm:t,totalScore:n.totalScore??1,useEnhancedPrompt:n.useEnhancedPrompt??false,promptLanguage:n.promptLanguage??"en",modelOptions:n.modelOptions??{temperature:.1,maxTokens:3e4}},this.log.debug("LLMRerankingModel created",{modelId:e,provider:this.provider,totalScore:this.options.totalScore,useEnhancedPrompt:this.options.useEnhancedPrompt,promptLanguage:this.options.promptLanguage});}async doRerank(e){let{query:t,documents:n,topN:r,abortSignal:o}=e,a;Array.isArray(n)?a=n:n.type==="text"?a=n.values.map((m,u)=>({content:m,id:`doc_${u+1}`})):a=n.values;let g=r??a.length;if(a.length===0)return {ranking:[],warnings:[]};try{let m=this.buildRerankPrompt(t,a),u="",p=null;for(let l=0;l<2;l++)try{if(u=await(await streamText({model:this.languageModel,prompt:m,maxOutputTokens:this.options.modelOptions.maxTokens,temperature:this.options.modelOptions.temperature,abortSignal:o})).text,this.log.debug("LLM raw output",{textContent:u,textLength:u?.length,attempt:l}),u&&u.trim().length>0)break;p=new Error("LLM returned empty response");}catch(h){p=h,this.log.warn(`LLM attempt ${l+1} failed: ${p.message}`);}if(!u||u.trim().length===0)throw this.log.warn("LLM returned empty response, using fallback"),p||new Error("LLM returned empty response");let d=this.parseRerankResult(u,a,g);return {ranking:d.results.map(l=>({index:l.index,relevanceScore:l.score,document:l.document})),warnings:d.warnings.map(l=>({type:"other",message:l})),response:{timestamp:new Date,modelId:this.modelId}}}catch(m){return this.log.error("Reranking failed",{error:m.message}),{ranking:a.map((p,d)=>({index:d,relevanceScore:this.options.totalScore*(1-d/a.length),document:p})),warnings:[{type:"other",message:`Reranking failed, using original order: ${m.message}`}],response:{timestamp:new Date,modelId:this.modelId}}}}extractTextContent(e){if(typeof e=="string")return e;if(e.text!==void 0)return e.text;if(e.content&&Array.isArray(e.content)){let t=e.content.find(n=>n.type==="text");if(t?.text)return t.text}return String(e)}buildRerankPrompt(e,t){let n=t.map((r,o)=>{let a=r.id||`doc_${o+1}`,g=r.content.slice(0,300);return `${o+1}. [ID: ${a}] ${g}`}).join(`
|
|
157
|
-
|
|
158
|
-
`);if(this.options.useEnhancedPrompt)if(this.options.promptLanguage==="en"){let r=this.options.totalScore===1?"1.0":`${this.options.totalScore}`;return `You are a professional information retrieval reranking expert. Please rank the following documents according to the query.
|
|
159
|
-
|
|
160
|
-
User query: ${e}
|
|
161
|
-
|
|
162
|
-
Documents to rank:
|
|
163
|
-
${n}
|
|
164
|
-
|
|
165
|
-
Please rank the documents according to the following requirements:
|
|
166
|
-
1. Carefully analyze the user's query intent and key information needs
|
|
167
|
-
2. Evaluate each document's relevance to the query
|
|
168
|
-
3. Consider the document's completeness, accuracy, and usefulness
|
|
169
|
-
4. Return the ranked document numbers separated by commas
|
|
170
|
-
5. Most relevant documents should be at the front
|
|
171
|
-
6. Use a scoring scale from ${r}
|
|
172
|
-
|
|
173
|
-
Example output format: 2,5,1,4,3
|
|
174
|
-
|
|
175
|
-
Please only output the ranked document numbers separated by commas, do not include the actual scores.`}else {let r=this.options.totalScore===1?"1.0":`${this.options.totalScore}`;return `\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u7684\u4FE1\u606F\u68C0\u7D22\u91CD\u6392\u5E8F\u4E13\u5BB6\u3002\u8BF7\u6839\u636E\u7528\u6237\u67E5\u8BE2\u5BF9\u6587\u6863\u8FDB\u884C\u76F8\u5173\u6027\u6392\u5E8F\u3002
|
|
176
|
-
|
|
177
|
-
\u7528\u6237\u67E5\u8BE2\uFF1A${e}
|
|
178
|
-
|
|
179
|
-
\u5F85\u6392\u5E8F\u6587\u6863\uFF1A
|
|
180
|
-
${n}
|
|
181
|
-
|
|
182
|
-
\u8BF7\u6309\u7167\u4EE5\u4E0B\u8981\u6C42\u8FDB\u884C\u6392\u5E8F\uFF1A
|
|
183
|
-
1. \u4ED4\u7EC6\u5206\u6790\u7528\u6237\u67E5\u8BE2\u7684\u610F\u56FE\u548C\u5173\u952E\u4FE1\u606F\u9700\u6C42
|
|
184
|
-
2. \u8BC4\u4F30\u6BCF\u4E2A\u6587\u6863\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027
|
|
185
|
-
3. \u8003\u8651\u6587\u6863\u7684\u5B8C\u6574\u6027\u3001\u51C6\u786E\u6027\u548C\u5B9E\u7528\u6027
|
|
186
|
-
4. \u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u6587\u6863\u7F16\u53F7\uFF0C\u7528\u9017\u53F7\u5206\u9694
|
|
187
|
-
5. \u6700\u76F8\u5173\u7684\u6587\u6863\u6392\u5728\u6700\u524D\u9762
|
|
188
|
-
6. \u8BF7\u4F7F\u7528 ${r} \u5206\u503C\u8FDB\u884C\u8BC4\u5206
|
|
189
|
-
|
|
190
|
-
\u793A\u4F8B\u8F93\u51FA\u683C\u5F0F\uFF1A2,5,1,4,3
|
|
191
|
-
|
|
192
|
-
\u8BF7\u53EA\u8F93\u51FA\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF0C\u4E0D\u8981\u5305\u542B\u5176\u4ED6\u89E3\u91CA\u3002`}else return `\u6839\u636E\u67E5\u8BE2 "${e}" \u5BF9\u4EE5\u4E0B\u7ED3\u679C\u76F8\u5173\u6027\u8FDB\u884C\u6392\u5E8F\uFF0C\u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF08\u7528\u9017\u53F7\u5206\u9694\uFF09\uFF1A
|
|
193
|
-
|
|
194
|
-
${n}
|
|
195
|
-
|
|
196
|
-
\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u524D\u9762\uFF09`}parseRerankResult(e,t,n){let r=[];try{let o=e.match(/\d+/g)?.map(d=>parseInt(d,10)-1).filter(d=>d>=0&&d<t.length)||[],a=[],g=new Set;for(let d of o)g.has(d)||(g.add(d),a.push(d));let i=[],m=new Set;for(let d of a)if(d!==void 0&&d>=0&&d<t.length&&!m.has(d)){m.add(d);let l=t[d];l&&i.push({index:d,score:this.options.totalScore*(1-i.length/a.length),document:l});}let u=new Set(a);for(let d=0;d<t.length;d++)if(!u.has(d)){let l=this.options.totalScore*.1*(1-d/t.length);i.push({index:d,score:l,document:t[d]});}let p=n?i.slice(0,n):i;return this.log.debug("Parsed rerank results",{extractedNumbers:o,uniqueIndices:a,resultCount:p.length,topK:n}),{results:p,warnings:r}}catch(o){return this.log.error("Failed to parse rerank result",{error:o.message,text:e}),{results:t.map((g,i)=>({index:i,score:this.options.totalScore*(1-i/t.length),document:g})),warnings:[`Failed to parse rerank result: ${o.message}`]}}}};var R=Log.create({service:"local-model-provider"}),ue={cacheDir:___default.join(process.env.EASBOT_CACHE_PATH??`${___default.join(J.homedir(),".cache","easbot")}`,"models"),verbose:false,autoUnloadTimeout:300*1e3,maxCachedModels:3,memoryWarningThreshold:1.5*1024*1024*1024,useEnhancedModel:true},j=class{constructor(e){c(this,"config");c(this,"modelCache");c(this,"engines",new Map);c(this,"modelInfo",new Map);this.config={...ue,...e},this.modelCache=new N({cacheDir:this.config.cacheDir});}languageModel(e,t){if(!x[e])throw new s(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return t?.useEnhancedModel??this.config.useEnhancedModel?(this.config.verbose&&R.debug("\u521B\u5EFA\u589E\u5F3A\u8BED\u8A00\u6A21\u578B",{modelId:e}),new $("local",e,n)):(this.config.verbose&&R.debug("\u521B\u5EFA\u57FA\u7840\u8BED\u8A00\u6A21\u578B",{modelId:e}),new O("local",e,n))}embeddingModel(e){if(!I[e])throw new s(`\u4E0D\u652F\u6301\u7684 Embedding Model ID: ${e}`,"MODEL_NOT_FOUND");let t=this.getOrCreateEngine(e,"embedding");return new q("local",e,t)}chat(e,t){return this.languageModel(e,t)}rerankingModel(e,t){if(this.config.verbose&&R.debug("\u521B\u5EFA\u91CD\u6392\u5E8F\u6A21\u578B",{modelId:e,hasExternalLlm:!!t?.llm}),t?.llm)return new B(e,t.llm,t);if(!x[e])throw new s(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return new F(e,this.config,n,t)}getOrCreateEngine(e,t){let n=`${t}:${e}`;if(this.engines.has(n)){let o=this.modelInfo.get(n);return o&&(o.lastUsed=Date.now(),o.useCount++),this.config.verbose&&R.debug("\u590D\u7528\u5DF2\u52A0\u8F7D\u7684\u6A21\u578B",{engineKey:n}),this.engines.get(n)}this.engines.size>=this.config.maxCachedModels&&this.evictLeastRecentlyUsed(),this.config.verbose&&R.debug("\u521B\u5EFA\u65B0\u5F15\u64CE",{engineKey:n});let r=new G({modelId:e,modelType:t,cacheDir:this.config.cacheDir,verbose:this.config.verbose});return this.engines.set(n,r),this.modelInfo.set(n,{modelId:e,modelType:t,isLoaded:false,lastUsed:Date.now(),useCount:1}),r}evictLeastRecentlyUsed(){let e=null,t=Number.POSITIVE_INFINITY;for(let[n,r]of this.modelInfo.entries())r.lastUsed<t&&(t=r.lastUsed,e=n);if(e){this.config.verbose&&R.debug("\u6DD8\u6C70\u6A21\u578B",{engineKey:e});let n=this.engines.get(e);n&&n.dispose().catch(r=>{R.error("\u5378\u8F7D\u6A21\u578B\u5931\u8D25",{error:r});}),this.engines.delete(e),this.modelInfo.delete(e);}}getLoadedModels(){return Array.from(this.modelInfo.values())}async dispose(){this.config.verbose&&R.debug("\u5378\u8F7D\u6240\u6709\u6A21\u578B");let e=Array.from(this.engines.values()).map(t=>t.dispose());await Promise.all(e),this.engines.clear(),this.modelInfo.clear(),this.config.verbose&&R.debug("\u6240\u6709\u6A21\u578B\u5DF2\u5378\u8F7D");}};function me(f){let e=new j(f);return Object.assign((n,r)=>e.languageModel(n,r),{languageModel:(n,r)=>e.languageModel(n,r),chat:(n,r)=>e.chat(n,r),embeddingModel:n=>e.embeddingModel(n),rerankingModel:(n,r)=>e.rerankingModel(n,r)})}var pe="0.1.0",fe="@easbot/local-model-sdk",he="Complete local model SDK for EASBot - Transformers.js + ONNX implementation";export{w as ErrorCode,E as ErrorHandler,B as LLMRerankingModel,q as LocalEmbeddingModel,$ as LocalEnhancedLanguageModel,O as LocalLanguageModel,s as LocalModelError,j as LocalModelProvider,F as LocalRerankingModel,he as SDK_DESCRIPTION,fe as SDK_NAME,I as SUPPORTED_EMBEDDING_MODELS,x as SUPPORTED_LANGUAGE_MODELS,pe as VERSION,me as createLocal};
|
|
117
|
+
`);if(this.options.useEnhancedPrompt)if(this.options.promptLanguage==="en"){this.options.totalScore===1?"1.0":String(this.options.totalScore);return ["You are a professional information retrieval reranking expert. Please rank the following documents according to the query.","",`User query: ${e}`,"","Documents to rank:",n,"","Please rank the documents according to the following requirements:","1. Carefully analyze the user's query intent and key information needs","2. Evaluate each document's relevance to the query","3. Consider the document's completeness, accuracy, and usefulness","4. Return ONLY the document numbers (1, 2, 3, ...) in order from MOST relevant to LEAST relevant, separated by commas","5. Most relevant documents MUST be at the front","6. Every document must appear exactly once in your output","","IMPORTANT:","- You MUST actually reorder the documents based on relevance to the query","- Do NOT output the original order unless it happens to be the correct relevance order","- Output format: just the numbers separated by commas, nothing else","","Example output format: 2,5,1,4,3 (where document 2 is most relevant to the query)"].join(`
|
|
118
|
+
`)}else {this.options.totalScore===1?"1.0":String(this.options.totalScore);return ["\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u7684\u4FE1\u606F\u68C0\u7D22\u91CD\u6392\u5E8F\u4E13\u5BB6\u3002\u8BF7\u6839\u636E\u7528\u6237\u67E5\u8BE2\u5BF9\u6587\u6863\u8FDB\u884C\u76F8\u5173\u6027\u6392\u5E8F\u3002","",`\u7528\u6237\u67E5\u8BE2\uFF1A${e}`,"","\u5F85\u6392\u5E8F\u6587\u6863\uFF1A",n,"","\u8BF7\u6309\u7167\u4EE5\u4E0B\u8981\u6C42\u8FDB\u884C\u6392\u5E8F\uFF1A","1. \u4ED4\u7EC6\u5206\u6790\u7528\u6237\u67E5\u8BE2\u7684\u610F\u56FE\u548C\u5173\u952E\u4FE1\u606F\u9700\u6C42","2. \u8BC4\u4F30\u6BCF\u4E2A\u6587\u6863\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027","3. \u8003\u8651\u6587\u6863\u7684\u5B8C\u6574\u6027\u3001\u51C6\u786E\u6027\u548C\u5B9E\u7528\u6027","4. \u53EA\u8F93\u51FA\u6587\u6863\u7F16\u53F7\uFF081, 2, 3, ...\uFF09\uFF0C\u6309\u201C\u6700\u76F8\u5173\u201D\u5230\u201C\u6700\u4E0D\u76F8\u5173\u201D\u987A\u5E8F\u6392\u5217\uFF0C\u7528\u9017\u53F7\u5206\u9694","5. \u6700\u76F8\u5173\u7684\u6587\u6863\u5FC5\u987B\u6392\u5728\u6700\u524D\u9762","6. \u6BCF\u4E2A\u6587\u6863\u5FC5\u987B\u51FA\u73B0\u4E14\u4EC5\u51FA\u73B0\u4E00\u6B21","","\u91CD\u8981\u63D0\u793A\uFF1A","- \u5FC5\u987B\u6839\u636E\u4E0E\u67E5\u8BE2\u7684\u76F8\u5173\u6027\u5B9E\u9645\u91CD\u65B0\u6392\u5E8F\u6587\u6863","- \u9664\u975E\u539F\u59CB\u987A\u5E8F\u6070\u597D\u5C31\u662F\u6B63\u786E\u7684\u76F8\u5173\u6027\u6392\u5E8F\uFF0C\u5426\u5219\u4E0D\u8981\u8F93\u51FA\u539F\u59CB\u987A\u5E8F","- \u8F93\u51FA\u683C\u5F0F\uFF1A\u4EC5\u5305\u542B\u7528\u9017\u53F7\u5206\u9694\u7684\u7F16\u53F7\uFF0C\u4E0D\u8981\u6709\u5176\u4ED6\u5185\u5BB9","","\u793A\u4F8B\u8F93\u51FA\u683C\u5F0F\uFF1A2,5,1,4,3\uFF08\u5176\u4E2D\u6587\u6863 2 \u4E0E\u67E5\u8BE2\u6700\u76F8\u5173\uFF09"].join(`
|
|
119
|
+
`)}else return [`\u6839\u636E\u67E5\u8BE2 "${e}" \u5BF9\u4EE5\u4E0B\u7ED3\u679C\u76F8\u5173\u6027\u8FDB\u884C\u6392\u5E8F\uFF0C\u8FD4\u56DE\u6392\u5E8F\u540E\u7684\u7F16\u53F7\uFF08\u7528\u9017\u53F7\u5206\u9694\uFF09\uFF1A`,"",n,"","\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u6700\u524D\u9762\uFF09"].join(`
|
|
120
|
+
`)}parseRerankResult(e,t,n){let r=[];try{let o=e.replaceAll(/<think>[\s\S]*?<\/think>/gi,"").replaceAll(/<thinking>[\s\S]*?<\/thinking>/gi,"").replaceAll(/<answer>[\s\S]*?<\/answer>/gi,"").trim().match(/\d+/g)?.map(c=>Number.parseInt(c,10)-1).filter(c=>c>=0&&c<t.length)||[],l=[],s=new Set;for(let c of o)s.has(c)||(s.add(c),l.push(c));let i=[],d=new Set;for(let c of l)c>=0&&c<t.length&&!d.has(c)&&(d.add(c),i.push({index:c,score:l.length>1?this.options.totalScore*(1-.9*i.length/(l.length-1)):this.options.totalScore}));let m=new Set(l);for(let c=0;c<t.length;c++)m.has(c)||i.push({index:c,score:this.options.totalScore*.1});let f=n?i.slice(0,n):i;return this.log.debug("Parsed rerank results",{extractedNumbers:o,uniqueIndices:l,resultCount:f.length,topK:n}),{results:f,warnings:r}}catch(a){return this.log.error("Failed to parse rerank result",{error:a.message,text:e}),{results:t.map((l,s)=>({index:s,score:t.length>1?this.options.totalScore*(1-.9*s/(t.length-1)):this.options.totalScore})),warnings:[`Failed to parse rerank result: ${a.message}`]}}}};var x=Log.create({service:"local-model-provider"}),ce={cacheDir:N__default.join(process.env.EASBOT_CACHE_PATH??`${N__default.join(z.homedir(),".cache","easbot")}`,"models"),verbose:false,autoUnloadTimeout:300*1e3,maxCachedModels:3,memoryWarningThreshold:1.5*1024*1024*1024,useEnhancedModel:true},V=class{constructor(e){g(this,"config");g(this,"modelCache");g(this,"engines",new Map);g(this,"modelInfo",new Map);this.config={...ce,...e},this.modelCache=new C({cacheDir:this.config.cacheDir});}languageModel(e,t){if(!w[e])throw new u(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return t?.useEnhancedModel??this.config.useEnhancedModel?(this.config.verbose&&x.debug("\u521B\u5EFA\u589E\u5F3A\u8BED\u8A00\u6A21\u578B",{modelId:e}),new _("local",e,n)):(this.config.verbose&&x.debug("\u521B\u5EFA\u57FA\u7840\u8BED\u8A00\u6A21\u578B",{modelId:e}),new v("local",e,n))}embeddingModel(e){if(!P[e])throw new u(`\u4E0D\u652F\u6301\u7684 Embedding Model ID: ${e}`,"MODEL_NOT_FOUND");let t=this.getOrCreateEngine(e,"embedding");return new U("local",e,t)}chat(e,t){return this.languageModel(e,t)}rerankingModel(e,t){if(this.config.verbose&&x.debug("\u521B\u5EFA\u91CD\u6392\u5E8F\u6A21\u578B",{modelId:e,hasExternalLlm:!!t?.llm}),t?.llm)return new F(e,t.llm,t);if(!w[e])throw new u(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return new q(e,this.config,n,t)}getOrCreateEngine(e,t){let n=`${t}:${e}`;if(this.engines.has(n)){let a=this.modelInfo.get(n);return a&&(a.lastUsed=Date.now(),a.useCount++),this.config.verbose&&x.debug("\u590D\u7528\u5DF2\u52A0\u8F7D\u7684\u6A21\u578B",{engineKey:n}),this.engines.get(n)}this.engines.size>=this.config.maxCachedModels&&this.evictLeastRecentlyUsed(),this.config.verbose&&x.debug("\u521B\u5EFA\u65B0\u5F15\u64CE",{engineKey:n});let r=new $({modelId:e,modelType:t,cacheDir:this.config.cacheDir,verbose:this.config.verbose});return this.engines.set(n,r),this.modelInfo.set(n,{modelId:e,modelType:t,isLoaded:false,lastUsed:Date.now(),useCount:1}),r}evictLeastRecentlyUsed(){let e=null,t=Number.POSITIVE_INFINITY;for(let[n,r]of this.modelInfo.entries())r.lastUsed<t&&(t=r.lastUsed,e=n);if(e){this.config.verbose&&x.debug("\u6DD8\u6C70\u6A21\u578B",{engineKey:e});let n=this.engines.get(e);n&&n.dispose().catch(r=>{x.error("\u5378\u8F7D\u6A21\u578B\u5931\u8D25",{error:r});}),this.engines.delete(e),this.modelInfo.delete(e);}}getLoadedModels(){return Array.from(this.modelInfo.values())}async dispose(){this.config.verbose&&x.debug("\u5378\u8F7D\u6240\u6709\u6A21\u578B");let e=Array.from(this.engines.values()).map(t=>t.dispose());await Promise.all(e),this.engines.clear(),this.modelInfo.clear(),this.config.verbose&&x.debug("\u6240\u6709\u6A21\u578B\u5DF2\u5378\u8F7D");}};function ge(p){let e=new V(p);return Object.assign((n,r)=>e.languageModel(n,r),{languageModel:(n,r)=>e.languageModel(n,r),chat:(n,r)=>e.chat(n,r),embeddingModel:n=>e.embeddingModel(n),rerankingModel:(n,r)=>e.rerankingModel(n,r)})}var fe="0.1.0",me="@easbot/local-model-sdk",pe="Complete local model SDK for EASBot - Transformers.js + ONNX implementation";export{L as ErrorCode,E as ErrorHandler,F as LLMRerankingModel,U as LocalEmbeddingModel,_ as LocalEnhancedLanguageModel,v as LocalLanguageModel,u as LocalModelError,V as LocalModelProvider,q as LocalRerankingModel,pe as SDK_DESCRIPTION,me as SDK_NAME,P as SUPPORTED_EMBEDDING_MODELS,w as SUPPORTED_LANGUAGE_MODELS,fe as VERSION,ge as createLocal};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easbot/local-model-sdk",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"description": "Complete local model SDK for EASBot - Transformers.js + ONNX implementation supporting both Language Model (text generation) and Embedding (text embedding) with full AI SDK
|
|
3
|
+
"version": "0.3.5",
|
|
4
|
+
"description": "Complete local model SDK for EASBot - Transformers.js + ONNX implementation supporting both Language Model (text generation) and Embedding (text embedding) with full AI SDK v3 compatibility",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.mjs",
|
|
@@ -57,8 +57,8 @@
|
|
|
57
57
|
"axios": "^1.16.1",
|
|
58
58
|
"onnxruntime-node": "^1.25.1",
|
|
59
59
|
"zod": "^4.4.3",
|
|
60
|
-
"@easbot/types": "0.3.
|
|
61
|
-
"@easbot/utils": "0.3.
|
|
60
|
+
"@easbot/types": "0.3.5",
|
|
61
|
+
"@easbot/utils": "0.3.5"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@biomejs/biome": "^2.4.14",
|