Skip to content

Python API ​

python
from ragx import RagX, Answer, Citation, Event, Settings, VARIANTS, get_variant

RagX ​

python
RagX(corpus, model=None, variant=None, **settings)
ParameterTypeMeaning
corpusstr | Patha file or a folder; ingested on construction
modelstr | Noneprovider:model; defaults from the environment (Models)
variantstr | Nonea variant; default ragx-lexical
**settingsany field of Settings

Construction ingests the corpus into its text mirror. The passage store and the ranker (including any embedding) are built on first use.

Methods ​

ask(question) -> Answer ​

Answers a question and returns an Answer. An empty question returns an empty, ungrounded answer without calling a model.

ask_stream(question) -> Iterator[Event] ​

Yields Events as the run proceeds; the generator's return value (StopIteration.value) is the Answer. See Streaming the trace.

search(query, top=8) -> list[str] ​

Retrieval only, no LLM call: the names (path:start-end) of the ranker's top passages. With the classic variant, the top files.

reingest() -> Corpus ​

Re-scans the corpus after files change (incrementally) and rebuilds the passages and ranker on next use.

stats() -> dict ​

Corpus statistics (files, lines, vocabulary), the model, the variant and the retriever.

Properties ​

PropertyMeaning
settingsthe resolved Settings
variantthe resolved Variant (name, retriever, route, backfill, think, summary)
retrieverthe ranker in use: bm25, dense, fused or rerank
thinkwhether thinking mode is on
storethe PassageStore (built on first access)
rankerthe ranker (built on first access)
corpusthe ingested Corpus

Answer ​

FieldTypeMeaning
textstrthe answer
citationslist[Citation]passages that contain the answer (grounding)
sourceslist[str]the citations as path:start-end strings
groundedboolwhether a cited passage contains the answer
confidencefloat | Noneset by the classic agent's both route
tracelist[Event]every event of the run
usagedictper-model calls, tokens, latency, cost; llm_calls; variant; total_cost_usd

str(answer) is the text; answer.to_dict(include_trace=False) is JSON-ready.

Citation ​

FieldMeaning
pathsource path, relative to the corpus
line_start, line_endthe line range
quotethe first non-empty line of the passage

str(citation) is path:start-end.

Event ​

FieldMeaning
kindplan, rank, read, search, thought, hop, backfill, verify, answer, ...
textwhat happened
stepthe forager turn, where there is one
detailextra data, kind-specific

Variants ​

python
from ragx import VARIANTS, get_variant

get_variant("ragx-lex")       # Variant(name='ragx-lexical', retriever='bm25', route='deep', backfill=0, ...)
list(VARIANTS)                # ['ragx', 'ragx-core', 'ragx-lexical', ...]

Lower-level building blocks ​

For custom pipelines, the pieces are importable on their own:

python
from ragx.passages import build_passages            # cut a Corpus into passages
from ragx.rankers import BM25Ranker, DenseRanker, FusedRanker, RerankRanker
from ragx.method import Method, MethodConfig         # the forage loop, slot chain and compose
from ragx.llm import LLM                             # the provider client

Method(store, ranker, MethodConfig(...)).run(question, llm) is a generator of events whose return value is a RunResult with the answer, the composed passage ids, the reads and the resolved chain.

Released under the Apache 2.0 License.