> ## Documentation Index
> Fetch the complete documentation index at: https://ladygraph.io.vn/llms.txt
> Use this file to discover all available pages before exploring further.

# sources

> Turn databases, config files and markdown into Documents.

## `sources.auto`

Find the knowledge in any configuration file, with no rules written for it.

#### `AutoConfig`

| Field | Type | Default |
| - | - | - |
| `keep_score` | `int` | `2` |
| `ambiguous_score` | `int` | `1` |
| `passage` | `Literal['entry', 'field']` | `'entry'` |
| `protected` | `Literal['skip', 'label']` | `'skip'` |
| `redact` | `list[Literal['email', 'phone']]` | `[]` |
| `judge_prose` | `bool` | `True` |
| `judge_min_words` | `int` | `12` |
| `declare` | `bool` | `True` |
| `rewrite` | `bool` | `False` |
| `types` | `dict[str, str]` | `{}` |
| `knowledge_names` | `list[str]` | `[]` |
| `system_names` | `list[str]` | `[]` |

#### `AutoFlattener`

Construct with `AutoFlattener(config: AutoConfig \| None = None, triage: 'FieldTriage \| None' = None)`.

| Method | What it does |
| - | - |
| `flatten(self, scope: str, raw: Any) -> list[Record]` | — |
| `analyse(self, raw: Any, *, judge: bool) -> list[Entry]` | — |
| `explain(self, raw: Any) -> list[Entry]` | What would happen to each leaf, without asking any model. |

#### `Entry`

| Field | Type | Default |
| - | - | - |
| `path` | `tuple[str, ...]` | required |
| `leaves` | `list[Leaf]` | factory |
| `profile` | `bool` | `False` |
| `status` | `str` | `'ok'` |
| `id` | `str` | `''` |
| `title` | `str` | `''` |
| `kind` | `str` | `''` |
| `unit` | `str` | `''` |
| `label` | `str` | `''` |
| `declared_id` | `str` | `''` |

| Method | What it does |
| - | - |
| `(property)` | — |

#### `Leaf`

| Field | Type | Default |
| - | - | - |
| `key` | `str` | required |
| `value` | `Any` | required |
| `kind` | `str` | `''` |
| `score` | `int` | `0` |
| `reasons` | `list[str]` | factory |
| `verdict` | `str` | `'drop'` |
| `note` | `str` | `''` |

#### `main`

```python theme={null}
main(argv: list[str] | None = None) -> None
```

#### `misses`

```python theme={null}
misses(entries: list[Entry]) -> list[tuple[Entry, Leaf]]
```

Fields dropped by score although the value reads as language: the places where a real fact may have been lost.

#### `report`

```python theme={null}
report(entries: list[Entry], show_all: bool = False) -> str
```

#### `report_misses`

```python theme={null}
report_misses(entries: list[Entry]) -> str
```

## `sources.combined`

Several Sources that feed one scope.

#### `CombinedSource`

Construct with `CombinedSource(*sources: Source)`.

| Method | What it does |
| - | - |
| `manifest(self, scope: str, now: datetime.datetime) -> dict[str, DocVersion]` | — |
| `read(self, scope: str, unit: str) -> Document` | — |
| `next_boundary(self, scope: str, now: datetime.datetime) -> datetime.datetime \| None` | — |

## `sources.entities`

Entity kinds from the ingestion config: codes, abbreviations, or any thing a deployment wants to name and explain.

#### `Answer`

| Field | Type | Default |
| - | - | - |
| `text` | `str` | required |
| `resolver` | `str` | required |
| `tier` | `str` | required |
| `at` | `Pointer` | required |

#### `Classes`

A label for an entry, read from the start of one key group: QT is a process, QĐ a regulation.

| Field | Type | Default |
| - | - | - |
| `from_` | `str` | required |
| `by_prefix` | `dict[str, str]` | required |

#### `ConceptRules`

Which lexicon entries become concepts, how they read, and which links to document concepts are kept.

