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

# llm

> One interface for language models, plus the translator.

LLM layer: one interface (`LlmClient`), backed by ADK models (Gemini or a LiteLLM endpoint) or a LiteRT-LM model on this machine.

## `llm.adk`

`LlmClient` on top of ADK models: `google.adk.models.Gemini` or `LiteLlm` (a LiteLLM endpoint).

#### `AdkLlmClient`

Construct with `AdkLlmClient(model: google.adk.models.base_llm.BaseLlm, config: LlmConfig, sleep=<function sleep at 0x102764b80>)`.

| Method | What it does |
| - | - |
| `acomplete(self, system: str, prompt: str, schema: type[~T]) -> ~T` | — |
| `complete(self, system: str, prompt: str, schema: type[~T]) -> ~T` | Sync wrapper. It keeps one event loop for the client's lifetime: `asyncio.run` per call would close the loop each time, and LiteLLM's background logging task is bound to the loop it started on. |
| `close(self) -> None` | — |

#### `build_model`

```python theme={null}
build_model(config: LlmConfig) -> google.adk.models.base_llm.BaseLlm
```

The ADK model object for `config`.

#### `create_client`

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

The client for `config`: a LiteRT-LM model on this machine for backend="litert", else an ADK model (Gemini or a LiteLLM endpoint).

#### `relaxed_json_schema`

```python theme={null}
relaxed_json_schema(schema: type[~T]) -> dict | None
```

The schema as JSON without `maxItems`/`minItems` on lists of objects, or None when there is nothing to remove.

## `llm.auth`

Build a `google.genai.Client` from a config that has `GoogleAuthFields`. Used by the LLM client and the embedder.

#### `GoogleAuthError`

The sign-in cannot be built: a missing file or variable, or a key that is not a service account key.

#### `build_genai_client`

```python theme={null}
build_genai_client(config: GoogleAuthFields)
```

The `google.genai.Client` for `config`, or None to let the library choose from the environment.

#### `is_permanent_error`

```python theme={null}
is_permanent_error(exc: Exception) -> bool
```

A request error that a retry cannot fix: bad key, bad request, unknown model.

#### `service_account_credentials`

```python theme={null}
service_account_credentials(config: GoogleAuthFields)
```

Credentials for the service account named in `config`, from a file or from an environment variable.

## `llm.base`

The interface the extractor depends on, and the types around it.

#### `GoogleAuthFields`

How a Google client signs in.

| Field | Type | Default |
| - | - | - |
| `api_key_env` | `str \| None` | `None` |
| `auth` | `Literal['environment', 'api_key', 'service_account']` | `'environment'` |
| `service_account_file` | `str \| None` | `None` |
| `service_account_json_env` | `str \| None` | `None` |
| `project` | `str \| None` | `None` |
| `location` | `str \| None` | `None` |

#### `LlmClient`

Structured output: send a prompt, get back an instance of `schema`.

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

| Method | What it does |
| - | - |
| `acomplete(self, system: str, prompt: str, schema: type[~T]) -> ~T` | — |
| `complete(self, system: str, prompt: str, schema: type[~T]) -> ~T` | — |

#### `LlmConfig`

| Field | Type | Default |
| - | - | - |
| `api_key_env` | `str \| None` | `None` |
| `auth` | `Literal['environment', 'api_key', 'service_account']` | `'environment'` |
| `service_account_file` | `str \| None` | `None` |
| `service_account_json_env` | `str \| None` | `None` |
| `project` | `str \| None` | `None` |
| `location` | `str \| None` | `None` |
| `backend` | `Literal['litellm', 'gemini', 'litert']` | `'litellm'` |
| `model` | `str` | required |
| `device` | `Literal['cpu', 'gpu', 'npu']` | `'cpu'` |
| `api_base` | `str \| None` | `None` |
| `temperature` | `float` | `0.0` |
| `max_output_tokens` | `int` | `8192` |
| `timeout_s` | `float` | `120.0` |
| `attempts` | `int` | `3` |
| `backoff_s` | `float` | `1.0` |
| `repair_attempts` | `int` | `1` |
| `thinking` | `Literal['default', 'off', 'on']` | `'default'` |
| `thinking_budget` | `int \| None` | `None` |
| `extra_body` | `dict[str, Any] \| None` | `None` |

#### `LlmError`

The call failed (network, quota, timeout, empty answer) after every retry.

#### `LlmOutputError`

The model answered, but not in the requested schema, even after repair attempts.

#### `LlmUsage`

