Skip to content

latent.rag.raptor.adapter

RAPTOR adapter with LiteLLM model support and MLflow tracking.

Wraps raptor-rag's RetrievalAugmentation with scalar constructor params (mappable from parameters.yaml) and automatic MLflow metric logging.

Classes

RaptorAdapter

RaptorAdapter(embedding_model: str = 'text-embedding-ada-002', summarization_model: str = 'gpt-4o-mini', qa_model: str = 'gpt-4o-mini', tb_max_tokens: int = 100, tb_num_layers: int = 5, tb_threshold: float = 0.5, tb_summarization_length: int = 100, tr_threshold: float = 0.5, tr_top_k: int = 5, tr_selection_mode: Literal['top_k', 'threshold'] = 'top_k', tr_num_layers: int | None = None, tr_start_layer: int | None = None, collapse_tree: bool = True)

RAPTOR adapter with config-from-scalars and MLflow tracking.

All constructor args are flat scalars suitable for YAML config. The underlying raptor-rag objects are created lazily on first use.

Args: embedding_model: LiteLLM model name for embeddings. summarization_model: LiteLLM model name for tree summarization. qa_model: LiteLLM model name for question answering. tb_max_tokens: Max tokens per tree builder chunk. tb_num_layers: Number of tree layers to build. tb_threshold: Similarity threshold for tree building. tb_summarization_length: Max tokens for node summaries. tr_threshold: Similarity threshold for retrieval. tr_top_k: Number of top results to retrieve. tr_selection_mode: Retrieval selection mode. tr_num_layers: Number of layers to traverse during retrieval. tr_start_layer: Starting layer for retrieval. collapse_tree: Default retrieval mode (collapsed vs layer-by-layer).

Methods

RaptorAdapter.answer

answer(question: str, kwargs: Any = {}) -> str

Retrieve context and generate an answer.

Args: question: The query string. **kwargs: Override answer params (top_k, collapse_tree, etc.).

Returns: Generated answer string.

RaptorAdapter.build_tree

build_tree(documents: str) -> Tree

Build a RAPTOR tree from documents and log MLflow metrics.

Args: documents: Text corpus to index.

Returns: The built raptor Tree object.

RaptorAdapter.from_params

from_params(params_dict: dict[str, Any]) -> Self

Create a RaptorAdapter from a params dictionary.

Intended for use with latent's params context object::

adapter = RaptorAdapter.from_params(params.raptor)

Args: params_dict: Dictionary of constructor kwargs (e.g. from YAML).

Returns: Configured RaptorAdapter instance.

RaptorAdapter.load_tree

load_tree(path: str | Path) -> None

Load a RAPTOR tree from a file.

Args: path: File path to a previously saved tree.

RaptorAdapter.retrieve

retrieve(question: str, kwargs: Any = {}) -> str

Retrieve relevant context for a question.

Args: question: The query string. **kwargs: Override retrieve params (top_k, collapse_tree, etc.).

Returns: Retrieved context string.

RaptorAdapter.save_tree

save_tree(path: str | Path) -> None

Save the current RAPTOR tree to a file.

Args: path: File path to save the pickled tree.

Raises: RuntimeError: If no tree has been built or loaded.