HypotheticalDocumentEmbedder implements HyDE retrieval: it asks an LLM to generate hypothetical answer text, encodes that text, and returns one vector for similarity search. This separates query-time generation from vector-store retrieval while hiding the pipeline behind embed_query(query). section 7.3
Startup-growth query
A prompt can ask for a passage answering “What are the best practices for a startup's user growth?” The embedder generates that passage, encodes it, and supplies the resulting vector to a FAISS similarity search. section 7.3
Listing 7.3 HyDE in LangChain.
from providers import get_chat_model, get_embeddings
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_classic.chains.hyde.base import (
HypotheticalDocumentEmbedder)
llm = get_chat_model(temperature=0)
base_embeddings = get_embeddings()
prompt = PromptTemplate(
input_variables=["question"],
template=("Please write a passage to answer the question.\n"
"Question: {question}\nPassage:")
) #A
hyde_embedder = HypotheticalDocumentEmbedder(
llm_chain=prompt | llm | StrOutputParser(), #B
base_embeddings=base_embeddings,
)
query = "What are the best practices for a startup's user growth?"
hypothetical_doc_embedding = hyde_embedder.embed_query(query) #C
The listing shows the practical wiring: prompt, chat model, string parser, base embeddings, and a single embed_query call. section 7.3
