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

> Tìm các khái niệm trong một Tài liệu.

## `extractors.build`

Dựng bộ trích xuất mà một lần triển khai yêu cầu: phần `extractor:` của ladygraph.yaml, và công tắc LADYGRAPH\_EXTRACTOR.

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

Bộ trích xuất cho `config` (một đối tượng cấu hình, hoặc đường dẫn của một ladygraph.yaml: phần `prompt:` của nó cũng được đọc).

#### `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`, trừ khi LADYGRAPH\_EXTRACTOR được đặt: khi đó là giá trị đó.

## `extractors.compare`

Chạy hai bộ trích xuất trên cùng một Tài liệu và cho biết chúng khác nhau ở đâu, mà không ghi gì: cách để xem một cờ sẽ thay đổi điều gì.

#### `Comparison`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `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` |

| Phương thức | Làm gì |
| - | - |
| `(property)` | Các khái niệm chung trên tất cả các khái niệm mà một trong hai bên tạo ra; 1.0 khi cả hai không tạo ra khái niệm nào. |

#### `compare_extractors`

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

`a` và `b` là các bộ trích xuất (bất cứ thứ gì có `extract`); mọi đoạn của `doc` được coi là đã thay đổi.

## `extractors.config`

Phần `extractor:` của ladygraph.yaml: bộ trích xuất nào dựng các khái niệm, và dựng như thế nào. Đây là cờ tính năng.

#### `ExtractorConfig`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `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`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `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`

Một mô hình GLiNER trên máy này (`ner.litert_gliner.LiteRtGliner`).

| Trường | Kiểu | Mặc định |
| - | - | - |
| `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`

Cho `translate_ner`: đoạn văn được dịch, bản dịch được gắn nhãn, và mỗi thứ tìm được được ánh xạ về một trích dẫn trong bản gốc.

| Trường | Kiểu | Mặc định |
| - | - | - |
| `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
```

Phần `extractor:` của một ladygraph.yaml; phiên bản đầu tiên (llm\_okf) khi tệp không có phần như vậy.

#### `resolve_paths`

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

Biến các đường dẫn tệp của cấu hình thành đường dẫn tuyệt đối, tính từ `base` (thư mục của ladygraph.yaml), để chương trình có thể khởi động từ bất kỳ đâu.

## `extractors.declared`

Các khái niệm từ những mục mà một người đã mô tả, không cần LLM (INTERFACE §6, `DeclaredExtractor`).

#### `DeclaredExtractor`

Khởi tạo bằng `DeclaredExtractor(*, embedder: Embedder \| None = None, types: dict[str, str] \| None = None, producer: str = 'declared', clock: Callable[[], datetime.datetime] = <function DeclaredExtractor.<lambda> at 0x10434c4a0>)`.

| Phương thức | Làm gì |
| - | - |
| `extract(self, doc: Document, changed: set[str], ontology: Ontology, registry: ContextRegistry, pinned: list[ContextChoice], context_only: bool) -> Contribution` | — |

#### `DeclaredOrLlm`

Một Tài liệu đã khai báo đi tới bộ trích xuất khai báo, mọi Tài liệu khác đi tới bộ trích xuất LLM.

Khởi tạo bằng `DeclaredOrLlm(declared: Extractor, llm: Extractor)`.

| Phương thức | Làm gì |
| - | - |
| `extract(self, doc: Document, changed: set[str], ontology: Ontology, registry: ContextRegistry, pinned: list[ContextChoice], context_only: bool) -> Contribution` | — |

## `extractors.lexicon`

Các mục từ điển như các khái niệm, nối với các khái niệm của các tài liệu (`LexiconExtractor`).

#### `LexiconExtractor`

Khởi tạo bằng `LexiconExtractor(inner: Extractor, engine: EntityEngine, lexicon: Lexicon, *, embedder: Embedder \| None = None, producer: str = 'lexicon', clock: Callable[[], datetime.datetime] = <function LexiconExtractor.<lambda> at 0x10434c400>)`.

| Phương thức | Làm gì |
| - | - |
| `extract(self, doc: Document, changed: set[str], ontology: Ontology, registry: ContextRegistry, pinned: list[ContextChoice], context_only: bool) -> Contribution` | — |

## `extractors.llm_okf`

Bộ trích xuất LLM: một Tài liệu đã thay đổi đi vào, một Contribution đi ra (xem extractors/docs/LOGIC.md).

#### `ContextPick`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `choice` | `int \| None` | required |
| `new_label` | `str \| None` | `None` |
| `new_description` | `str \| None` | `None` |

#### `LlmOkfExtractor`

Khởi tạo bằng `LlmOkfExtractor(llm: LlmClient, *, embedder: Embedder \| None = None, producer: str = 'llm-okf', clock: Callable[[], datetime.datetime] = <function LlmOkfExtractor.<lambda> at 0x1042fa160>, prompt: PromptConfig \| None = None)`.

| Phương thức | Làm gì |
| - | - |
| `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
```

Tìm `quote` trong văn bản đã chuẩn hóa trung thực của một đoạn đã thay đổi.

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

Toàn bộ tài liệu dưới dạng một văn bản.

## `extractors.ner_extractor`

Các bộ trích xuất tìm các khái niệm bằng một bộ gắn nhãn khoảng văn bản thay cho (hoặc bên cạnh) việc một mô hình đọc tài liệu.

#### `NerExtractor`

Khởi tạo bằng `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`

Cách diễn đạt của các prompt trích xuất, dưới dạng dữ liệu: phần `prompt:` của ladygraph.yaml.

#### `ExampleConcept`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `word` | `str` | required |
| `type` | `str` | required |
| `description` | `str` | required |
| `quote` | `str` | required |

#### `ExampleRelation`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `source` | `str` | required |
| `type` | `str` | required |
| `target` | `str` | required |
| `quote` | `str` | required |

#### `PickPrompt`

Prompt đặt một khái niệm vào cây chủ đề.

| Trường | Kiểu | Mặc định |
| - | - | - |
| `domain` | `str \| None` | `None` |
| `label_language` | `str` | `'the language of the documents'` |
| `label_example` | `str \| None` | `None` |

#### `PromptConfig`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `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.'` |

| Phương thức | Làm gì |
| - | - |
| `stamp_suffix(self) -> str` | "" cho cách diễn đạt mặc định, nếu không thì "-" và 8 chữ số hex của cấu hình: cùng một cấu hình luôn cho cùng một hậu tố. |
| `render_system(self, *, brief: str, types: list[str], relations: list[str]) -> str` | System prompt của lần gọi trích xuất. |
| `render_relations_system(self, *, brief: str, relations: list[str]) -> str` | System prompt của một lần gọi chỉ về quan hệ: các khái niệm đã được tìm thấy, mô hình cho biết cái nào có liên quan và trích dẫn nơi văn bản nói như vậy. |
| `render_pick_system(self) -> str` | System prompt đặt một khái niệm vào cây chủ đề. |

#### `PromptExample`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `passage` | `str` | required |
| `candidates` | `list[ExampleConcept]` | required |
| `relations` | `list[ExampleRelation]` | `[]` |

#### `load_prompt`

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

Phần `prompt:` của một ladygraph.yaml; cách diễn đạt mặc định khi tệp không có phần như vậy.

## `extractors.reading`

Viết mỗi mục cấu hình đã làm phẳng thành một bản đọc mạch lạc trước khi nó được trích xuất (`ReadingExtractor`).

#### `Reading`

Mục đó dưới dạng vài trường ngắn có kiểu mà mã ghép thành một khối văn xuôi, theo thứ tự cố định.

| Trường | Kiểu | Mặc định |
| - | - | - |
| `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 |

| Phương thức | Làm gì |
| - | - |
| `(property)` | — |

#### `ReadingExtractor`

Bọc một bộ trích xuất. Một mục yêu cầu viết lại sẽ nhận một bản đọc và các từ trong ngoặc kép của nó; mọi Tài liệu khác (đã khai báo, văn xuôi, SQL) đi thẳng tới bộ trích xuất bên trong.

Khởi tạo bằng `ReadingExtractor(inner: Extractor, llm: LlmClient, *, clock: Callable[[], datetime.datetime] = <function ReadingExtractor.<lambda> at 0x10434d580>)`.

| Phương thức | Làm gì |
| - | - |
| `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
```

Mỗi câu trích dẫn thành một token `[[Q1: term]]`.

#### `quoted_spans`

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

Mọi câu trích dẫn dùng được trong văn bản, kèm dấu ngoặc kép của nó, đúng như đã viết.

#### `quoted_words`

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

Các từ mà một người đặt trong dấu ngoặc kép, dưới dạng các mệnh đề khớp (`a & b & !c`), theo thứ tự và không lặp lại.

#### `unmask_quotes`

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

Văn bản với mọi token được thay bằng câu trích dẫn gốc, từng ký tự một, bất kể mô hình đã viết gì bên trong token.

## `extractors.translate_ner`

`translate_ner`: các khái niệm tìm thấy trong một BẢN DỊCH, có căn cứ vào BẢN GỐC.

#### `TranslateNerExtractor`

Khởi tạo bằng `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.