@tobilu/qmd 1.0.0 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +287 -42
- package/README.md +3 -1
- package/dist/collections.d.ts +115 -0
- package/dist/collections.js +282 -0
- package/dist/db.d.ts +33 -0
- package/dist/db.js +34 -0
- package/dist/formatter.d.ts +119 -0
- package/dist/formatter.js +350 -0
- package/dist/llm.d.ts +375 -0
- package/dist/llm.js +1036 -0
- package/dist/mcp.d.ts +21 -0
- package/dist/mcp.js +545 -0
- package/dist/qmd.d.ts +1 -0
- package/dist/qmd.js +2231 -0
- package/dist/store.d.ts +696 -0
- package/dist/store.js +2374 -0
- package/package.json +4 -4
- package/qmd +1 -1
- package/src/bench-rerank.ts +0 -327
- package/src/collections.ts +0 -390
- package/src/db.ts +0 -52
- package/src/formatter.ts +0 -429
- package/src/llm.ts +0 -1397
- package/src/mcp.ts +0 -687
- package/src/qmd.ts +0 -2568
- package/src/store.ts +0 -3057
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tobilu/qmd",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Query Markup Documents - On-device hybrid search for markdown files with BM25, vector search, and LLM reranking",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"qmd": "qmd"
|
|
8
8
|
},
|
|
9
9
|
"files": [
|
|
10
|
-
"
|
|
11
|
-
"!src/**/*.test.ts",
|
|
12
|
-
"!src/test-preload.ts",
|
|
10
|
+
"dist/",
|
|
13
11
|
"qmd",
|
|
14
12
|
"LICENSE",
|
|
15
13
|
"CHANGELOG.md"
|
|
16
14
|
],
|
|
17
15
|
"scripts": {
|
|
16
|
+
"prepare": "[ -d .git ] && ./scripts/install-hooks.sh || true",
|
|
17
|
+
"build": "tsc -p tsconfig.build.json",
|
|
18
18
|
"test": "vitest run --reporter=verbose test/",
|
|
19
19
|
"qmd": "tsx src/qmd.ts",
|
|
20
20
|
"index": "tsx src/qmd.ts index",
|
package/qmd
CHANGED
package/src/bench-rerank.ts
DELETED
|
@@ -1,327 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
/**
|
|
3
|
-
* QMD Reranker Benchmark
|
|
4
|
-
*
|
|
5
|
-
* Measures reranking performance across different configurations.
|
|
6
|
-
* Reports device, parallelism, memory, VRAM, and throughput.
|
|
7
|
-
*
|
|
8
|
-
* Usage:
|
|
9
|
-
* bun src/bench-rerank.ts # full benchmark
|
|
10
|
-
* bun src/bench-rerank.ts --quick # quick smoke test (10 docs, 1 iteration)
|
|
11
|
-
* bun src/bench-rerank.ts --docs 100 # custom doc count
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import {
|
|
15
|
-
getLlama,
|
|
16
|
-
getLlamaGpuTypes,
|
|
17
|
-
resolveModelFile,
|
|
18
|
-
LlamaLogLevel,
|
|
19
|
-
type Llama,
|
|
20
|
-
type LlamaModel,
|
|
21
|
-
} from "node-llama-cpp";
|
|
22
|
-
import { homedir } from "os";
|
|
23
|
-
import { join } from "path";
|
|
24
|
-
import { cpus } from "os";
|
|
25
|
-
|
|
26
|
-
// ============================================================================
|
|
27
|
-
// Config
|
|
28
|
-
// ============================================================================
|
|
29
|
-
|
|
30
|
-
const RERANK_MODEL = "hf:ggml-org/Qwen3-Reranker-0.6B-Q8_0-GGUF/qwen3-reranker-0.6b-q8_0.gguf";
|
|
31
|
-
const MODEL_CACHE = join(homedir(), ".cache", "qmd", "models");
|
|
32
|
-
const CONTEXT_SIZE = 2048;
|
|
33
|
-
|
|
34
|
-
const args = process.argv.slice(2);
|
|
35
|
-
const quick = args.includes("--quick");
|
|
36
|
-
const docsIdx = args.indexOf("--docs");
|
|
37
|
-
const DOC_COUNT = docsIdx >= 0 ? parseInt(args[docsIdx + 1]!) : (quick ? 10 : 40);
|
|
38
|
-
const ITERATIONS = quick ? 1 : 3;
|
|
39
|
-
const PARALLEL_CONFIGS = quick ? [1, 4] : [1, 2, 4, 8];
|
|
40
|
-
|
|
41
|
-
// ============================================================================
|
|
42
|
-
// Test data — realistic-ish chunks of varying length
|
|
43
|
-
// ============================================================================
|
|
44
|
-
|
|
45
|
-
const QUERY = "How do AI agents work and what are their limitations?";
|
|
46
|
-
|
|
47
|
-
function generateDocs(n: number): string[] {
|
|
48
|
-
const templates = [
|
|
49
|
-
"Artificial intelligence agents are software systems that perceive their environment and take actions to achieve goals. They use techniques like reinforcement learning, planning, and natural language processing to operate autonomously.",
|
|
50
|
-
"The transformer architecture, introduced in 2017, revolutionized natural language processing. Self-attention mechanisms allow models to weigh the importance of different parts of input sequences when generating outputs.",
|
|
51
|
-
"Machine learning models require careful evaluation to avoid overfitting. Cross-validation, holdout sets, and metrics like precision, recall, and F1 score help assess generalization performance.",
|
|
52
|
-
"Retrieval-augmented generation combines information retrieval with language models. Documents are embedded into vector spaces, retrieved based on query similarity, and used as context for generation.",
|
|
53
|
-
"Neural network training involves forward propagation, loss computation, and backpropagation. Optimizers like Adam and SGD adjust weights to minimize the loss function over training iterations.",
|
|
54
|
-
"Large language models exhibit emergent capabilities at scale, including few-shot learning, chain-of-thought reasoning, and instruction following. These properties were not explicitly trained for.",
|
|
55
|
-
"Embedding models convert text into dense vector representations that capture semantic meaning. Similar texts produce similar vectors, enabling efficient similarity search and clustering.",
|
|
56
|
-
"Autonomous agents face challenges including hallucination, lack of grounding, limited planning horizons, and difficulty with multi-step reasoning. Safety and alignment remain open research problems.",
|
|
57
|
-
"The attention mechanism computes query-key-value interactions to determine which parts of the input are most relevant. Multi-head attention allows the model to attend to different representation subspaces.",
|
|
58
|
-
"Fine-tuning adapts a pre-trained model to specific tasks using domain-specific data. Techniques like LoRA reduce the number of trainable parameters while maintaining performance.",
|
|
59
|
-
];
|
|
60
|
-
return Array.from({ length: n }, (_, i) => templates[i % templates.length]!);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// ============================================================================
|
|
64
|
-
// Helpers
|
|
65
|
-
// ============================================================================
|
|
66
|
-
|
|
67
|
-
function formatBytes(bytes: number): string {
|
|
68
|
-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
69
|
-
if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
70
|
-
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function getMemUsage(): { rss: number; heapUsed: number } {
|
|
74
|
-
const m = process.memoryUsage();
|
|
75
|
-
return { rss: m.rss, heapUsed: m.heapUsed };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function median(arr: number[]): number {
|
|
79
|
-
const sorted = [...arr].sort((a, b) => a - b);
|
|
80
|
-
const mid = Math.floor(sorted.length / 2);
|
|
81
|
-
return sorted.length % 2 !== 0 ? sorted[mid]! : (sorted[mid - 1]! + sorted[mid]!) / 2;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// ============================================================================
|
|
85
|
-
// Benchmark runner
|
|
86
|
-
// ============================================================================
|
|
87
|
-
|
|
88
|
-
interface BenchResult {
|
|
89
|
-
parallelism: number;
|
|
90
|
-
contextSize: number;
|
|
91
|
-
flashAttention: boolean;
|
|
92
|
-
times: number[]; // ms per run
|
|
93
|
-
medianMs: number;
|
|
94
|
-
docsPerSec: number;
|
|
95
|
-
vramPerContext: number; // bytes
|
|
96
|
-
totalVram: number; // bytes
|
|
97
|
-
peakRss: number; // bytes
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async function benchmarkConfig(
|
|
101
|
-
model: LlamaModel,
|
|
102
|
-
llama: Llama,
|
|
103
|
-
docs: string[],
|
|
104
|
-
parallelism: number,
|
|
105
|
-
flash: boolean,
|
|
106
|
-
): Promise<BenchResult> {
|
|
107
|
-
// Measure VRAM before
|
|
108
|
-
const vramBefore = llama.gpu ? await llama.getVramState() : null;
|
|
109
|
-
const rssBefore = getMemUsage().rss;
|
|
110
|
-
|
|
111
|
-
// Create contexts. On CPU, split threads evenly across contexts.
|
|
112
|
-
const cpuThreads = !llama.gpu ? Math.floor(llama.cpuMathCores / parallelism) : 0;
|
|
113
|
-
const contexts = [];
|
|
114
|
-
for (let i = 0; i < parallelism; i++) {
|
|
115
|
-
try {
|
|
116
|
-
contexts.push(await model.createRankingContext({
|
|
117
|
-
contextSize: CONTEXT_SIZE,
|
|
118
|
-
flashAttention: flash,
|
|
119
|
-
...(cpuThreads > 0 ? { threads: cpuThreads } : {}),
|
|
120
|
-
}));
|
|
121
|
-
} catch {
|
|
122
|
-
if (contexts.length === 0) {
|
|
123
|
-
// Try without flash
|
|
124
|
-
contexts.push(await model.createRankingContext({
|
|
125
|
-
contextSize: CONTEXT_SIZE,
|
|
126
|
-
...(cpuThreads > 0 ? { threads: cpuThreads } : {}),
|
|
127
|
-
}));
|
|
128
|
-
}
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
const actualParallelism = contexts.length;
|
|
133
|
-
|
|
134
|
-
// Measure VRAM after context creation
|
|
135
|
-
const vramAfter = llama.gpu ? await llama.getVramState() : null;
|
|
136
|
-
const vramUsed = vramBefore && vramAfter ? vramAfter.used - vramBefore.used : 0;
|
|
137
|
-
const vramPerCtx = actualParallelism > 0 ? vramUsed / actualParallelism : 0;
|
|
138
|
-
|
|
139
|
-
// Warm up
|
|
140
|
-
await contexts[0]!.rankAll(QUERY, docs.slice(0, 2));
|
|
141
|
-
|
|
142
|
-
// Benchmark iterations
|
|
143
|
-
const times: number[] = [];
|
|
144
|
-
let peakRss = getMemUsage().rss;
|
|
145
|
-
|
|
146
|
-
for (let iter = 0; iter < ITERATIONS; iter++) {
|
|
147
|
-
const chunkSize = Math.ceil(docs.length / actualParallelism);
|
|
148
|
-
|
|
149
|
-
const t0 = performance.now();
|
|
150
|
-
const allScores = await Promise.all(
|
|
151
|
-
Array.from({ length: actualParallelism }, (_, i) => {
|
|
152
|
-
const chunk = docs.slice(i * chunkSize, (i + 1) * chunkSize);
|
|
153
|
-
return chunk.length > 0 ? contexts[i]!.rankAll(QUERY, chunk) : Promise.resolve([]);
|
|
154
|
-
})
|
|
155
|
-
);
|
|
156
|
-
const elapsed = performance.now() - t0;
|
|
157
|
-
times.push(elapsed);
|
|
158
|
-
|
|
159
|
-
// Verify scores are valid
|
|
160
|
-
const flat = allScores.flat();
|
|
161
|
-
if (flat.some(s => s < 0 || s > 1 || isNaN(s))) {
|
|
162
|
-
throw new Error("Invalid scores detected");
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const currentRss = getMemUsage().rss;
|
|
166
|
-
if (currentRss > peakRss) peakRss = currentRss;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Cleanup
|
|
170
|
-
for (const ctx of contexts) await ctx.dispose();
|
|
171
|
-
|
|
172
|
-
const med = median(times);
|
|
173
|
-
return {
|
|
174
|
-
parallelism: actualParallelism,
|
|
175
|
-
contextSize: CONTEXT_SIZE,
|
|
176
|
-
flashAttention: flash,
|
|
177
|
-
times,
|
|
178
|
-
medianMs: med,
|
|
179
|
-
docsPerSec: (docs.length / med) * 1000,
|
|
180
|
-
vramPerContext: vramPerCtx,
|
|
181
|
-
totalVram: vramUsed,
|
|
182
|
-
peakRss,
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// ============================================================================
|
|
187
|
-
// Main
|
|
188
|
-
// ============================================================================
|
|
189
|
-
|
|
190
|
-
async function main() {
|
|
191
|
-
console.log("═══════════════════════════════════════════════════════════════");
|
|
192
|
-
console.log(" QMD Reranker Benchmark");
|
|
193
|
-
console.log("═══════════════════════════════════════════════════════════════\n");
|
|
194
|
-
|
|
195
|
-
// Detect GPU
|
|
196
|
-
const gpuTypes = await getLlamaGpuTypes();
|
|
197
|
-
const preferred = (["cuda", "metal", "vulkan"] as const).find(g => gpuTypes.includes(g));
|
|
198
|
-
|
|
199
|
-
let llama: Llama;
|
|
200
|
-
let gpuLabel: string;
|
|
201
|
-
if (preferred) {
|
|
202
|
-
try {
|
|
203
|
-
llama = await getLlama({ gpu: preferred, logLevel: LlamaLogLevel.error });
|
|
204
|
-
gpuLabel = `${preferred}`;
|
|
205
|
-
} catch {
|
|
206
|
-
llama = await getLlama({ gpu: false, logLevel: LlamaLogLevel.error });
|
|
207
|
-
gpuLabel = "cpu (gpu init failed)";
|
|
208
|
-
}
|
|
209
|
-
} else {
|
|
210
|
-
llama = await getLlama({ gpu: false, logLevel: LlamaLogLevel.error });
|
|
211
|
-
gpuLabel = "cpu";
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// System info
|
|
215
|
-
const cpuInfo = cpus();
|
|
216
|
-
const cpuModel = cpuInfo[0]?.model || "unknown";
|
|
217
|
-
const cpuCount = cpuInfo.length;
|
|
218
|
-
|
|
219
|
-
console.log("System");
|
|
220
|
-
console.log(` CPU: ${cpuModel}`);
|
|
221
|
-
console.log(` Cores: ${cpuCount} (${llama.cpuMathCores} math)`);
|
|
222
|
-
console.log(` Device: ${gpuLabel}`);
|
|
223
|
-
|
|
224
|
-
if (llama.gpu) {
|
|
225
|
-
const gpuNames = await llama.getGpuDeviceNames();
|
|
226
|
-
const counts = new Map<string, number>();
|
|
227
|
-
for (const name of gpuNames) counts.set(name, (counts.get(name) || 0) + 1);
|
|
228
|
-
const devStr = Array.from(counts.entries())
|
|
229
|
-
.map(([name, n]) => n > 1 ? `${n}× ${name}` : name).join(", ");
|
|
230
|
-
console.log(` GPU: ${devStr}`);
|
|
231
|
-
const vram = await llama.getVramState();
|
|
232
|
-
console.log(` VRAM: ${formatBytes(vram.total)} total, ${formatBytes(vram.free)} free`);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
console.log(` RAM: ${formatBytes(getMemUsage().rss)} RSS at start`);
|
|
236
|
-
|
|
237
|
-
// Load model
|
|
238
|
-
console.log(`\nModel`);
|
|
239
|
-
console.log(` URI: ${RERANK_MODEL}`);
|
|
240
|
-
const modelPath = await resolveModelFile(RERANK_MODEL, MODEL_CACHE);
|
|
241
|
-
const vramPreModel = llama.gpu ? await llama.getVramState() : null;
|
|
242
|
-
const model = await llama.loadModel({ modelPath });
|
|
243
|
-
const vramPostModel = llama.gpu ? await llama.getVramState() : null;
|
|
244
|
-
const modelVram = vramPreModel && vramPostModel ? vramPostModel.used - vramPreModel.used : 0;
|
|
245
|
-
console.log(` Params: ${model.trainContextSize} train ctx`);
|
|
246
|
-
if (modelVram > 0) console.log(` VRAM: ${formatBytes(modelVram)} (model weights)`);
|
|
247
|
-
|
|
248
|
-
// Generate test docs
|
|
249
|
-
const docs = generateDocs(DOC_COUNT);
|
|
250
|
-
console.log(`\nBenchmark`);
|
|
251
|
-
console.log(` Documents: ${DOC_COUNT}`);
|
|
252
|
-
console.log(` Ctx size: ${CONTEXT_SIZE}`);
|
|
253
|
-
console.log(` Iterations:${ITERATIONS}`);
|
|
254
|
-
console.log(` Query: "${QUERY.slice(0, 50)}..."`);
|
|
255
|
-
|
|
256
|
-
// Run benchmarks
|
|
257
|
-
const results: BenchResult[] = [];
|
|
258
|
-
|
|
259
|
-
for (const p of PARALLEL_CONFIGS) {
|
|
260
|
-
if (!llama.gpu && p > 1) {
|
|
261
|
-
// CPU: only test if we have enough cores (at least 4 per context)
|
|
262
|
-
if (llama.cpuMathCores < p * 4) {
|
|
263
|
-
console.log(`\n [${p} ctx] skipped (need ${p * 4} cores, have ${llama.cpuMathCores})`);
|
|
264
|
-
continue;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// Test with flash attention
|
|
269
|
-
process.stdout.write(`\n [${p} ctx, flash] running...`);
|
|
270
|
-
try {
|
|
271
|
-
const r = await benchmarkConfig(model, llama, docs, p, true);
|
|
272
|
-
results.push(r);
|
|
273
|
-
process.stdout.write(` ${r.medianMs.toFixed(0)}ms (${r.docsPerSec.toFixed(1)} docs/s)\n`);
|
|
274
|
-
} catch (e: any) {
|
|
275
|
-
process.stdout.write(` failed: ${e.message}\n`);
|
|
276
|
-
// Try without flash
|
|
277
|
-
process.stdout.write(` [${p} ctx, no flash] running...`);
|
|
278
|
-
try {
|
|
279
|
-
const r = await benchmarkConfig(model, llama, docs, p, false);
|
|
280
|
-
results.push(r);
|
|
281
|
-
process.stdout.write(` ${r.medianMs.toFixed(0)}ms (${r.docsPerSec.toFixed(1)} docs/s)\n`);
|
|
282
|
-
} catch (e2: any) {
|
|
283
|
-
process.stdout.write(` failed: ${e2.message}\n`);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// Summary table
|
|
289
|
-
console.log("\n═══════════════════════════════════════════════════════════════");
|
|
290
|
-
console.log(" Results");
|
|
291
|
-
console.log("═══════════════════════════════════════════════════════════════\n");
|
|
292
|
-
|
|
293
|
-
const header = " Ctx Flash Median Docs/s VRAM/ctx Total VRAM Peak RSS";
|
|
294
|
-
const sep = " ─── ───── ────── ────── ──────── ────────── ────────";
|
|
295
|
-
console.log(header);
|
|
296
|
-
console.log(sep);
|
|
297
|
-
|
|
298
|
-
const baseline = results[0]?.medianMs ?? 1;
|
|
299
|
-
for (const r of results) {
|
|
300
|
-
const speedup = baseline / r.medianMs;
|
|
301
|
-
const speedupStr = r === results[0] ? " " : `(${speedup.toFixed(1)}×)`;
|
|
302
|
-
console.log(
|
|
303
|
-
` ${String(r.parallelism).padStart(3)} ` +
|
|
304
|
-
`${r.flashAttention ? " yes " : " no "} ` +
|
|
305
|
-
`${r.medianMs.toFixed(0).padStart(5)}ms ` +
|
|
306
|
-
`${r.docsPerSec.toFixed(1).padStart(6)} ` +
|
|
307
|
-
`${formatBytes(r.vramPerContext).padStart(8)} ` +
|
|
308
|
-
`${formatBytes(r.totalVram).padStart(10)} ` +
|
|
309
|
-
`${formatBytes(r.peakRss).padStart(8)} ` +
|
|
310
|
-
speedupStr
|
|
311
|
-
);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
// Best config
|
|
315
|
-
if (results.length > 0) {
|
|
316
|
-
const best = results.reduce((a, b) => a.docsPerSec > b.docsPerSec ? a : b);
|
|
317
|
-
console.log(`\n Best: ${best.parallelism} contexts, flash=${best.flashAttention}`);
|
|
318
|
-
console.log(` ${best.medianMs.toFixed(0)}ms for ${DOC_COUNT} docs (${best.docsPerSec.toFixed(1)} docs/s)`);
|
|
319
|
-
if (best.totalVram > 0) console.log(` ${formatBytes(best.totalVram)} VRAM`);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
console.log("");
|
|
323
|
-
await model.dispose();
|
|
324
|
-
await llama.dispose();
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
main().catch(console.error);
|