Use this file to discover all available pages before exploring further.
Agentic RAG introduces an LLM-based decision-making loop where an agent controls the retrieval process. Instead of a fixed pipeline, the agent chooses actions based on query analysis, retrieved content quality, and answer evaluation.
from vectordb.haystack.components import AgenticRouterrouter = AgenticRouter( model="llama-3.3-70b-versatile", api_key=os.getenv("GROQ_API_KEY"))# Agent decides which tool to usetool = router.select_tool("What is 15% of 240?")print(tool) # "calculation"tool = router.select_tool("What is photosynthesis?")print(tool) # "retrieval"
def _handle_calculation(self, query: str): prompt = f"""Solve this problem step by step:{query}Provide the calculation steps and final answer.""" result = self.generator.run(prompt=prompt) answer = result.get("replies", [""])[0] return {"documents": [], "answer": answer, "tool": "calculation"}
def _handle_reasoning(self, query: str, top_k: int): # Retrieve context for reasoning documents = self._retrieve(query, top_k) context = "\n\n".join([doc.content for doc in documents[:5]]) # Structured reasoning prompt prompt = f"""You are a helpful assistant that answers questions using step-by-step reasoning.Context:{context}Question: {query}Think through this step-by-step:1. First, identify what information is relevant2. Then, analyze the key points3. Finally, synthesize into a comprehensive answerAnswer:""" result = self.generator.run(prompt=prompt) answer = result.get("replies", [""])[0] return {"documents": documents, "answer": answer, "tool": "reasoning"}
The AgenticRouter scores answers and refines them iteratively:
class AgenticRouter: def self_reflect_loop( self, query: str, answer: str, context: str, max_iterations: int = 3, quality_threshold: int = 75 ) -> str: current_answer = answer for iteration in range(max_iterations): # Score current answer score = self.score_answer(query, current_answer, context) if score >= quality_threshold: return current_answer # Good enough # Refine answer current_answer = self._refine_answer( query, current_answer, context, score ) return current_answer # Return best attempt def score_answer(self, query: str, answer: str, context: str) -> int: prompt = f"""Rate the quality of this answer on a scale of 0-100:Question: {query}Context: {context}Answer: {answer}Consider:- Completeness: Does it address all parts of the question?- Accuracy: Is it supported by the context?- Clarity: Is it well-structured?Score (0-100):""" response = self.generator.run(prompt=prompt) # Parse score from response score = self._parse_score(response) return score