AI / LangChain4j interview questions
What is the HypotheticalDocumentEmbedder (HyDE) technique and how does LangChain4j support it?
HyDE (Hypothetical Document Embedder) is a query enhancement technique for RAG that improves retrieval quality by addressing a fundamental mismatch: the user's question is short and query-like, while the stored documents are long and answer-like. Embedding a question and a document paragraph in the same vector space often produces sub-optimal similarity scores because their styles differ.
The HyDE solution: before embedding the user's query, ask the LLM to generate a hypothetical document that would answer the question — essentially a plausible answer written in the style of the stored documents. Then embed this hypothetical document instead of the question. The resulting vector is much more similar to real matching documents.
// Custom HyDE QueryTransformer
class HydeQueryTransformer implements QueryTransformer {
private final ChatLanguageModel languageModel;
@Override
public Collection<Query> transform(Query originalQuery) {
String hypothetical = languageModel.generate(
"Write a short paragraph that would answer this question: "
+ originalQuery.text()
);
return List.of(Query.from(hypothetical));
}
}
// Wire into the RAG pipeline
RetrievalAugmentor augmentor = DefaultRetrievalAugmentor.builder()
.queryTransformer(new HydeQueryTransformer(chatModel))
.contentRetriever(EmbeddingStoreContentRetriever.from(store))
.build();
Assistant assistant = AiServices.builder(Assistant.class)
.chatLanguageModel(chatModel)
.retrievalAugmentor(augmentor)
.build();HyDE adds one additional LLM call per user query (to generate the hypothetical), which increases latency and cost. It is most effective for complex technical queries against document corpora where direct question embedding produces poor recall. Simpler query rewriting (compressing conversation context into a standalone question) is usually the better default trade-off.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
