> ## 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.

# extractors

> Find concepts in a Document.

## `extractors.build`

Build the extractor a deployment asked for: the `extractor:` section of ladygraph.yaml, and the LADYGRAPH\_EXTRACTOR switch.

#### `build_extractor`

```python theme={null}
build_extractor(config: ExtractorConfig | pathlib.Path | str, *, llm: LlmClient | None = None, embedder: Embedder | None = None, prompt: PromptConfig | None = None, tagger: SpanTagger | None = None, env: Mapping[str, str] | None = None, hints: Callable[[str], str] | None = None, check: Callable[[str, str], list[str]] | None = None, clock: Callable[[], datetime.datetime] = <function <lambda> at 0x1042fba60>) -> LlmOkfExtractor
```

The extractor for `config` (a config object, or the path of a ladygraph.yaml: its `prompt:` section is read too).

#### `build_llm`

```python theme={null}
build_llm(config: LlmConfig) -> LlmClient
```

#### `build_tagger`

```python theme={null}
build_tagger(config: TaggerConfig) -> SpanTagger
```

#### `effective_kind`

```python theme={null}
effective_kind(config: ExtractorConfig, env: Mapping[str, str] | None = None) -> str
```

`extractor.kind`, unless LADYGRAPH\_EXTRACTOR is set: then that.

## `extractors.compare`

Run two extractors on the same Document and say where they differ, without writing anything: the way to see what a flag would change.

#### `Comparison`

| Field | Type | Default |
| - | - | - |
| `document` | `str` | required |
| `only_a` | `list[str]` | factory |
| `only_b` | `list[str]` | factory |
| `both` | `list[str]` | factory |
| `a` | `Contribution \| None` | `None` |
| `b` | `Contribution \| None` | `None` |

| Method | What it does |
| - | - |
| `(property)` | Shared concepts over all concepts either one made; 1.0 when neither made any. |

#### `compare_extractors`

```python theme={null}
compare_extractors(doc: Document, ontology: Ontology, registry: ContextRegistry, a, b) -> Comparison
```

`a` and `b` are extractors (anything with `extract`); every passage of `doc` is treated as changed.

## `extractors.config`

The `extractor:` section of ladygraph.yaml: which extractor builds the concepts, and how. This is the feature flag.

#### `ExtractorConfig`

| Field | Type | Default |
| - | - | - |
| `kind` | `Literal['llm_okf', 'ner', 'translate_ner']` | `'llm_okf'` |
| `llm` | `LlmConfig \| None` | `None` |
| `ner` | `NerConfig \| None` | `None` |
| `translate` | `TranslateSettings \| None` | `None` |
| `relations` | `bool` | `True` |
| `relations_llm` | `LlmConfig \| None` | `None` |

#### `NerConfig`

| Field | Type | Default |
| - | - | - |
| `tagger` | `TaggerConfig \| None` | `None` |
| `labels` | `dict[str, str]` | required |
| `threshold` | `float` | `0.5` |
| `window_words` | `int` | `8` |
| `overlap_words` | `int` | `2` |
| `description` | `str` | `'{label}: {text}'` |

#### `TaggerConfig`

A GLiNER model on this machine (`ner.litert_gliner.LiteRtGliner`).

| Field | Type | Default |
| - | - | - |
| `kind` | `Literal['litert_gliner']` | `'litert_gliner'` |
| `tflite` | `str` | required |
| `tokenizer` | `str` | `'gliner-community/gliner_small-v2.5'` |
| `device` | `Literal['cpu', 'gpu']` | `'gpu'` |
| `cache_dir` | `str \| None` | `None` |
| `local_files_only` | `bool` | `False` |
| `encoder_kind` | `Literal['auto', 'tokenizers', 'gliner']` | `'auto'` |

#### `TranslateSettings`

For `translate_ner`: the passage is translated, the translation is tagged, and each find is mapped back to a quote in the original.

| Field | Type | Default |
| - | - | - |
| `translation` | `AlignedConfig` | factory |
| `translate_llm` | `LlmConfig \| None` | `None` |
| `escalate_llm` | `LlmConfig \| None` | `None` |
| `backquote_llm` | `LlmConfig \| None` | `None` |
| `verify_llm` | `LlmConfig \| None` | `None` |
| `min_chars` | `int` | `3` |
| `drop_bare_numbers` | `bool` | `True` |
| `ignore` | `list[str]` | `[]` |

#### `load_extractor_config`

```python theme={null}
load_extractor_config(path: pathlib.Path | str) -> ExtractorConfig
```

The `extractor:` section of a ladygraph.yaml; the first version (llm\_okf) when the file has no such section.

#### `resolve_paths`

