How can I split a query into sentences for sentence window retrieval and reranking, while still sending it as a single query to the LLM? #13835
-
So here is a snippet of my program:
The issue here is that my qureries are not the typical questions that one might do with RAG but they are huge paragraphs due to which the retrieval is not really that good. Query -> broken into sentences -> each sentence retrieiving a few documents (which includes the sentence window and Rerank -> The Query + All the retrieved documenrs from all the sentences going to the LLM as {context} Of course, there is the option to use SubquestionPostProcessors, MultiStepPostProcessors, etc. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey there, @Satyam7166-tech! I'm Dosu, a bot here to help you with bug fixes, answering questions, and becoming a contributor while you wait for a human maintainer. How can I assist you today? To split a query into sentences for sentence window retrieval and reranking while still sending it as a single query to the LLM efficiently, you can use the
Here's a modified version of your code snippet incorporating these steps: from llama_index.core.retrievers.fusion_retriever import QueryFusionRetriever, FUSION_MODES
from llama_index.core.llms.utils import resolve_llm
from llama_index.core.schema import QueryBundle
from llama_index.legacy.node_parser.text.sentence import SentenceSplitter
from llama_index.core.postprocessor import MetadataReplacementPostProcessor
# Initialize the QueryFusionRetriever
retrievers = [BaseRetriever1(), BaseRetriever2()] # Replace with actual retrievers
llm = resolve_llm("openai-gpt-3") # Replace with actual LLM
query_fusion_retriever = QueryFusionRetriever(
retrievers=retrievers,
llm=llm,
mode=FUSION_MODES.RECIPROCAL_RANK, # Choose the desired fusion mode
num_queries=4, # Number of queries to generate
use_async=True # Use asynchronous execution
)
# Initialize the SentenceSplitter
sentence_splitter = SentenceSplitter.from_defaults()
# Example query
query = "This is the first sentence. This is the second sentence. This is the third sentence."
query_bundle = QueryBundle(query)
# Retrieve and rerank results
results = query_fusion_retriever._retrieve(query_bundle)
# Split sentences
split_sentences = sentence_splitter.split_text(query)
# Combine results into a single query string
single_query = " ".join(split_sentences)
# Use the combined query in your query engine
query_engine = sentence_index.as_query_engine(
similarity_top_k=top_k,
node_postprocessors=[
MetadataReplacementPostProcessor(target_metadata_key="window"), rerank
],
)
window_response = query_engine.query(single_query)
print(window_response) This approach ensures that your query is split into sentences, processed for semantic similarity, optimized for relevance, and then combined back into a single query string for efficient LLM processing [1][2][3][4]. |
Beta Was this translation helpful? Give feedback.
Hey there, @Satyam7166-tech! I'm Dosu, a bot here to help you with bug fixes, answering questions, and becoming a contributor while you wait for a human maintainer. How can I assist you today?
To split a query into sentences for sentence window retrieval and reranking while still sending it as a single query to the LLM efficiently, you can use the
QueryFusionRetriever
andSentenceSplitter
classes from the LlamaIndex framework. Here's how you can integrate these into your existing code:Initialize the
QueryFusionRetriever
:Use the
SentenceSpli…