| Field | Type | Default |
| - | - | - |
| `type` | `str` | `'term'` |
| `relation` | `str` | `'REFERS_TO'` |
| `actor` | `str` | `'entities-config'` |
| `describe_resolved` | `str` | `'{meaning} ({code}).'` |
| `describe_unresolved` | `str` | `'{code}, no full name is written in the loaded documents.'` |
| `max_share` | `float` | `0.08` |
| `min_passages_unresolved` | `int` | `3` |
| `lowercase_words` | `int` | `3` |
| `evidence_per_document` | `int` | `3` |
| `max_links` | `int` | `4` |
| `heading_chars` | `int` | `120` |
| `max_codes_in_passage` | `int` | `4` |

#### `EntityConfig`

| Field | Type | Default |
| - | - | - |
| `order` | `list[str]` | required |
| `kinds` | `dict[str, EntityKind]` | required |
| `concepts` | `ConceptRules \| None` | `None` |

#### `EntityEngine`

Construct with `EntityEngine(config: EntityConfig)`.

| Method | What it does |
| - | - |
| `claimed(self, text: str) -> list[tuple[str, re.Match]]` | — |
| `key_of(self, kind: str, m: re.Match) -> tuple[str, ...]` | — |
| `mentions(self, doc: Document, passage) -> list[tuple[tuple[str, ...], Pointer]]` | Every thing found in one passage, as its key and where it is. |
| `label_of(self, kind: str) -> str` | — |
| `build(self, documents: Iterable[Document]) -> Lexicon` | — |

#### `EntityKind`

| Field | Type | Default |
| - | - | - |
| `answer` | `Literal['title', 'expansion']` | required |
| `pattern` | `str` | required |
| `key` | `list[str]` | required |
| `label` | `str` | `''` |
| `concept` | `bool` | `True` |
| `classes` | `Classes \| None` | `None` |
| `title` | `TitleRules` | factory |
| `resolvers` | `list[Resolver]` | `[]` |

#### `Entry`

| Field | Type | Default |
| - | - | - |
| `kind` | `str` | required |
| `key` | `tuple[str, ...]` | required |
| `display` | `str` | required |
| `label` | `str` | `''` |
| `passages` | `int` | `0` |
| `documents` | `set[str]` | factory |
| `answers` | `list[Answer]` | factory |
| `mentions` | `list[Pointer]` | factory |
| `code` | `str` | `''` |

| Method | What it does |
| - | - |
| `best(self) -> Answer \| None` | The answer of the first resolver, in config order, that gave one. |
| `agree(self) -> bool` | Two or more resolvers answered and their wordings start the same. |

#### `Lexicon`

Construct with `Lexicon(entries: dict[tuple[str, ...], Entry], *, passages: int = 0, lowercase: collections.Counter \| None = None, expansions: frozenset[str] = frozenset())`.

| Method | What it does |
| - | - |
| `eligible(self, entry: Entry, rules: ConceptRules) -> bool` | Does this entry deserve a concept? |
| `of(self, kind: str) -> list[Entry]` | — |
| `get(self, kind: str, *key_parts: str) -> Entry \| None` | — |
| `unresolved(self, kind: str, min_passages: int = 1) -> list[Entry]` | — |

#### `Pointer`

Where something was found.

| Field | Type | Default |
| - | - | - |
| `document` | `str` | required |
| `passage` | `str` | `''` |
| `start` | `int` | `-1` |
| `length` | `int` | `0` |
| `fingerprint` | `str` | `''` |

#### `Resolver`

| Field | Type | Default |
| - | - | - |
| `type` | `Literal['metadata', 'title', 'text', 'paren_initials']` | required |
| `code` | `str \| None` | `None` |
| `pattern` | `str \| None` | `None` |
| `check` | `Optional[Literal['initials']]` | `None` |
| `tier` | `str` | `''` |
| `in_sections` | `str \| None` | `None` |
| `skip_title_matching` | `str \| None` | `None` |
| `skip_words` | `list[str]` | `['cua', 'va', 'doi', 'voi', 'cho', 'tai', 'tu', 'den', 'la', 'the', 'of', 'for', 'and', 'hoac']` |

#### `TitleRules`

What a good answer looks like.

| Field | Type | Default |
| - | - | - |
| `strip` | `list[Literal['numbers', 'extension', 'kinds']]` | `[]` |
| `min_words` | `int` | `1` |
| `reject` | `list[str]` | `[]` |

#### `expand`

