chapter ten

10 Query generation, routing, and retrieval postprocessing

 

This chapter covers

  • Generating metadata queries directly from user questions
  • Converting user questions into database-specific queries (e.g., SQL, SPARQL)
  • Routing questions to the appropriate handler based on intent
  • Enhancing result relevance using Reciprocal Rank Fusion

In chapters 8 and 9, you improved Retrieval-Augmented Generation (RAG) answer accuracy using advanced indexing and query transformations. Optimizing indexing strengthens embedding effectiveness for broader chunks, adding richer context, while query transformations boost the precision of vector store retrieval.

Now we’ll dive into three more advanced RAG techniques. First, you’ll learn to generate queries specific to the type of content store in use. For instance, you’ll see how to generate SQL from a user’s natural language question to retrieve data from a relational database. Your setup might include several types of content stores—such as vector stores, a relational database, or even a knowledge graph database. You’ll use the large language model (LLM) to direct the user’s question to the right content store. Finally, you’ll refine the retrieved results to send only the most relevant content for synthesis, filtering out unnecessary data to maintain clarity and relevance.

10.1 Content database query generation

10.2 Self-querying (metadata query enrichment)

10.2.1 Ingestion: Metadata enrichment

10.2.2 Q&A on a metadata-enriched collection

10.3 Generating a structured SQL query

10.3.1 Installing SQLite

10.3.2 Setting up and connecting to the database

10.3.3 Generating SQL queries from natural language

10.3.4 Executing the SQL query

10.4 Generating a semantic SQL query

10.4.1 Standard SQL query

10.4.2 Semantic SQL query

10.4.3 Creating the embeddings

10.4.4 Performing a semantic SQL search

10.4.5 Automating semantic SQL search

10.4.6 Benefits of a semantic SQL search

10.5 Generating queries for a graph database