Ask your library: rag and ask

paperful rag ingest builds a search index from the PDFs and abstracts in the on-disk mirror. paperful ask answers a question from that index and names the papers and pages it used. Default mode is simple (raw excerpts, one completion); advanced scores passages first — Ask modes. paperful rag search shows the matching passages without a chat model. On the workbench, that is Ask on the default nav. Extra cites for a draft reuse that retriever: stuff.

Ask: a cited answer from PDFs already on disk in ocean/BBNJ

Everything here is optional and off until [rag].enabled = true.

What it reads and writes

  • Reads out_dir only: record.json and the PDFs beside it. It never calls Zotero, Mendeley or EndNote, and works with the reference manager closed.

  • Writes under state_dir/rag/: extracted text, the index, a ledger of what was indexed, and (in advanced Ask) scored passage summaries under evidence/. The index is a cache; delete the folder and run rag ingest to rebuild it. Evidence files are a question cache; they do not require a re-ingest.

  • Changes one thing in the mirror: a scanned PDF. With [rag].ocr = "auto" (the default) ingest runs OCRmyPDF on PDFs that have no text layer and replaces the file under out_dir with the text-layer version, the same rewrite paperful ocr --apply does, but without an --apply flag. It also records the file in state/manifest.jsonl, as ocr does. Set ocr = "off" or pass --no-ocr to leave scans alone.

Born-digital PDFs are never sent to OCR. A PDF counts as a scan when fewer than 60% of its pages have a text layer, so a figure-only cover does not trigger OCR and a typed cover in front of a scan does not hide it.

Coverage depends on the mirror

Only PDFs that are in out_dir can be indexed. The default [mirror].pdfs = "all" copies every library PDF into the mirror on refresh; with "lazy" or "none" (the legacy "additional" loads as lazy), PDFs that live only in the reference manager are not yet in the mirror, so those items are indexed from their abstract (or not at all when they have none). paperful rag status counts them. To bring them in:

uv run paperful snapshot --library --pdfs all
uv run paperful rag ingest --library

snapshot is the step that talks to the reference manager; ingest does not.

Install

uv sync --extra rag                 # LanceDB
ollama pull nomic-embed-text        # default embedding model
# or build the heavy Compose image (includes [rag]):
# PAPERFUL_IMAGE_MODE=heavy docker compose build

ask also needs a chat model: follow llm.md and set [llm].enabled = true. For local vs hosted scale (including embeddings cost order of magnitude), see AI for notes and Ask.

LanceDB ships wheels for Apple Silicon, Linux (x86_64, aarch64) and Windows. On an Apple Silicon Mac, use an arm64 Python (uv venv --python /opt/homebrew/bin/python3). On Intel macOS, run Ask from the heavy Compose image (PAPERFUL_IMAGE_MODE=heavy), which includes [rag]. light (CI default) is the [serve] pack. See Docker.

Configure

[rag]
enabled = true
auto_ingest = false            # true: index new PDFs after run / attach / inbox / snapshot / ocr
ocr = "auto"                   # auto | off
parser = "light"               # light | docling | grobid | liteparse | hybrid
embed_provider = "ollama"      # ollama | litellm
embed_model = "nomic-embed-text"
top_k = 10                     # passages sent to the model per question (simple mode)
ask_mode = "simple"            # simple | advanced — `--ask-mode` overrides one run
# model = "qwen2.5:14b"        # chat model for ask; default is [llm].model

All keys and defaults are in config.example.toml.

Ask modes

Ask has two modes on the same index. The global default is simple. A run can override it from paperful ask --ask-mode, the Ask / batch form, or MCP ask_mode. The override is not written back to config. rag search has no mode: it stays retrieval-only.

Mode

What it does

Cost

simple

One hybrid search (top_k / -k), raw excerpts, one chat completion

One completion per question

advanced

Retrieve evidence_k passages, summarize each for this question with a 0–10 score, drop the weak ones, answer from at most answer_max_sources papers

About evidence_k extra completions, then one answer. Repeated questions reuse state/rag/evidence/

