Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Alexandria — v0.0.12

This project is entirely coded by LLM agents. It is an experiment to learn how to use those.

A local-first research library that unifies a Firefox bookmark collection, a Zotero academic library, a Calibre ebook collection, and an unstructured images folder into a single semantically indexed, searchable knowledge base.

Inference runs through swappable providers — on-device via Ollama by default, or against a hosted endpoint when the local machine cannot run a model large enough to be useful.

Alexandria is local-first, not local-only: the default configuration keeps your library contents and your queries on the machine, and every outbound path is a separate named setting that is off until you turn it on. Nothing phones home, and there is no telemetry or analytics in any configuration. See DESIGN.md §1.1 for the network-access policy and what crosses the wire in each case.

Repository layout

pka/                          # Python backend
├── config.py                 # Pydantic settings (ALEXANDRIA_ env prefix, .env supported)
├── constants.py              # Source, FetchStatus, TagOrigin enums
├── pipeline.py               # _ingest_text_block + per-source ingestion funcs
├── cli/                      # `alexandria` CLI (init, sync, clustering, purge, …)
├── db/                       # SQLAlchemy Core schema + queries
├── connectors/               # zotero, firefox, calibre, images
├── ingestion/                # chunker, fetcher, extractors, image_pipeline
├── storage/                  # ChromaDB wrapper
├── clustering/               # UMAP + HDBSCAN engine + lifecycle (drift/merge)
└── api/                      # FastAPI app (routers + Pydantic schema modules)

scripts/                      # thin shims around pka/cli for repo-local runs
tests/                        # pytest suite; conftest mocks Ollama, Chroma, HTTP
frontend/                     # Vue 3 + Vite + Pinia
├── index.html
├── package.json, vite.config.ts, tsconfig.json
└── src/
    ├── main.ts, router.ts, App.vue
    ├── api/client.ts         # typed fetch wrappers (timeout + ApiError)
    ├── stores/               # search, clusters, ingestion, ui, toast
    ├── views/                # routed views
    ├── components/           # AppSidebar, DocCard, DocDetailPanel, ScatterPlot, …
    └── styles/global.css

Setup

# Python 3.11+
pip install -e .
# Optional extras
pip install -e '.[dev]'    # pytest, ruff, mypy

# Database — idempotent, safe to re-run
alexandria init            # or: python scripts/init_db.py

# Frontend
cd frontend
npm install

Installing the package provides the alexandria console command (also runnable as python -m pka.cli). The scripts/ files are thin shims kept for python scripts/<name>.py workflows; both forms run the same code.

