SDK#
nlql.sdk —— Engine(应用主入口)与流式 QueryBuilder。NLQL 语句、链式构造、LLM IR 三种查询入口均通过 Engine 执行,编译到同一份 IR。
Engine 通过构造参数接收 embedder、store、reranker 等组件;OpenAI 兼容渠道直接使用 OpenAIEmbedder(base_url=...)。
sdk
#
High-level SDK: the Engine and the Query Builder.
__all__
module-attribute
#
__all__ = ['Engine', 'QueryBuilder', 'select', 'E', 'F', 'Meta', 'content', 'field', 'similarity', 'contains', 'length']
E
#
E(expr: Expr)
A fluent wrapper over an IR expression, overloading Python operators.
QueryBuilder
#
Accumulates clauses and compiles to a :class:~nlql.ir.nodes.Query.
源代码位于: src/nlql/sdk/builder.py
let
#
let(name: str, expr: Any) -> QueryBuilder
where
#
where(*conditions: Any) -> QueryBuilder
order_by
#
order_by(key: Any, *, desc: bool = False) -> QueryBuilder
limit
#
limit(n: int) -> QueryBuilder
Engine
#
Engine(embedder: Embedder, *, store: Store | None = None, registry: Registry | None = None, granularity: str = 'chunk', normalizer: Normalizer | None = None, cache: EmbeddingCache | None = None, field_types: dict[str, TypeTag] | None = None, type_handlers: dict | None = None, reranker: Reranker | None = None, rerank_factor: int = 5, named_embedders: dict[str, Embedder] | None = None)
A retrieval engine: ingest documents, run NLQL queries, extend capabilities.
The embedder is injected — it is the extensibility point, so there are no
provider-specific constructors to multiply. Any OpenAI-compatible channel is one
:class:~nlql.embed.OpenAIEmbedder parameterized by base_url; any other provider
is just another :class:~nlql.embed.base.Embedder implementation::
from nlql import Engine
from nlql.embed import FakeEmbedder, OpenAIEmbedder
Engine(OpenAIEmbedder(base_url="https://your-gateway/v1", api_key="sk-..."))
Engine(FakeEmbedder()) # deterministic, offline (tests/demos)
Engine(MyCohereEmbedder(...)) # your own backend, no core change
源代码位于: src/nlql/sdk/engine.py
add_text
#
add_text(text: str, *, id: str | None = None, metadata: dict[str, Any] | None = None, source: str | None = None) -> str
Ingest a single text; returns the (generated or provided) document id.
源代码位于: src/nlql/sdk/engine.py
add_image
#
add_image(image: bytes | str, *, id: str | None = None, metadata: dict[str, Any] | None = None, source: str | None = None, mime: str | None = None) -> str
Ingest a single image as a multimodal document; returns its id.
image may be raw bytes, a local file path, or an http(s) / data: URL.
Needs a multimodal embedder (e.g. DoubaoVisionEmbedder or ClipEmbedder) — the
image is embedded into the same space as text, so text queries retrieve it. Use
granularity="chunk" for image collections.
源代码位于: src/nlql/sdk/engine.py
add_file
#
add_file(path: str, *, metadata: dict[str, Any] | None = None, loader: Loader | None = None) -> list[str]
Load a file (.txt / .md / .docx / .pdf / …) and ingest it.
Dispatches by extension (override with loader=). Returns the document ids. Needs
the relevant extra for docx/pdf: pip install python-nlql[loaders].
源代码位于: src/nlql/sdk/engine.py
add_files
#
Load and ingest multiple files; returns all document ids.
源代码位于: src/nlql/sdk/engine.py
add_multivector
#
add_multivector(id: str, *, content: str, named: dict[str, str | bytes], metadata: dict[str, Any] | None = None, kind: str | None = None) -> str
Ingest one record with several named vectors, each queryable via vec.<name>.
content is the record's text (and its default vector, via the primary embedder).
named maps a vector name to its source — a str (embedded as text) or image
bytes (embedded via that embedder's embed_images). Configure per-name embedders
with Engine(..., named_embedders={"image": ClipEmbedder(), ...}). Query a specific
vector with SIMILARITY(vec.image, "…").
源代码位于: src/nlql/sdk/engine.py
add_documents
#
add_documents(documents: Iterable[Document], *, batch: int = 256) -> None
Ingest documents in batches (normalize → split → embed(cache) → index).
源代码位于: src/nlql/sdk/engine.py
search
#
search(query: str | Query, *, limit: int | None = None, reranker: Reranker | None = None, rerank_query: str | None = None) -> list[Unit]
Run an NLQL string or Query IR and return matching units.
A reranker (here or on the engine) refines the recalled candidates: recall
over-fetches, then the reranker re-scores each (query, passage) pair and the top
limit are returned. rerank_query overrides the text used (default: the primary
SIMILARITY query).
源代码位于: src/nlql/sdk/engine.py
explain
#
explain(query: str | Query) -> dict[str, Any]
Return the query plan (parsed IR, scores, filter, recall strategy).
源代码位于: src/nlql/sdk/engine.py
register_function
#
register_function(name: str, *, signature: Signature | None = None, provides_score: bool = False, pushdownable: bool = False, overwrite: bool = False) -> Callable[[Callable[..., Any]], Callable[..., Any]]
Register a function for this engine only (instance-scoped).
源代码位于: src/nlql/sdk/engine.py
register_splitter
#
register_splitter(name: str, *, overwrite: bool = False) -> Callable[[Callable[..., Any]], Callable[..., Any]]
Register/override a splitter for this engine only.
register_type
#
Register a custom type at the instance level (shadows global).
Supports both direct call and @register_type decorator mode.
源代码位于: src/nlql/sdk/engine.py
function_schema
#
function_tool
#
An OpenAI-style tool definition an LLM can call to emit a Query IR.
源代码位于: src/nlql/sdk/engine.py
contains
#
length
#
select
#
select(unit: str, window: int | None = None) -> QueryBuilder