Use this file to discover all available pages before exploring further.
The Query Rewriter Agent transforms user queries to improve retrieval quality in RAG applications by bridging the gap between how users ask questions and how information is stored.Agents: 1 — Specialized agent for query optimization.
from praisonaiagents import QueryRewriterAgentagent = QueryRewriterAgent(model="gpt-4o-mini")result = agent.rewrite("AI trends")print(result.primary_query)# Output: "What are the current trends in Artificial Intelligence?"
from praisonaiagents import QueryRewriterAgentagent = QueryRewriterAgent(model="gpt-4o-mini")# Note: QueryRewriterAgent uses .rewrite() method# For API serving, integrate with standard agent
Expands abbreviations, fixes typos, and adds context to short keyword queries.
from praisonaiagents import QueryRewriterAgent, RewriteStrategyagent = QueryRewriterAgent(model="gpt-4o-mini")# Short keyword queryresult = agent.rewrite("AI trends", strategy=RewriteStrategy.BASIC)print(result.primary_query)# "What are the current trends in Artificial Intelligence (AI)?"# With abbreviationsresult = agent.rewrite("RAG best practices")print(result.primary_query)# "What are the best practices for Retrieval-Augmented Generation (RAG)?"
Generates a hypothetical document that would answer the query, improving semantic matching.
from praisonaiagents import QueryRewriterAgent, RewriteStrategyagent = QueryRewriterAgent(model="gpt-4o-mini")result = agent.rewrite("What is quantum computing?", strategy=RewriteStrategy.HYDE)print(result.hypothetical_document)# A detailed hypothetical answer about quantum computing# This document is used for embedding-based retrieval
Generates broader, higher-level questions to retrieve background context.
from praisonaiagents import QueryRewriterAgent, RewriteStrategyagent = QueryRewriterAgent(model="gpt-4o-mini")result = agent.rewrite( "What is the difference between GPT-4 and Claude 3?", strategy=RewriteStrategy.STEP_BACK)print(result.primary_query)# Rewritten specific queryprint(result.step_back_question)# "What are the key characteristics of large language models?"
Breaks complex multi-part questions into focused sub-queries.
from praisonaiagents import QueryRewriterAgent, RewriteStrategyagent = QueryRewriterAgent(model="gpt-4o-mini")result = agent.rewrite( "How do I set up a RAG pipeline and what embedding models should I use?", strategy=RewriteStrategy.SUB_QUERIES)for i, query in enumerate(result.sub_queries, 1): print(f"{i}. {query}")# 1. How do I set up a RAG pipeline?# 2. What are the best embedding models for RAG?
Uses conversation history to resolve pronouns and references.
from praisonaiagents import QueryRewriterAgent, RewriteStrategyagent = QueryRewriterAgent(model="gpt-4o-mini")chat_history = [ {"role": "user", "content": "Tell me about Python"}, {"role": "assistant", "content": "Python is a programming language..."}, {"role": "user", "content": "What frameworks are popular?"}, {"role": "assistant", "content": "Django, FastAPI, PyTorch..."}]result = agent.rewrite( "What about its performance?", strategy=RewriteStrategy.CONTEXTUAL, chat_history=chat_history)print(result.primary_query)# "How does Python's performance compare to other programming languages?"
The Query Rewriter Agent can use tools (e.g., search) to gather context before rewriting. The agent decides when to use tools based on the query.
from praisonaiagents import QueryRewriterAgentfrom praisonaiagents import internet_search# Agent with search tool - agent decides when to use itagent = QueryRewriterAgent( model="gpt-4o-mini", tools=[internet_search],)# For ambiguous queries, agent may search firstresult = agent.rewrite("latest developments in AI")print(result.primary_query)# Agent searched for context, then rewrote with current information
from praisonaiagents import QueryRewriterAgentdef my_search_tool(query: str) -> str: """Search for information.""" # Your search implementation return "Search results..."agent = QueryRewriterAgent( model="gpt-4o-mini", tools=[my_search_tool])result = agent.rewrite("company XYZ products")# Agent may use your tool to understand what XYZ is
# Rewrite before researchpraisonai research --query-rewrite "AI trends"# Rewrite with tools, then researchpraisonai research --query-rewrite --rewrite-tools "internet_search" "AI trends"# Full pipeline: rewrite + tools + research + savepraisonai research --query-rewrite --rewrite-tools "internet_search" --save "AI trends"
from praisonaiagents import QueryRewriterAgent, RewriteStrategy# Initialize rewriterrewriter = QueryRewriterAgent(model="gpt-4o-mini")# User queryuser_query = "ML best practices"# Rewrite for better retrievalresult = rewriter.rewrite(user_query, strategy=RewriteStrategy.MULTI_QUERY)# Use all queries for retrievalfor query in result.all_queries: # Retrieve documents using each query docs = vector_store.similarity_search(query) # Combine results...