Query Rewriter Agent • AI Agent SDK

Query Rewriter Agent Module

query_rewriter_agent

AI Agent

Query Rewriter Agent Module

This module provides the QueryRewriterAgent class for transforming user queries to improve retrieval quality in RAG applications.

Supported Rewriting Strategies:

  • BASIC: Simple rephrasing for clarity and keyword optimization
  • HYDE: Hypothetical Document Embeddings – generates a hypothetical answer
  • STEP_BACK: Generates higher-level concept questions for complex queries
  • SUB_QUERIES: Decomposes multi-part questions into focused sub-queries
  • MULTI_QUERY: Generates multiple paraphrased versions for ensemble retrieval
  • CONTEXTUAL: Uses conversation history to resolve references and context

Example:

python
from praisonaiagents import QueryRewriterAgent, RewriteStrategy

agent = QueryRewriterAgent()

# Basic rewriting
result = agent.rewrite("AI trends")
print(result.rewritten_queries)

# HyDE for better semantic matching
result = agent.rewrite("What is quantum computing?", strategy=RewriteStrategy.HYDE)
print(result.hypothetical_document)

# Contextual with chat history
chat_history = [
    {"role": "user", "content": "Tell me about Python"},
    {"role": "assistant", "content": "Python is a programming language..."}
]
result = agent.rewrite("What about its performance?", chat_history=chat_history)

# With search tools for context-aware rewriting
from praisonaiagents.tools import internet_search
agent = QueryRewriterAgent(tools=[internet_search])
result = agent.rewrite("latest AI developments")  # Searches first, then rewrites

Import

python
from praisonaiagents.agent import query_rewriter_agent

Classes


Related Documentation