Skip to content

Commit 7837af7

Browse files
authored
feat: improve error message when llama.cpp source is not downloaded (#27)
1 parent fd332e1 commit 7837af7

File tree

6 files changed

+27
-21
lines changed

6 files changed

+27
-21
lines changed

src/cli/commands/BuildCommand.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import process from "process";
22
import {CommandModule} from "yargs";
33
import chalk from "chalk";
4+
import fs from "fs-extra";
45
import {compileLlamaCpp} from "../../utils/compileLLamaCpp.js";
56
import withOra from "../../utils/withOra.js";
67
import {clearTempFolder} from "../../utils/clearTempFolder.js";
7-
import {defaultLlamaCppCudaSupport, defaultLlamaCppMetalSupport} from "../../config.js";
8+
import {defaultLlamaCppCudaSupport, defaultLlamaCppMetalSupport, llamaCppDirectory} from "../../config.js";
89

910
type BuildCommand = {
1011
arch?: string,
@@ -43,6 +44,11 @@ export const BuildCommand: CommandModule<object, BuildCommand> = {
4344
};
4445

4546
export async function BuildLlamaCppCommand({arch, nodeTarget, metal, cuda}: BuildCommand) {
47+
if (!(await fs.pathExists(llamaCppDirectory))) {
48+
console.log(chalk.red('llama.cpp is not downloaded. Please run "node-llama-cpp download" first'));
49+
process.exit(1);
50+
}
51+
4652
if (metal && process.platform === "darwin") {
4753
console.log(`${chalk.yellow("Metal:")} enabled`);
4854
}
@@ -59,7 +65,7 @@ export async function BuildLlamaCppCommand({arch, nodeTarget, metal, cuda}: Buil
5965
await compileLlamaCpp({
6066
arch: arch ? arch : undefined,
6167
nodeTarget: nodeTarget ? nodeTarget : undefined,
62-
setUsedBingFlag: true,
68+
setUsedBinFlag: true,
6369
metal,
6470
cuda
6571
});

src/cli/commands/DownloadCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export async function DownloadLlamaCppCommand({
165165
await compileLlamaCpp({
166166
arch: arch ? arch : undefined,
167167
nodeTarget: nodeTarget ? nodeTarget : undefined,
168-
setUsedBingFlag: true,
168+
setUsedBinFlag: true,
169169
metal,
170170
cuda
171171
});

src/llamaEvaluator/LlamaGrammar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class LlamaGrammar {
2626

2727
const grammarFile = path.join(grammarsFolder, type + ".gbnf");
2828

29-
if (await fs.exists(grammarFile)) {
29+
if (await fs.pathExists(grammarFile)) {
3030
const grammar = await fs.readFile(grammarFile, "utf8");
3131
return new LlamaGrammar({grammar});
3232
}

src/utils/compileLLamaCpp.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import {spawnCommand} from "./spawnCommand.js";
1010
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1111

1212
export async function compileLlamaCpp({
13-
arch = process.arch, nodeTarget = process.version, setUsedBingFlag = true, metal = false, cuda = false
13+
arch = process.arch, nodeTarget = process.version, setUsedBinFlag: setUsedBinFlagArg = true, metal = false, cuda = false
1414
}: {
15-
arch?: string, nodeTarget?: string, setUsedBingFlag?: boolean, metal?: boolean, cuda?: boolean
15+
arch?: string, nodeTarget?: string, setUsedBinFlag?: boolean, metal?: boolean, cuda?: boolean
1616
}) {
1717
try {
18-
if (!(await fs.exists(llamaCppDirectory))) {
18+
if (!(await fs.pathExists(llamaCppDirectory))) {
1919
throw new Error(`"${llamaCppDirectory}" directory does not exist`);
2020
}
2121

@@ -46,12 +46,12 @@ export async function compileLlamaCpp({
4646

4747
await spawnCommand("npm", ["run", "-s", "node-gyp-llama", "--", "configure", "--arch=" + arch, "--target=" + nodeTarget, "--", "-f", "compile_commands_json"], __dirname, nodeGypEnv);
4848

49-
if (await fs.exists(path.join(llamaDirectory, "Release", "compile_commands.json"))) {
49+
if (await fs.pathExists(path.join(llamaDirectory, "Release", "compile_commands.json"))) {
5050
await fs.move(
5151
path.join(llamaDirectory, "Release", "compile_commands.json"),
5252
path.join(llamaDirectory, "compile_commands.json")
5353
);
54-
} else if (await fs.exists(path.join(llamaDirectory, "Debug", "compile_commands.json"))) {
54+
} else if (await fs.pathExists(path.join(llamaDirectory, "Debug", "compile_commands.json"))) {
5555
await fs.move(
5656
path.join(llamaDirectory, "Debug", "compile_commands.json"),
5757
path.join(llamaDirectory, "compile_commands.json")
@@ -64,11 +64,11 @@ export async function compileLlamaCpp({
6464

6565
await spawnCommand("npm", ["run", "-s", "node-gyp-llama-build", "--", "--arch=" + arch, "--target=" + nodeTarget], __dirname, nodeGypEnv);
6666

67-
if (setUsedBingFlag) {
67+
if (setUsedBinFlagArg) {
6868
await setUsedBinFlag("localBuildFromSource");
6969
}
7070
} catch (err) {
71-
if (setUsedBingFlag)
71+
if (setUsedBinFlagArg)
7272
await setUsedBinFlag("prebuiltBinaries");
7373

7474
throw err;
@@ -78,7 +78,7 @@ export async function compileLlamaCpp({
7878
export async function getCompiledLlamaCppBinaryPath() {
7979
const modulePath = path.join(__dirname, "..", "..", "llama", "build", "Release", "llama.node");
8080

81-
if (await fs.exists(modulePath))
81+
if (await fs.pathExists(modulePath))
8282
return modulePath;
8383

8484
return null;

src/utils/getBin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export async function getPrebuildBinPath(): Promise<string | null> {
2525
for (const nodeVersion of nodeVersions) {
2626
const binPath = createPath(platform, arch, nodeVersion);
2727

28-
if (await fs.exists(binPath))
28+
if (await fs.pathExists(binPath))
2929
return binPath;
3030
}
3131

@@ -53,9 +53,9 @@ export async function getPrebuildBinPath(): Promise<string | null> {
5353
}
5454

5555
export async function loadBin(): Promise<LlamaCppNodeModule> {
56-
const usedBingFlag = await getUsedBinFlag();
56+
const usedBinFlag = await getUsedBinFlag();
5757

58-
if (usedBingFlag === "prebuiltBinaries") {
58+
if (usedBinFlag === "prebuiltBinaries") {
5959
const prebuildBinPath = await getPrebuildBinPath();
6060

6161
if (prebuildBinPath == null) {

src/utils/getGrammarsFolder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import {llamaBinsGrammarsDirectory, llamaCppGrammarsDirectory} from "../config.j
33
import {getUsedBinFlag} from "./usedBinFlag.js";
44

55
export async function getGrammarsFolder() {
6-
const usedBingFlag = await getUsedBinFlag();
6+
const usedBinFlag = await getUsedBinFlag();
77

8-
if (usedBingFlag === "localBuildFromSource") {
9-
if (await fs.exists(llamaCppGrammarsDirectory))
8+
if (usedBinFlag === "localBuildFromSource") {
9+
if (await fs.pathExists(llamaCppGrammarsDirectory))
1010
return llamaCppGrammarsDirectory;
11-
} else if (usedBingFlag === "prebuiltBinaries") {
12-
if (await fs.exists(llamaBinsGrammarsDirectory))
11+
} else if (usedBinFlag === "prebuiltBinaries") {
12+
if (await fs.pathExists(llamaBinsGrammarsDirectory))
1313
return llamaBinsGrammarsDirectory;
14-
else if (await fs.exists(llamaCppGrammarsDirectory))
14+
else if (await fs.pathExists(llamaCppGrammarsDirectory))
1515
return llamaCppGrammarsDirectory;
1616
}
1717

0 commit comments

Comments
 (0)