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

# core

> Mô hình dữ liệu, các giao diện, và các quy tắc mà mọi gói khác tuân theo.

Lõi LadyGraph: mô hình dữ liệu, các quy tắc và logic cập nhật dùng chung cho mọi hệ thống.

## `core.address`

Địa chỉ: các chuỗi URI chuẩn, `<scheme>://<scope>/<locator>#<anchor>`.

#### `AddressParts`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `scheme` | `str` | required |
| `scope` | `str` | required |
| `locator` | `str` | required |
| `anchor` | `str` | required |

#### `canonical_address`

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

#### `parse_address`

```python theme={null}
parse_address(address: str) -> AddressParts
```

## `core.concept`

#### `Candidate`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `word` | `str` | required |
| `type` | `Literal['Rule', 'Body']` | required |
| `description` | `str` | required |
| `quote` | `str` | required |

#### `Found`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `candidates` | `list[Candidate]` | required |

#### `extraction_schema`

```python theme={null}
extraction_schema(types: list[str], relations: list[str], max_items: int = 40) -> type[pydantic.main.BaseModel]
```

Lược đồ câu trả lời cho một ontology: `type` và `type` của quan hệ chỉ có thể là những giá trị mà nó khai báo.

## `core.merge`

Áp dụng một Contribution, và hợp nhất các Concept (INTERFACE §3, quyền sở hữu).

#### `ApplyResult`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `concepts` | `dict[str, Concept]` | required |
| `events` | `list[tuple[Literal['created', 'updated', 'deprecated', 'deleted', 'merged'], str]]` | factory |

#### `apply_contribution`

```python theme={null}
apply_contribution(concepts: dict[str, Concept], c: Contribution) -> ApplyResult
```

1. Gỡ Evidence của đơn vị này cho các đoạn bị thay thế (tất cả các đoạn khi `replaces` là None), và các Relation mất hết Evidence.

#### `merge_concepts`

```python theme={null}
merge_concepts(keep: Concept, other: Concept) -> tuple[Concept, Concept]
```

Hợp nhất `other` vào `keep`.

## `core.model`

Mô hình dữ liệu LadyGraph. Các thuật ngữ được định nghĩa trong CONTEXT.md; xem docs/INTERFACE.md §2–3.

#### `Actorstamp`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `by` | `str` | required |
| `at` | `datetime.datetime` | required |

#### `Concept`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `id` | `str` | required |
| `scope` | `str` | required |
| `path` | `str` | required |
| `type` | `str` | required |
| `context` | `str` | required |
| `title` | `str` | required |
| `words` | `list[Word]` | `[]` |
| `description` | `str` | `''` |
| `resource` | `Optional[Annotated[str, AfterValidator(func=<function canonical_address at 0x10154a340>)]]` | `None` |
| `body` | `str` | `''` |
| `evidence` | `list[Evidence]` | `[]` |
| `generated` | `Actorstamp` | required |
| `verified` | `list[Actorstamp]` | `[]` |
| `status` | `Literal['draft', 'stable', 'deprecated']` | `'stable'` |
| `relations` | `list[Relation]` | `[]` |
| `merged_into` | `str \| None` | `None` |
| `embedding` | `list[float] \| None` | `None` |
| `embedding_model` | `str` | `''` |
| `extra` | `dict[str, Any]` | `{}` |

| Phương thức | Làm gì |
| - | - |
| `(property)` | — |
| `trust_tier(self) -> Literal['unverified', 'machine', 'human']` | — |

#### `Context`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `id` | `str` | required |
| `scope` | `str` | required |
| `parent` | `str \| None` | `None` |
| `label` | `str` | required |
| `description` | `str` | `''` |
| `metadata_seen` | `dict[str, list[str]]` | `{}` |
| `embedding` | `list[float] \| None` | `None` |
| `embedding_model` | `str` | `''` |
| `status` | `Literal['draft', 'stable']` | `'draft'` |
| `created_at` | `datetime.datetime` | required |

#### `ContextChoice`

Một lựa chọn ngữ cảnh đã ghim; một lần dựng lại phát lại nó thay vì hỏi LLM.

| Trường | Kiểu | Mặc định |
| - | - | - |
| `passage_fingerprint` | `str` | required |
| `metadata_version` | `str` | required |
| `word` | `str` | required |
| `context` | `str` | required |
| `generated` | `Actorstamp` | required |

