-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-llama-cpp.ts
More file actions
75 lines (67 loc) · 2.49 KB
/
Copy pathnode-llama-cpp.ts
File metadata and controls
75 lines (67 loc) · 2.49 KB
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
66
67
68
69
70
71
72
73
74
75
/**
* Fully in-process: embeddings from a GGUF model through `node-llama-cpp`, and
* persistence in SQLite through `better-sqlite3`. No server, no socket, no
* network stack at all.
*
* Both are **optional peers**. limbic's core has zero runtime dependencies;
* these adapters `import()` their package lazily and throw
* `EmbedderUnavailableError` with an install hint if it is not there, so a
* consumer who never touches them never installs them.
*
* Prerequisites:
* npm i node-llama-cpp better-sqlite3
* # and a GGUF embedding model on disk, e.g. nomic-embed-text-v1.5.Q4_K_M.gguf
*
* Run (`tsx` is fetched by npx; it is not a devDependency):
* MODEL_PATH=/path/to/model.gguf npx tsx examples/node-llama-cpp.ts
*/
import {
NodeLlamaCppEmbedder,
SqliteStore,
createLimbic,
isEmbedderUnavailable,
} from "../src/index.js";
async function main(): Promise<void> {
const modelPath = process.env["MODEL_PATH"];
if (modelPath === undefined) {
throw new Error("set MODEL_PATH to a GGUF embedding model");
}
// The store owns its own file. `SqliteStore` mirrors the origin engine's `memories`
// table, embeddings included: a BLOB of little-endian f32, byte-compatible
// with the layout the origin engine writes.
const store = await SqliteStore.open("./limbic-example.db");
const limbic = createLimbic({
store,
embedder: new NodeLlamaCppEmbedder({ modelPath }),
// No `complete`: this example never calls an LLM, so `extract()` would
// throw rather than quietly return nothing. Scoring and retrieval do not
// need one.
});
await limbic.remember("User's cat is called Whiskers", {
category: "relationship",
importance: 0.8,
keywords: ["cat", "whiskers", "pet"],
});
await limbic.remember("User is allergic to shellfish", {
category: "health",
importance: 0.95,
keywords: ["allergy", "shellfish"],
});
for (const hit of await limbic.retrieve("tell me about the pet", 3)) {
console.log(`${hit.score.toFixed(4)} ${hit.memory.content}`);
}
// Release the SQLite handle and the loaded GGUF model. This example exits
// right after, but as the reference usage it must not teach a long-running
// caller to leak both.
await limbic.close();
}
main().catch((error: unknown) => {
if (isEmbedderUnavailable(error)) {
// The adapter says what to install rather than leaving the caller with a
// bare ERR_MODULE_NOT_FOUND.
console.error(error.message);
} else {
console.error(error);
}
process.exitCode = 1;
});