An agent that finds public domain books, pulls them onto your machine, reads them, and answers questions with the book, chapter and paragraph behind every sentence.
I wanted a way to ask the old finance books real questions. What Bagehot said a central bank should do in a panic, how Adam Smith described the division of labour, what people wrote about the commercial crises of 1847 and 1857 while they were happening. The books are free and public. Reading all of them to find one passage isn't.
Try it live. index.html puts the same catalog in front of a person. Search Project Gutenberg by title, author or subject, browse twelve subject shelves, open any book's record and read the whole book on the page. It keeps your place and the books you save in your browser, and it reads books the same way the agent does: the Gutenberg header and licence come off and the headings become the contents. A dated copy of the catalog's front pages is built in, so it opens on real books even when the catalog is slow. No build step, and it also opens straight from disk.
You ask a question in plain words. The agent searches Project Gutenberg's catalog by subject, and the Internet Archive when Gutenberg is down, and keeps only books that are public domain in the United States. It pulls the best few, one at a time, and caches them on your machine.
Then it reads. The Gutenberg header and licence come off, every paragraph gets a number and the chapter it sits in, and BM25 ranks every paragraph against the question. The six best go to a local model, which writes an answer using only those passages and cites one in every sentence. Code then checks the answer: a citation that points to a passage it never got, or a sentence with no citation, fails the whole answer, and you get the passages instead.
With no model running, it skips the writing step and gives you the passages with their citations.
Standard library Python, nothing to install.
python3 library.py search "lombard street" # titles and authors
python3 library.py search banking --topic # subjects and shelves
python3 library.py search "panic of 1893" --source archive # the Internet Archive
python3 library.py get archive:lombardstreet00bage # pull one book onto the shelf
python3 library.py find "lend freely" --book archive:lombardstreet00bage # the best paragraphs in it
python3 library.py ask "What should a central bank do in a panic?" --topic banking
python3 library.py shelf # what is already on this machineTo get a written answer, point it at any local model server that speaks the OpenAI chat API, such as Ollama, LM Studio or an MLX server:
python3 library.py ask "What caused the crisis of 1857?" --model http://127.0.0.1:11434 --model-name llama3.1Books are cached in ~/.library-agent (or LIBRARY_HOME).
- Public domain only. Gutenberg's own copyright flag, and for the Internet Archive, published in 1930 or earlier.
- One request at a time, two seconds apart, with a cache, so nobody's library gets hammered. For bulk work, use a Gutenberg mirror.
- Books stay on your machine and are never redistributed.
- Nothing is answered from memory. Every sentence points to a passage, and code checks it.
- Private by design. The question, the books and the answer never leave the machine unless you point it at a remote model yourself.
| File | What it does |
|---|---|
library.py |
The catalogs, the polite downloader, the cleaner, the paragraph reader, the BM25 ranker, the answer step and the citation check |
index.html, web/app.js, web/app.css |
The library page: the catalog as a table, subject shelves, a book's record, a reader and a saved shelf |
web/build_catalog.py |
Saves the dated copy of the catalog's front pages (web/catalog.js) that the page opens on |
tests/test_library.py |
Cleaning, chapters, ranking, both catalogs' answers and the citation check, all on fixtures. No network |
tests/test_page.py |
The saved catalog copy has every list the page opens on, and the page's files are all where it looks for them |
python3 -m unittest discover -s testsThe same loop fits a fund's own library: filings, research and transcripts instead of public domain books. That is how the fund memory on Mac Studio Prepared works, with the answer's data class set by the most private passage it used.
All rights reserved. Shared to show my work.
