Traditional RAG systems follow a rigid pattern: retrieve documents, stuff them into context, generate a response. But what happens when the first retrieval misses the mark, when multiple sources need synthesis, or when the query requires decomposition into sub-questions? Agentic RAG represents a paradigm shift where your retrieval system gains the ability to reason about its own performance, iterate on failed retrievals, and orchestrate complex multi-step information gathering.
123456789101112from typing import List, Tuple from dataclasses import dataclass @dataclass class RetrievalResult: documents: List[str] relevance_scores: List[float] source: str class AgenticRAG: def __init__(self, llm, retriever, max_iterations=3): self.llm = llm
123456789101112from typing import List, Dict, Optional from dataclasses import dataclass from enum import Enum class ActionType(Enum): SEARCH_DOCS = "search_docs" SEARCH_CODE = "search_code" QUERY_DATABASE = "query_database" SYNTHESIZE = "synthesize" @dataclass class AgentState:
123456789101112interface Tool { name: string; description: string; parameters: JSONSchema; execute: (params: any) => Promise<ToolResult>; } interface ToolResult { success: boolean; data: any; confidence: number; source: string;
123456789101112from typing import List, Dict, Tuple from dataclasses import dataclass from enum import Enum import asyncio class RetrievalQuality(Enum): EXCELLENT = "excellent" # >0.8 relevance ACCEPTABLE = "acceptable" # 0.5-0.8 relevance POOR = "poor" # <0.5 relevance FAILED = "failed" # No results @dataclass
123456789101112from typing import List, Dict, Any, Callable from abc import ABC, abstractmethod import json class RAGTool(ABC): """Base class for RAG tools.""" name: str description: str @abstractmethod async def execute(self, **kwargs) -> Dict[str, Any]: pass
123456789101112from dataclasses import dataclass from typing import List, Dict, Optional from enum import Enum import asyncio import json class QueryType(Enum): SIMPLE_LOOKUP = "simple_lookup" MULTI_HOP = "multi_hop" REQUIRES_CORRECTION = "requires_correction" MULTI_SOURCE = "multi_source" UNANSWERABLE = "unanswerable"