diff --git a/alpaca.js b/alpaca.js index 2cfd4e8..f4930e9 100644 --- a/alpaca.js +++ b/alpaca.js @@ -65,10 +65,36 @@ class Alpaca { * 5. Download models + convert + quantize * **************************************************************************************************************/ - const outputFile = path.resolve(this.home, 'models', model, 'ggml-model-q4_0.bin') - if (fs.existsSync(outputFile)) { - console.log(`Skip conversion, file already exists: ${outputFile}`) - } else { + + const currentVersions = { + "7B": 'ggml-model-q4_0.bin', + "13B": 'ggml-model-q4_1.bin', + "30B": 'ggml-model-q4_0.bin', + } + + const outputFiles = Object.keys(currentVersions).map((m) => { + return path.resolve(this.home, 'models', m, currentVersions[m]) + }) + + const modelExists = outputFiles.some((f) => { + if (fs.existsSync(f)) { + console.log(`Skip conversion, file already exists: ${f}`) + return true + } + // delete other model files in folder in case of an upgrade + + const dir = path.dirname(f); + const files = fs.readdirSync(dir); + if (files.length !== 0) console.log("Upgrading model, removing old files...") + + for (const file of files) { + fs.unlinkSync(path.join(dir, file)); + console.log(`Removed old model file: ${file} in ${dir}`) + } + return false + }) + + if (!modelExists) { const dir = path.resolve(this.home, "models", model) console.log("dir", dir) await fs.promises.mkdir(dir, { recursive: true }).catch((e) => { @@ -76,39 +102,12 @@ class Alpaca { }) console.log("downloading torrent") let url - switch (model) { - case "7B": - //await this.root.torrent.add('magnet:?xt=urn:btih:5aaceaec63b03e51a98f04fd5c42320b2a033010&dn=ggml-alpaca-7b-q4.bin&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fopentracker.i2p.rocks%3A6969%2Fannounce', dir) - //console.log("renaming") - //await fs.promises.rename( - // path.resolve(dir, "ggml-alpaca-7b-q4.bin"), - // path.resolve(dir, "ggml-model-q4_0.bin") - //) - url = "https://huggingface.co/Pi3141/alpaca-7B-ggml/resolve/main/ggml-model-q4_0.bin" - await this.root.down(url, path.resolve(dir, "ggml-model-q4_0.bin")) - break; - - case "13B": - /* - await this.root.torrent.add('magnet:?xt=urn:btih:053b3d54d2e77ff020ebddf51dad681f2a651071&dn=ggml-alpaca-13b-q4.bin&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fopentracker.i2p.rocks%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.com%3A2810%2Fannounce', dir) - console.log("renaming") - await fs.promises.rename( - path.resolve(dir, "ggml-alpaca-13b-q4.bin"), - path.resolve(dir, "ggml-model-q4_0.bin") - ) - */ - url = "https://huggingface.co/Pi3141/alpaca-13B-ggml/resolve/main/ggml-model-q4_1.bin" - await this.root.down(url, path.resolve(dir, "ggml-model-q4_0.bin")) - break; - case "30B": - url = "https://huggingface.co/Pi3141/alpaca-30B-ggml/resolve/main/ggml-model-q4_0.bin" - await this.root.down(url, path.resolve(dir, "ggml-model-q4_0.bin")) - break; - - default: - console.log("Select either model 7B, 13B, or 30B") - break; + if (Object.keys(currentVersions).includes(model)) { + url = `https://huggingface.co/Pi3141/alpaca-${model}-ggml/resolve/main/${currentVersions[model]}` + await this.root.down(url, path.resolve(dir, currentVersions[model])) + } else { + console.log("Select either model 7B, 13B, or 30B") } } } diff --git a/index.js b/index.js index ea1cf3a..a6fe753 100644 --- a/index.js +++ b/index.js @@ -225,13 +225,19 @@ class Dalai { seed: req.seed || -1, threads: req.threads || 8, n_predict: req.n_predict || 128, - model: `models/${Model || "7B"}/ggml-model-q4_0.bin`, + model: "", } let e = await exists(path.resolve(this.home, Core, "models", Model)) if (!e) { cb(`File does not exist: ${Model}. Try "dalai ${Core} get ${Model}" first.`) return + } else { + const potentialBinFiles = await fs.promises.readdir(path.resolve(this.home, Core, "models", Model)) + if (potentialBinFiles.length === 0) { + cb(`No model files found in ${Model}. Try "dalai ${Core} get ${Model}" first.`) + } + o.model = path.resolve(this.home, Core, "models", Model, potentialBinFiles[0]) } if (req.top_k) o.top_k = req.top_k @@ -367,8 +373,11 @@ class Dalai { console.log({ modelFolders }) for(let modelFolder of modelFolders) { - let e = await exists(path.resolve(modelsPath, modelFolder, 'ggml-model-q4_0.bin')) - if (e) { + // get bin files in model folder + + const binFiles = fs.readdirSync(path.resolve(modelsPath, modelFolder)).filter((f) => f.endsWith(".bin")) + + if (binFiles.length !== 0) { modelNames.push(`${core}.${modelFolder}`) console.log("exists", modelFolder) } diff --git a/llama.js b/llama.js index 6e54625..d195c91 100644 --- a/llama.js +++ b/llama.js @@ -111,8 +111,16 @@ npx dalai install 7B 13B } for(let i=0; i f.endsWith(`.bin`)) + + if (potentialOutputFiles.length === 0) { + throw new Error(`No bin file found in ./models/${model}/`) + } + const outputFile1 = path.resolve(this.home, `./models/${model}/ggml-model-f16.bin${suffix}`) - const outputFile2 = path.resolve(this.home, `./models/${model}/ggml-model-q4_0.bin${suffix}`) + const outputFile2 = path.resolve(this.home, `./models/${model}/${potentialOutputFiles[0]}${suffix}`) + if (fs.existsSync(outputFile1) && fs.existsSync(outputFile2)) { console.log(`Skip quantization, files already exists: ${outputFile1} and ${outputFile2}}`) continue