```python theme={null}
resolve_paths(config: ExtractorConfig, base: pathlib.Path) -> ExtractorConfig
```

Make the file paths of the config absolute, taken from `base` (the folder of ladygraph.yaml), so the program can start from anywhere.

## `extractors.declared`

Concepts from entries that a human already described, with no LLM (INTERFACE §6, `DeclaredExtractor`).

#### `DeclaredExtractor`

Construct with `DeclaredExtractor(*, embedder: Embedder \| None = None, types: dict[str, str] \| None = None, producer: str = 'declared', clock: Callable[[], datetime.datetime] = <function DeclaredExtractor.<lambda> at 0x10434c4a0>)`.

| Method | What it does |
| - | - |
| `extract(self, doc: Document, changed: set[str], ontology: Ontology, registry: ContextRegistry, pinned: list[ContextChoice], context_only: bool) -> Contribution` | — |

#### `DeclaredOrLlm`

A declared Document goes to the declared extractor, any other to the LLM extractor.

Construct with `DeclaredOrLlm(declared: Extractor, llm: Extractor)`.

| Method | What it does |
| - | - |
| `extract(self, doc: Document, changed: set[str], ontology: Ontology, registry: ContextRegistry, pinned: list[ContextChoice], context_only: bool) -> Contribution` | — |

## `extractors.lexicon`

Lexicon entries as concepts, chained to the concepts of the documents (`LexiconExtractor`).

#### `LexiconExtractor`

Construct with `LexiconExtractor(inner: Extractor, engine: EntityEngine, lexicon: Lexicon, *, embedder: Embedder \| None = None, producer: str = 'lexicon', clock: Callable[[], datetime.datetime] = <function LexiconExtractor.<lambda> at 0x10434c400>)`.

| Method | What it does |
| - | - |
| `extract(self, doc: Document, changed: set[str], ontology: Ontology, registry: ContextRegistry, pinned: list[ContextChoice], context_only: bool) -> Contribution` | — |

## `extractors.llm_okf`

LLM extractor: one changed Document in, one Contribution out (see extractors/docs/LOGIC.md).

#### `ContextPick`

| Field | Type | Default |
| - | - | - |
| `choice` | `int \| None` | required |
| `new_label` | `str \| None` | `None` |
| `new_description` | `str \| None` | `None` |

#### `LlmOkfExtractor`

Construct with `LlmOkfExtractor(llm: LlmClient, *, embedder: Embedder \| None = None, producer: str = 'llm-okf', clock: Callable[[], datetime.datetime] = <function LlmOkfExtractor.<lambda> at 0x1042fa160>, prompt: PromptConfig \| None = None)`.

| Method | What it does |
| - | - |
| `extract(self, doc: Document, changed: set[str], ontology: Ontology, registry: ContextRegistry, pinned: list[ContextChoice], context_only: bool) -> Contribution` | — |

#### `evidence_id`

```python theme={null}
evidence_id(passage: Passage, start: int, length: int) -> str
```

#### `locate_quote`

```python theme={null}
locate_quote(doc: Document, targets: list[tuple[Passage, str]], quote: str) -> tuple[Passage, Evidence] | None
```

Find `quote` in the faithfully normalized text of a changed passage.

#### `pick_prompt`

```python theme={null}
pick_prompt(doc: Document, g: _Grounded, options: list[Context]) -> str
```

#### `render_document`

```python theme={null}
render_document(doc: Document, changed: set[str]) -> str
```

The whole document as one text.

## `extractors.ner_extractor`

Extractors that find concepts with a span tagger instead of (or beside) a model's reading of the document.

#### `NerExtractor`

Construct with `NerExtractor(llm: LlmClient, tagger: SpanTagger, config: NerConfig, *, embedder: Embedder \| None = None, producer: str \| None = None, clock: Callable[[], datetime.datetime] = <function NerExtractor.<lambda> at 0x1042fb100>, prompt: PromptConfig \| None = None, relations: bool = True, relations_llm: LlmClient \| None = None)`.

## `extractors.prompt`

The wording of the extraction prompts, as data: the `prompt:` section of ladygraph.yaml.

#### `ExampleConcept`

| Field | Type | Default |
| - | - | - |
| `word` | `str` | required |
| `type` | `str` | required |
| `description` | `str` | required |
| `quote` | `str` | required |

#### `ExampleRelation`

| Field | Type | Default |
| - | - | - |
| `source` | `str` | required |
| `type` | `str` | required |
| `target` | `str` | required |
| `quote` | `str` | required |

#### `PickPrompt`

The prompt that places a concept in the topic tree.