simple ignores the advanced knobs. Setting evidence_k does not change a simple run; -k does not change an advanced run.

Advanced excerpt headers include venue, item type, and DOI when the ledger has them. A follow-up turn may cite only the markers in the current excerpts; prior turns stay in the chat for continuity, not as evidence. --show-context still lists the raw passages.

Optional advanced knobs default so the mode stays one summary pass. Raising them is a config change, not a third mode (query_variants, insufficient_retries, mmr_lambda, cite_hops, context_neighbors). cite_hops reads citation edges already on disk (snowball citation_graph.json); it does not download or call OpenAlex. Batch and rag answered follow the same default-or-flag rule. Keys: config.md.

uv run paperful ask "What does the BBNJ Agreement require for EIAs?" -C ocean/BBNJ
uv run paperful ask "…" --ask-mode advanced

Embedding model

The default, nomic-embed-text, is small, fast and mainly English. For a library with papers in several languages use bge-m3:

ollama pull bge-m3
[rag]
embed_model = "bge-m3"

Each embedding model has its own index folder (state/rag/ollama__nomic-embed-text/, state/rag/ollama__bge-m3/). Changing embed_model therefore starts a new index and leaves the old one untouched; run rag ingest again to fill it. Text extraction is cached, so only the embedding is repeated. Switch back and the earlier index is still there.

For a hosted model set embed_provider = "litellm" and a LiteLLM model name such as openai/text-embedding-3-small (needs paperful[llm] and the provider’s API key in the environment). Passage text then leaves the machine, and the commands say so.

Parser

light uses pdftotext (Poppler), with pypdf as fallback. It is fast and keeps page numbers; section headings are detected by rule (synonym list shared with extract-sections via paperful/sections/headings.py).

docling reads layout and gives cleaner headings and tables, at a large cost in install size (it pulls in PyTorch) and time:

uv sync --extra rag --extra rag-docling
[rag]
parser = "docling"