```python theme={null}
expand(template: str, own: str, patterns: dict[str, str]) -> str
```

#### `expanded_patterns`

```python theme={null}
expanded_patterns(kinds: dict[str, EntityKind]) -> dict[str, str]
```

Each kind's pattern with \{other\_kind} replaced by that kind's own pattern, by dependency and not by priority.

#### `fold`

```python theme={null}
fold(text: str) -> str
```

The key form of a text: lowercase, no accents, no separators.

#### `load_entities`

```python theme={null}
load_entities(path: pathlib.Path | str) -> EntityConfig | None
```

The `entities:` section of ladygraph.yaml, or None when it has none.

#### `main`

```python theme={null}
main(argv: list[str] | None = None) -> None
```

#### `report`

```python theme={null}
report(lexicon: Lexicon, config: EntityConfig, kind: str | None = None, unresolved: bool = False, frequent: int = 5) -> str
```

## `sources.factory`

Build Sources from the `sources:` list of ladygraph.yaml.

#### `load_sources`

```python theme={null}
load_sources(path: pathlib.Path | str, decider: Decider | None = None) -> dict[str, Source]
```

Name -> Source for every entry that has an adapter.

## `sources.flatten`

Flatten and normalize unstructured input into Records, the common input of the JSON, pydantic and markdown sources.

#### `EntryFlattener`

Flattens parsed JSON (or a dumped pydantic model) by `EntryRule`s.

Construct with `EntryFlattener(rules: list[EntryRule], triage: 'FieldTriage \| None' = None)`.

| Method | What it does |
| - | - |
| `flatten(self, scope: str, raw: Any) -> list[Record]` | — |

#### `EntryRule`

Which part of a structured document to ingest, and how to read it.

| Field | Type | Default |
| - | - | - |
| `pointer` | `str` | required |
| `kind` | `str` | `''` |
| `id` | `str \| None` | `None` |
| `title` | `str \| None` | `None` |
| `where` | `dict[str, Any]` | `{}` |
| `words` | `list[str]` | `[]` |
| `declare` | `bool` | `False` |
| `rewrite` | `bool` | `False` |
| `concept_type` | `str` | `''` |
| `passage` | `Literal['field', 'entry']` | `'field'` |
| `fields` | `list[str] \| None` | `None` |
| `exclude` | `list[str]` | `[]` |
| `render` | `dict[str, Literal['text', 'dnf']]` | `{}` |
| `labels` | `dict[str, str]` | `{}` |
| `metadata` | `dict[str, str]` | `{}` |
| `access_labels` | `list[str]` | `[]` |
| `valid_from` | `str \| None` | `None` |
| `valid_until` | `str \| None` | `None` |

#### `FlatSource`

A `Source` over Records.

Construct with `FlatSource(*, name: str, scheme: str, shape: Literal['assemble', 'decompose', 'map'], load: Callable[[str], typing.Any], flattener: Flattener, document_scheme: str \| None = None, scope: str \| None = None)`.

| Method | What it does |
| - | - |
| `manifest(self, scope: str, now: datetime.datetime) -> dict[str, DocVersion]` | — |
| `read(self, scope: str, unit: str) -> Document` | — |
| `next_boundary(self, scope: str, now: datetime.datetime) -> datetime.datetime \| None` | — |

#### `Flattener`

Construct with `Flattener(*args, **kwargs)`.

| Method | What it does |
| - | - |
| `flatten(self, scope: str, raw: Any) -> list[Record]` | Turn the raw input for one scope into Records. |

#### `Part`

One passage of a Record.

| Field | Type | Default |
| - | - | - |
| `anchor` | `str` | required |
| `text` | `str` | required |
| `section` | `str` | `''` |

#### `Record`

One change unit, flattened.

| Field | Type | Default |
| - | - | - |
| `unit` | `str` | required |
| `title` | `str` | required |
| `parts` | `list[Part]` | required |
| `metadata` | `dict[str, Any]` | factory |
| `access_labels` | `list[str]` | factory |
| `valid_from` | `date \| None` | `None` |
| `valid_until` | `date \| None` | `None` |
| `declared` | `Declared \| None` | `None` |
| `rewrite` | `bool` | `False` |

