jypi
  • Explore
ChatWays to LearnMind mapAbout

jypi

  • About Us
  • Our Mission
  • Team
  • Careers

Resources

  • Ways to Learn
  • Mind map
  • Blog
  • Help Center
  • Community Guidelines
  • Contributor Guide

Legal

  • Terms of Service
  • Privacy Policy
  • Cookie Policy
  • Content Policy

Connect

  • Twitter
  • Discord
  • Instagram
  • Contact Us
jypi

© 2026 jypi. All rights reserved.

Generative AI: Prompt Engineering Basics
Chapters

1Foundations of Generative AI

2LLM Behavior and Capabilities

3Core Principles of Prompt Engineering

4Writing Clear, Actionable Instructions

5Roles, Personas, and System Prompts

6Supplying Context and Grounding

7Examples: Zero-, One-, and Few-Shot

8Structuring Outputs and Formats

9Reasoning and Decomposition Techniques

10Iteration, Testing, and Prompt Debugging

11Evaluation, Metrics, and Quality Control

12Safety, Ethics, and Risk Mitigation

13Tools, Functions, and Agentic Workflows

14Retrieval-Augmented Generation (RAG)

RAG Concepts and BenefitsEmbeddings and VectorizationIndexing and Chunking TacticsQuery Construction PromptsRe-Ranking and FusionCitation and Attribution FormatsFreshness and Recency StrategiesReducing Hallucinations with RAGHybrid Sparse–Dense SearchContext Compression TechniquesBudget-Aware RetrievalRAG Evaluation MethodsAnswer–Source SeparationDynamic Routing and SwitchingVector Store Hygiene

15Multimodal and Advanced Prompt Patterns

Courses/Generative AI: Prompt Engineering Basics/Retrieval-Augmented Generation (RAG)

Retrieval-Augmented Generation (RAG)

23123 views

Combine prompts with retrieval to ground answers in external knowledge, improving accuracy and traceability.

Content

4 of 15

Query Construction Prompts

RAG Query Wizardry - Planner to Executor
1181 views
intermediate
humorous
machine learning
education theory
gpt-5-mini
1181 views

Versions:

RAG Query Wizardry - Planner to Executor

Watch & Learn

AI-discovered learning video

Sign in to watch the learning video for this topic.

Sign inSign up free

Start learning for free

Sign up to save progress, unlock study materials, and track your learning.

  • Bookmark content and pick up later
  • AI-generated study materials
  • Flashcards, timelines, and more
  • Progress tracking and certificates

Free to join · No credit card required

Query Construction Prompts for RAG: From Brainstorm to Bulletproof Retrieval

"You do not retrieve the truth; you retrieve the most relevant passages you asked for. So ask better."

Alright, you clever prompt hacker — you've already indexed wisely, chunked with an artist's precision, and turned raw text into beautiful vectors. You also know how to call tools and orchestrate planner–executor workflows. Now it is time to teach the planner how to ask the retrieval layer for the right stuff. This is Query Construction Prompts: the tiny, spicy layer that determines whether your RAG system serenades users with gold or hums confidently while hallucinating.


Why query construction matters (without repeating old lessons)

Indexing and embeddings made your content searchable. But embeddings only answer the question: what is similar to this query vector? The quality of that query vector is determined by how you construct the prompt that produces it. Bad query = irrelevant neighbors. Good query = high-signal retrieval that your generator can synthesize into reliable answers.

Remember: the planner in your planner–executor pattern doesn't just pick actions. It frames what should be retrieved, how narrowly, and which filters to apply. If you want better retrieval, change the question.


What a Query Construction Prompt does (in plain English)

  • Takes a user intent or an LLM-generated plan and converts it into one or more retrieval queries.
  • Specifies scope via filters or metadata constraints.
  • Controls verbosity and search style (semantic vs lexical, hybrid).
  • Creates fallback queries if the first retrieval is empty or low-confidence.

Core strategies for building query prompts

1) Rewriting into focused retrieval queries

Instead of feeding the user question verbatim, ask the planner to rewrite it for retrieval. This means removing ambiguity, adding keywords from domain ontology, and specifying expected answer type.

Example prompt template (pseudo):

Rewrite user input into a retrieval query optimized for semantic search. Keep it short (10-20 tokens), include 2-3 domain keywords, and state expected document type (policy, tutorial, API doc). Original: {user_question}

Why? LLMs tend to produce verbose, conversational text. Retrieval likes crisp, concept-dense phrases.

2) Keyword expansion and synonyms

Generate a small set of expanded queries to improve recall. Use synonyms, acronyms, and related concepts.

  • User: How to onboard a contractor
  • Expanded queries: contractor onboarding checklist, contingent worker onboarding, contractor orientation policy