grobid POSTs the PDF to an operator-owned GROBID sidecar and projects TEI to markdown pages (# Heading) so this chunker keeps section labels. It needs [grobid].enabled and a reachable server, or a warm TEI cache for that PDF hash. It is not the default. See GROBID.

[rag]
parser = "grobid"

liteparse and hybrid use LiteParse locally (paperful[rag-liteparse]). They do not rewrite the mirror; OCRmyPDF still owns scanned PDFs when [rag].ocr = "auto". Parse-time OCR (bundled Tesseract, [ocr].languages) only runs on pages LiteParse still marks as needing OCR (embedded images, sparse figures). ocria-nano is not pulled.

  • liteparse — markdown for every page.

  • hybrid — light for the body; LiteParse markdown only on routed pages. [rag].liteparse_route is all (default), tables (table-likely / multi-column), or ocr (needs_ocr).

uv sync --extra rag --extra rag-liteparse
[rag]
parser = "hybrid"
# liteparse_route = "all"   # all | tables | ocr

doctor ambers when the parser is liteparse or hybrid and the extra is missing. Neither extra is in the Docker images.

Changing the parser re-indexes every PDF on the next ingest.

Build the index

uv run paperful rag ingest -C BBNJ --dry-run     # what would be indexed; writes nothing
uv run paperful rag ingest -C BBNJ               # one collection
uv run paperful rag ingest --library             # everything in the mirror
uv run paperful rag ingest --library --limit 500 # a long first build, in parts
uv run paperful rag status                       # what is indexed, what is behind

-C is a folder path under out_dir, for example ocean/BBNJ.

Ingest is incremental. An item is worked on again only when its PDF changed, a PDF arrived for an item that had only an abstract, its abstract changed, or the chunking settings changed. Unchanged items cost one file stat. Stop it with Ctrl-C at any time; the next run continues.

Flag

Effect

--item KEY, -C PATH, --library

Scope. One is required.

--year-from, --year-to, --type

Filter the scope.

--limit N

Work on at most N items this run.

--dry-run

Print the plan. No embedding, no OCR, no writes.

--no-ocr

Do not OCR scans this run. They are picked up by a later run.

--retry-failed

Try again PDFs that failed earlier (unreadable file, OCR error).

--force

Re-index items in scope even when nothing changed.

--library with no filter and no --limit also removes items from the index that are no longer in the mirror.

An item with several distinct PDFs is indexed from the newest one. rag status counts these.

A first build of a large library takes a while: locally, expect roughly 75 passages a second with nomic-embed-text, and around 60 passages for a typical paper. OCR adds seconds to minutes per scan.

Keep it current automatically

[rag]
auto_ingest = true

With this on, run, attach, inbox, snapshot, ocr --apply, snowball with --fetch-pdfs, and the handoff walks index the PDFs they just landed, after their own work is done. A failure there never fails the command: it prints one line and rag ingest catches up later. It is off by default.

Search and ask

uv run paperful rag search "environmental impact assessment thresholds" -C ocean/BBNJ
uv run paperful ask "What does the BBNJ Agreement require for EIAs?" -C ocean/BBNJ
uv run paperful ask "…" --ask-mode advanced
uv run paperful ask "…" --show-context     # also list the passages used
uv run paperful ask "…" --top-k 20         # passages sent to the model (default [rag].top_k)
uv run paperful rag search "…" --top-k 20  # hits to print
uv run paperful ask "…" --format json      # one paperful.agent.json.v1 object (implies --no-stream)
uv run paperful ask "…" --focus gaps       # prompt preset (default|questions|gaps|methods|answered|conflict)
uv run paperful ask --from-file questions.txt -C ocean/BBNJ   # batch → state/ask-batch/
uv run paperful ask --claim-check --file ./draft.md -C ocean/BBNJ
uv run paperful ask                        # prompt for several; follow-ups share a thread
uv run paperful ask --thread new "…"       # start a stored thread
uv run paperful ask --thread <id> "and the EIA part?"

--format json needs a question on the command line (or --from-file; no TTY multi-question loop). Agents that can shell out should prefer that over paperful mcp (ask and other read-only tools return the same envelope; see commands — Agent channel). Exit codes follow the commands table.

ask prints the answer as it is written, then the sources it cited:

… stomach content analysis of fish caught by midwater trawl [S1].

Sources
[S1] Gjøsæter (1973). The food of the myctophid fish … pp. 1-2, p. 7. [2WZ8G7GW]

Claim-check

paperful ask --claim-check --file DRAFT (or --item KEY) labels each sentence against the local index. No chat model. -C / year / type are the search scope (--item is the manuscript, not a scope key). Labels:

  • supported — a quote that occurs in a held chunk, plus item key and pages

  • mixed — hits exist; the pack uses the fixed phrase “passages touch this topic and do not state the claim”

  • unsupported — no index hit; no guessed citation

Fail closed when [rag] is off or the index is empty. Writes state/ask-claims/<stamp>/ (paperful.claim_check.v1 + pack.md). --apply and --from-file are not valid here. Cited works only, on a peer-review pack: --lane claims with -C — Peer review.

The marker in the text, the paper, the pages the passages came from, and the item key. When the model cites nothing, the retrieved papers are listed as “Retrieved, not cited” so you can see what it was given. --item, -C, --year-from, --year-to and --type narrow what is searched. Each run writes state/runs/<stamp>-ask.json with the questions, answers and sources.

Without a question on a TTY, ask prompts for one at a time and keeps a thread under state/rag/threads/. Follow-ups are rewritten into a standalone search query before retrieval; the model still sees the conversation. Piped lines and a one-shot paperful ask "question" stay independent unless you pass --thread.

Batch, focus, and research questions

--from-file (or -) runs one question per line with no thread rewrite. Results land in state/ask-batch/<stamp>/ (pack.json = paperful.ask_batch.v1, plus answers.md). Unchanged question + focus + index tip rows are skipped unless --force. Optional --apply -C … --to zotero|both writes a collection note (never a silent parent edit).

--focus selects a bundled system prompt (default, questions, gaps, methods, answered, conflict). conflict names agreement vs disagreement and says when the excerpts do not decide. [rag].focus sets the default; --prompt FILE wins over focus. Named run profiles may set focus alongside scope keys. Focus is a prompt, not a third Ask mode.

uv run paperful rag questions -C ocean/BBNJ          # rules → state/rag/questions/
uv run paperful rag questions --library --llm        # also grounded LLM extract
uv run paperful rag answered --from-extract -C ocean/BBNJ
uv run paperful rag answered --from-file qs.txt --after-item AAAA1111

rag questions writes per-item JSON with provenance rule or llm. rag answered reuses the batch engine with --focus answered and writes state/rq-answered/<stamp>/ (paperful.rq_answered.v1). --after-item keeps only newer years and drops the asking paper from retrieval.

Structured sections (mirror-only)

paperful extract-sections is not part of the LanceDB index. It reads the same mirror PDFs (or state/rag/text/<key>.json when ingest already cached pages) and writes IMRaD-shaped fields to state/sections/<key>.json (paperful.sections.v1). The deterministic lane always runs; --grobid (or [grobid].enabled) prefers TEI from the sidecar and falls back to heading rules; --llm fills only missing MVP fields when [llm].enabled and [extract_sections].llm (or --llm on the CLI). It does not write Zotero notes and does not require [rag].enabled. Advanced Index has the same form.

Use rag questions when you need a corpus-wide RQ queue for rag answered. Use extract-sections when you want structured abstract, methodology, conclusion, and research_questions beside each item for scripts, gap context, or downstream synthesis — without HTML summaries.

uv run paperful extract-sections -C ocean/BBNJ
uv run paperful extract-sections --library --grobid
uv run paperful extract-sections --library --llm --dry-run

Heading synonyms ship in the tree; merge publisher-specific titles from state/section-headings.toml or [extract_sections].headings. See config.md.

Workbench (Ask and Advanced Index)

Ask is on the default nav. With [rag] and [llm] on, that page runs threaded Ask. A Mode select (simple / advanced) starts at [rag].ask_mode; changing it is this run only and is not written back to config. After an advanced answer the page shows how many passages were scored and how many were kept. Batch Ask is on the same page when Advanced is on (same Mode select). With an empty index, Ask shows an Ingest first panel (Dry-run ingest / Ingest) and warns when the embedding or answer model is remote. A Focus select and New thread link sit above the question box. Advanced Index covers ingest, search, extract questions, already-answered checks, Extract sections, and synthesize. Scope follows the collection chip (plus optional item keys, years, types, top-k). Custom system prompts on Ask/batch, in order:

  1. Inline textarea

  2. Uploaded .md/.txt (stored under state/gui/uploads/)

  3. Path field (resolved like [rag].prompt, relative to the config file)

  4. Saved file from state/prompts/ (Save inline/upload as + Save name on the form)

  5. Else [rag].prompt / focus preset

Batch packs list under state/ask-batch/; answered packs under state/rq-answered/ (pack.md). Routes: gui.md. CLI still owns TTY multi-turn, --show-context, named run profiles, ask --claim-check, and paperful all. Claim-check is not on /ask (stage 1 is the pack on disk).

Answers are only as good as the passages found. The model is told to answer from the excerpts alone and to say when they do not contain the answer, but a local 7B model can still misread or over-claim; check the cited pages.

Check

uv run paperful doctor        # RAG embeddings / RAG index rows
uv run paperful rag status    # index against the mirror

doctor is amber when the rag extra is missing, the embedding model is not pulled, or no index has been built. rag status shows how many items a new ingest would touch, how many scans wait for OCR, and how many PDFs failed.

Troubleshooting

Message

Fix

rag.enabled is false in config.toml

Set [rag].enabled = true.

lancedb is not installed

uv sync --extra rag.

embedding model … not in Ollama tags

ollama pull <model>.

no index for … yet

paperful rag ingest --library (or -C).

index at … has dim = …, but the configuration gives …

The folder was built with other settings. Delete that folder and ingest again.

Stopped: Ollama unreachable during ingest

Start Ollama and run the same command; items already indexed are kept.

A scan is indexed from its abstract only

Install ocrmypdf (and the Tesseract language packs in [ocr].languages), then ingest again.