#### `Contribution`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `format` | `Literal['ladygraph/v1']` | `'ladygraph/v1'` |
| `scope` | `str` | required |
| `unit` | `str` | required |
| `version` | `DocVersion` | required |
| `replaces` | `list[str] \| None` | `None` |
| `access_labels` | `list[str]` | `[]` |
| `stale_after` | `datetime.datetime \| None` | `None` |
| `concepts` | `list[Concept]` | `[]` |
| `contexts` | `list[Context]` | `[]` |
| `choices` | `list[ContextChoice]` | `[]` |

#### `Declared`

Một mục mà ý nghĩa của nó do một người khai báo trong cấu hình, nên không cần mô hình để đọc nó.

| Trường | Kiểu | Mặc định |
| - | - | - |
| `word` | `str` | required |
| `kind` | `str` | `''` |
| `type` | `str` | `''` |
| `keywords` | `list[str]` | `[]` |
| `facts` | `list[str]` | `[]` |
| `source` | `str` | `'config'` |

#### `Document`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `address` | `str` | required |
| `scope` | `str` | required |
| `unit` | `str` | required |
| `shape` | `Literal['assemble', 'decompose', 'map']` | required |
| `version` | `DocVersion` | required |
| `title` | `str` | required |
| `passages` | `list[Passage]` | required |
| `metadata` | `dict[str, Any]` | `{}` |
| `access_labels` | `list[str]` | `[]` |
| `valid_from` | `datetime.datetime \| None` | `None` |
| `valid_until` | `datetime.datetime \| None` | `None` |
| `declared` | `Declared \| None` | `None` |
| `rewrite` | `bool` | `False` |
| `reading` | `str` | `''` |
| `source` | `str` | `''` |

#### `DocVersion`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `file` | `str` | required |
| `metadata` | `str` | required |
| `passages` | `str` | required |

| Phương thức | Làm gì |
| - | - |
| `combined(self) -> str` | — |
| `of_passages(fingerprints: list[str]) -> str` | — |

#### `Evidence`

Một mục `sources[]` của OKF: một vị trí và một Fingerprint, không bao giờ là văn bản nguồn.

| Trường | Kiểu | Mặc định |
| - | - | - |
| `id` | `str` | required |
| `resource` | `str` | required |
| `passage` | `str` | required |
| `start` | `int` | required |
| `length` | `int` | required |
| `fingerprint` | `str` | required |
| `unit` | `str` | required |
| `access_labels` | `list[str]` | `[]` |
| `stale_after` | `datetime.datetime \| None` | `None` |

#### `Passage`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `address` | `str` | required |
| `text` | `str` | required |
| `fingerprint` | `str` | required |
| `section` | `str` | `''` |

#### `Relation`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `type` | `str` | required |
| `target` | `str` | required |
| `weight` | `float` | `1.0` |
| `evidence_ids` | `list[str]` | `[]` |
| `unit` | `str` | required |

#### `Resolution`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `status` | `Literal['confirmed', 'moved', 'stale', 'unconfirmed']` | required |
| `start` | `int \| None` | `None` |
| `content` | `str \| None` | `None` |

#### `Word`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `text` | `str` | required |
| `kind` | `Literal['document', 'user']` | required |
| `generated` | `Actorstamp` | required |

#### `concept_id`

```python theme={null}
concept_id(scope: str, context_id: str, founding_word: str) -> str
```

Không mang ý nghĩa và vĩnh viễn.

#### `concept_path`

```python theme={null}
concept_path(scope: str, context_path: list[str], title: str) -> str
```

#### `context_id`

```python theme={null}
context_id(scope: str, parent: str | None, founding_label: str) -> str
```

#### `is_human`

```python theme={null}
is_human(actor: str) -> bool
```

## `core.normalize`

Hai kiểu chuẩn hóa văn bản. Chúng không bao giờ được đổi chỗ cho nhau.

#### `faithful`

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

#### `fingerprint`

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

Dấu vân tay của văn bản đã được chuẩn hóa trung thực.

#### `for_matching`

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

#### `locate`

```python theme={null}
locate(normalized_passage: str, span_fingerprint: str, length: int, hint_start: int | None = None) -> int | None
```

Khoảng văn bản có dấu vân tay này bắt đầu ở đâu trong đoạn văn, hoặc None.

#### `passage_fingerprint`

```python theme={null}
passage_fingerprint(raw_passage: str) -> str
```

