> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/avnlp/vectordb/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain components

> Reusable component APIs for LangChain RAG pipelines

Reusable components for building custom LangChain RAG applications.

## AgenticRouter

Route queries to search, reflect, or generate actions using LLM reasoning for agentic RAG patterns.

### Constructor

```python theme={null}
AgenticRouter(llm: ChatGroq)
```

<ParamField path="llm" type="ChatGroq" required>
  ChatGroq LLM instance for routing decisions. Should be configured with low temperature (0.0-0.3) for consistent routing
</ParamField>

### Methods

#### route

Route a query to the appropriate action based on current pipeline state.

```python theme={null}
route(
    query: str,
    has_documents: bool = False,
    current_answer: str | None = None,
    iteration: int = 1,
    max_iterations: int = 3
) -> dict[str, Any]
```

<ParamField path="query" type="str" required>
  The user's original query text
</ParamField>

<ParamField path="has_documents" type="bool" default="False">
  Indicates whether documents have already been retrieved in previous iterations
</ParamField>

<ParamField path="current_answer" type="str" optional>
  The answer generated so far, if any. Used to assess whether reflection or generation is appropriate
</ParamField>

<ParamField path="iteration" type="int" default="1">
  Current iteration number (1-indexed). Used to track progress and enforce iteration limits
</ParamField>

<ParamField path="max_iterations" type="int" default="3">
  Maximum number of routing iterations allowed. Prevents infinite loops
</ParamField>

<ResponseField name="action" type="str">
  One of 'search', 'reflect', or 'generate'
</ResponseField>

<ResponseField name="reasoning" type="str">
  Human-readable explanation of the routing decision
</ResponseField>

***

## ContextCompressor

Compress retrieved context using reranking or LLM-based extraction to reduce token usage.

### Constructor

```python theme={null}
ContextCompressor(
    mode: str = "reranking",
    llm: ChatGroq | None = None,
    reranker: HuggingFaceCrossEncoder | None = None
)
```

<ParamField path="mode" type="str" default="reranking">
  Compression mode: "reranking" or "llm\_extraction"
</ParamField>

<ParamField path="llm" type="ChatGroq" optional>
  ChatGroq instance for LLM extraction mode. Required when mode is "llm\_extraction"
</ParamField>

<ParamField path="reranker" type="HuggingFaceCrossEncoder" optional>
  HuggingFaceCrossEncoder instance for reranking mode. Required when mode is "reranking"
</ParamField>

### Methods

#### compress

Compress documents using the configured compression strategy.

```python theme={null}
compress(
    query: str,
    documents: list[Document],
    top_k: int = 5
) -> list[Document]
```

<ParamField path="query" type="str" required>
  The user's query text. Used to determine relevance
</ParamField>

<ParamField path="documents" type="list[Document]" required>
  List of LangChain Document objects to compress
</ParamField>

<ParamField path="top_k" type="int" default="5">
  Number of documents to return (only used in reranking mode)
</ParamField>

<ResponseField name="compressed" type="list[Document]">
  Compressed list of documents. Structure depends on mode:

  * reranking: List of top\_k Document objects, sorted by relevance
  * llm\_extraction: List containing single synthesized Document
</ResponseField>

#### compress\_reranking

Compress documents using cross-encoder reranking.

```python theme={null}
compress_reranking(
    query: str,
    documents: list[Document],
    top_k: int = 5
) -> list[Document]
```

<ParamField path="query" type="str" required>
  Query text for relevance scoring
</ParamField>

<ParamField path="documents" type="list[Document]" required>
  Documents to rerank
</ParamField>

<ParamField path="top_k" type="int" default="5">
  Number of top documents to return
</ParamField>

<ResponseField name="reranked" type="list[Document]">
  Top-k documents sorted by relevance score (highest first)
</ResponseField>

#### compress\_llm\_extraction

Compress documents using LLM-based passage extraction.

```python theme={null}
compress_llm_extraction(
    query: str,
    documents: list[Document]
) -> list[Document]
```

<ParamField path="query" type="str" required>
  Query text to guide extraction
</ParamField>

<ParamField path="documents" type="list[Document]" required>
  Documents to extract from
</ParamField>

<ResponseField name="extracted" type="list[Document]">
  List containing a single Document with extracted passages. Metadata includes 'source': 'compressed' and 'original\_doc\_count'
</ResponseField>

***

## QueryEnhancer

Enhance queries using multi-query generation, HyDE (Hypothetical Document Embeddings), and step-back techniques.

### Constructor

```python theme={null}
QueryEnhancer(llm: ChatGroq)
```

