----------------------
In LlamaIndex, the retrieval process is an important step in the RAG (Retrieve and Generate) pipeline. The retriever is responsible for fetching the most relevant document chunks or nodes based on the user's query.
Here is a basic example of how to retrieve relevant documents against a query using LlamaIndex:
retriever = index.as_retriever(similarity_top_k=2)
retrieved_nodes = retriever.retrieve("What is DoRA?")
In this example, the
retrieve
method of the
retriever
object is used to fetch the most relevant document chunks for the query "What is DoRA?".
If you're finding that the retrieved nodes are not relevant to the question asked, it could be due to the
similarity_top_k
parameter. This parameter determines the number of most similar document chunks to retrieve. You might want to experiment with different values for this parameter to see if it improves the relevance of the retrieved nodes.
Additionally, LlamaIndex also supports re-ranking, which involves reordering nodes based on relevance to the query and choosing the top nodes. Different evaluations are performed based on the number of nodes returned after re-ranking. This could also help in improving the relevance of the retrieved nodes. Here is an example of how to rerank the nodes:
ranker = LLMRerank(
choice_batch_size=5, top_n=3, llm=OpenAI(model="gpt-4o-mini")
)
new_nodes = ranker.postprocess_nodes(
ev.nodes, query_str=ctx.data.get("query")
)
In this example, the
LLMRerank
object is used to rerank the nodes based on their relevance to the query.