llm-kb 0.0.1 → 0.1.0

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/README.md CHANGED
@@ -1,21 +1,113 @@
1
1
  # llm-kb
2
2
 
3
- LLM-powered knowledge base. Drop documents, build a wiki, ask questions.
3
+ Drop files into a folder. Get a knowledge base you can query.
4
4
 
5
5
  Inspired by [Karpathy's LLM Knowledge Bases](https://x.com/karpathy/status/2039805659525644595).
6
6
 
7
+ ## Quick Start
8
+
7
9
  ```bash
8
- npx llm-kb run ./my-documents
10
+ npm install -g llm-kb
11
+ llm-kb run ./my-documents
9
12
  ```
10
13
 
14
+ That's it. Your PDFs get parsed to markdown, an index is built, and a file watcher keeps it up to date.
15
+
16
+ ### Prerequisites
17
+
18
+ - **Node.js 18+**
19
+ - **Pi SDK** installed and authenticated (`npm install -g @mariozechner/pi-coding-agent` + run `pi` once to set up auth)
20
+
21
+ Pi handles the LLM auth — no separate API key configuration needed.
22
+
11
23
  ## What It Does
12
24
 
13
- - **Ingest** — drop PDFs, Excel, Word, PowerPoint, images, or text into a folder
14
- - **Parse** — automatically converts to markdown (LiteParse for PDFs, ExcelJS for spreadsheets, Mammoth for Word)
15
- - **Index** — LLM reads all sources, maintains an index with summaries and topics
16
- - **Query** — ask questions, get answers with citations
17
- - **Research** — answers saved back to the wiki, compounding knowledge
18
- - **Eval** — checks answers against sources, reports failures
25
+ ```
26
+ llm-kb run ./my-documents
27
+ ```
28
+
29
+ ```
30
+ llm-kb v0.0.1
31
+
32
+ Scanning ./my-documents...
33
+ Found 9 files (9 PDF)
34
+ 9 parsed
35
+
36
+ Building index...
37
+ Index built: .llm-kb/wiki/index.md
38
+
39
+ Output: ./my-documents/.llm-kb/wiki/sources
40
+
41
+ Watching for new files... (Ctrl+C to stop)
42
+ ```
43
+
44
+ 1. **Scans** the folder for PDFs
45
+ 2. **Parses** each PDF to markdown + bounding boxes (using [LiteParse](https://github.com/run-llama/liteparse))
46
+ 3. **Builds an index** — Pi SDK agent reads all sources and writes `index.md` with summaries
47
+ 4. **Watches** — drop a new PDF in while it's running, it gets parsed and indexed automatically
48
+
49
+ ### What It Creates
50
+
51
+ ```
52
+ ./my-documents/
53
+ ├── (your files — untouched)
54
+ └── .llm-kb/
55
+ └── wiki/
56
+ ├── index.md ← summary of all sources
57
+ └── sources/
58
+ ├── report.md ← parsed text (spatial layout)
59
+ ├── report.json ← bounding boxes (for citations)
60
+ └── ...
61
+ ```
62
+
63
+ Your original files are never modified. Delete `.llm-kb/` to start fresh.
64
+
65
+ ## OCR for Scanned PDFs
66
+
67
+ Most PDFs have native text — they just work. For scanned PDFs:
68
+
69
+ **Local (default when enabled):**
70
+ ```bash
71
+ OCR_ENABLED=true llm-kb run ./my-documents
72
+ ```
73
+ Uses Tesseract.js (built-in, slower but works everywhere).
74
+
75
+ **Remote OCR server (faster, better quality):**
76
+ ```bash
77
+ OCR_SERVER_URL="http://localhost:8080/ocr?key=YOUR_KEY" llm-kb run ./my-documents
78
+ ```
79
+ Routes scanned pages to an Azure Document Intelligence bridge. Native-text pages still processed locally (free).
80
+
81
+ ## Non-PDF Files
82
+
83
+ PDFs are parsed at ingest time. Other file types (Excel, Word, PowerPoint, CSV, images) are handled dynamically by the Pi SDK agent at query time — it writes quick scripts using pre-bundled libraries:
84
+
85
+ | Library | File Types |
86
+ |---|---|
87
+ | exceljs | `.xlsx`, `.xls` |
88
+ | mammoth | `.docx` |
89
+ | officeparser | `.pptx` |
90
+
91
+ No separate install needed — all bundled with llm-kb.
92
+
93
+ ## How It Works
94
+
95
+ - **PDF parsing** — `@llamaindex/liteparse` extracts text with spatial layout + per-word bounding boxes. Runs locally, no cloud calls.
96
+ - **Indexing** — Pi SDK `createAgentSession` reads each source and generates a summary table in `index.md`.
97
+ - **File watching** — `chokidar` watches the folder. New/changed PDFs trigger re-parse + re-index (debounced for batch drops).
98
+ - **Auth** — uses Pi SDK's auth storage (`~/.pi/agent/auth.json`). No API keys in your project.
99
+
100
+ ## Development
101
+
102
+ ```bash
103
+ git clone https://github.com/satish860/llm-kb
104
+ cd llm-kb
105
+ bun install
106
+ bun run build
107
+ npm link
108
+
109
+ llm-kb run ./test-folder
110
+ ```
19
111
 
20
112
  ## Tutorial
21
113
 
package/SPEC.md ADDED
@@ -0,0 +1,275 @@
1
+ # llm-kb — Product Spec
2
+
3
+ > **One-liner:** Drop files into a folder. Get a knowledge base you can query.
4
+ > **npm:** `npx llm-kb run ./my-documents`
5
+ > **Status:** Phase 1 complete. Ingest pipeline + CLI.
6
+
7
+ ---
8
+
9
+ ## Who Is This For
10
+
11
+ A developer or technical researcher who has 20-200 documents (PDFs, spreadsheets, slide decks, notes) scattered across folders. They want to ask questions across all of them without building a RAG pipeline or setting up a vector database.
12
+
13
+ **They will try this if:** it works in under 2 minutes with one command.
14
+ **They will keep using it if:** the answers are good and the wiki compounds over time.
15
+ **They will abandon it if:** setup is painful, it eats tokens without useful results, or it feels like a demo.
16
+
17
+ ---
18
+
19
+ ## What Success Looks Like
20
+
21
+ ```bash
22
+ npx llm-kb run ./research
23
+ ```
24
+
25
+ Terminal output:
26
+ ```
27
+ llm-kb v0.0.1
28
+
29
+ Scanning ./research...
30
+ Found 12 files (12 PDF)
31
+ 12 parsed
32
+
33
+ Building index...
34
+ Index built: .llm-kb/wiki/index.md
35
+
36
+ Output: ./research/.llm-kb/wiki/sources
37
+
38
+ Watching for new files... (Ctrl+C to stop)
39
+ ```
40
+
41
+ Drop more files in while it's running. They get ingested automatically.
42
+
43
+ **That's the whole first-run experience.** No config file. No API key prompt (uses Pi SDK auth). No Docker. Just point at a folder.
44
+
45
+ ---
46
+
47
+ ## Commands
48
+
49
+ ### `llm-kb run <folder>` (Phase 1 ✅)
50
+
51
+ The main command. Does everything:
52
+
53
+ 1. Scans the folder for PDF files
54
+ 2. Parses each PDF to markdown + JSON bounding boxes (via LiteParse)
55
+ 3. Skips already-parsed files (mtime check — re-runs are instant)
56
+ 4. Builds `index.md` from all parsed sources (via Pi SDK agent)
57
+ 5. Starts a file watcher on the folder (new PDFs get auto-ingested + re-indexed)
58
+
59
+ **Data layout it creates inside the folder:**
60
+
61
+ ```
62
+ ./my-documents/
63
+ ├── (your original files — untouched)
64
+ └── .llm-kb/
65
+ └── wiki/
66
+ ├── index.md
67
+ └── sources/
68
+ ├── report.md ← spatial text layout
69
+ ├── report.json ← per-word bounding boxes
70
+ └── ...
71
+ ```
72
+
73
+ **Key decision:** `.llm-kb/` lives inside the user's folder, not in a global location. The knowledge base is co-located with the documents. Delete the folder, delete the KB. Copy the folder to another machine, the KB comes with it.
74
+
75
+ ### `llm-kb query <question>` (Phase 2)
76
+
77
+ Query from the terminal without starting the web UI:
78
+
79
+ ```bash
80
+ llm-kb query "what are the reserve requirements?" --folder ./research
81
+ llm-kb query "compare Q3 vs Q4 guidance" --folder ./research --save
82
+ ```
83
+
84
+ ### `llm-kb status` (Phase 2)
85
+
86
+ Show what's in the knowledge base.
87
+
88
+ ### `llm-kb eval` (Phase 4)
89
+
90
+ Run the eval loop manually.
91
+
92
+ ---
93
+
94
+ ## File Type Strategy
95
+
96
+ **Key architectural decision: PDF is the only file type parsed at ingest time.** All other file types are handled dynamically by the Pi SDK agent at query time using pre-bundled libraries.
97
+
98
+ ### Why?
99
+
100
+ - PDFs are binary, slow to parse, and need specialized libraries — worth pre-processing
101
+ - Everything else (Excel, Word, PPT, CSV) — the Pi SDK agent can write a quick script to read them on demand
102
+ - This eliminates 6 parser adapters, a router, and an adapter interface
103
+ - The agent is smarter than a static adapter — it can decide what's relevant
104
+
105
+ ### PDF Parsing (Ingest Time)
106
+
107
+ | Extension | Parser | Output | Bounding Boxes |
108
+ |---|---|---|---|
109
+ | `.pdf` | @llamaindex/liteparse | `.md` + `.json` | ✅ Yes |
110
+
111
+ ### Other File Types (Query Time — Agent Handles Dynamically)
112
+
113
+ These libraries are pre-bundled in llm-kb and available to the agent via `NODE_PATH`:
114
+
115
+ | Library | File Types |
116
+ |---|---|
117
+ | exceljs | `.xlsx`, `.xls` |
118
+ | mammoth | `.docx` |
119
+ | officeparser | `.pptx` |
120
+
121
+ The agent's `AGENTS.md` context (injected via Pi SDK `agentsFilesOverride`) tells it which libraries are available and how to use them.
122
+
123
+ ---
124
+
125
+ ## OCR Strategy
126
+
127
+ Page-level routing — only scanned pages get OCR, native text pages are free and instant.
128
+
129
+ ```
130
+ PDF Page → LiteParse classifies → native text? → keep local (free)
131
+ → scanned? → route to OCR
132
+ ```
133
+
134
+ **OCR is off by default** (most PDFs have native text, avoids noisy Tesseract warnings).
135
+
136
+ **Enable via env vars:**
137
+ - `OCR_ENABLED=true` → local Tesseract.js (built into LiteParse)
138
+ - `OCR_SERVER_URL=http://...` → remote Azure Document Intelligence bridge (faster, better quality)
139
+
140
+ The OCR server is a separate project. llm-kb just calls it if the env var is set.
141
+
142
+ ---
143
+
144
+ ## Auth & Model
145
+
146
+ **No API key handling in llm-kb.** Uses Pi SDK's `createAgentSession()` with defaults:
147
+ - Auth from `~/.pi/agent/auth.json` (existing Pi installation)
148
+ - Model from Pi's settings (whatever the user has configured)
149
+ - No config file, no env var for model selection
150
+
151
+ ```typescript
152
+ const { session } = await createAgentSession({
153
+ cwd: folder,
154
+ resourceLoader: loader, // injects AGENTS.md
155
+ tools: [readTool, bashTool, writeTool],
156
+ sessionManager: SessionManager.inMemory(),
157
+ });
158
+ ```
159
+
160
+ ---
161
+
162
+ ## Tech Stack (Phase 1 — What's Built)
163
+
164
+ ```
165
+ TypeScript (strict)
166
+ ├── CLI: Commander
167
+ ├── Build: tsup (single bin/cli.js)
168
+ ├── Dev: Bun
169
+ ├── PDF parsing: @llamaindex/liteparse (local, bounding boxes)
170
+ ├── OCR: Tesseract.js (via LiteParse) or remote OCR server
171
+ ├── File watching: chokidar (debounced)
172
+ ├── Indexing: @mariozechner/pi-coding-agent (createAgentSession)
173
+ ├── Pre-bundled for agent: exceljs, mammoth, officeparser
174
+ └── No database. No vector store. Files only.
175
+ ```
176
+
177
+ ---
178
+
179
+ ## Project Structure (Actual)
180
+
181
+ ```
182
+ llm-kb/
183
+ ├── bin/
184
+ │ └── cli.js ← Built by tsup (single file)
185
+ ├── src/
186
+ │ ├── cli.ts ← Commander entry point
187
+ │ ├── scan.ts ← Recursive folder scan + extension filter
188
+ │ ├── pdf.ts ← LiteParse → .md + .json
189
+ │ ├── indexer.ts ← Pi SDK agent → writes index.md
190
+ │ └── watcher.ts ← chokidar file watcher (debounced)
191
+ ├── package.json
192
+ ├── tsconfig.json
193
+ ├── plan.md ← Emergent build plan
194
+ ├── README.md
195
+ └── SPEC.md ← This file
196
+ ```
197
+
198
+ ---
199
+
200
+ ## Constraints
201
+
202
+ 1. **Zero config for first run.** `npx llm-kb run ./folder` must work with Pi SDK auth. No config file needed. No init step.
203
+
204
+ 2. **No global state.** Everything lives in `.llm-kb/` inside the user's folder. Two different folders = two independent knowledge bases.
205
+
206
+ 3. **Original files are never modified.** Reads from the folder, writes only to `.llm-kb/`.
207
+
208
+ 4. **Graceful on bad files.** Corrupted PDF? Log a warning, skip it, continue. Show clean summary: `9 parsed, 1 failed`.
209
+
210
+ 5. **Token-conscious.** Pi SDK uses whatever model the user has configured. Indexing reads first ~500 chars of each file.
211
+
212
+ 6. **Offline-capable parsing.** PDF parsing runs locally via LiteParse. OCR is the only optional cloud dependency.
213
+
214
+ 7. **Works on Windows, Mac, Linux.** Tested on Windows. All Node.js, no shell scripts.
215
+
216
+ 8. **Skip up-to-date files.** Re-runs are instant — mtime check skips already-parsed PDFs.
217
+
218
+ ---
219
+
220
+ ## What We're NOT Building (Yet)
221
+
222
+ - **Multi-user auth.** Personal/team tool. No login.
223
+ - **Cloud hosting.** Runs locally. Docker later.
224
+ - **Real-time collaboration.** One user at a time.
225
+ - **Vector search.** If the wiki outgrows context windows, we add it. Not before.
226
+ - **Custom embeddings.** No ML pipeline. The LLM reads markdown.
227
+ - **Config file.** Nothing reads it yet. Add when Phase 2/3 needs it (model selection, port, etc).
228
+ - **Static file adapters.** No Excel/Word/PPT adapters. Pi SDK agent handles them dynamically.
229
+
230
+ ---
231
+
232
+ ## Pre-Mortem: How This Fails
233
+
234
+ | Failure | Why It Happened | Prevention |
235
+ |---|---|---|
236
+ | "Nobody tried it" | `npx` didn't work. Pi SDK not installed. | Clear prerequisites in README. |
237
+ | "Tried it, too slow" | Indexing 20 PDFs took 5 minutes. | ✅ Progress bar. Skip up-to-date. Parse once. |
238
+ | "Answers were bad" | Index summaries garbage → wrong files selected. | Test with real corpora. Eval loop (Phase 4). |
239
+ | "Too expensive" | LLM burned tokens on indexing. | Agent reads first ~500 chars per file, not full content. |
240
+ | "Broke on my files" | Encrypted PDF. 500MB file. | ✅ Graceful skip. Clean error messages. |
241
+ | "Felt like a toy" | CLI only, no UI, no saved state. | Web UI in Phase 3. |
242
+
243
+ ---
244
+
245
+ ## Build Order (Maps to Blog Series)
246
+
247
+ | Phase | What | Status |
248
+ |---|---|---|
249
+ | **1** | CLI + PDF parsing + indexer + watcher | ✅ Done |
250
+ | **2** | Query + Research sessions + terminal query command | Next |
251
+ | **3** | Web UI (chat, upload, sources, activity) | Planned |
252
+ | **4** | Eval (trace logger, eval session, report) | Planned |
253
+ | **5** | Docker + deploy | Planned |
254
+ | **6** | Citations (bounding boxes → highlight in PDF) | Planned |
255
+
256
+ ---
257
+
258
+ ## Phase 1 — Definition of Done
259
+
260
+ - [x] `llm-kb run ./folder` scans and parses PDFs
261
+ - [x] Inline progress shows parsing status
262
+ - [x] `.llm-kb/wiki/sources/` contains `.md` + `.json` per PDF
263
+ - [x] `.llm-kb/wiki/index.md` generated with summary table
264
+ - [x] File watcher auto-ingests new PDFs dropped into the folder
265
+ - [x] Corrupt files skipped with warning, don't crash
266
+ - [x] Re-runs skip up-to-date files (instant)
267
+ - [x] OCR support via env var (local Tesseract or remote server)
268
+ - [x] Auth via Pi SDK (no separate API key config)
269
+ - [x] Works on Windows (tested), Mac/Linux (Node.js, should work)
270
+ - [x] README has quickstart
271
+ - [ ] Blog Part 2 written with real terminal output screenshots
272
+
273
+ ---
274
+
275
+ *Spec written April 4, 2026. Updated after Phase 1 build. DeltaXY.*