QueryRewriterAgent Module
TheQueryRewriterAgent class transforms user queries to improve retrieval quality in RAG applications.
Supported 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
- AUTO: Automatically selects the best strategy
Import
Quick Example
Constructor
QueryRewriterAgent()
Creates a new QueryRewriterAgent instance.
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
name | str | "Query Rewriter" | Agent name |
model | str | "gpt-4o-mini" | LLM model to use |
verbose | bool | False | Enable verbose logging |
tools | list | [] | Optional tools for context-aware rewriting |
Methods
rewrite(query, strategy, chat_history)
Rewrites a query using the specified strategy.
Parameters:
query(str): The original user querystrategy(RewriteStrategy): Rewriting strategy to use (default: AUTO)chat_history(list): Optional conversation history for contextual rewriting
RewriteResult - Contains rewritten queries and metadata
RewriteStrategy Enum
RewriteResult
Examples
Contextual Rewriting with Chat History
With Search Tools
Related
- Agent Module - Base Agent class
- PromptExpanderAgent Module - Expand prompts for tasks
- Knowledge Module - Knowledge base integration

