Skip to content

Commit f63b6d0

Browse files
committed
initial commit
0 parents  commit f63b6d0

File tree

7 files changed

+2739
-0
lines changed

7 files changed

+2739
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
models/*
2+
!models/README.md
3+
node_modules

index.mjs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { fileURLToPath } from "url";
2+
import path from "path";
3+
import os from "os";
4+
import readline from "readline";
5+
import fs from "fs/promises";
6+
import inquirer from "inquirer";
7+
import { ChatLlamaCpp } from "langchain/chat_models/llama_cpp";
8+
9+
// Ensure no-metal binding is installed if on MacOS Intel: npx --no node-llama-cpp download --no-metal
10+
const cpus = os.cpus();
11+
if (cpus.some((cpu) => cpu.model.includes("Apple"))) {
12+
console.log("Is Apple Silicon.");
13+
process.env.NODE_LLAMA_CPP_METAL = true;
14+
} else {
15+
process.env.NODE_LLAMA_CPP_METAL = false;
16+
}
17+
18+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
19+
20+
async function main() {
21+
const modelsDir = path.join(__dirname, "models");
22+
const files = await fs.readdir(modelsDir);
23+
const ggufFiles = files.filter((file) => file.endsWith(".gguf"));
24+
25+
const { selectedModel } = await inquirer.prompt([
26+
{
27+
type: "list",
28+
name: "selectedModel",
29+
message: "Select a model to load:",
30+
choices: ggufFiles,
31+
},
32+
]);
33+
34+
const model = new ChatLlamaCpp({
35+
modelPath: path.join(modelsDir, selectedModel),
36+
temperature: 0.7,
37+
});
38+
39+
console.clear();
40+
console.log(`\x1b[32mSelected Model: ${selectedModel}\x1b[0m\n`);
41+
42+
const rl = readline.createInterface({
43+
input: process.stdin,
44+
output: process.stdout,
45+
});
46+
47+
rl.setPrompt("\nYou: ");
48+
rl.prompt();
49+
50+
rl.on("line", async (line) => {
51+
const stream = await model.stream(line);
52+
process.stdout.write("\nResponse: ");
53+
54+
for await (const chunk of stream) {
55+
process.stdout.write(chunk.content);
56+
}
57+
58+
console.log();
59+
rl.prompt();
60+
}).on("close", () => {
61+
console.log("Chat session ended.");
62+
process.exit(0);
63+
});
64+
}
65+
66+
main();

models/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Instructions
2+
3+
- Place your model files in this directory.
4+
- Ensure that the models are in the `.gguf` file format.
5+
6+
This setup enables the chatbot to detect and use the models for generating responses.
7+
8+
## Model Selection
9+
10+
- When starting the chatbot, you will be prompted to select a model.
11+
- Only models in `.gguf` format within this folder will be listed.

output.txt

Lines changed: 377 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "llama",
3+
"type": "module",
4+
"version": "1.0.0",
5+
"main": "index.js",
6+
"license": "MIT",
7+
"dependencies": {
8+
"inquirer": "^9.2.12",
9+
"ipull": "^1.2.1",
10+
"langchain": "^0.0.201",
11+
"node-llama-cpp": "^2.8.0",
12+
"prettier": "^3.1.0"
13+
},
14+
"scripts": {
15+
"lint": "prettier --write index.mjs"
16+
}
17+
}

0 commit comments

Comments
 (0)