Skip to content

API Reference parrot.knowledge.bookstore.carding

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.knowledge.bookstore.carding

View source

Card derivation helpers — slugs, ToC digests, and LLM carding.

Pure functions plus one LLM call (:func:generate_card_fields) with a deterministic no-LLM fallback (:func:fallback_card_fields), so a library stays usable without any model configured.

Alias CardDraft

Re-exported from parrot.knowledge.bookstore.models.CardDraft.

Alias Genre

Re-exported from parrot.knowledge.bookstore.models.Genre.

Alias TocEntry

Re-exported from parrot.knowledge.bookstore.models.TocEntry.

Function derive_toc

Walk a PageIndex tree dict into ToC entries plus a text digest.

derive_toc(tree: dict[str, Any], max_depth: int = 2) -> tuple[list[TocEntry], str]

Parameter Type Default Description
tree dict[str, Any] A tree as returned by PageIndexToolkit.get_tree — {doc_name, doc_description?, structure: [nodes]} where each node carries title / node_id / start_index / end_index / nodes.
max_depth int 2 Deepest level included (1 = chapters only).

Returns: tuple[list[TocEntry], str] — (entries, digest) — the structured entries and a rendered digest with one "1.2 Title (pp. 34-58)" style line per entry, suitable for FTS indexing and LLM prompts.

Function disambiguate_title

Return a normalized, unique display title.

Hint order: (1) the first toc_entries title whose casefold differs from title's and whose combination is not taken; (2) the de-slugified stem; (3) "<title> — <stem> (N)" for the first free N >= 2. taken holds casefolded titles. Blank titles fall back to the de-slugified stem, the stripped stem, or "Untitled". Never returns a taken title.

disambiguate_title(title: str, taken: set[str], *, toc_entries: list[TocEntry], stem: str) -> str

Parameter Type Default Description
title str
taken set[str]
toc_entries list[TocEntry]
stem str

Returns: str

Function fallback_card_fields

Deterministic card draft when no LLM is configured.

Title comes from the ebook metadata when available, otherwise from the de-slugified filename stem; topics from the top-level chapter titles.

fallback_card_fields(file_path: Path, toc_entries: list[TocEntry], *, metadata_title: str | None = None) -> CardDraft

Parameter Type Default Description
file_path Path The ingested source file.
toc_entries list[TocEntry] Structured ToC from :func:derive_toc.
metadata_title str | None None Book title extracted from EPUB/MOBI metadata.

Returns: CardDraft

Function generate_card_fields

LLM-generate the descriptive card fields via structured output.

generate_card_fields(adapter: Any, *, filename: str, doc_description: str, toc_digest: str, samples: list[str]) -> CardDraft

Parameter Type Default Description
adapter Any A :class:~parrot.knowledge.pageindex.llm_adapter.PageIndexLLMAdapter (or compatible) exposing ask_structured(prompt, schema).
filename str Source filename (context only, never trusted as title).
doc_description str PageIndex doc_description when available.
toc_digest str Rendered ToC from :func:derive_toc.
samples list[str] Content excerpts (each capped to ~2000 chars here).

Returns: CardDraft — The LLM-filled :class:CardDraft.

Function sample_sections

Load up to max_samples representative sidecar bodies.

Picks the first node and one from the middle of the book, which is usually enough signal for carding without paying for a full read.

sample_sections(content_loader: Any, node_ids: list[str], max_samples: int = 2) -> list[str]

Parameter Type Default Description
content_loader Any NodeContentStore.loader_for(tree) callable mapping node_id → markdown (or None).
node_ids list[str] Candidate node ids in reading order.
max_samples int 2 Upper bound on returned samples.

Returns: list[str]

Function slugify

Turn text into a filesystem/tree-safe slug.

NFKD-normalizes, strips diacritics, lowercases, and collapses any non-alphanumeric run into a single -. Result is capped at 64 chars and never empty (falls back to "book"). Non-Latin titles (CJK, Cyrillic…) have no ASCII decomposition and collapse to that fallback — the tree store's name regex is ASCII-only — so pass an explicit title/slug for those or accept book-N ids.

slugify(text: str) -> str

Parameter Type Default Description
text str Free-form title or filename stem.

Returns: str — A slug matching [a-z0-9-]+ (also valid for class:~parrot.knowledge.pageindex.store.JSONTreeStore names).

Function unique_slug

Return base or the first free base-N (N >= 2) variant.

unique_slug(base: str, taken: set[str]) -> str

Parameter Type Default Description
base str Candidate slug (already slugified).
taken set[str] Slugs already in use (catalog rows and trees on disk).

Returns: str

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally