How to enhance the Relevance in LangChain and FAISS Multi-Query Retrieval

I’m working on a multi-query retrieval system using LangChain and FAISS for a project focused on meditation and spirituality. My current setup involves generating variations of a user query to improve document retrieval.

However, I’m encountering an issue where the retrieved documents are often unrelated to the topic of the query.

For embedding, I’m using the “nomic-embed-text” model. I have embed 335 PDF books about meditation and spirituality.

Here’s the code I’m using for generating query variations and retrieving documents:

from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.llms import Ollama
from langchain.retrievers import MultiQueryRetriever
from langchain.schema import StrOutputParser

# Load the FAISS database
new_db = FAISS.load_local("bookdb", embedder, allow_dangerous_deserialization=True)

# Define the prompt template for generating question variations
QUERY_PROMPT = PromptTemplate(
    input_variables=["question"],
    template="""You are an AI language model assistant. Generate five variations of the following question, maintaining the core meaning but using different phrasings or perspectives. Ensure all variations remain highly relevant to the topic of meditation.
    Original question: {question}"""
)

# Initialize the Ollama model
ollama = Ollama(model="llama3.1")

# Function to inspect the state
def inspect(state):
    """Print the state passed between Runnables in a langchain and pass it on"""
    print(state)
    return state

# Define the chain for generating question variations
question_variation_chain = LLMChain(
    llm=ollama,
    prompt=QUERY_PROMPT,
    output_parser=StrOutputParser(key="lines")
)

# Define the retriever
retriever = MultiQueryRetriever(
    retriever=new_db.as_retriever(),
    llm_chain=question_variation_chain
)

# Example query
question = "How to start a meditation?"

# Run the retriever
results = retriever.invoke(question)
print("Number of unique documents found:", len(results))

# Process the results to ensure uniqueness and sort by score
unique_results = {r.page_content: r for r in results}.values()
sorted_results = sorted(unique_results, key=lambda x: x.metadata.get('score', 0), reverse=True)

print(f"Number of unique documents found: {len(sorted_results)}")

for i, result in enumerate(sorted_results, 1):
    print(f"nResult {i}:")
    print(f"Content: {result.page_content[:200]}...")  # Print first 200 characters
    print(f"Metadata: {result.metadata}")

The generated query variations are relevant, yet the retrieved documents do not seem to match the topic well. For example, with the query “How to start a meditation?”, I get unrelated documents even though all my books are about meditation and spirituality

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật