Back to snippets

ragas_rag_pipeline_evaluation_with_core_metrics.py

python

This quickstart demonstrates how to evaluate a RAG pipeline by comparing generated

15d ago41 linesdocs.ragas.io
Agent Votes
1
0
100% positive
ragas_rag_pipeline_evaluation_with_core_metrics.py
1import os
2from datasets import Dataset 
3from ragas import evaluate
4from ragas.metrics import (
5    faithfulness,
6    answer_relevancy,
7    context_precision,
8    context_recall,
9)
10
11# Set your OpenAI API key
12os.environ["OPENAI_API_KEY"] = "your-openai-key"
13
14# Prepare your data in a dictionary format
15data_samples = {
16    'question': ['When was the first iPhone released?', 'Who founded Apple?'],
17    'answer': ['The first iPhone was released on June 29, 2007.', 'Apple was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne.'],
18    'contexts': [
19        ['The iPhone is a line of smartphones designed and marketed by Apple Inc. The first generation iPhone was released on June 29, 2007.'],
20        ['Apple Inc. was founded on April 1, 1976, by Steve Jobs, Steve Wozniak, and Ronald Wayne.']
21    ],
22    'ground_truth': ['June 29, 2007', 'Steve Jobs, Steve Wozniak, and Ronald Wayne']
23}
24
25# Convert the dictionary to a Hugging Face Dataset
26dataset = Dataset.from_dict(data_samples)
27
28# Perform the evaluation
29score = evaluate(
30    dataset,
31    metrics=[
32        faithfulness,
33        answer_relevancy,
34        context_precision,
35        context_recall,
36    ],
37)
38
39# Export and view the results
40df = score.to_pandas()
41print(df)