Retrieval API¶
Retriever¶
Retriever ¶
Retriever(
vector_store: BaseVectorStore,
number_of_chunks: int,
method: Literal["simple"] = "simple",
search_mode: Literal["vector", "hybrid", "graph"] = "vector",
ranker_strategy: str | None = None,
ranker_k: int | None = None,
ranker_alpha: float | None = None,
search_kwargs: dict[str, Any] | None = None,
)
Class responsible for retrieving data from given vector store.
Parameters:
-
vector_store(BaseVectorStore) –Vector store / vector index to retrieve data from.
-
method(Literal['simple'], default:'simple') –Method describing how data should be retrieved.
-
number_of_chunks(int) –Number of chunks to retrieve.
-
search_mode(Literal['vector', 'hybrid', 'graph'], default:"vector") –Search mode passed to the vector store: "vector", "hybrid", or "graph".
-
ranker_strategy(str | None, default:None) –Ranking strategy for hybrid search: "rrf", "weighted", or "normalized".
-
ranker_k(int | None, default:None) –Parameter k for the ranking function.
-
ranker_alpha(float | None, default:None) –Alpha parameter for weighted ranking strategy.
Source code in ai4rag/rag/retrieval/retriever.py
Methods:¶
retrieve ¶
Retrieve relevant chunks from vector store.
Parameters:
-
query(str) –Question for which chunks should be retrieved.
Returns:
-
list[AI4RAGChunk]–Chunks with their metadata corresponding to the query.
Source code in ai4rag/rag/retrieval/retriever.py
Document Compressor¶
LLMDocumentCompressor uses a LangChain chat model to extract query-relevant, verbatim excerpts from AI4RAGChunk objects while preserving their metadata. AgenticRAG uses it for its context-overflow fallback. Both Retriever and LLMDocumentCompressor are exported from ai4rag.rag.retrieval.
LLMDocumentCompressor ¶
Extract query-relevant excerpts from chunks with a LangChain chat model.
Parameters:
-
chat_model(BaseChatModel) –Model used to identify relevant passages in each chunk.
Source code in ai4rag/rag/retrieval/document_compressor.py
Methods:¶
compress ¶
compress(query: str, chunks: list[AI4RAGChunk], *, config: RunnableConfig | None = None) -> list[AI4RAGChunk]
Return relevant excerpts while preserving each source chunk's metadata.
Parameters:
-
query(str) –Query used to determine which passages are relevant.
-
chunks(list[AI4RAGChunk]) –Retrieved chunks to compress.
-
config(RunnableConfig | None, default:None) –Invocation configuration, including callbacks for compression model calls.
Returns:
-
list[AI4RAGChunk]–Extracted excerpts, omitting chunks with no relevant evidence.
Source code in ai4rag/rag/retrieval/document_compressor.py
compression_stats staticmethod ¶
Return JSON-safe estimated-token and chunk counts for one compression pass.