RAG Query Rewriter

Transform a raw user question into a structured, retrieval-optimized query package that maximizes accuracy and recall when searching a knowledge base.

Category
AI & Prompt Engineering
Output
JSON
Fields
3
  • prompt-engineering
  • ai-search
  • output-optimization
  • data-structuring
  • workflow-design

Fill in the details

The raw question or request that needs to be rewritten for retrieval.

Brief description of the documents, topics, or data this system searches.

Previous conversation turns to help resolve pronouns or missing context.

Prompt

ROLE: You are a Retrieval-Augmented Generation (RAG) systems architect and prompt engineer. Your task is to transform a raw user question into a structured, retrieval-optimized query package that maximizes accuracy and recall when searching a knowledge base. INPUTS: - Knowledge Base Scope: Knowledge Base Scope - Chat History (optional): Chat History (Optional) - Raw User Question: User Question INSTRUCTIONS: 1. Context Resolution: If chat history is provided, resolve pronouns, implicit references, and missing context to make the query standalone. If no history is given, note that context is missing. 2. Standalone Query: Generate a single, clean, decontextualized query that precisely captures the user's core information need. 3. Alternative Phrasings: Produce 2–3 alternative versions or sub-queries using different terminology, synonyms, or decomposition strategies to widen recall. 4. Metadata Extraction: Identify key entities, dates, product names, or specific filters that should be used for metadata filtering or vector search constraints. 5. Intent Classification: Tag the query with one primary intent: lookup, comparison, how-to, troubleshooting, or exploratory. 6. Edge-Case Handling: Briefly specify how the system should respond to vague questions, multi-part questions, or queries that fall outside the provided knowledge base scope. CONSTRAINTS: - Use only the information provided in the inputs. Do not invent facts, document names, or system capabilities. - If critical context is missing or ambiguous, list it under "open_questions" instead of guessing. - Return ONLY valid JSON. Do not include markdown code fences, explanations, or text before or after the JSON object. - Ensure all keys match the exact structure below. OUTPUT STRUCTURE (JSON): { "standalone_query": "string", "alternative_queries": ["string", "string"], "metadata_filters": { "key": "value" }, "primary_intent": "lookup | comparison | how-to | troubleshooting | exploratory", "edge_case_handling": { "vague_questions": "string", "multi_part_questions": "string", "out_of_scope": "string" }, "open_questions": ["string"] }

How to use this prompt

  1. Fill in the fields above. Required ones are marked with *.
  2. Check the prompt preview: your answers appear highlighted in place.
  3. Select Copy prompt and paste it into ChatGPT, Claude, Gemini or any AI assistant.
See all AI & Prompt Engineering prompts