Elffuss BraidHub

kikocis/lancedb public

main · 2.3 KiBRenderedRawPermalink
1**HyDE: Hypothetical Document Embeddings 🤹‍♂️**
2====================================================================
3HyDE, stands for Hypothetical Document Embeddings is an approach used for precise zero-shot dense retrieval without relevance labels. It focuses on augmenting and improving similarity searches, often intertwined with vector stores in information retrieval. The method generates a hypothetical document for an incoming query, which is then embedded and used to look up real documents that are similar to the hypothetical document.
4
5**[Official Paper](https://arxiv.org/pdf/2212.10496)**
6
7<figure markdown="span">
8 ![hyde](https://raw.githubusercontent.com/lancedb/assets/main/docs/assets/rag/hyde.png)
9 <figcaption>HyDE: <a href="https://arxiv.org/pdf/2212.10496">Source</a></figcaption>
10</figure>
11
12[![Open In Colab](../../assets/colab.svg)](https://colab.research.google.com/github/lancedb/vectordb-recipes/blob/main/examples/Advance-RAG-with-HyDE/main.ipynb)
13
14Here’s a code snippet for using HyDE with Langchain
15
16```python
17from langchain.llms import OpenAI
18from langchain.embeddings import OpenAIEmbeddings
19from langchain.prompts import PromptTemplate
20from langchain.chains import LLMChain, HypotheticalDocumentEmbedder
21from langchain.vectorstores import LanceDB
22
23# set OPENAI_API_KEY as env variable before this step
24# initialize LLM and embedding function
25llm = OpenAI()
26emebeddings = OpenAIEmbeddings()
27
28# HyDE embedding
29embeddings = HypotheticalDocumentEmbedder(llm_chain=llm_chain,base_embeddings=embeddings)
30
31# load dataset
32
33# LanceDB retriever
34retriever = LanceDB.from_documents(documents, embeddings, connection=table)
35
36# prompt template
37prompt_template = """
38As a knowledgeable and helpful research assistant, your task is to provide informative answers based on the given context. Use your extensive knowledge base to offer clear, concise, and accurate responses to the user's inquiries.
39if quetion is not related to documents simply say you dont know
40Question: {question}
41
42Answer:
43"""
44
45prompt = PromptTemplate(input_variables=["question"], template=prompt_template)
46
47# LLM Chain
48llm_chain = LLMChain(llm=llm, prompt=prompt)
49
50# vector search
51retriever.similarity_search(query)
52llm_chain.run(query)
53```
54
55[![Open In Colab](../../assets/colab.svg)](https://colab.research.google.com/github/lancedb/vectordb-recipes/blob/main/examples/Advance-RAG-with-HyDE/main.ipynb)