Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export llama.cpp release info #91

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ node_modules

/llama/compile_commands.json
/llama/llama.cpp
/llama/llama.cpp.tag.json
/llama/gitRelease.bundle
/llama/.temp
/llama/build
Expand Down
11 changes: 8 additions & 3 deletions src/cli/commands/BuildCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {getIsInDocumentationMode} from "../../state.js";
type BuildCommand = {
arch?: string,
nodeTarget?: string,
metal: boolean,
cuda: boolean
metal?: boolean,
cuda?: boolean
};

export const BuildCommand: CommandModule<object, BuildCommand> = {
Expand Down Expand Up @@ -48,7 +48,12 @@ export const BuildCommand: CommandModule<object, BuildCommand> = {
handler: BuildLlamaCppCommand
};

export async function BuildLlamaCppCommand({arch, nodeTarget, metal, cuda}: BuildCommand) {
export async function BuildLlamaCppCommand({
arch = undefined,
nodeTarget = undefined,
metal = defaultLlamaCppMetalSupport,
cuda = defaultLlamaCppCudaSupport
}: BuildCommand) {
if (!(await fs.pathExists(llamaCppDirectory))) {
console.log(chalk.red('llama.cpp is not downloaded. Please run "node-llama-cpp download" first'));
process.exit(1);
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/ClearCommand.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {CommandModule} from "yargs";
import fs from "fs-extra";
import chalk from "chalk";
import {llamaCppDirectory} from "../../config.js";
import {llamaCppDirectory, llamaCppDirectoryTagFilePath} from "../../config.js";
import withOra from "../../utils/withOra.js";
import {clearLlamaBuild} from "../../utils/clearLlamaBuild.js";
import {setUsedBinFlag} from "../../utils/usedBinFlag.js";
Expand Down Expand Up @@ -35,6 +35,7 @@ export async function ClearLlamaCppBuildCommand({type}: ClearCommand) {
fail: chalk.blue("Failed to clear source")
}, async () => {
await fs.remove(llamaCppDirectory);
await fs.remove(llamaCppDirectoryTagFilePath);
});
}

Expand Down
24 changes: 18 additions & 6 deletions src/cli/commands/DownloadCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {Octokit} from "octokit";
import fs from "fs-extra";
import chalk from "chalk";
import {
defaultLlamaCppCudaSupport, defaultLlamaCppGitHubRepo, defaultLlamaCppMetalSupport, defaultLlamaCppRelease, isCI, llamaCppDirectory
defaultLlamaCppCudaSupport, defaultLlamaCppGitHubRepo, defaultLlamaCppMetalSupport, defaultLlamaCppRelease, isCI,
llamaCppDirectory, llamaCppDirectoryTagFilePath
} from "../../config.js";
import {compileLlamaCpp} from "../../utils/compileLLamaCpp.js";
import withOra from "../../utils/withOra.js";
Expand All @@ -20,14 +21,16 @@ import {
import {cloneLlamaCppRepo} from "../../utils/cloneLlamaCppRepo.js";

type DownloadCommandArgs = {
repo: string,
release: "latest" | string,
repo?: string,
release?: "latest" | string,
arch?: string,
nodeTarget?: string,
metal: boolean,
cuda: boolean,
metal?: boolean,
cuda?: boolean,
skipBuild?: boolean,
noBundle?: boolean,

/** @internal */
updateBinariesReleaseMetadataAndSaveGitBundle?: boolean
};

Expand Down Expand Up @@ -92,7 +95,15 @@ export const DownloadCommand: CommandModule<object, DownloadCommandArgs> = {
};

export async function DownloadLlamaCppCommand({
repo, release, arch, nodeTarget, metal, cuda, skipBuild, noBundle, updateBinariesReleaseMetadataAndSaveGitBundle
repo = defaultLlamaCppGitHubRepo,
release = defaultLlamaCppRelease,
arch = undefined,
nodeTarget = undefined,
metal = defaultLlamaCppMetalSupport,
cuda = defaultLlamaCppCudaSupport,
skipBuild = false,
noBundle = false,
updateBinariesReleaseMetadataAndSaveGitBundle = false
}: DownloadCommandArgs) {
const useBundle = noBundle != true;
const octokit = new Octokit();
Expand Down Expand Up @@ -162,6 +173,7 @@ export async function DownloadLlamaCppCommand({
fail: chalk.blue("Failed to remove existing llama.cpp directory")
}, async () => {
await fs.remove(llamaCppDirectory);
await fs.remove(llamaCppDirectoryTagFilePath);
});

console.log(chalk.blue("Cloning llama.cpp"));
Expand Down
3 changes: 2 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {BuildLlamaCppCommand} from "./cli/commands/BuildCommand.js";
import {DownloadLlamaCppCommand} from "./cli/commands/DownloadCommand.js";
import {ClearLlamaCppBuildCommand} from "./cli/commands/ClearCommand.js";
import {getBuildDefaults} from "./utils/getBuildDefaults.js";

export {BuildLlamaCppCommand, DownloadLlamaCppCommand, ClearLlamaCppBuildCommand};
export {BuildLlamaCppCommand, DownloadLlamaCppCommand, ClearLlamaCppBuildCommand, getBuildDefaults};
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const tempDownloadDirectory = path.join(os.tmpdir(), "node-llama-cpp", uu
export const chatCommandHistoryFilePath = path.join(os.homedir(), ".node-llama-cpp.chat_repl_history");
export const usedBinFlagJsonPath = path.join(llamaDirectory, "usedBin.json");
export const binariesGithubReleasePath = path.join(llamaDirectory, "binariesGithubRelease.json");
export const llamaCppDirectoryTagFilePath = path.join(llamaDirectory, "llama.cpp.tag.json");
export const currentReleaseGitBundlePath = path.join(llamaDirectory, "gitRelease.bundle");
export const xpackDirectory = path.join(llamaDirectory, "xpack");
export const localXpacksStoreDirectory = path.join(xpackDirectory, "store");
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {GeneralChatPromptWrapper} from "./chatWrappers/GeneralChatPromptWrapper.
import {ChatMLChatPromptWrapper} from "./chatWrappers/ChatMLChatPromptWrapper.js";
import {FalconChatPromptWrapper} from "./chatWrappers/FalconChatPromptWrapper.js";
import {getChatWrapperByBos} from "./chatWrappers/createChatWrapperByBos.js";
import {getReleaseInfo} from "./utils/getReleaseInfo.js";

import {type ConversationInteraction, type Token} from "./types.js";
import {
Expand Down Expand Up @@ -48,6 +49,7 @@ export {
ChatMLChatPromptWrapper,
FalconChatPromptWrapper,
getChatWrapperByBos,
getReleaseInfo,
type Token,
type GbnfJsonSchema,
type GbnfJsonSchemaToType,
Expand Down
35 changes: 34 additions & 1 deletion src/utils/cloneLlamaCppRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import simpleGit, {SimpleGit} from "simple-git";
import cliProgress from "cli-progress";
import chalk from "chalk";
import fs from "fs-extra";
import {llamaCppDirectory} from "../config.js";
import {llamaCppDirectory, llamaCppDirectoryTagFilePath} from "../config.js";
import {getGitBundlePathForRelease} from "./gitReleaseBundles.js";

type ClonedLlamaCppRepoTagFile = {
tag: string
};


export async function cloneLlamaCppRepo(githubOwner: string, githubRepo: string, tag: string, useBundles: boolean = true) {
const gitBundleForTag = !useBundles ? null : await getGitBundlePathForRelease(githubOwner, githubRepo, tag);
Expand Down Expand Up @@ -54,6 +58,7 @@ export async function cloneLlamaCppRepo(githubOwner: string, githubRepo: string,
return;
} catch (err) {
await fs.remove(llamaCppDirectory);
await fs.remove(llamaCppDirectoryTagFilePath);
console.error("Failed to clone git bundle, cloning from GitHub instead", err);

printCloneErrorHelp(String(err));
Expand All @@ -73,6 +78,20 @@ export async function cloneLlamaCppRepo(githubOwner: string, githubRepo: string,

throw err;
}

try {
const clonedLlamaCppRepoTagJson: ClonedLlamaCppRepoTagFile = {
tag
};

await fs.writeJson(llamaCppDirectoryTagFilePath, clonedLlamaCppRepoTagJson, {
spaces: 4
});
} catch (err) {
console.error("Failed to write llama.cpp tag file", err);

throw err;
}
}

function printCloneErrorHelp(error: string) {
Expand All @@ -87,3 +106,17 @@ function printCloneErrorHelp(error: string) {
'git config --global --add safe.directory "*"'
);
}

export async function getClonedLlamaCppRepoReleaseTag() {
if (!(await fs.pathExists(llamaCppDirectoryTagFilePath)))
return null;

try {
const clonedLlamaCppRepoTagJson: ClonedLlamaCppRepoTagFile = await fs.readJson(llamaCppDirectoryTagFilePath);

return clonedLlamaCppRepoTagJson.tag;
} catch (err) {
console.error("Failed to read llama.cpp tag file", err);
return null;
}
}
12 changes: 12 additions & 0 deletions src/utils/getBuildDefaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {
defaultLlamaCppCudaSupport, defaultLlamaCppGitHubRepo, defaultLlamaCppMetalSupport, defaultLlamaCppRelease
} from "../config.js";

export async function getBuildDefaults() {
return {
repo: defaultLlamaCppGitHubRepo,
release: defaultLlamaCppRelease,
metalSupport: defaultLlamaCppMetalSupport,
cudaSupport: defaultLlamaCppCudaSupport
};
}
35 changes: 35 additions & 0 deletions src/utils/getReleaseInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import path from "path";
import {fileURLToPath} from "url";
import fs from "fs-extra";
import {getUsedBinFlag} from "./usedBinFlag.js";
import {getClonedLlamaCppRepoReleaseTag} from "./cloneLlamaCppRepo.js";
import {getBinariesGithubRelease} from "./binariesGithubRelease.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export async function getReleaseInfo() {
const [usedBinFlag, moduleVersion] = await Promise.all([
getUsedBinFlag(),
getModuleVersion()
]);

const release = usedBinFlag === "prebuiltBinaries"
? await getBinariesGithubRelease()
: (await getClonedLlamaCppRepoReleaseTag() ?? await getBinariesGithubRelease());

return {
llamaCpp: {
binarySource: usedBinFlag === "prebuiltBinaries"
? "included"
: "builtLocally",
release
},
moduleVersion
};
}

async function getModuleVersion(): Promise<string> {
const packageJson = await fs.readJson(path.join(__dirname, "..", "..", "package.json"));

return packageJson.version;
}