| Field | Type | Default |
| - | - | - |
| `calls` | `int` | `0` |
| `retries` | `int` | `0` |
| `repairs` | `int` | `0` |
| `prompt_tokens` | `int` | `0` |
| `output_tokens` | `int` | `0` |
| `thought_tokens` | `int` | `0` |

#### `check_google_auth`

```python theme={null}
check_google_auth(c: GoogleAuthFields) -> None
```

Refuse a half-written sign-in at load time, not at the first request minutes later.

## `llm.fake`

A scripted client for tests: no network, no ADK.

#### `FakeLlmClient`

Returns canned replies in order and records every request.

Construct with `FakeLlmClient(replies: list[pydantic.main.BaseModel \| Exception \| Callable[[str, str, type], pydantic.main.BaseModel]] \| None = None, model: str = 'fake/model')`.

| Method | What it does |
| - | - |
| `acomplete(self, system: str, prompt: str, schema: type[~T]) -> ~T` | — |
| `complete(self, system: str, prompt: str, schema: type[~T]) -> ~T` | — |
| `complete_text(self, system: str, prompt: str) -> str` | The plain-text request: a reply is a string, an exception to raise, or a function of (system, prompt, None) that returns a string. |

## `llm.litert`

An LlmClient that runs a LiteRT-LM model on this machine (Gemma 4 E2B as a `.litertlm` file), for work that should not leave it: quick translation, short extraction. It is the local counterpart of `AdkLlmClient`, and takes the same `complete` / `acomplete` calls.

#### `LiteRtLlmClient`

Construct with `LiteRtLlmClient(model_path: str \| pathlib.Path, *, device: str = 'cpu', max_output_tokens: int = 2048, repair_attempts: int = 1, constrained: bool = True, thinking: Literal['default', 'off', 'on'] = 'default', speculative_decoding: bool = False, cache_dir: str \| pathlib.Path \| None = None, engine: _Engine \| None = None)`.

| Method | What it does |
| - | - |
| `from_config(cls, config: LlmConfig, **kwargs) -> 'LiteRtLlmClient'` | — |
| `complete(self, system: str, prompt: str, schema: type[~T]) -> ~T` | — |
| `acomplete(self, system: str, prompt: str, schema: type[~T]) -> ~T` | — |
| `complete_text(self, system: str, prompt: str) -> str` | Plain text, no schema. |
| `acomplete_text(self, system: str, prompt: str) -> str` | — |
| `close(self) -> None` | — |

## `llm.structured`

Turning a model's text into a pydantic object, for a backend that cannot constrain the output itself.

#### `json_instruction`

```python theme={null}
json_instruction(schema: type[pydantic.main.BaseModel]) -> str
```

What to add to the system prompt when the output is not constrained.

#### `strip_fence`

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

The JSON inside a \`\`\`json fence, or the text itself when it has no fence.

## `llm.translate`

Vietnamese to English translation behind the `LlmClient` Protocol.

#### `Glossary`

| Field | Type | Default |
| - | - | - |
| `terms` | `tuple[Term, ...]` | required |
| `examples` | `tuple[tuple[str, str], ...]` | required |

| Method | What it does |
| - | - |
| `(property)` | The terms the gate checks: the approved ones. |
| `load(cls, path: str \| pathlib.Path = PosixPath('/Users/davestran/Downloads/project_self_driven/Graphindex/ladyGraph/ladygraph/llm/glossary.yaml')) -> 'Glossary'` | — |

#### `Term`

| Field | Type | Default |
| - | - | - |
| `vi` | `tuple[str, ...]` | required |
| `en` | `str` | required |
| `expect` | `tuple[str, ...]` | required |
| `ok_if` | `tuple[str, ...]` | required |
| `contamination` | `bool` | `True` |
| `status` | `str` | `'approved'` |
| `kind` | `str` | `''` |
| `confidence` | `str` | `''` |
| `source` | `str` | `''` |

#### `Translated`

| Field | Type | Default |
| - | - | - |
| `text` | `str` | required |
| `ok` | `bool` | required |
| `flags` | `list[str]` | factory |
| `attempts` | `int` | `0` |

#### `Translator`

Translate with any `LlmClient`; gate every answer; retry with the reasons.

Construct with `Translator(llm: LlmClient, glossary: Glossary \| None = None, *, retries: int = 1)`.

| Method | What it does |
| - | - |
| `atranslate(self, text: str) -> Translated` | — |
| `translate(self, text: str) -> Translated` | — |

#### `gate`

```python theme={null}
gate(source: str, english: str, glossary: Glossary) -> list[str]
```

Why `english` is not an acceptable translation of `source`.


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