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

> Choose which extractor finds concepts, and which model it uses.

Which extractor finds the concepts is a setting, not code. With no `extractor:` section, the first version, `llm_okf`, is built.

```yaml theme={null}
extractor:
  kind: ner             # llm_okf (default) | ner | translate_ner
  llm: {backend: litert, model: model/gemma-4-E2B-it-gpu.litertlm, device: gpu}
  ner:
    tagger: {tflite: model/tflite/gliner_gpu.tflite, tokenizer: model/gliner_small-v2.5, device: gpu}
    labels: {organization: unit, "performance indicator": rule}    # tagger label -> ontology type
```

| Kind | Who finds the concepts | Model calls | Relations |
| - | - | - | - |
| `llm_okf` | A model reads the document and lists them. | One per document, plus one per concept to place it. | Yes |
| `ner` | The span tagger (GLiNER). Descriptions are a template. | One per concept, to place it, and one per document for relations. | Yes, from the model |
| `translate_ner` | The tagger on a translation, grounded in the original. See [Translation and verification](https://ladygraph.io.vn/guides/translation.md). | As `ner`, plus translation and back-quotes. | As `ner` |

What stays the same in every kind: a concept's evidence is a quote found in the passage as written, placement and relation checks are shared code, and the result goes through the same validation. Only the step that finds concepts differs.

## Choose a model: `extractor.llm`

Every kind needs a model, because every kind places each concept in a context.

| Key | Meaning |
| - | - |
| `backend` | `litellm` (a gateway you run), `gemini`, or `litert` (a model file on this machine). |
| `model` | For `litellm`, the gateway's model name. For `gemini`, a Gemini model name. For `litert`, the path of a `.litertlm` file. |
| `device` | `litert` only: `cpu`, `gpu` or `npu`. |
| `api_base`, `api_key_env` | For `litellm`, the address, and the name of the environment variable that holds the key. |
| `temperature`, `max_output_tokens`, `timeout_s`, `attempts`, `backoff_s`, `repair_attempts` | Request limits. The defaults are fine to start. |
| `thinking` | `default`, `off` or `on`. A thinking-off model is much faster for extraction. |

The defaults, from `LlmConfig`: `temperature` 0.0 (the extractor must be reproducible), `max_output_tokens` 8192, `timeout_s` 120 per request, `attempts` 3, `backoff_s` 1.0 (doubling after each failed attempt), and `repair_attempts` 1.

> [!WARNING]
>
> The backend of a `litert` model must match how the file was built. See [Local models](https://ladygraph.io.vn/guides/local-models.md).

## The `ner` and `translate_ner` kinds

`ner` and `translate_ner` also need an `ner:` section with the GLiNER model and the labels to ask for. This section is tested:

```yaml theme={null}
kind: ner
llm: {backend: litert, model: models/gemma-4-E2B-it-gpu.litertlm, device: gpu}
ner:
  tagger:
    tflite: models/gliner_gpu.tflite          # the converted GLiNER model
    tokenizer: models/gliner_small-v2.5       # the checkpoint folder it was converted from, or a Hugging Face name
    device: gpu                               # cpu | gpu
  labels:                                     # GLiNER label -> ontology type; only these are asked for
    organization: unit
    "performance indicator": rule
  threshold: 0.5
  window_words: 8
  overlap_words: 2
```

| Key | Meaning |
| - | - |
| `tflite` | The converted `.tflite` file. Relative paths are read from the folder of `ladygraph.yaml`. |
| `tokenizer` | A local folder of the original checkpoint, which needs `tokenizer.json` and `gliner_config.json`, or a Hugging Face name such as `gliner-community/gliner_small-v2.5`. A name is looked up in `cache_dir`. Add `local_files_only: true` to stay offline. |
| `labels` | The words GLiNER is asked for, each mapped to an ontology type. Every type must exist in `ontology.yaml`. The label is a zero-shot name, so write it the way the text would say it ("performance indicator"). The converted model carries 5 labels per call. |
| `window_words`, `overlap_words` | The converted model takes about 15 words at a time. Text is cut into windows of `window_words` words, and `overlap_words` of them repeat. |

If the tokenizer folder lacks those two files, loading falls back to the `gliner` package, which is slower and needs torch. `encoder_kind: tokenizers` makes that an error instead. Load time is about 8 s on the GPU, mostly compiling the model. There is no torch in the default path.

### Relations

A tagger names no relation, so one model call per document asks for them. It uses the `llm_okf` schema and checks: the quote must be in the text, and the types and cardinality must fit. What fails is dropped.

* The model is `extractor.relations_llm` if set, otherwise `extractor.llm`.
* `relations: false` turns the call off.
* The call is skipped when fewer than two concepts were found, or when the ontology has no relation a machine may create.

> [!NOTE]
>
> Measured once with the E2B model on 3 real chunks: the call ran and E2B proposed none. A stronger `relations_llm` is the first thing to try.

## Switch and build

* **Switch one run without editing the file** with `LADYGRAPH_EXTRACTOR=ner`. A wrong value is an error, not a silent default. The variable cannot add settings the file lacks: `ner` without an `extractor.ner:` section fails with a message.
* **Build it** with `build_extractor("ladygraph.yaml", embedder=...)`. It reads this section and the `prompt:` section. Arguments `llm=`, `tagger=` and `embedder=` win over the file. A local model builds lazily, so nothing loads until the first call.

## Compare before you switch

`compare_extractors(doc, ontology, registry, a, b)` runs two extractors on one document. It returns what only `a` made, what only `b` made, what both made, and an agreement score. It writes nothing.

Lesson 9 shows all three kinds side by side on one document and compares them.

> [!WARNING]
>
> How `ner` and `translate_ner` compare with `llm_okf` on real documents has not been measured. The tests use fakes. `docs/extractor.example.yaml` in the repository is a full example.

## A note on short concepts

A one-word or bare-figure concept is kept, such as "KPI" or "70%". Only an e-mail address or a phone number, with at most a stray word, is dropped.


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