#### `as_date`

```python theme={null}
as_date(value: Any) -> datetime.date | None
```

#### `as_list`

```python theme={null}
as_list(value: Any) -> list[str]
```

#### `clean`

```python theme={null}
clean(value: Any) -> str
```

A scalar as plain text.

#### `describe_markers`

```python theme={null}
describe_markers(clauses: Any) -> str
```

Marker clauses as a sentence.

#### `is_blank`

```python theme={null}
is_blank(value: Any) -> bool
```

#### `lines_for`

```python theme={null}
lines_for(value: Any, label: str) -> list[str]
```

The default rendering of one field: `label: text` lines, nested keys joined by dots.

#### `pointer_tokens`

```python theme={null}
pointer_tokens(pointer: str) -> list[str]
```

#### `walk`

```python theme={null}
walk(value: Any, tokens: list[str], so_far: tuple[str, ...] = ()) -> Iterator[tuple[tuple[str, ...], typing.Any]]
```

Every (concrete path, value) the pointer reaches.

## `sources.json`

JSON source: business configuration kept in a JSON (or YAML) file, one Document per entry (INTERFACE §7).

#### `JsonSource`

Construct with `JsonSource(config: JsonSourceConfig, base_dir: pathlib.Path \| None = None, decider: Decider \| None = None)`.

#### `JsonSourceConfig`

| Field | Type | Default |
| - | - | - |
| `kind` | `Literal['json']` | `'json'` |
| `name` | `str` | required |
| `scheme` | `str` | required |
| `document_scheme` | `str \| None` | `None` |
| `shape` | `Literal['assemble', 'decompose', 'map']` | `'map'` |
| `scope` | `str \| None` | `None` |
| `path` | `str` | required |
| `rules` | `list[EntryRule]` | factory |
| `auto` | `AutoConfig \| None` | `None` |
| `triage` | `TriageConfig \| None` | `None` |

#### `TriageConfig`

| Field | Type | Default |
| - | - | - |
| `cache` | `str` | required |
| `drop_threshold` | `float` | `0.7` |

#### `make_flattener`

```python theme={null}
make_flattener(config: JsonSourceConfig | typing.Any, triage: FieldTriage | None)
```

`auto` reads any file by its shape.

#### `make_triage`

```python theme={null}
make_triage(config: TriageConfig | None, base_dir: pathlib.Path | None, decider: Decider | None) -> FieldTriage | None
```

#### `read_structured`

```python theme={null}
read_structured(path: pathlib.Path) -> Any
```

Parse a JSON or YAML file.

#### `resolve_path`

```python theme={null}
resolve_path(template: str, scope: str, base_dir: pathlib.Path | None) -> pathlib.Path
```

## `sources.markdown`

Markdown source: knowledge kept as markdown files, one Document per section (INTERFACE §7, shape `decompose`).

#### `MarkdownFile`

| Field | Type | Default |
| - | - | - |
| `path` | `str` | required |
| `text` | `str` | required |

#### `MarkdownFlattener`

Construct with `MarkdownFlattener(config: MarkdownSourceConfig)`.

| Method | What it does |
| - | - |
| `flatten(self, scope: str, raw: list[MarkdownFile]) -> list[Record]` | — |

#### `MarkdownSource`

Construct with `MarkdownSource(config: MarkdownSourceConfig, base_dir: pathlib.Path \| None = None)`.

#### `MarkdownSourceConfig`

| Field | Type | Default |
| - | - | - |
| `kind` | `Literal['markdown']` | `'markdown'` |
| `name` | `str` | required |
| `scheme` | `str` | required |
| `document_scheme` | `str \| None` | `None` |
| `shape` | `Literal['assemble', 'decompose', 'map']` | `'decompose'` |
| `scope` | `str \| None` | `None` |
| `path` | `str` | required |
| `split_level` | `int` | `2` |
| `target_chars` | `int` | `800` |
| `skip_status` | `list[str]` | `['draft', 'deprecated']` |
| `metadata` | `dict[str, str]` | `{}` |
| `chunking` | `ChunkingConfig \| None` | `None` |

#### `find_files`

```python theme={null}
find_files(template: str, scope: str, base_dir: pathlib.Path | None) -> list[MarkdownFile]
```