This is especially useful for sparse corpora or when embedding space lacks coverage.

3) Metadata and filter-aware prompts

Tell the planner to output not only text queries but also metadata filters. Useful fields: department, doc_type, date_range, security_level.

Example output schema: a tuple of (query_text, filters)

query_text: contractor onboarding checklist
filters: {department: HR, doc_type: policy, date_after: 2019}

4) Multi-stage progressive queries

Start broad for recall, then refine for precision. The planner can orchestrate this: fetch top 50 passages, synthesize candidate answers, then query again for supporting docs.

This maps neatly to planner -> executor: planner issues stage 1, executor retrieves, planner synthesizes and issues stage 2.

5) Safety, hallucination prevention, and source requirements

Always instruct the query constructor whether the generator requires exact citations, high-trust sources only, or permissive exploration. This affects filters and how strict the retriever should be.

Example: flag require_cited_sources: true will make the executor prefer canonical docs and raise alerts if retrieval returns low-confidence matches.


Integrating function calling and tools

Remember our tool-enabled workflows? Make the planner return structured outputs that feed into your retrieval function call. The generator or planner can even call a retrieval tool like a human calls a librarian.

Pseudo function call pattern (planner output feeds into executor):

# planner output
{
  'stage': 1,
  'queries': ['contractor onboarding checklist', 'contingent worker orientation'],
  'filters': {'department': 'HR', 'doc_type': 'policy'}
}

# executor receives this and calls the retriever API with top_k, hybrid, etc

Use function calling support from the LLM to ensure structure and observability. If the LLM can return JSON objects (function args), the executor can parse and log everything deterministically.


Observability, error handling, and scopes

You already built observability for tools; extend it to query construction.

  • Log the original user input, constructed queries, filter choices, and top-k results.
  • Track metrics per query type: precision@k, recall against annotated question sets, MRR.
  • If retrieval returns no hits, trigger fallback queries (synonym expansion or broaden metadata filters).
  • If retrieved passages conflict, flag for human review or ask clarifying questions.

Failure modes and handling:

  • Empty results: broaden filters, expand queries, or ask user to clarify.
  • Too many results/noisy: tighten filters, increase chunk granularity, or request a document type.
  • Conflicting sources: return multi-source answer with provenance and confidence scores.

Prompt templates you can copy-paste

  1. Focused retrieval rewrite
Task: Rewrite the user question into a short retrieval query (10-20 tokens). Include important domain keywords. Output only the query.
User: {user_question}
  1. Structured retrieval plan
Task: Produce a retrieval plan object. Fields: queries (list), filters (map), require_cited_sources (bool). Use department names and doc types from our schema. Base on: {user_question}
  1. Progressive retrieval planner
Task: Create stage 1 and stage 2 queries. Stage 1 should maximize recall. Stage 2 should maximize precision using stage 1 synth. Output as JSON.
User: {user_question}

Short table: query styles and when to use them

Style Goal Use when
Short rewritten query Precision Corpora with dense embeddings or exact phrase matches
Expanded keyword set Recall Sparse or heterogeneous corpora
Filtered query + metadata Scoped retrieval Enterprise docs with department/date metadata
Progressive queries Balanced Complex tasks needing multiple passes

Small worked example

User: "How do we handle lithium-ion battery fires in our warehouse?"

Planner constructs:

  • Query 1: lithium-ion battery fire response checklist
  • Filters: {department: safety, doc_type: procedure, region: US}
  • require_cited_sources: true

Executor retrieves top passages, planner synthesizes a short answer and issues stage 2: "Find exact evacuation steps and approved fire extinguisher types for lithium-ion." Then the generator writes the final answer with citations.

This avoids hallucination by forcing retrieval for safety procedures and demanding cited sources.


Final pep talk and key takeaways

  • Good retrieval starts with good questions. A clever index and brilliant embeddings are useless if the query is nebulous.
  • Make the planner structured. Use function calls so queries, filters, and stages are machine-readable and observable.
  • Design for failure. Build progressive queries, fallback expansions, and clear error logs so the system handles silence and noise gracefully.
  • Measure and iterate. Track retrieval metrics per query template and tune prompts based on empirical performance.

Closing mic drop: If the retriever is your library, the planner is your librarian, and the query prompt is the way you whisper your needs into the librarian's ear. Be specific, be intentional, and for the love of vectors, include the department filter.

Go forth and construct queries that demand good answers.

Flashcards
Mind Map
Speed Challenge

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!

Ready to practice?

Sign up now to study with flashcards, practice questions, and more — and track your progress on this topic.

Study with flashcards, timelines, and more
Earn certificates for completed courses
Bookmark content for later reference
Track your progress across all topics