Query Rewrite

Optimize queries for better RAG retrieval using QueryRewriterAgent

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

Quick Start

bash
praisonai "AI trends" --query-rewrite

!Query Rewrite Demo

Usage

Basic Query Rewrite

bash
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

bash
# Built-in SDK tool
praisonai "latest developments" --query-rewrite --rewrite-tools "internet_search"

# praisonai-tools / entry-point plugin — now resolved via the full tool chain
praisonai "latest developments" --query-rewrite --rewrite-tools "duckduckgo_search"

# Custom local tool (requires the env-var gate)
PRAISONAI_ALLOW_LOCAL_TOOLS=1 praisonai "latest developments" --query-rewrite --rewrite-tools "my_search"

Combine with Other Flags

bash
# 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
Diagram

BASIC

HYDE

STEP_BACK

SUB_QUERIES

Original Query

QueryRewriterAgent

Strategy

Expand & Clarify

Hypothetical Doc

Broader Context

Decompose

Rewritten Query

Execute 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

bash
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

bash
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

bash
praisonai "What about cost?" --query-rewrite

Rewritten to: “What are the cost considerations and pricing models for implementing this solution?”

Choosing the rewriter’s model

The CLI’s --query-rewrite step honours the standard cascade — CLI flag wins, env vars next, gpt-4o-mini fallback last:

bash
# Rewriter uses claude-3-5-sonnet-latest, matching the main agent
praisonai "What about cost?" --query-rewrite --llm claude-3-5-sonnet-latest
Diagram

yes

no

yes

no

yes

no

–llm CLI flag

Set?

Use it

MODEL_NAME env

Set?

OPENAI_MODEL_NAME env

Set?

gpt-4o-mini

To keep a cheap rewriter regardless of the main model, pin it explicitly with OPENAI_MODEL_NAME=gpt-4o-mini while --llm drives the main agent — but the recommended pattern is running the rewriter and main agent on the same model.

Programmatic Usage

The model= kwarg on QueryRewriterAgent is unchanged — the cascade above applies only to the CLI path.

python
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 CaseFlag
RAG/Search optimization--query-rewrite
Task prompt expansion--expand-prompt
Both--query-rewrite --expand-prompt

Related