#### `slug`

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

## `core.ontology`

Ontology: các kiểu khái niệm và kiểu quan hệ mà một hệ thống cho phép.

#### `Ontology`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `types` | `dict[str, TypeSpec]` | `{}` |
| `relations` | `dict[str, RelationSpec]` | `{}` |

| Phương thức | Làm gì |
| - | - |
| `load(cls, path: pathlib.Path) -> Ontology` | — |
| `from_yaml(cls, text: str) -> Ontology` | — |
| `fingerprint(self) -> str` | Được lưu trong bản kê. |
| `is_known_type(self, type_: str) -> bool` | — |
| `check_relation(self, rel_type: str, src_type: str, dst_type: str \| None, actor: str) -> tuple[RelationCheck, str]` | `dst_type` là None khi đích chưa tồn tại; ràng buộc của nó được kiểm tra lại khi đích xuất hiện. |
| `check_cardinality(self, rel_type: str, targets_of_src: int, sources_of_dst: int) -> tuple[RelationCheck, str]` | Các số đếm bao gồm cả quan hệ đang được kiểm tra. |
| `traversable(self, requested: list[str] \| None) -> list[str]` | Các kiểu đã khai báo với traverse=True; các kiểu chưa khai báo chỉ khi được nêu tên. |
| `extractor_brief(self) -> str` | Các kiểu và quan hệ mà bộ trích xuất LLM có thể phát ra. |

#### `RelationCheck`

Khởi tạo bằng `RelationCheck(*values)`.

#### `RelationSpec`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `description` | `str` | `''` |
| `from_` | `list[str]` | factory |
| `to` | `list[str]` | factory |
| `requires_actor` | `Optional[Literal['human', 'process']]` | `None` |
| `origin` | `str` | `''` |
| `traverse` | `bool` | `True` |
| `weight` | `float` | `1.0` |
| `max_targets` | `int \| None` | `None` |
| `max_sources` | `int \| None` | `None` |
| `limit_hint` | `str` | `''` |

| Phương thức | Làm gì |
| - | - |
| `(property)` | nguồn:đích, chẳng hạn N:1 khi mỗi nguồn có nhiều nhất một đích. |

#### `TypeSpec`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `description` | `str` | `''` |

## `core.pipeline`

Các giao diện của pipeline và giai đoạn cập nhật (INTERFACE §6).

#### `Embedder`

Từ văn bản thành vector, đồng bộ và bất đồng bộ như `llm.LlmClient`.

Khởi tạo bằng `Embedder(*args, **kwargs)`.

| Phương thức | Làm gì |
| - | - |
| `embed(self, texts: Sequence[str], *, kind: Literal['document', 'query'] = 'document') -> list[list[float]]` | — |
| `aembed(self, texts: Sequence[str], *, kind: Literal['document', 'query'] = 'document') -> list[list[float]]` | — |

#### `Extractor`

Khởi tạo bằng `Extractor(*args, **kwargs)`.

