Use this file to discover all available pages before exploring further.
Re-ranking involves reordering nodes based on relevance to the query and choosing top n nodes. Different evaluations are performed based on the number of nodes returned after re-ranking.
Same Number of NodesContext Reranking: Checks if the order of re-ranked nodes is more relevant to the query than the original order.
Different Number of Nodes:Context Conciseness: Examines whether the reduced number of nodes still provides all the required information.
These evaluations collectively ensure the robustness and effectiveness of the RAG query engine, SubQuestionQueryGeneration operator, and the re-ranking process in the LlamaIndex pipeline.
You can use the open-source evaluation service to evaluate your model. In this case, you will need to provie an OpenAI API key. You can get yours here.Parameters:
key_type=“openai”
api_key=“OPENAI_API_KEY”
project_name_prefix=“PROJECT_NAME_PREFIX”
callback_handler = UpTrainCallbackHandler( key_type="openai", api_key="sk-...", # Replace with your OpenAI API key project_name_prefix="llama",)Settings.callback_manager = CallbackManager([callback_handler])
4
Load and Parse Documents
Load documents from Paul Graham’s essay “What I Worked On”.
Re-ranking is the process of reordering the nodes based on their relevance to the query. There are multiple classes of re-ranking algorithms offered by Llamaindex. We have used CohereRerank for this example.The re-ranker allows you to enter the number of top n nodes that will be returned after re-ranking. If this value remains the same as the original number of nodes, the re-ranker will only re-rank the nodes and not change the number of nodes. Otherwise, it will re-rank the nodes and return the top n nodes.We will perform different evaluations based on the number of nodes returned after re-ranking.
5.1
Re-ranking (With same number of nodes)
If the number of nodes returned after re-ranking is the same as the original number of nodes, the following evaluation will be performed:
Context Reranking: Check if the order of the re-ranked nodes is more relevant to the query than the original order.
api_key = "**********************************" # Insert cohere API key herecohere_rerank = CohereRerank( api_key=api_key, top_n=5) # In this example, the number of nodes before re-ranking is 5 and after re-ranking is also 5.index = VectorStoreIndex.from_documents( documents=documents, service_context=service_context)query_engine = index.as_query_engine( similarity_top_k=10, node_postprocessors=[cohere_rerank], service_context=service_context,)response = query_engine.query( "What did Sam Altman do in this essay?",)
Question: What did Sam Altman do in this essay?Context Reranking Score: 0.0
5.2
Re-ranking (With different number of nodes)
If the number of nodes returned after re-ranking is the lesser as the original number of nodes, the following evaluation will be performed:
Context Conciseness: If the re-ranked nodes are able to provide all the information required by the query.
api_key = "**********************************" # insert cohere API key herecohere_rerank = CohereRerank( api_key=api_key, top_n=2) # In this example, the number of nodes before re-ranking is 5 and after re-ranking is 2.index = VectorStoreIndex.from_documents( documents=documents, service_context=service_context)query_engine = index.as_query_engine( similarity_top_k=10, node_postprocessors=[cohere_rerank], service_context=service_context,)# Use your advanced RAGresponse = query_engine.query( "What did Sam Altman do in this essay?",)
Question: What did Sam Altman do in this essay?Context Conciseness Score: 1.0