#### `split_front_matter`

```python theme={null}
split_front_matter(text: str, path: str) -> tuple[dict[str, typing.Any], str]
```

## `sources.plan`

Show what an update would do, without extracting or writing anything.

#### `main`

```python theme={null}
main(argv: list[str] | None = None) -> None
```

## `sources.pydantic`

Pydantic source: configuration that already lives in code as pydantic models (INTERFACE §7).

#### `PydanticSource`

Construct with `PydanticSource(config: PydanticSourceConfig, provider: Callable[[str], typing.Any] \| None = None, base_dir: pathlib.Path \| None = None, decider: Decider \| None = None)`.

#### `PydanticSourceConfig`

| Field | Type | Default |
| - | - | - |
| `kind` | `Literal['pydantic']` | `'pydantic'` |
| `name` | `str` | required |
| `scheme` | `str` | required |
| `document_scheme` | `str \| None` | `None` |
| `shape` | `Literal['assemble', 'decompose', 'map']` | `'map'` |
| `scope` | `str \| None` | `None` |
| `provider` | `str` | required |
| `exclude_defaults` | `bool` | `False` |
| `rules` | `list[EntryRule]` | factory |
| `auto` | `AutoConfig \| None` | `None` |
| `triage` | `TriageConfig \| None` | `None` |

#### `dump`

```python theme={null}
dump(value: Any, *, exclude_defaults: bool = False) -> Any
```

Plain data from a model, or from dicts and lists that contain models.

#### `import_provider`

```python theme={null}
import_provider(spec: str) -> Callable[[str], typing.Any]
```

## `sources.resolver`

A Resolver that reads the live text back through a Source (INTERFACE section 6, "Resolver").

#### `SourceResolver`

Construct with `SourceResolver(source: Source, *, max_workers: int = 4)`.

| Method | What it does |
| - | - |
| `resolve_many(self, evidence: Sequence[Evidence], timeout_s: float, with_content: bool = False) -> dict[str, Resolution]` | Keyed by evidence id. |

## `sources.signals`

Deterministic signals that tell knowledge from system configuration, for any config in any domain.

#### `describe_clause`

```python theme={null}
describe_clause(clause: str) -> str
```

`a & b & !c` as words.

#### `effective_kind`

```python theme={null}
effective_kind(key: str, value: Any, extra_knowledge: tuple[str, ...] = (), extra_system: tuple[str, ...] = ()) -> str
```

`value_kind`, except that single words under a name that leans to knowledge are a vocabulary: `["laptop", "invoice"]` under `aliases` is keywords, under `route_codes` it is code words.

#### `is_protected`

```python theme={null}
is_protected(key: str, value: Any) -> bool
```

A field that marks its entry as secret or private, such as `kind: secret` or `visibility: restricted`.

#### `key_words`

```python theme={null}
key_words(key: str) -> list[str]
```

#### `name_lean`

```python theme={null}
name_lean(key: str, extra_knowledge: tuple[str, ...] = (), extra_system: tuple[str, ...] = ()) -> int
```

+1 for a field name that suggests knowledge, -1 for one that suggests configuration, 0 otherwise.

#### `normalized`

```python theme={null}
normalized(text: Any) -> str
```

#### `redact`

```python theme={null}
redact(text: str, kinds: tuple[str, ...] = ('email', 'phone')) -> str
```

Mask contact details, for a deployment that must not send them to a model.

#### `score_leaf`

```python theme={null}
score_leaf(key: str, value: Any, *, refers: int = 0, extra_knowledge: tuple[str, ...] = (), extra_system: tuple[str, ...] = ()) -> tuple[int, list[str]]
```

The score of one leaf and the reasons, in plain words.

#### `value_kind`

```python theme={null}
value_kind(value: Any) -> str
```

One of: empty, flag, number, time, contact, address, token, tokens, word, phrase, sentence, vocab, objects, mixed.

## `sources.sql`

SQL source: builds Documents from database rows (INTERFACE §7).

#### `ConnectionConfig`

| Field | Type | Default |
| - | - | - |
| `dsn` | `str` | required |

#### `DocumentMap`