| 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` | Tạo các Concept cho các đoạn trong `changed` (các Fingerprint); để yên mọi đoạn khác. |

#### `GraphStore`

Khởi tạo bằng `GraphStore(*args, **kwargs)`.

| Phương thức | Làm gì |
| - | - |
| `apply(self, contribution: Contribution) -> None` | — |
| `drop_unit(self, scope: str, unit: str) -> None` | — |
| `versions(self, scope: str) -> dict[str, DocVersion]` | — |
| `pinned(self, scope: str, unit: str) -> list[ContextChoice]` | — |
| `known_types(self, scope: str) -> dict[str, str]` | — |
| `registry(self, scope: str) -> ContextRegistry` | — |
| `search(self, q: Query) -> list[Hit]` | — |
| `export(self, scope: str, path: pathlib.Path) -> None` | — |

#### `Resolver`

Khởi tạo bằng `Resolver(*args, **kwargs)`.

| Phương thức | Làm gì |
| - | - |
| `resolve_many(self, evidence: list[Evidence], timeout_s: float, with_content: bool = False) -> dict[str, Resolution]` | Được đánh khóa theo mã bằng chứng. |

#### `Source`

Khởi tạo bằng `Source(*args, **kwargs)`.

| Phương thức | Làm gì |
| - | - |
| `manifest(self, scope: str, now: datetime.datetime) -> dict[str, DocVersion]` | Chỉ các đơn vị thay đổi có hiệu lực, kèm phiên bản của chúng. |
| `read(self, scope: str, unit: str) -> Document` | — |
| `next_boundary(self, scope: str, now: datetime.datetime) -> datetime.datetime \| None` | valid\_from hoặc valid\_until sắp tới sớm nhất. |

#### `UpdateResult`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `applied` | `dict[str, Literal['add', 'file', 'passages', 'metadata', 'unchanged', 'drop']]` | factory |
| `dropped` | `list[str]` | factory |
| `rejected` | `dict[str, Report]` | factory |
| `next_run` | `datetime.datetime \| None` | `None` |

#### `changed_passages`

```python theme={null}
changed_passages(doc: Document, action: Literal['add', 'file', 'passages', 'metadata', 'unchanged', 'drop'], pinned: list[ContextChoice]) -> tuple[set[str], set[str] | None]
```

(các đoạn mà bộ trích xuất chạy trên đó, các đoạn mà bằng chứng của chúng bị thay thế).

#### `classify`

```python theme={null}
classify(old: DocVersion | None, new: DocVersion | None) -> Literal['add', 'file', 'passages', 'metadata', 'unchanged', 'drop']
```

#### `contribution_scope`

```python theme={null}
contribution_scope(doc: Document, replaces: set[str] | None) -> dict
```

Các trường mà mọi Contribution từ `doc` phải mang.

#### `plan_update`

```python theme={null}
plan_update(source_versions: dict[str, DocVersion], store_versions: dict[str, DocVersion]) -> dict[str, typing.Literal['add', 'file', 'passages', 'metadata', 'unchanged', 'drop']]
```

#### `replaces_for`

```python theme={null}
replaces_for(doc: Document, changed: set[str], pinned: list[ContextChoice], context_only: bool) -> set[str] | None
```

Tập `replaces` mà `run_update` mong đợi, tính từ những gì bộ trích xuất được đưa.

#### `run_update`

```python theme={null}
run_update(scope: str, source: Source, extractor: Extractor, store: GraphStore, ontology: Ontology, now: datetime.datetime) -> UpdateResult
```

## `core.query`

Giao diện truy vấn (INTERFACE §8).

#### `Hit`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `address` | `str` | required |
| `score` | `float` | required |
| `path` | `list[str]` | required |
| `evidence_id` | `str` | required |
| `context_path` | `list[str]` | `[]` |
| `access_labels` | `list[str]` | `[]` |
| `resolution` | `Optional[Literal['confirmed', 'moved', 'unconfirmed']]` | `None` |
| `content` | `str \| None` | `None` |

#### `Query`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `scope` | `str` | required |
| `text` | `str \| None` | `None` |
| `vector` | `list[float] \| None` | `None` |
| `seeds` | `int` | `5` |
| `hops` | `int` | `1` |
| `fanout` | `int` | `10` |
| `relation_types` | `list[str] \| None` | `None` |
| `facets` | `dict[str, list[Any]]` | `{}` |
| `access_filter` | `list[str] \| None` | `None` |
| `resolve` | `bool` | `False` |
| `now` | `datetime.datetime \| None` | `None` |

## `core.redaction`

Thông tin liên hệ: các mẫu, các lớp che tùy chọn, và quy tắc rằng một thông tin liên hệ không bao giờ là một khái niệm.

#### `contact_sentences`

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

(đầu, cuối) của mỗi câu trong `text` chứa một thông tin liên hệ hoặc một lớp che, theo thứ tự, không lặp lại.

#### `only_contact_detail`

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

Đúng khi `text` là một thông tin liên hệ hoặc một lớp che, cùng nhiều nhất một từ lẻ xung quanh.

## `core.registry`

Sổ đăng ký Context và xếp hạng ứng viên (INTERFACE §3, "Context").

#### `Candidate`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `context` | `Context` | required |
| `score` | `float` | required |

#### `ContextRegistry`

Khởi tạo bằng `ContextRegistry(scope: str, contexts: list[Context])`.

| Phương thức | Làm gì |
| - | - |
| `copy(self) -> ContextRegistry` | Một sổ đăng ký độc lập với cùng các ngữ cảnh, để bên gọi thêm các bản nháp mà không có tác dụng phụ. |
| `get(self, context_id: str) -> Context \| None` | — |
| `add(self, context: Context) -> None` | — |
| `children(self, parent: str \| None) -> list[Context]` | — |
| `depth(self, context_id: str) -> int` | 1 cho một ngữ cảnh cấp trên cùng, 2 cho các con của nó, và cứ thế. |
| `path(self, context_id: str) -> list[str]` | Các nhãn từ cấp trên cùng đi xuống ngữ cảnh này. |
| `rank(self, parents: list[str \| None], vector: list[float] \| None, metadata: dict[str, list[str] \| str], section: str = '', k: int = 3) -> list[Candidate]` | Top-k con của `parents`: độ tương đồng vector, cộng điểm thưởng khi siêu dữ liệu của tài liệu khớp và điểm thưởng lớn hơn khi phần văn bản khớp. |

## `core.runner`

Khi nào chạy bản cập nhật (INTERFACE §6).

#### `Scheduler`

Khởi tạo bằng `Scheduler(source: Source, extractor: Extractor, store: GraphStore, ontology: Ontology, scopes: list[str], *, poll: datetime.timedelta = datetime.timedelta(seconds=900), clock: Callable[[], datetime.datetime] = <function _now at 0x102524400>)`.

| Phương thức | Làm gì |
| - | - |
| `trigger(self, scope: str) -> None` | Nguồn đã thay đổi: chạy phạm vi này ở lần tick kế tiếp. |
| `next_due(self) -> datetime.datetime` | — |
| `tick(self) -> dict[str, UpdateResult]` | Chạy mọi phạm vi đến hạn. |
| `run_forever(self, stop: threading.Event \| None = None) -> None` | — |

## `core.validate`

Kiểm tra một Contribution trước khi nó được áp dụng (INTERFACE §4).

#### `Report`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `errors` | `list[str]` | factory |
| `warnings` | `list[str]` | factory |

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

#### `validate_contribution`

```python theme={null}
validate_contribution(c: Contribution, ontology: Ontology, known_types: dict[str, str]) -> tuple[Contribution, Report]
```

Trả về một bản sao đã làm sạch của `c` và một báo cáo.

## `core.validity`

Thời hạn hiệu lực: một Tài liệu chỉ được lập chỉ mục khi nó đang có hiệu lực.

#### `Period`

| Trường | Kiểu | Mặc định |
| - | - | - |
| `start` | `datetime.datetime` | required |
| `end` | `datetime.datetime \| None` | required |

| Phương thức | Làm gì |
| - | - |
| `contains(self, now: datetime.datetime) -> bool` | — |

#### `is_valid`

```python theme={null}
is_valid(published: bool, active: bool, period: Period, now: datetime.datetime) -> bool
```

#### `local_midnight`

```python theme={null}
local_midnight(day: datetime.date, tz: zoneinfo.ZoneInfo = zoneinfo.ZoneInfo(key='Asia/Ho_Chi_Minh')) -> datetime.datetime
```

#### `next_boundary`

```python theme={null}
next_boundary(periods: list[Period], now: datetime.datetime) -> datetime.datetime | None
```

Thời điểm sớm nhất sau `now` khi một Tài liệu nào đó đi vào hoặc rời đi.

#### `validity_period`

```python theme={null}
validity_period(published_at: datetime.datetime, effective: datetime.date | None, expiry: datetime.date | None, replacement_effective: list[datetime.date], tz: zoneinfo.ZoneInfo = zoneinfo.ZoneInfo(key='Asia/Ho_Chi_Minh')) -> Period
```

`replacement_effective` chứa các ngày có hiệu lực của những Tài liệu thay thế hoàn toàn tài liệu này VÀ bản thân chúng đang có hiệu lực; bên gọi tự xác định điều đó.

## `core.words`

Khớp một câu hỏi với các từ mà một người đã khai báo cho một khái niệm.

#### `canonical`

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

Nối một mã biểu mẫu với chữ cái biến thể của nó, trên văn bản mà `for_matching` đã chuẩn hóa.

#### `matches`

```python theme={null}
matches(prepared_question: str, clause: str) -> bool
```

#### `positive_terms`

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

Các thuật ngữ của một mệnh đề phải xuất hiện, đúng như đã viết (kèm dấu), để hiển thị cho một người.

#### `prepare_question`

```python theme={null}
prepare_question(question: str) -> str
```

Câu hỏi ở dạng mà `matches` cần, nên nó được chuẩn hóa một lần cho mỗi lần tìm kiếm.


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