Skip to main content
The --query-rewrite flag transforms user queries to improve RAG retrieval quality using the QueryRewriterAgent.

Quick Start

praisonai "AI trends" --query-rewrite

Usage

Basic Query Rewrite

praisonai "AI trends" --query-rewrite
Expected Output:
๐Ÿ”„ Query rewritten: "What are the current trends in Artificial Intelligence?"

โ•ญโ”€ Agent Info โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚  ๐Ÿ‘ค Agent: DirectAgent                                                       โ”‚
โ”‚  Role: Assistant                                                             โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Response โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Current AI trends include...                                                 โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

With Search Tools

# Rewrite with search tools (agent decides when to search)
praisonai "latest developments" --query-rewrite --rewrite-tools "internet_search"

# Works with any prompt
praisonai "explain quantum computing" --query-rewrite -v

Combine with Other Flags

# Query rewrite with prompt expansion
praisonai "AI news" --query-rewrite --expand-prompt

# Query rewrite with deep research
praisonai research --query-rewrite "AI trends"

# Query rewrite with verbose output
praisonai "explain quantum computing" --query-rewrite -v

How It Works

  1. Query Analysis: The QueryRewriterAgent analyzes your input
  2. Strategy Selection: Automatically selects the best rewrite strategy
  3. Query Transformation: Expands abbreviations, adds context, fixes typos
  4. Execution: The rewritten query is used for the task

Rewrite Strategies

StrategyDescriptionBest For
BASICExpand abbreviations, fix typos, add contextGeneral queries
HYDEGenerate hypothetical document for semantic matchingConceptual questions
STEP_BACKGenerate higher-level concept questionsSpecific technical queries
SUB_QUERIESDecompose multi-part questionsComplex queries
MULTI_QUERYGenerate multiple paraphrased versionsAmbiguous queries
CONTEXTUALResolve references using conversation historyFollow-up questions
AUTOAutomatically detect best strategyDefault

Examples

Technical Query

praisonai "GPT-4 vs Claude 3?" --query-rewrite
Rewritten to: โ€œWhat are the key differences between OpenAIโ€™s GPT-4 and Anthropicโ€™s Claude 3 language models in terms of capabilities, performance, and use cases?โ€

Ambiguous Query

praisonai "RAG setup" --query-rewrite
Rewritten to: โ€œHow do I set up a Retrieval-Augmented Generation (RAG) system, including document ingestion, embedding generation, vector storage, and query processing?โ€

Follow-up Query

praisonai "What about cost?" --query-rewrite
Rewritten to: โ€œWhat are the cost considerations and pricing models for implementing this solution?โ€

Programmatic Usage

from praisonaiagents import QueryRewriterAgent, RewriteStrategy

agent = QueryRewriterAgent(model="gpt-4o-mini")

# Basic rewrite
result = agent.rewrite("AI trends")
print(result.primary_query)  # "What are the current trends in Artificial Intelligence?"

# With specific strategy
result = agent.rewrite("What is quantum computing?", strategy=RewriteStrategy.HYDE)

# Step-back for broader context
result = agent.rewrite("GPT-4 vs Claude 3?", strategy=RewriteStrategy.STEP_BACK)

# Sub-queries for complex questions
result = agent.rewrite("RAG setup and best embedding models?", strategy=RewriteStrategy.SUB_QUERIES)

# Contextual with chat history
result = agent.rewrite("What about cost?", chat_history=[...])

Best Practices

Use --query-rewrite for RAG and search optimization. For expanding task prompts, use --expand-prompt instead.
Query rewriting adds an additional LLM call. Use --metrics to monitor token usage.
Use CaseFlag
RAG/Search optimization--query-rewrite
Task prompt expansion--expand-prompt
Both--query-rewrite --expand-prompt