README
¶
Conditional RAG Example
This example demonstrates a conditional RAG pipeline with dynamic routing based on relevance scores.
Overview
The conditional RAG pattern uses intelligent routing:
- Retrieve: Find potentially relevant documents
- Rerank: Score documents for relevance
- Route: Conditionally choose the next step based on relevance
- High relevance → Generate answer directly
- Low relevance → Trigger fallback search
- Generate: Create answer with available context
- Format: Add citations
This implementation is suitable for:
- Hybrid search systems
- Applications with variable query types
- Systems requiring fallback mechanisms
- Production systems needing robustness
Features
- Conditional Routing: Dynamic path selection based on relevance scores
- Relevance Threshold: Configurable threshold for routing decisions
- Fallback Search: Alternative search when primary retrieval fails
- Transparency: Shows which path was taken and why
- Adaptive Behavior: Different handling for different query types
Running the Example
cd examples/rag_conditional
go run main.go
Key Components
- Retriever: Initial document retrieval
- Reranker: Scores documents for relevance
- Conditional Edge: Routes based on relevance threshold
- Fallback Search: Alternative search mechanism
- LLM: DeepSeek-v3 for answer generation
- Pipeline: Conditional RAG with branching logic
Pipeline Flow
Query → Retrieve → Rerank → Check Relevance Score
↓
┌─────────┴─────────┐
↓ ↓
Score >= Threshold Score < Threshold
↓ ↓
Generate Fallback Search
↓ ↓
└─────────┬─────────┘
↓
Format Citations → Result
Routing Logic
The pipeline uses a relevance threshold (default: 0.5) to decide:
- High Relevance (≥ 0.5): Documents are relevant, proceed to generation
- Low Relevance (< 0.5): Documents may not be relevant, trigger fallback
Example Queries
The example includes queries that demonstrate both paths:
- Relevant queries: "How does checkpointing work?" → Direct generation
- Irrelevant queries: "What is the weather?" → Fallback search
Use Cases
- Hybrid Search: Combine vector search with keyword or web search
- Quality Control: Only use retrieved docs if they're truly relevant
- Graceful Degradation: Provide alternative responses for out-of-domain queries
- Multi-Source RAG: Route to different knowledge sources based on query type
Customization
You can customize:
- Relevance threshold for routing
- Fallback search implementation (e.g., web search, keyword search)
- Routing logic and conditions
- Number of documents to retrieve
- Reranking algorithm
Best Practices
- Tune Threshold: Adjust based on your use case and quality requirements
- Implement Fallback: Provide meaningful fallback (web search, default response)
- Monitor Routing: Track which path queries take for optimization
- Test Edge Cases: Ensure good behavior for both relevant and irrelevant queries
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.