| Field | Type | Default |
| - | - | - |
| `title` | `str` | required |
| `metadata` | `dict[str, str]` | `{}` |
| `access_labels` | `list[str]` | `[]` |
| `valid_from` | `str \| None` | `None` |
| `valid_until` | `str \| None` | `None` |
| `version` | `VersionMap` | required |
| `passage` | `PassageMap` | required |

#### `Executor`

Construct with `Executor(*args, **kwargs)`.

| Method | What it does |
| - | - |
| `run(self, connection: str, sql: str, params: dict[str, typing.Any]) -> list[dict[str, typing.Any]]` | — |

#### `PassageMap`

| Field | Type | Default |
| - | - | - |
| `text` | `str` | required |
| `anchor` | `str \| None` | `None` |
| `section` | `str \| None` | `None` |

#### `Queries`

| Field | Type | Default |
| - | - | - |
| `manifest` | `QueryConfig` | required |
| `document` | `QueryConfig` | required |
| `passages` | `QueryConfig \| None` | `None` |
| `passages_digest` | `QueryConfig \| None` | `None` |
| `replacements` | `QueryConfig \| None` | `None` |
| `get_scope` | `QueryConfig \| None` | `None` |

#### `QueryConfig`

| Field | Type | Default |
| - | - | - |
| `sql` | `str` | required |
| `connection` | `str \| None` | `None` |

#### `Relationship`

| Field | Type | Default |
| - | - | - |
| `from_` | `str` | required |
| `to` | `str` | required |
| `cardinality` | `Literal['1:1', '1:N', 'N:M']` | required |
| `pick` | `Optional[Literal['latest']]` | `None` |
| `order_by` | `str \| None` | `None` |

#### `SqlAlchemyExecutor`

Construct with `SqlAlchemyExecutor(connections: dict[str, ConnectionConfig])`.

| Method | What it does |
| - | - |
| `run(self, connection: str, sql: str, params: dict[str, typing.Any]) -> list[dict[str, typing.Any]]` | — |

#### `SqlSource`

Construct with `SqlSource(config: SqlSourceConfig, executor: Executor \| None = None)`.

| Method | What it does |
| - | - |
| `manifest(self, scope: str, now: datetime.datetime) -> dict[str, DocVersion]` | Valid units and their versions. |
| `read(self, scope: str, unit: str) -> Document` | — |
| `next_boundary(self, scope: str, now: datetime.datetime) -> datetime.datetime \| None` | — |

#### `SqlSourceConfig`

| Field | Type | Default |
| - | - | - |
| `kind` | `Literal['sql']` | `'sql'` |
| `name` | `str` | required |
| `scheme` | `str` | required |
| `document_scheme` | `str \| None` | `None` |
| `shape` | `Literal['assemble', 'decompose', 'map']` | required |
| `connections` | `dict[str, ConnectionConfig]` | required |
| `tables` | `dict[str, TableConfig]` | required |
| `relationships` | `list[Relationship]` | `[]` |
| `queries` | `Queries` | required |
| `map` | `DocumentMap` | required |
| `serves_from` | `str \| None` | `None` |

#### `TableConfig`

| Field | Type | Default |
| - | - | - |
| `connection` | `str` | required |
| `key` | `str \| list[str] \| None` | `None` |
| `columns` | `list[str]` | required |

#### `VersionMap`

| Field | Type | Default |
| - | - | - |
| `file` | `list[str]` | required |
| `metadata` | `list[str]` | `[]` |

## `sources.triage`

Let a Decider judge which fields of a config entry are knowledge, so nobody has to list `exclude:` by hand.

#### `FieldTriage`

Construct with `FieldTriage(decider: Decider \| None, cache: pathlib.Path \| str, *, drop_threshold: float = 0.7)`.

| Method | What it does |
| - | - |
| `keep(self, kind: str, field: str, value: Any) -> bool` | — |
| `keep_text(self, title: str, text: str) -> bool` | Is this long text knowledge, or only an instruction to an assistant? |

#### `build_decider`

```python theme={null}
build_decider(name: str) -> Decider
```

#### `main`

```python theme={null}
main(argv: list[str] | None = None) -> None
```

#### `value_type`

```python theme={null}
value_type(value: Any) -> str
```

A stable English phrase for the kind of value.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.