Builds PaperTrail from the base skeleton into a full evidence-verification platform for the "Built with Claude: Life Sciences" hackathon (Builder Track). New named-user features (Build Beyond the Bench): - Lab Notebook Companion — rough bench notes -> structured, searchable, grounded experiment records (lib/labNotebook, /console/lab-notebook, migration 0059) - Clinical Trial Matcher — de-identified patient notes -> ranked eligible ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning (lib/trialMatcher, /console/trial-matcher, migrations 0060) Platform: - Deterministic verification moat (biostats recompute, exact-span grounding) - 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar, PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier) - Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise governance (RBAC, /api/v1 gateway, metering, audit chain, retention) - 17 OSS projects assimilated into native TypeScript on our stack (backend/engines retained + credited in NOTICE) - Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site - Next.js 16 upgrade, benchmark harness (honest smoke-test caveat) Licensing: Apache-2.0 (LICENSE + NOTICE).
9.3 KiB
This guide demonstrates how to evaluate your R2R RAG outputs using the Ragas evaluation framework.
In this tutorial, you will:
- Prepare a sample dataset in R2R
- Use R2R's
/ragendpoint to perform Retrieval-Augmented Generation - Install and configure Ragas for evaluation
- Evaluate the generated responses using multiple metrics
- Analyze evaluation traces for deeper insights
Setting Up Ragas for R2R Evaluation
Installing Ragas
First, install Ragas and its dependencies:
%pip install ragas langchain-openai -q
Configuring Ragas with OpenAI
Ragas uses an LLM to perform evaluations. Set up an OpenAI model as the evaluator:
from langchain_openai import ChatOpenAI
from ragas.llms import LangchainLLMWrapper
# Make sure your OPENAI_API_KEY environment variable is set
llm = ChatOpenAI(model="gpt-4o-mini")
evaluator_llm = LangchainLLMWrapper(llm)
# If you'll be using embeddings for certain metrics
from langchain_openai import OpenAIEmbeddings
from ragas.embeddings import LangchainEmbeddingsWrapper
evaluator_embeddings = LangchainEmbeddingsWrapper(OpenAIEmbeddings())
Sample Dataset and R2R RAG Implementation
For this guide, we assume you have:
- An initialized R2R client
- A dataset about AI companies already ingested into R2R
- Basic knowledge of R2R's RAG capabilities
Here's a quick example of using R2R's /rag endpoint to generate an answer:
from r2r import R2RClient
client = R2RClient() # Assuming R2R_API_KEY is set in your environment
query = "What makes Meta AI's LLaMA models stand out?"
search_settings = {
"limit": 2,
"graph_settings": {"enabled": False, "limit": 2},
}
response = client.retrieval.rag(
query=query,
search_settings=search_settings
)
print(response.results.generated_answer)
The output might look like:
Meta AI's LLaMA models stand out due to their open-source nature, which supports innovation and experimentation by making high-quality models accessible to researchers and developers [1]. This approach democratizes AI development, fostering collaboration across industries and enabling researchers without access to expensive resources to work with advanced AI models [2].
Evaluating R2R with Ragas
Ragas provides a comprehensive evaluation framework specifically designed for RAG systems. The R2R-Ragas integration makes it easy to assess the quality of your R2R implementation.
Creating a Test Dataset
First, prepare a set of test questions and reference answers:
questions = [
"Who are the major players in the large language model space?",
"What is Microsoft's Azure AI platform known for?",
"What kind of models does Cohere provide?",
]
references = [
"The major players include OpenAI (GPT Series), Anthropic (Claude Series), Google DeepMind (Gemini Models), Meta AI (LLaMA Series), Microsoft Azure AI (integrating GPT Models), Amazon AWS (Bedrock with Claude and Jurassic), Cohere (business-focused models), and AI21 Labs (Jurassic Series).",
"Microsoft's Azure AI platform is known for integrating OpenAI's GPT models, enabling businesses to use these models in a scalable and secure cloud environment.",
"Cohere provides language models tailored for business use, excelling in tasks like search, summarization, and customer support.",
]
Collecting R2R Responses
Generate responses using your R2R implementation:
r2r_responses = []
search_settings = {
"limit": 2,
"graph_settings": {"enabled": False, "limit": 2},
}
for que in questions:
response = client.retrieval.rag(query=que, search_settings=search_settings)
r2r_responses.append(response)
The R2R-Ragas Integration
Ragas includes a dedicated integration for R2R that handles the conversion of R2R's response format to Ragas's evaluation dataset format:
from ragas.integrations.r2r import transform_to_ragas_dataset
# Convert R2R responses to Ragas format
ragas_eval_dataset = transform_to_ragas_dataset(
user_inputs=questions,
r2r_responses=r2r_responses,
references=references
)
print(ragas_eval_dataset)
# Output: EvaluationDataset(features=['user_input', 'retrieved_contexts', 'response', 'reference'], len=3)
The transform_to_ragas_dataset function extracts the necessary components from R2R responses, including:
- The generated answer
- The retrieved context chunks
- Citation information
Key Evaluation Metrics for R2R
Ragas offers several metrics that are particularly useful for evaluating R2R implementations:
from ragas.metrics import AnswerRelevancy, ContextPrecision, Faithfulness
from ragas import evaluate
# Define the metrics to use
ragas_metrics = [
AnswerRelevancy(llm=evaluator_llm), # How relevant is the answer to the query?
ContextPrecision(llm=evaluator_llm), # How precisely were the right documents retrieved?
Faithfulness(llm=evaluator_llm) # Does the answer stick to facts in the context?
]
# Run the evaluation
results = evaluate(dataset=ragas_eval_dataset, metrics=ragas_metrics)
Each metric provides valuable insights:
- Answer Relevancy: Measures how well the R2R-generated response addresses the user's query
- Context Precision: Evaluates if R2R's retrieval mechanism is bringing back the most relevant documents
- Faithfulness: Checks if R2R's generated answers accurately reflect the information in the retrieved documents
Interpreting Evaluation Results
The evaluation results show detailed scores for each sample and metric:
# View results as a dataframe
df = results.to_pandas()
print(df)
Example output:
user_input retrieved_contexts response reference answer_relevancy context_precision faithfulness
0 Who are the major players... [In the rapidly advancing field of...] The major players in the large language... The major players include OpenAI... 1.000000 1.0 1.000000
1 What is Microsoft's Azure AI... [Microsoft's Azure AI platform is famous for...] Microsoft's Azure AI platform is known for... Microsoft's Azure AI platform is... 0.948908 1.0 0.833333
2 What kind of models does Cohere provide? [Cohere is well-known for its language models...] Cohere provides language models tailored for... Cohere provides language models... 0.903765 1.0 1.000000
Advanced Visualization with Ragas App
For a more interactive analysis, upload results to the Ragas app:
# Make sure RAGAS_APP_TOKEN is set in your environment
results.upload()
This generates a shareable dashboard with:
- Detailed scores per metric and sample
- Visual comparisons across metrics
- Trace information showing why scores were assigned
- Suggestions for improvement
You can examine:
- Which queries R2R handled well
- Where retrieval or generation could be improved
- Patterns in your RAG system's performance
Advanced Evaluation Features
Non-LLM Metrics for Fast Evaluation
In addition to LLM-based metrics, you can use non-LLM metrics for faster evaluations:
from ragas.metrics import BleuScore
# Create a BLEU score metric
bleu_metric = BleuScore()
# Add it to your evaluation
quick_metrics = [bleu_metric]
quick_results = evaluate(dataset=ragas_eval_dataset, metrics=quick_metrics)
Custom Evaluation Criteria with AspectCritic
For tailored evaluations specific to your use case, AspectCritic allows you to define custom evaluation criteria:
from ragas.metrics import AspectCritic
# Define a custom evaluation aspect
custom_metric = AspectCritic(
name="factual_accuracy",
llm=evaluator_llm,
definition="Verify if the answer accurately states company names, model names, and specific capabilities without any factual errors."
)
# Evaluate with your custom criteria
custom_results = evaluate(dataset=ragas_eval_dataset, metrics=[custom_metric])
Training Your Own Metric
If you want to fine-tune metrics to your specific requirements:
- Use the Ragas app to annotate evaluation results
- Download the annotations as JSON
- Train your custom metric:
from ragas.config import InstructionConfig, DemonstrationConfig
demo_config = DemonstrationConfig(embedding=evaluator_embeddings)
inst_config = InstructionConfig(llm=evaluator_llm)
# Train your metric with your annotations
metric.train(
path="your-annotations.json",
demonstration_config=demo_config,
instruction_config=inst_config
)
Conclusion
This guide demonstrated how to use Ragas to thoroughly evaluate your R2R RAG implementation. By leveraging these evaluation tools, you can:
- Measure the quality of your R2R system across multiple dimensions
- Identify specific areas for improvement in retrieval and generation
- Track performance improvements as you refine your implementation
- Establish benchmarks for consistent quality
Through regular evaluation with Ragas, you can optimize your R2R configuration to deliver the most accurate, relevant, and helpful responses to your users.
For more information on R2R features, refer to the R2R documentation. To explore additional evaluation metrics and techniques with Ragas, visit the Ragas documentation.