ngxson / wllama

WebAssembly binding for llama.cpp - Enabling in-browser LLM inference
https://ngxson.github.io/wllama/examples/basic/
MIT License
231 stars 5 forks source link

The mystery of Schrodinger's exit function #82

Closed flatsiedatsie closed 2 days ago

flatsiedatsie commented 2 days ago

This is a mystery to me. In the code below I check that Wllama exists, and that exitis a function on the Wllama object.

Yet when I call it, I sometimes get this error:

Screenshot 2024-07-03 at 12 20 33
if(window.llama_cpp_app != null && typeof window.llama_cpp_app.exit === 'function'){
            console.log("calling Wllama exit");
            try{
                await window.llama_cpp_app.exit();
            }
            catch(err){
                console.error("caught error trying to stop Wllama: ", err);
            }

        }
        else{
            console.warn(" window.llama.cpp was null, or window.llama_cpp_app.exit was undefined.  window.llama_cpp_app: ", window.llama_cpp_app);
        }
ngxson commented 2 days ago

You need to call loadModel before exit. Otherwise this.proxy is null:

https://github.com/ngxson/wllama/blob/d20441c36907938f60491ccd1fefc48186a2fb53/src/wllama.ts#L302

Additionally, we can add a optional chaining (?.) inside exit function to make sure we don't call proxy if it's null

  /**
   * Unload the model and free all memory
   */
  async exit(): Promise<void> {
    await this.proxy?.wllamaExit();
  }
flatsiedatsie commented 2 days ago

Ah, that must be it. Makes total sense. I had to start loading Wllama "empty" from the start because of the new caching system so I could get a list of already cached files from it when the page first loads.

Would you like me to create a PR?