Replies: 1 comment 1 reply
-
The inconsistency in the retrieved document chunks when using different LLMs with the same retriever setup in LangChain can be attributed to several factors:
To diagnose and resolve these inconsistencies:
Here is an example of how to set up a consistent chunking strategy and de-duplication: from langchain_text_splitters import TokenTextSplitter
# Define a consistent chunking strategy
text_splitter = TokenTextSplitter(
chunk_size=2000, # Ensure this is within the context window of all LLMs used
chunk_overlap=20,
)
texts = text_splitter.split_text(document.page_content)
# Extract information using the same retriever setup
extractions = extractor.batch(
[{"text": text} for text in texts],
{"max_concurrency": 5},
)
# Merge and de-duplicate results
key_developments = []
seen_evidence = set()
for extraction in extractions:
for key_development in extraction.key_developments:
if key_development.evidence not in seen_evidence:
key_developments.append(key_development)
seen_evidence.add(key_development.evidence)
key_developments[:10] Additionally, consider using the from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.language_models import BaseLanguageModel
from langchain_core.retrievers import BaseRetriever
from langchain.retrievers.multi_query import MultiQueryRetriever
# Define the prompt template for generating multiple queries
DEFAULT_QUERY_PROMPT = PromptTemplate(
input_variables=["question"],
template="""You are an AI language model assistant. Your task is
to generate 3 different versions of the given user
question to retrieve relevant documents from a vector database.
By generating multiple perspectives on the user question,
your goal is to help the user overcome some of the limitations
of distance-based similarity search. Provide these alternative
questions separated by newlines. Original question: {question}""",
)
# Initialize MultiQueryRetriever
multi_query_retriever = MultiQueryRetriever.from_llm(
retriever=your_retriever,
llm=your_llm,
prompt=DEFAULT_QUERY_PROMPT,
include_original=True
) By ensuring consistency in chunking, relevance scoring, and query rephrasing, and by using tools like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
xxx
Description
I have created a RAG application with weaviate as vectorstore, embedding="BAAI/bge-base-en-v1.5". I have split some Dutch text using the RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200, add_start_index=True).
retriever =vector_store.as_retriever(search_kwargs={"k":5,"score_threshold":0.6},search_type='similarity_score_threshold')
When I tested with openai gpt4o-mini, gemini-1.5-flash, gemini-1.5-pro and asks the following question: “wie betaalt de pgb voor de wmo?” I get the following answer: “Ik weet niet wie de pgb voor de Wmo betaalt.” Which is not correct! If I asks the same question using gpt-4o then I get the correct answer: “De gemeente betaalt het persoonsgebonden budget (pgb) niet direct aan u, maar aan de Sociale Verzekeringsbank (SVB). De SVB beheert het geld en betaalt uw zorgaanbieder achteraf, aan het eind van de maand, nadat u de facturen heeft gecontroleerd en opdracht heeft gegeven om te betalen”.
If I print the response using response = retriever.invoke(input="wie betaalt de pgb voor de wmo?") then I see in the 5 retrieved document chunks about the information regarding the “PGB” and which organization is paying it (should answer with SVB).
I don’t understand the inconsistency and why the retrieved chunks are NOT used when I am not using gpt4-o… See attachments of the results of the retrieved chunks when using gpt4o-mini (no info about SVB) but the info is available when using gpt4-o. How is this possible?
Versions of libraries used:
rag_results.docx
System Info
python 3.11
Beta Was this translation helpful? Give feedback.
All reactions