| Field | Type | Default |
| - | - | - |
| `domain` | `str \| None` | `None` |
| `label_language` | `str` | `'the language of the documents'` |
| `label_example` | `str \| None` | `None` |

#### `PromptConfig`

| Field | Type | Default |
| - | - | - |
| `domain` | `str` | `'documents'` |
| `concept_hint` | `str` | `'something a reader could search for, of one of the concept types below'` |
| `word_language` | `str` | `"the document's own language and spelling, keeping diacritics"` |
| `description_language` | `str \| None` | `None` |
| `skip` | `list[str]` | `['article or section numbers and headings', 'anything the passage only mentions without saying something about it']` |
| `extra` | `str` | `''` |
| `example` | `PromptExample \| None` | factory |
| `pick` | `PickPrompt` | factory |
| `contact_note` | `str` | `'Also gives context on who to contact, with an e-mail address or phone number.'` |

| Method | What it does |
| - | - |
| `stamp_suffix(self) -> str` | "" for the default wording, else "-" and 8 hex digits of the config: the same config always gives the same suffix. |
| `render_system(self, *, brief: str, types: list[str], relations: list[str]) -> str` | The system prompt of the extraction call. |
| `render_relations_system(self, *, brief: str, relations: list[str]) -> str` | The system prompt of a relations-only call: the concepts are already found, the model says which are related and quotes where the text says so. |
| `render_pick_system(self) -> str` | The system prompt that places a concept in the topic tree. |

#### `PromptExample`

| Field | Type | Default |
| - | - | - |
| `passage` | `str` | required |
| `candidates` | `list[ExampleConcept]` | required |
| `relations` | `list[ExampleRelation]` | `[]` |

#### `load_prompt`

```python theme={null}
load_prompt(path: pathlib.Path | str) -> PromptConfig
```

The `prompt:` section of a ladygraph.yaml; the default wording when the file has no such section.

## `extractors.reading`

Write each flattened config entry as a cohesive reading before it is extracted (`ReadingExtractor`).

#### `Reading`

The entry as a few short typed fields that the code stitches into one block of prose, in a fixed order.

| Field | Type | Default |
| - | - | - |
| `what` | `str` | required |
| `when` | `list[Annotated[str, StringConstraints(strip_whitespace=None, to_upper=None, to_lower=None, strict=None, min_length=None, max_length=320, pattern=None, ascii_only=None)]]` | required |
| `leads_to` | `str` | required |
| `related` | `list[Annotated[str, StringConstraints(strip_whitespace=None, to_upper=None, to_lower=None, strict=None, min_length=None, max_length=320, pattern=None, ascii_only=None)]]` | required |

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

#### `ReadingExtractor`

Wraps an extractor. An entry that asks for a rewrite gets a reading and its quoted words; any other Document (declared, prose, SQL) goes straight to the inner extractor.

Construct with `ReadingExtractor(inner: Extractor, llm: LlmClient, *, clock: Callable[[], datetime.datetime] = <function ReadingExtractor.<lambda> at 0x10434d580>)`.

| Method | What it does |
| - | - |
| `extract(self, doc: Document, changed: set[str], ontology: Ontology, registry: ContextRegistry, pinned: list[ContextChoice], context_only: bool) -> Contribution` | — |

#### `mask_quotes`

```python theme={null}
mask_quotes(text: str, spans: list[str]) -> str
```

Each quotation as a token `[[Q1: term]]`.

#### `quoted_spans`

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

Every usable quotation in the text, with its quotation marks, exactly as written.

#### `quoted_words`

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

The words a person put in quotation marks, as match clauses (`a & b & !c`), in order and without repeats.

#### `unmask_quotes`

```python theme={null}
unmask_quotes(text: str, spans: list[str]) -> tuple[str, list[str]]
```

The text with every token replaced by the original quotation, character for character, whatever the model wrote inside the token.

## `extractors.translate_ner`

`translate_ner`: concepts found in a TRANSLATION, grounded in the ORIGINAL.

#### `TranslateNerExtractor`

Construct with `TranslateNerExtractor(llm: LlmClient, tagger: SpanTagger, config: NerConfig, settings: TranslateSettings \| None = None, *, translate_llm: LlmClient \| None = None, escalate_llm: LlmClient \| None = None, backquote_llm: LlmClient \| None = None, hints: Callable[[str], str] \| None = None, check: Callable[[str, str], list[str]] \| None = None, embedder: Embedder \| None = None, producer: str \| None = None, clock: Callable[[], datetime.datetime] = <function TranslateNerExtractor.<lambda> at 0x1042fb060>, prompt: PromptConfig \| None = None, relations: bool = True, relations_llm: LlmClient \| None = None)`.


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