<ParamField path="llm" type="ChatGroq" required>
  ChatGroq LLM instance for query enhancement
</ParamField>

### Methods

#### generate\_multi\_queries

Generate multiple query variations for better retrieval coverage.

```python theme={null}
generate_multi_queries(
    query: str,
    num_queries: int = 3
) -> list[str]
```

<ParamField path="query" type="str" required>
  Original query
</ParamField>

<ParamField path="num_queries" type="int" default="3">
  Number of query variations to generate
</ParamField>

<ResponseField name="queries" type="list[str]">
  List of query variations including the original query
</ResponseField>

#### generate\_hyde\_document

Generate a hypothetical document that would answer the query.

```python theme={null}
generate_hyde_document(query: str) -> str
```

<ParamField path="query" type="str" required>
  Query to generate hypothetical document for
</ParamField>

<ResponseField name="document" type="str">
  Hypothetical document text that can be embedded and used for retrieval
</ResponseField>

#### generate\_step\_back\_query

Generate a step-back query that asks a more general question.

```python theme={null}
generate_step_back_query(query: str) -> str
```

<ParamField path="query" type="str" required>
  Specific query to generalize
</ParamField>

<ResponseField name="step_back" type="str">
  More general query useful for retrieving background context
</ResponseField>

***

## MMRHelper

Maximal Marginal Relevance utilities for diversity-optimized retrieval.

### Methods

#### mmr\_rerank

Rerank documents using MMR algorithm to balance relevance and diversity.

```python theme={null}
MMRHelper.mmr_rerank(
    documents: list[Document],
    embeddings: list[list[float]],
    query_embedding: list[float],
    k: int = 10,
    lambda_param: float = 0.5
) -> list[Document]
```

<ParamField path="documents" type="list[Document]" required>
  Documents to rerank
</ParamField>

<ParamField path="embeddings" type="list[list[float]]" required>
  Document embeddings corresponding to documents list
</ParamField>

<ParamField path="query_embedding" type="list[float]" required>
  Query embedding vector
</ParamField>

<ParamField path="k" type="int" default="10">
  Number of documents to return
</ParamField>

<ParamField path="lambda_param" type="float" default="0.5">
  Balance parameter between relevance (1.0) and diversity (0.0). Default 0.5 balances both
</ParamField>

<ResponseField name="reranked" type="list[Document]">
  Reranked documents optimized for relevance and diversity
</ResponseField>

***

## Usage Examples

### Agentic routing

```python theme={null}
from langchain_groq import ChatGroq
from vectordb.langchain.components import AgenticRouter

llm = ChatGroq(model="llama-3.3-70b-versatile", temperature=0)
router = AgenticRouter(llm)

# Initial routing - should suggest 'search'
decision = router.route(
    "What is quantum computing?",
    has_documents=False
)
print(decision)
# {'action': 'search', 'reasoning': 'No documents retrieved yet'}

# After retrieval - may suggest 'reflect' or 'generate'
decision = router.route(
    "What is quantum computing?",
    has_documents=True,
    current_answer="Quantum computing uses qubits...",
    iteration=2
)
```

### Context compression with reranking

```python theme={null}
from langchain_community.cross_encoders import HuggingFaceCrossEncoder
from vectordb.langchain.components import ContextCompressor

reranker = HuggingFaceCrossEncoder(model_name="BAAI/bge-reranker-base")
compressor = ContextCompressor(mode="reranking", reranker=reranker)

# Compress 10 documents down to top 3
compressed = compressor.compress(
    query="What is AI?",
    documents=retrieved_documents,
    top_k=3
)
```

### Context compression with LLM extraction

```python theme={null}
from langchain_groq import ChatGroq
from vectordb.langchain.components import ContextCompressor

llm = ChatGroq(model="llama-3.3-70b-versatile")
compressor = ContextCompressor(mode="llm_extraction", llm=llm)

# Extract relevant passages from documents
compressed = compressor.compress(
    query="Explain neural networks",
    documents=retrieved_documents
)
```

### Query enhancement

```python theme={null}
from langchain_groq import ChatGroq
from vectordb.langchain.components import QueryEnhancer

llm = ChatGroq(model="llama-3.3-70b-versatile")
enhancer = QueryEnhancer(llm)

# Generate multiple query variations
queries = enhancer.generate_multi_queries(
    "What are the applications of AI?",
    num_queries=3
)

# Generate hypothetical document
hyde_doc = enhancer.generate_hyde_document(
    "How does machine learning work?"
)

# Generate step-back query
step_back = enhancer.generate_step_back_query(
    "What is the training process for GPT-4?"
)
# Returns: "What are the general principles of training large language models?"
```
