Skip to main content

Chapter 1: Query Transformation

Why Transform Queries?​

User queries are often:

  • Ambiguous or incomplete
  • Using different vocabulary than documents
  • Too short for good embedding

Query Rewriting​

from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate

rewrite_prompt = PromptTemplate.from_template("""
Rewrite the following query to be more specific and searchable.
Original: {query}
Rewritten:""")

llm = ChatOpenAI(model="gpt-4")
chain = rewrite_prompt | llm

# "how does it work?" → "How does retrieval augmented generation work?"
rewritten = chain.invoke({"query": "how does it work?"})

Query Expansion​

Add related terms:

expansion_prompt = PromptTemplate.from_template("""
Expand this query with related terms and synonyms:
Query: {query}
Expanded query with alternatives:""")

# "ML" → "machine learning ML deep learning artificial intelligence AI"

HyDE (Hypothetical Document Embeddings)​

Generate a hypothetical answer, embed it, search with that:

def hyde_retrieval(query, llm, retriever):
# Step 1: Generate hypothetical document
hyde_prompt = f"""Write a short paragraph that would
answer this question: {query}"""

hypothetical_doc = llm.invoke(hyde_prompt)

# Step 2: Embed the hypothetical document
# (embeddings will be closer to real documents)

# Step 3: Retrieve using hypothetical embedding
results = retriever.get_relevant_documents(
hypothetical_doc.content
)

return results

Why HyDE works:

  • Hypothetical answer uses document vocabulary
  • Better embedding alignment with corpus
  • Especially good for complex queries

Summary​

TechniqueWhen to UseImprovement
RewritingVague queriesClarity
ExpansionMissing synonymsRecall
HyDEComplex questionsPrecision