forked from cocktailpeanut/dalai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alpaca.js
65 lines (64 loc) · 2.58 KB
/
alpaca.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const path = require('path');
const term = require( 'terminal-kit' ).terminal;
const git = require('isomorphic-git');
const Downloader = require("nodejs-file-downloader");
const http = require('isomorphic-git/http/node');
const os = require('os');
const fs = require("fs");
const platform = os.platform()
class Alpaca {
constructor(root) {
this.root = root
this.home = path.resolve(this.root.home, "alpaca")
this.url = "https://github.com/cocktailpeanut/alpaca.cpp.git"
}
async make() {
let success
if (platform === "win32") {
// CMake on Windows
const venv_path = path.join(this.root.home, "venv")
const cmake_path = path.join(venv_path, "Scripts", "cmake")
await this.root.exec("mkdir build", this.home)
await this.root.exec(`Remove-Item -path ${path.resolve(this.home, "build", "CMakeCache.txt")}`, this.home)
await this.root.exec(`${cmake_path} ..`, path.resolve(this.home, "build"))
await this.root.exec(`${cmake_path} --build . --config Release`, path.resolve(this.home, "build"))
} else {
// Make on linux + mac
success = await this.root.exec(`make`, this.home)
if (!success) {
throw new Error("running 'make' failed")
return
}
}
}
async add (...models) {
models = models.map((m) => {
return m.toUpperCase()
})
console.log("alpaca.add", models)
for(let model of models) {
const venv_path = path.join(this.root.home, "venv")
const python_path = platform == "win32" ? path.join(venv_path, "Scripts", "python.exe") : path.join(venv_path, 'bin', 'python')
/**************************************************************************************************************
*
* 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 task = `downloading ${outputFile}`
const url = "https://ipfs.io/ipfs/QmQ1bf2BTnYxq73MFJWu1B7bQ2UD6qG7D7YDCxhTndVkPC"
const dir = path.resolve(this.home, "models", model)
console.log("dir", dir)
await fs.promises.mkdir(dir, { recursive: true }).catch((e) => {
console.log("mkdir", e)
})
console.log("down", url)
await this.root.down(url, path.resolve(dir, "ggml-model-q4_0.bin"))
}
}
}
}
module.exports = Alpaca