UNPKG

@mediapipe/tasks-genai-experimental

Version:

MediaPipe GenAI Experimental Tasks

3 lines (2 loc) 4.48 kB
let t,e=globalThis.trustedTypes;function r(r){void 0===t&&(t=function(){let t=null;if(!e)return t;try{const r=t=>t;t=e.createPolicy("goog#html",{createHTML:r,createScript:r,createScriptURL:r})}catch(t){}return t}());var n=t;return new class{constructor(t){this.h=t}toString(){return this.h+""}}(n?n.createScriptURL(r):r)}function n(t,...e){if(0===e.length)return r(t[0]);let n=t[0];for(let r=0;r<e.length;r++)n+=encodeURIComponent(e[r])+t[r+1];return r(n)}let o;const i=new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,10,1,8,0,65,0,253,15,253,98,11]);async function s(){if(void 0===o)try{await WebAssembly.instantiate(i),o=!0}catch{o=!1}return o}async function a(t,e=n``){const r=await s()?"wasm_internal":"wasm_nosimd_internal";return{wasmLoaderPath:`${e}/${t}_${r}.js`,wasmBinaryPath:`${e}/${t}_${r}.wasm`}}var c=class{};async function l(t){if("function"!=typeof importScripts){const e=document.createElement("script");return e.src=t.toString(),e.crossOrigin="anonymous",new Promise(((t,r)=>{e.addEventListener("load",(()=>{t()}),!1),e.addEventListener("error",(t=>{r(t)}),!1),document.body.appendChild(e)}))}importScripts(t.toString())}c.forVisionTasks=function(t){return a("vision",t)},c.forTextTasks=function(t){return a("text",t)},c.forGenAiExperimentalTasks=function(t){return a("genai_experimental",t)},c.forGenAiTasks=function(t){return a("genai",t)},c.forAudioTasks=function(t){return a("audio",t)},c.isSimdSupported=function(){return s()};async function u(t,e){e=await fetch(e.toString());const r=Number(e.headers.get("content-length"));if(!e.body)throw Error("Response body is not available.");if(!r)throw Error("File size is 0.");return async function(t,e,r){t=new f(t,r);let n=0;for(e=e.getReader();;){const{value:r,done:o}=await e.read();if(o)break;t.set(r,n),n+=r.byteLength}if(r!==n)throw h(t),Error(`File could not be fully loaded to memory, so was not retained. Loaded ${n}/${r} bytes before failure`);return t}(t,e.body,r)}function h(t){if(t.h)try{t.l._free(t.i)}catch{}finally{t.h=!1}}var f=class{constructor(t,e){this.l=t,this.m=e,this.i=this.l._malloc(e)>>>0,this.o=this.l.HEAPU8,this.h=!!this.i}get offset(){if(!this.h)throw Error("WasmFileReference has been freed.");return this.i}get size(){if(!this.h)throw Error("WasmFileReference has been freed.");return this.m}set(t,e){this.o.set(t,this.i+(e??0))}};function d(t,e,r){return r=r(e=t.h.stringToNewUTF8(e)),t.h._release(e),r}var y=class{constructor(t,e){this.i=t,this.h=e}s(t){const e=this.h._allocateStringVector(t.length);if(!e)throw Error("Unable to allocate new string vector on heap.");for(const r of t)d(this,r,(t=>{this.h._addStringVectorEntry(e,t)}));return this.h.ccall("recordBatchedMemory","void",["number"],[e],{async:!0})}j(t,e,r){return r="function"==typeof e?e:r,async function(t,e,r=2){return e=await d(t,e,(e=>t.h.ccall("invoke","number",["number","number"],[e,r],{async:!0}))),t.h.UTF8ToString(e)}(this,t,"number"==typeof e?e:2).then((t=>r?this.i.j(t,r):this.i.j(t)))}close(){}};y.prototype.close=y.prototype.close,y.prototype.generateResponse=y.prototype.j,y.prototype.recordBatchedMemory=y.prototype.s,y.createWithEmbeddingModel=async function(t,e,r){const n=await(async(t,e,r,n)=>{if(e&&await l(e),!self.ModuleFactory)throw Error("ModuleFactory not set.");if(r&&(await l(r),!self.ModuleFactory))throw Error("ModuleFactory not set.");return self.Module&&n&&((e=self.Module).locateFile=n.locateFile,n.mainScriptUrlOrBlob&&(e.mainScriptUrlOrBlob=n.mainScriptUrlOrBlob)),n=await self.ModuleFactory(self.Module||n),self.ModuleFactory=self.Module=void 0,new t(n,null)})(y.bind(null,e),t.wasmLoaderPath,t.assetLoaderPath,{locateFile:e=>e.endsWith(".wasm")?t.wasmBinaryPath.toString():t.assetBinaryPath&&e.endsWith(".data")?t.assetBinaryPath.toString():e});let o;return o=r instanceof Uint8Array?function(t,e){return(t=new f(t,e.length)).set(e),t}(n.h,r):await u(n.h,r),await d(n,"<start_of_turn>system\nYou are an assistant for question-answering tasks. You are given facts and you need to answer a question only using the facts provided.\n<end_of_turn>\n<start_of_turn>context\nHere are the facts:\n{memory}\n<end_of_turn>\n<start_of_turn>user\nUse the facts to answer questions from the User.\nUser query:{query}\n<end_of_turn>\n<start_of_turn>model\n",(t=>n.h.ccall("initializeChain","void",["number","number","number"],[o.offset,o.size,t],{async:!0}))),h(o),n};export{c as FilesetResolver,y as RagPipeline}; //# sourceMappingURL=genai_experimental_bundle_mjs.js.map