System prerequisites:

  • Ollama for clustering labels and image vision (ollama pull llava or your chat model). Text chunk embeddings run locally through sentence-transformers (ALEXANDRIA_EMBEDDING_MODEL, default intfloat/multilingual-e5-small, downloaded on first use). An archive built before the setting keeps all-MiniLM-L6-v2 until alexandria reembed moves it to the configured model.
  • Ollama Cloud (optional) runs bigger models than the machine fits, without the prompts touching a third-party aggregator. Two routes, both per-capability so chat can go remote while OCR and embeddings stay local:
    • Through the local daemon — ollama signin, ollama pull gpt-oss:120b-cloud, then set ALEXANDRIA_CHAT_MODEL=gpt-oss:120b-cloud. Provider stays ollama.
    • Direct to ollama.com — set ALEXANDRIA_CHAT_PROVIDER=ollama_cloud (and/or _VISION_PROVIDER), an API key from https://ollama.com/settings/keys as SECRET_ALEXANDRIA_OLLAMA_CLOUD_API_KEY, and ALEXANDRIA_OLLAMA_CLOUD_CHAT_MODEL (no -cloud suffix on this route). No local daemon needed.
  • Reddit reads your saved posts through the private feed URL from https://www.reddit.com/prefs/feeds/ — no app registration, which currently needs an API-access clearance personal accounts do not get. Paste either the RSS or the JSON form into SECRET_ALEXANDRIA_REDDIT_FEED_URL in .secrets; either is normalised to the Atom (.rss) endpoint, the only one Reddit serves to automated clients. That URL grants read access to your saved list: treat it like a password. This feed URL is the only credential the connector takes — the OAuth API (praw) route has been removed, since the "script" app it needs is no longer obtainable. Every poll is archived to data/reddit/<timestamp>/ (the raw feed pages, plus a manifest) and merged into data/reddit/saved.jsonl, which holds one deduplicated line per saved item; alexandria reddit --from-archive re-ingests from that log with no network access, for when the token dies or the database has to be rebuilt. Turn the archive off with ALEXANDRIA_REDDIT_ARCHIVE_ENABLED=0.
  • Image OCR runs through the vision model by default (ocr_provider=vlm, no extra install). Set ocr_provider=easyocr to use the bundled EasyOCR backend instead — a pip dependency (no system binary); its recognition models download on first use.
  • Scanned PDFs (Calibre books and Zotero attachments with no text layer) are marked no_text_layer and OCR'd by alexandria ocr-scans, off by default (ALEXANDRIA_PDF_OCR_ENABLED=1). Two backends, chosen by ALEXANDRIA_PDF_OCR_BACKEND:
    • easyocr (default) — local, no extra install. About 20 s a page on CPU and weak on symbols and numbers; set ALEXANDRIA_PDF_OCR_MAX_PAGES to bound a run.
    • pdfcraft — pdf-craft sending each page image to a hosted DeepSeek-OCR 2 endpoint: far better text, maths as LaTeX, a few seconds a page. Page images leave the machine. Install it without PySide6, which it needs only to write text back into PDFs: pip install --no-deps pdf-craft==2.4.1 then pip install -e '.[pdfocr]' (pip check will then report PySide6 missing; that is expected). Configure any OpenAI-compatible endpoint serving the model, e.g. Novita: ALEXANDRIA_PDF_OCR_BASE_URL=https://api.novita.ai/v3/openai, ALEXANDRIA_PDF_OCR_MODEL=deepseek/deepseek-ocr-2, and the key as SECRET_ALEXANDRIA_PDF_OCR_API_KEY in .secrets. At Novita's October 2026 price that came to about $0.06 per 1,000 pages. Each OCR'd scan also gets a searchable PDF — the untouched page images with the text as an invisible layer — written by OCRmyPDF from the same OCR result (pip install -e '.[pdflayer]'; no Tesseract or Ghostscript needed). By default it is a copy under the data dir, opened from the document's detail panel. Set ALEXANDRIA_PDF_OCR_OUTPUT=replace to write it over the Calibre / Zotero file instead, so their search finds it too: the original is saved under data_dir/ocr_originals/ first, and alexandria ocr-scans --restore <id> (or --restore-all) puts it back. ALEXANDRIA_PDF_OCR_TEXT_LAYER=0 turns the PDF off.
  • CLIP visual search is off by default (ALEXANDRIA_CLIP_ENABLED=1 to turn it on). With it off, no image embedding pass runs and no CLIP model is downloaded; images are still searchable through the text the pipeline infers from them (per-type content extraction + description + OCR), which is indexed alongside every other document. Turn it on for purely visual queries whose words appear nowhere in that text. /images/search accepts mode=hybrid|clip|text and reports which path matched each hit.
  • Image admission gate (on by default) filters incoming images before the expensive passes: an image is kept only if EasyOCR finds it is at least 5% text and a fast VLM (Ollama moondream by default) classifies it into a category of interest. Rejected paths are cached in the image_rejections table and skipped on later runs. Tune with ALEXANDRIA_IMAGE_GATE_* (see .env.example) or bypass per-run with alexandria images --skip-gate.

Running

Ingest sources

alexandria zotero
alexandria firefox                  # metadata + async fetch + embed
alexandria calibre --fulltext
alexandria images

Common flags across the subcommands: --dry-run, --force-reindex. See alexandria <command> --help for the full set.

alexandria ocr-scans --dry-run      # queued scans and their page counts; reads nothing
alexandria ocr-scans --source zotero
alexandria ocr-scans --limit 5 --max-pages 50
alexandria ocr-scans --layer-only    # searchable PDFs for scans OCR'd without one
alexandria ocr-scans --restore 1234  # undo one: original back, or copy deleted

The same pass runs from the app: the Ingestion page's Scanned PDFs panel shows how many scans are waiting, starts the run in the background, and reports progress per document with a Stop button.

alexandria domain-report            # domains by frequency (prioritize fetch handlers)
alexandria domain-report --source firefox --limit 50
alexandria domain-report --rejected # sorted by unfetchable count instead

Purge and re-run parts of the pipeline

Swapping an embedding or summarisation backend does not mean re-ingesting everything. alexandria purge clears one kind of artifact — leaving the expensive fetched text, and your own tags and reading lists, in place.

alexandria purge --list                       # targets, and what regenerates each
alexandria purge summaries --dry-run          # counts first; nothing is deleted
alexandria purge summaries --source firefox   # or scope it to one connector
alexandria purge vectors                      # then alexandria reembed

Before this release every Calibre ingest appended another copy of each book's full-text and summary chunks. duplicate_chunks removes the extra copies and keeps the first, and only where a document's runs are exact repeats:

alexandria purge duplicate_chunks --dry-run   # how many documents and chunks
alexandria purge duplicate_chunks

Summaries record which model made them, so swapping a backend does not mean discarding the work you are keeping:

alexandria purge --runs                              # what ran, when, at what cost
alexandria purge summaries --model qwen2.5:3b        # only the old model's work
alexandria purge summaries --unknown                 # only the unstamped backlog

A target whose artifact carries no stamp rejects those filters rather than quietly purging everything.

Clearing an artifact is usually the whole re-trigger: the next sync regenerates whatever is missing. Summaries are the exception — their skip gate is "does this document have chunks", which stays true — so they get an explicit pass:

curl -X POST 'localhost:8420/ingestion/enrich?kind=summary'

The body text of anything fetched or extracted is kept verbatim (document_texts, setting retain_document_text, default on), so changing the chunker can be applied to documents already in the archive without fetching a single URL again (an embedding-model change needs only alexandria reembed, which re-embeds the chunks as they are):

alexandria rechunk --dry-run          # how many documents have retained text
alexandria rechunk --source firefox   # re-cut with the current chunker settings

It replaces body chunks and their vectors only; a generated summary, an external synopsis and Calibre's metadata pass are left alone. Documents ingested before retention shipped have no stored text — there is no backfill — so they are skipped until something re-fetches them.

Books are the exception to keeping text whole: Calibre full text is retained only to its first ALEXANDRIA_BOOK_RETAIN_MAX_PAGES pages (20), since a several-hundred-page PDF is the only thing that makes this cost real disk and the file is still on disk anyway. Those documents are skipped by rechunk — re-cutting a prefix would shrink the index to it — so re-run the full-text pass for them instead. Fetched pages are never capped. GET /documents/{id}/text serves a retained body back, which is also how to see what the fetcher actually got when a page ingests badly.

alexandria purge-source <source> remains the blunt instrument: it removes a whole connector's documents. It now keeps manually-applied and learned tags plus reading-list entries, since re-ingesting cannot recreate those; pass --include-user-data to delete them too.

Cluster and review

alexandria clustering               # creates a stored, unaccepted run
alexandria clustering --accept      # accept it immediately
alexandria clustering --drift       # drift report on the active run
alexandria clustering --merges      # merge candidates on the active run
alexandria clustering --assign-new  # assign new docs to existing clusters

Serve the API and frontend

# Dev: one command starts both, streams both logs, and opens the browser
alexandria dev

This runs uvicorn --reload (port 8421) and npm run dev (port 5173) together in one terminal and opens http://localhost:5173 once the frontend is ready. Ctrl+C (or closing the window) stops both, and if either server doesn't come up within 15s (e.g. the port is already taken by something else) the command reports that and shuts everything down instead of leaving a half-working stack running. On Windows, double-clicking scripts/run_dev.bat does the same — useful as the target of a Desktop shortcut.

Equivalent manual two-terminal form, if you want the processes' logs kept separate:

# Terminal 1: backend with CORS enabled (frontend on :5173 talks to backend on :8421)
ALEXANDRIA_DEV=1 uvicorn pka.api.main:app --reload --port 8421

# Terminal 2
cd frontend && npm run dev
# Production: build the frontend, drop CORS, mount /dist at the API root
cd frontend && npm run build
uvicorn pka.api.main:app --port 8420

The build output ships to frontend/dist. The FastAPI app mounts that directory at / automatically when it exists, so a single uvicorn process serves both API and UI.

Tests

pytest
pytest --cov=pka --cov-report=term-missing

All external calls (Ollama chat/vision, ChromaDB, HTTP) are mocked at the module boundary by fixtures in tests/conftest.py. No real services are touched.

Frontend unit tests (Vitest, pure TS helpers):

cd frontend && npm run test

Configuration

All settings can be overridden via the ALEXANDRIA_ environment-variable prefix or a .env file. See .env.example for the full list. Once the app is running, /settings shows what the current process actually resolved — provider, model, endpoint and credential presence per capability (with a reachability check for local/remote backends), plus every other setting grouped and marked where it differs from its default. It is read-only; .env / .secrets remain the way to change anything.

Design

DESIGN.md is the living design specification (data flow, provider layer, instructions for adding a new source connector). The original design document is archived at docs/archive/initial_design.pdf; it records the project's initial intent and is superseded wherever it disagrees with DESIGN.md. The audit pass that produced v0.2.0 is recorded in CHANGELOG.md.

Note on naming: the Python package is pka (Personal Knowledge Archive, the project's original name); "Alexandria" is the user-facing brand. The ALEXANDRIA_ env prefix and the alexandria CLI follow the brand.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages