Metrics
MetricType
Bases: Enum
Enumeration of metric types in Ragas.
Attributes:
| Name | Type | Description |
|---|---|---|
SINGLE_TURN |
str
|
Represents a single-turn metric type. |
MULTI_TURN |
str
|
Represents a multi-turn metric type. |
Metric
dataclass
Metric(_required_columns: Dict[MetricType, Set[str]] = dict(), name: str = '')
Bases: ABC
Abstract base class for metrics in Ragas.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the metric. |
required_columns |
Dict[str, Set[str]]
|
A dictionary mapping metric type names to sets of required column names. This is
a property and raises |
score
Calculates the score for a single row of data.
Note
This method is deprecated and will be removed in 0.3. Please use single_turn_ascore or multi_turn_ascore instead.
Source code in src/ragas/metrics/base.py
ascore
async
Asynchronously calculates the score for a single row of data.
Note
This method is deprecated and will be removed in 0.3. Please use single_turn_ascore instead.
Source code in src/ragas/metrics/base.py
MetricWithLLM
dataclass
MetricWithLLM(_required_columns: Dict[MetricType, Set[str]] = dict(), name: str = '', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = None)
Bases: Metric, PromptMixin
A metric class that uses a language model for evaluation.
Attributes:
| Name | Type | Description |
|---|---|---|
llm |
Optional[BaseRagasLLM]
|
The language model used for the metric. |
train
train(path: str, demonstration_config: Optional[DemonstrationConfig] = None, instruction_config: Optional[InstructionConfig] = None, callbacks: Optional[Callbacks] = None, run_config: Optional[RunConfig] = None, batch_size: Optional[int] = None, with_debugging_logs=False, raise_exceptions: bool = True) -> None
Train the metric using local JSON data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to local JSON training data file |
required |
demonstration_config
|
DemonstrationConfig
|
Configuration for demonstration optimization |
None
|
instruction_config
|
InstructionConfig
|
Configuration for instruction optimization |
None
|
callbacks
|
Callbacks
|
List of callback functions |
None
|
run_config
|
RunConfig
|
Run configuration |
None
|
batch_size
|
int
|
Batch size for training |
None
|
with_debugging_logs
|
bool
|
Enable debugging logs |
False
|
raise_exceptions
|
bool
|
Whether to raise exceptions during training |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If path is not provided or not a JSON file |
Source code in src/ragas/metrics/base.py
SingleTurnMetric
dataclass
SingleTurnMetric(_required_columns: Dict[MetricType, Set[str]] = dict(), name: str = '')
Bases: Metric
A metric class for evaluating single-turn interactions.
This class provides methods to score single-turn samples, both synchronously and asynchronously.
single_turn_score
single_turn_score(sample: SingleTurnSample, callbacks: Callbacks = None) -> float
Synchronously score a single-turn sample.
May raise ImportError if nest_asyncio is not installed in a Jupyter-like environment.
Source code in src/ragas/metrics/base.py
single_turn_ascore
async
single_turn_ascore(sample: SingleTurnSample, callbacks: Callbacks = None, timeout: Optional[float] = None) -> float
Asynchronously score a single-turn sample with an optional timeout.
May raise asyncio.TimeoutError if the scoring process exceeds the specified timeout.
Source code in src/ragas/metrics/base.py
MultiTurnMetric
dataclass
MultiTurnMetric(_required_columns: Dict[MetricType, Set[str]] = dict(), name: str = '')
Bases: Metric
A metric class for evaluating multi-turn conversations.
This class extends the base Metric class to provide functionality for scoring multi-turn conversation samples.
multi_turn_score
multi_turn_score(sample: MultiTurnSample, callbacks: Callbacks = None) -> float
Score a multi-turn conversation sample synchronously.
May raise ImportError if nest_asyncio is not installed in Jupyter-like environments.
Source code in src/ragas/metrics/base.py
multi_turn_ascore
async
multi_turn_ascore(sample: MultiTurnSample, callbacks: Callbacks = None, timeout: Optional[float] = None) -> float
Score a multi-turn conversation sample asynchronously.
May raise asyncio.TimeoutError if the scoring process exceeds the specified timeout.
Source code in src/ragas/metrics/base.py
Ensember
Combine multiple llm outputs for same input (n>1) to a single output
from_discrete
Simple majority voting for binary values, ie [0,0,1] -> 0 inputs: list of list of dicts each containing verdict for a single input
Source code in src/ragas/metrics/base.py
SimpleBaseMetric
dataclass
Bases: ABC
Base class for simple metrics that return MetricResult objects.
SimpleLLMMetric
dataclass
SimpleLLMMetric(name: str, allowed_values: AllowedValuesType = (lambda: ['pass', 'fail'])(), prompt: Optional[Union[str, 'Prompt']] = None)
Bases: SimpleBaseMetric
LLM-based metric that uses prompts to generate structured responses.
save
Save the metric configuration to a JSON file.
Parameters:
path : str, optional File path to save to. If not provided, saves to "./{metric.name}.json" Use .gz extension for compression.
Note:
If the metric has a response_model, its schema will be saved for reference but the model itself cannot be serialized. You'll need to provide it when loading.
Examples:
All these work:
metric.save() # → ./response_quality.json metric.save("custom.json") # → ./custom.json metric.save("/path/to/metrics/") # → /path/to/metrics/response_quality.json metric.save("no_extension") # → ./no_extension.json metric.save("compressed.json.gz") # → ./compressed.json.gz (compressed)
Source code in src/ragas/metrics/base.py
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 | |
load
classmethod
load(path: str, response_model: Optional[Type['BaseModel']] = None, embedding_model: Optional['EmbeddingModelType'] = None) -> 'SimpleLLMMetric'
Load a metric from a JSON file.
Parameters:
path : str File path to load from. Supports .gz compressed files. response_model : Optional[Type[BaseModel]] Pydantic model to use for response validation. Required for custom SimpleLLMMetrics. embedding_model : Optional[Any] Embedding model for DynamicFewShotPrompt. Required if the original used one.
Returns:
SimpleLLMMetric Loaded metric instance
Raises:
ValueError If file cannot be loaded, is invalid, or missing required models
Source code in src/ragas/metrics/base.py
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 | |
get_correlation
abstractmethod
Calculate the correlation between gold scores and predicted scores. This is a placeholder method and should be implemented based on the specific metric.
Source code in src/ragas/metrics/base.py
align_and_validate
align_and_validate(dataset: 'Dataset', embedding_model: 'EmbeddingModelType', llm: 'BaseRagasLLM', test_size: float = 0.2, random_state: int = 42, **kwargs: Dict[str, Any])
Args: dataset: experiment to align the metric with. embedding_model: The embedding model used for dynamic few-shot prompting. llm: The LLM instance to use for scoring.
Align the metric with the specified experiments and validate it against a gold standard experiment. This method combines alignment and validation into a single step.
Source code in src/ragas/metrics/base.py
align
Args: train_dataset: train_dataset to align the metric with. embedding_model: The embedding model used for dynamic few-shot prompting.
Align the metric with the specified experiments by different optimization methods.
Source code in src/ragas/metrics/base.py
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 | |
validate_alignment
Args: llm: The LLM instance to use for scoring. test_dataset: An Dataset instance containing the gold standard scores. mapping: A dictionary mapping variable names expected by metrics to their corresponding names in the gold experiment.
Validate the alignment of the metric by comparing the scores against a gold standard experiment. This method computes the Cohen's Kappa score and agreement rate between the gold standard scores and the predicted scores from the metric.
Source code in src/ragas/metrics/base.py
create_auto_response_model
Create a response model and mark it as auto-generated by Ragas.
This function creates a Pydantic model using create_model and marks it with a special attribute to indicate it was auto-generated. This allows the save() method to distinguish between auto-generated models (which are recreated on load) and custom user models.
Parameters:
name : str Name for the model class **fields Field definitions in create_model format Each field is specified as: field_name=(type, default_or_field_info)
Returns:
Type[BaseModel] Pydantic model class marked as auto-generated
Examples:
from pydantic import Field
Simple model with required fields
ResponseModel = create_auto_response_model( ... "ResponseModel", ... value=(str, ...), ... reason=(str, ...) ... )
Model with Field validators and descriptions
ResponseModel = create_auto_response_model( ... "ResponseModel", ... value=(str, Field(..., description="The predicted value")), ... reason=(str, Field(..., description="Reasoning for the prediction")) ... )
Source code in src/ragas/metrics/base.py
AnswerCorrectness
dataclass
AnswerCorrectness(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'user_input', 'response', 'reference'}})(), name: str = 'answer_correctness', embeddings: Optional[Union[BaseRagasEmbeddings, BaseRagasEmbedding]] = None, llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = None, correctness_prompt: PydanticPrompt = CorrectnessClassifier(), statement_generator_prompt: PydanticPrompt = StatementGeneratorPrompt(), weights: list[float] = (lambda: [0.75, 0.25])(), beta: float = 1.0, answer_similarity: Optional[AnswerSimilarity] = None, max_retries: int = 1)
Bases: MetricWithLLM, MetricWithEmbeddings, SingleTurnMetric
Measures answer correctness compared to ground truth as a combination of factuality and semantic similarity.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
string
|
The name of the metrics |
weights |
list[float]
|
a list of two weights corresponding to factuality and semantic similarity Defaults [0.75, 0.25] |
answer_similarity |
Optional[AnswerSimilarity]
|
The AnswerSimilarity object |
ResponseRelevancy
dataclass
ResponseRelevancy(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'user_input', 'response'}})(), name: str = 'answer_relevancy', embeddings: Optional[Union[BaseRagasEmbeddings, BaseRagasEmbedding]] = None, llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = None, question_generation: PydanticPrompt = ResponseRelevancePrompt(), strictness: int = 3)
Bases: MetricWithLLM, MetricWithEmbeddings, SingleTurnMetric
Scores the relevancy of the answer according to the given question. Answers with incomplete, redundant or unnecessary information is penalized. Score can range from 0 to 1 with 1 being the best.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
string
|
The name of the metrics |
strictness |
int
|
Here indicates the number questions generated per answer. Ideal range between 3 to 5. |
embeddings |
Embedding
|
The langchain wrapper of Embedding object. E.g. HuggingFaceEmbeddings('BAAI/bge-base-en') |
SemanticSimilarity
dataclass
SemanticSimilarity(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'reference', 'response'}})(), name: str = 'semantic_similarity', embeddings: Optional[Union[BaseRagasEmbeddings, BaseRagasEmbedding]] = None, is_cross_encoder: bool = False, threshold: Optional[float] = None)
Bases: MetricWithEmbeddings, SingleTurnMetric
Scores the semantic similarity of ground truth with generated answer. cross encoder score is used to quantify semantic similarity. SAS paper: https://arxiv.org/pdf/2108.06130.pdf
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
|
model_name |
The model to be used for calculating semantic similarity Defaults open-ai-embeddings select cross-encoder model for best results https://huggingface.co/spaces/mteb/leaderboard |
|
threshold |
Optional[float]
|
The threshold if given used to map output to binary Default 0.5 |
AspectCritic
AspectCritic(name: str, definition: str, llm: Optional[BaseRagasLLM] = None, required_columns: Optional[Dict[MetricType, Set[str]]] = None, output_type: Optional[MetricOutputType] = BINARY, single_turn_prompt: Optional[PydanticPrompt] = None, multi_turn_prompt: Optional[PydanticPrompt] = None, strictness: int = 1, max_retries: int = 1)
Bases: MetricWithLLM, SingleTurnMetric, MultiTurnMetric
Judges the submission to give binary results using the criteria specified in the metric definition.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
name of the metrics |
definition |
str
|
criteria to judge the submission, example "Is the submission spreading fake information?" |
strictness |
int
|
The number of times self consistency checks is made. Final judgement is made using majority vote. |
Source code in src/ragas/metrics/_aspect_critic.py
ContextEntityRecall
dataclass
ContextEntityRecall(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'reference', 'retrieved_contexts'}})(), name: str = 'context_entity_recall', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = None, context_entity_recall_prompt: PydanticPrompt = ExtractEntitiesPrompt(), max_retries: int = 1)
Bases: MetricWithLLM, SingleTurnMetric
Calculates recall based on entities present in ground truth and context. Let CN be the set of entities present in context, GN be the set of entities present in the ground truth.
Then we define can the context entity recall as follows: Context Entity recall = | CN ∩ GN | / | GN |
If this quantity is 1, we can say that the retrieval mechanism has retrieved context which covers all entities present in the ground truth, thus being a useful retrieval. Thus this can be used to evaluate retrieval mechanisms in specific use cases where entities matter, for example, a tourism help chatbot.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
|
batch_size |
int
|
Batch size for openai completion. |
IDBasedContextPrecision
dataclass
IDBasedContextPrecision(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'retrieved_context_ids', 'reference_context_ids'}})(), name: str = 'id_based_context_precision', output_type: MetricOutputType = CONTINUOUS)
Bases: SingleTurnMetric
Calculates context precision by directly comparing retrieved context IDs with reference context IDs. The score represents what proportion of the retrieved context IDs are actually relevant (present in reference).
This metric works with both string and integer IDs.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Name of the metric |
LLMContextPrecisionWithReference
dataclass
LLMContextPrecisionWithReference(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'user_input', 'retrieved_contexts', 'reference'}})(), name: str = 'llm_context_precision_with_reference', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = None, context_precision_prompt: PydanticPrompt = ContextPrecisionPrompt(), max_retries: int = 1)
Bases: MetricWithLLM, SingleTurnMetric
Average Precision is a metric that evaluates whether all of the relevant items selected by the model are ranked higher or not.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
|
evaluation_mode |
EvaluationMode
|
|
context_precision_prompt |
Prompt
|
|
IDBasedContextRecall
dataclass
IDBasedContextRecall(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'retrieved_context_ids', 'reference_context_ids'}})(), name: str = 'id_based_context_recall', output_type: MetricOutputType = CONTINUOUS)
Bases: SingleTurnMetric
Calculates context recall by directly comparing retrieved context IDs with reference context IDs. The score represents what proportion of the reference IDs were successfully retrieved.
This metric works with both string and integer IDs.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Name of the metric |
LLMContextRecall
dataclass
LLMContextRecall(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'user_input', 'retrieved_contexts', 'reference'}})(), name: str = 'context_recall', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = CONTINUOUS, context_recall_prompt: PydanticPrompt = ContextRecallClassificationPrompt(), max_retries: int = 1)
Bases: MetricWithLLM, SingleTurnMetric
Estimates context recall by estimating TP and FN using annotated answer and retrieved context.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
|
FactualCorrectness
dataclass
FactualCorrectness(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'response', 'reference'}})(), name: str = 'factual_correctness', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = CONTINUOUS, mode: Literal['precision', 'recall', 'f1'] = 'f1', beta: float = 1.0, atomicity: Literal['low', 'high'] = 'low', coverage: Literal['low', 'high'] = 'low', claim_decomposition_prompt: PydanticPrompt = ClaimDecompositionPrompt(), nli_prompt: PydanticPrompt = NLIStatementPrompt(), language: str = 'english')
Bases: MetricWithLLM, SingleTurnMetric
FactualCorrectness is a metric class that evaluates the factual correctness of responses generated by a language model. It uses claim decomposition and natural language inference (NLI) to verify the claims made in the responses against reference texts.
Attributes: name (str): The name of the metric, default is "factual_correctness". _required_columns (Dict[MetricType, Set[str]]): A dictionary specifying the required columns for each metric type. Default is {"SINGLE_TURN": {"response", "reference"}}. mode (Literal["precision", "recall", "f1"]): The mode of evaluation, can be "precision", "recall", or "f1". Default is "f1". beta (float): The beta value used for the F1 score calculation. A beta > 1 gives more weight to recall, while beta < 1 favors precision. Default is 1.0. atomicity (Literal["low", "high"]): The level of atomicity for claim decomposition. Default is "low". coverage (Literal["low", "high"]): The level of coverage for claim decomposition. Default is "low". claim_decomposition_prompt (PydanticPrompt): The prompt used for claim decomposition. nli_prompt (PydanticPrompt): The prompt used for natural language inference (NLI).
Faithfulness
dataclass
Faithfulness(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'user_input', 'response', 'retrieved_contexts'}})(), name: str = 'faithfulness', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = CONTINUOUS, nli_statements_prompt: PydanticPrompt = NLIStatementPrompt(), statement_generator_prompt: PydanticPrompt = StatementGeneratorPrompt(), max_retries: int = 1)
Bases: MetricWithLLM, SingleTurnMetric
FaithfulnesswithHHEM
dataclass
FaithfulnesswithHHEM(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'user_input', 'response', 'retrieved_contexts'}})(), name: str = 'faithfulness_with_hhem', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = CONTINUOUS, nli_statements_prompt: PydanticPrompt = NLIStatementPrompt(), statement_generator_prompt: PydanticPrompt = StatementGeneratorPrompt(), max_retries: int = 1, device: str = 'cpu', batch_size: int = 10)
Bases: Faithfulness
NoiseSensitivity
dataclass
NoiseSensitivity(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'user_input', 'response', 'reference', 'retrieved_contexts'}})(), name: str = 'noise_sensitivity', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = CONTINUOUS, mode: Literal['relevant', 'irrelevant'] = 'relevant', nli_statements_prompt: PydanticPrompt = NLIStatementPrompt(), statement_generator_prompt: PydanticPrompt = StatementGeneratorPrompt(), max_retries: int = 1)
Bases: MetricWithLLM, SingleTurnMetric
AnswerAccuracy
dataclass
AnswerAccuracy(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'user_input', 'response', 'reference'}})(), name: str = 'nv_accuracy', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = None)
Bases: MetricWithLLM, SingleTurnMetric
Measures answer accuracy compared to ground truth given a user_input. This metric averages two distinct judge prompts to evaluate.
Top10, Zero-shoot LLM-as-a-Judge Leaderboard: 1)- nvidia/Llama-3_3-Nemotron-Super-49B-v1 2)- mistralai/mixtral-8x22b-instruct-v0.1 3)- mistralai/mixtral-8x7b-instruct-v0.1 4)- meta/llama-3.1-70b-instruct 5)- meta/llama-3.3-70b-instruct 6)- meta/llama-3.1-405b-instruct 7)- mistralai/mistral-nemo-12b-instruct 8)- nvidia/llama-3.1-nemotron-70b-instruct 9)- meta/llama-3.1-8b-instruct 10)- google/gemma-2-2b-it The top1 LB model have high correlation with human judges (~0.92).
Attributes:
| Name | Type | Description |
|---|---|---|
name |
string
|
The name of the metrics |
answer_accuracy |
The AnswerAccuracy object |
ContextRelevance
dataclass
ContextRelevance(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'user_input', 'retrieved_contexts'}})(), name: str = 'nv_context_relevance', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = None)
Bases: MetricWithLLM, SingleTurnMetric
Parameters: Score the relevance of the retrieved contexts be based on the user input.
Input: data: list of Dicts with keys: user_input, retrieved_contexts Output: 0.0: retrieved_contexts is not relevant for the user_input 0.5: retrieved_contexts is partially relevant for the user_input 1.0: retrieved_contexts is fully relevant for the user_input
ResponseGroundedness
dataclass
ResponseGroundedness(_required_columns: Dict[MetricType, Set[str]] = (lambda: {SINGLE_TURN: {'response', 'retrieved_contexts'}})(), name: str = 'nv_response_groundedness', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = None)
Bases: MetricWithLLM, SingleTurnMetric
Parameters: Score the groundedness of the response based on the retrieved contexts.
Input: data: list of Dicts with keys: response, retrieved contexts Output: 0.0: response is not grounded in the retrieved contexts 0.5: response is partially grounded in the retrieved contexts 1.0: response is fully grounded in the retrieved contexts
SimpleCriteriaScore
SimpleCriteriaScore(name: str, definition: str, llm: Optional[BaseRagasLLM] = None, required_columns: Optional[Dict[MetricType, Set[str]]] = None, output_type: Optional[MetricOutputType] = DISCRETE, single_turn_prompt: Optional[PydanticPrompt] = None, multi_turn_prompt: Optional[PydanticPrompt] = None, strictness: int = 1)
Bases: MetricWithLLM, SingleTurnMetric, MultiTurnMetric
Judges the submission to give binary results using the criteria specified in the metric definition.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
name of the metrics |
definition |
str
|
criteria to score the submission |
strictness |
int
|
The number of times self consistency checks is made. Final judgement is made using majority vote. |
Source code in src/ragas/metrics/_simple_criteria.py
ToolCallAccuracy
dataclass
ToolCallAccuracy(_required_columns: Dict[MetricType, Set[str]] = (lambda: {MULTI_TURN: {'user_input', 'reference_tool_calls'}})(), name: str = 'tool_call_accuracy', strict_order: bool = True, arg_comparison_metric: SingleTurnMetric = (lambda: ExactMatch())())
Bases: MultiTurnMetric
Tool Call Accuracy metric measures how accurately an LLM agent makes tool calls compared to reference tool calls.
The metric supports two evaluation modes: 1. Strict order (default): Tool calls must match exactly in sequence 2. Flexible order: Tool calls can be in any order (parallel evaluation)
The metric evaluates two aspects: 1. Sequence alignment: Whether predicted and reference tool calls match in the required order 2. Argument accuracy: How well tool call arguments match between predicted and reference
Score calculation: - If sequences don't align: score = 0 - If sequences align: score = (average argument accuracy) * sequence_alignment_factor - Length mismatches result in warnings and proportional penalty
Edge cases: - No predicted tool calls: returns 0.0 - Length mismatch: compares only the overlapping portion and applies coverage penalty - Missing arguments: contributes 0 to the argument score for that tool call
The final score is always between 0.0 and 1.0.
Args: strict_order: If True (default), tool calls must match exactly in sequence. If False, tool calls can be in any order (parallel evaluation).
Metric
dataclass
Metric(_required_columns: Dict[MetricType, Set[str]] = dict(), name: str = '')
Bases: ABC
Abstract base class for metrics in Ragas.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the metric. |
required_columns |
Dict[str, Set[str]]
|
A dictionary mapping metric type names to sets of required column names. This is
a property and raises |
score
Calculates the score for a single row of data.
Note
This method is deprecated and will be removed in 0.3. Please use single_turn_ascore or multi_turn_ascore instead.
Source code in src/ragas/metrics/base.py
ascore
async
Asynchronously calculates the score for a single row of data.
Note
This method is deprecated and will be removed in 0.3. Please use single_turn_ascore instead.
Source code in src/ragas/metrics/base.py
MetricType
Bases: Enum
Enumeration of metric types in Ragas.
Attributes:
| Name | Type | Description |
|---|---|---|
SINGLE_TURN |
str
|
Represents a single-turn metric type. |
MULTI_TURN |
str
|
Represents a multi-turn metric type. |
MetricWithLLM
dataclass
MetricWithLLM(_required_columns: Dict[MetricType, Set[str]] = dict(), name: str = '', llm: Optional[BaseRagasLLM] = None, output_type: Optional[MetricOutputType] = None)
Bases: Metric, PromptMixin
A metric class that uses a language model for evaluation.
Attributes:
| Name | Type | Description |
|---|---|---|
llm |
Optional[BaseRagasLLM]
|
The language model used for the metric. |
train
train(path: str, demonstration_config: Optional[DemonstrationConfig] = None, instruction_config: Optional[InstructionConfig] = None, callbacks: Optional[Callbacks] = None, run_config: Optional[RunConfig] = None, batch_size: Optional[int] = None, with_debugging_logs=False, raise_exceptions: bool = True) -> None
Train the metric using local JSON data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to local JSON training data file |
required |
demonstration_config
|
DemonstrationConfig
|
Configuration for demonstration optimization |
None
|
instruction_config
|
InstructionConfig
|
Configuration for instruction optimization |
None
|
callbacks
|
Callbacks
|
List of callback functions |
None
|
run_config
|
RunConfig
|
Run configuration |
None
|
batch_size
|
int
|
Batch size for training |
None
|
with_debugging_logs
|
bool
|
Enable debugging logs |
False
|
raise_exceptions
|
bool
|
Whether to raise exceptions during training |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If path is not provided or not a JSON file |
Source code in src/ragas/metrics/base.py
MultiTurnMetric
dataclass
MultiTurnMetric(_required_columns: Dict[MetricType, Set[str]] = dict(), name: str = '')
Bases: Metric
A metric class for evaluating multi-turn conversations.
This class extends the base Metric class to provide functionality for scoring multi-turn conversation samples.
multi_turn_score
multi_turn_score(sample: MultiTurnSample, callbacks: Callbacks = None) -> float
Score a multi-turn conversation sample synchronously.
May raise ImportError if nest_asyncio is not installed in Jupyter-like environments.
Source code in src/ragas/metrics/base.py
multi_turn_ascore
async
multi_turn_ascore(sample: MultiTurnSample, callbacks: Callbacks = None, timeout: Optional[float] = None) -> float
Score a multi-turn conversation sample asynchronously.
May raise asyncio.TimeoutError if the scoring process exceeds the specified timeout.
Source code in src/ragas/metrics/base.py
BaseMetric
dataclass
Bases: ABC
Base class for simple metrics that return MetricResult objects.
LLMMetric
dataclass
LLMMetric(name: str, allowed_values: AllowedValuesType = (lambda: ['pass', 'fail'])(), prompt: Optional[Union[str, 'Prompt']] = None)
Bases: SimpleBaseMetric
LLM-based metric that uses prompts to generate structured responses.
save
Save the metric configuration to a JSON file.
Parameters:
path : str, optional File path to save to. If not provided, saves to "./{metric.name}.json" Use .gz extension for compression.
Note:
If the metric has a response_model, its schema will be saved for reference but the model itself cannot be serialized. You'll need to provide it when loading.
Examples:
All these work:
metric.save() # → ./response_quality.json metric.save("custom.json") # → ./custom.json metric.save("/path/to/metrics/") # → /path/to/metrics/response_quality.json metric.save("no_extension") # → ./no_extension.json metric.save("compressed.json.gz") # → ./compressed.json.gz (compressed)
Source code in src/ragas/metrics/base.py
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 | |
load
classmethod
load(path: str, response_model: Optional[Type['BaseModel']] = None, embedding_model: Optional['EmbeddingModelType'] = None) -> 'SimpleLLMMetric'
Load a metric from a JSON file.
Parameters:
path : str File path to load from. Supports .gz compressed files. response_model : Optional[Type[BaseModel]] Pydantic model to use for response validation. Required for custom SimpleLLMMetrics. embedding_model : Optional[Any] Embedding model for DynamicFewShotPrompt. Required if the original used one.
Returns:
SimpleLLMMetric Loaded metric instance
Raises:
ValueError If file cannot be loaded, is invalid, or missing required models
Source code in src/ragas/metrics/base.py
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 | |
get_correlation
abstractmethod
Calculate the correlation between gold scores and predicted scores. This is a placeholder method and should be implemented based on the specific metric.
Source code in src/ragas/metrics/base.py
align_and_validate
align_and_validate(dataset: 'Dataset', embedding_model: 'EmbeddingModelType', llm: 'BaseRagasLLM', test_size: float = 0.2, random_state: int = 42, **kwargs: Dict[str, Any])
Args: dataset: experiment to align the metric with. embedding_model: The embedding model used for dynamic few-shot prompting. llm: The LLM instance to use for scoring.
Align the metric with the specified experiments and validate it against a gold standard experiment. This method combines alignment and validation into a single step.
Source code in src/ragas/metrics/base.py
align
Args: train_dataset: train_dataset to align the metric with. embedding_model: The embedding model used for dynamic few-shot prompting.
Align the metric with the specified experiments by different optimization methods.
Source code in src/ragas/metrics/base.py
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 | |
validate_alignment
Args: llm: The LLM instance to use for scoring. test_dataset: An Dataset instance containing the gold standard scores. mapping: A dictionary mapping variable names expected by metrics to their corresponding names in the gold experiment.
Validate the alignment of the metric by comparing the scores against a gold standard experiment. This method computes the Cohen's Kappa score and agreement rate between the gold standard scores and the predicted scores from the metric.
Source code in src/ragas/metrics/base.py
SingleTurnMetric
dataclass
SingleTurnMetric(_required_columns: Dict[MetricType, Set[str]] = dict(), name: str = '')
Bases: Metric
A metric class for evaluating single-turn interactions.
This class provides methods to score single-turn samples, both synchronously and asynchronously.
single_turn_score
single_turn_score(sample: SingleTurnSample, callbacks: Callbacks = None) -> float
Synchronously score a single-turn sample.
May raise ImportError if nest_asyncio is not installed in a Jupyter-like environment.
Source code in src/ragas/metrics/base.py
single_turn_ascore
async
single_turn_ascore(sample: SingleTurnSample, callbacks: Callbacks = None, timeout: Optional[float] = None) -> float
Asynchronously score a single-turn sample with an optional timeout.
May raise asyncio.TimeoutError if the scoring process exceeds the specified timeout.
Source code in src/ragas/metrics/base.py
DiscreteMetric
dataclass
DiscreteMetric(name: str, allowed_values: List[str] = (lambda: ['pass', 'fail'])(), prompt: Optional[Union[str, 'Prompt']] = None)
Bases: SimpleLLMMetric, DiscreteValidator
get_correlation
Calculate the correlation between gold labels and predictions. This is a placeholder method and should be implemented based on the specific metric.
Source code in src/ragas/metrics/discrete.py
load
classmethod
load(path: str, embedding_model: Optional[EmbeddingModelType] = None) -> DiscreteMetric
Load a DiscreteMetric from a JSON file.
Parameters:
path : str File path to load from. Supports .gz compressed files. embedding_model : Optional[Any] Embedding model for DynamicFewShotPrompt. Required if the original used one.
Returns:
DiscreteMetric Loaded metric instance
Raises:
ValueError If file cannot be loaded or is not a DiscreteMetric
Source code in src/ragas/metrics/discrete.py
NumericMetric
dataclass
NumericMetric(name: str, allowed_values: Union[Tuple[float, float], range] = (0.0, 1.0), prompt: Optional[Union[str, 'Prompt']] = None)
Bases: SimpleLLMMetric, NumericValidator
get_correlation
Calculate the correlation between gold labels and predictions. This is a placeholder method and should be implemented based on the specific metric.
Source code in src/ragas/metrics/numeric.py
load
classmethod
load(path: str, embedding_model: Optional[EmbeddingModelType] = None) -> NumericMetric
Load a NumericMetric from a JSON file.
Parameters:
path : str File path to load from. Supports .gz compressed files. embedding_model : Optional[Any] Embedding model for DynamicFewShotPrompt. Required if the original used one.
Returns:
NumericMetric Loaded metric instance
Raises:
ValueError If file cannot be loaded or is not a NumericMetric
Source code in src/ragas/metrics/numeric.py
RankingMetric
dataclass
Bases: SimpleLLMMetric, RankingValidator
get_correlation
Calculate the correlation between gold labels and predictions. This is a placeholder method and should be implemented based on the specific metric.
Source code in src/ragas/metrics/ranking.py
load
classmethod
load(path: str, embedding_model: Optional[EmbeddingModelType] = None) -> RankingMetric
Load a RankingMetric from a JSON file.
Parameters:
path : str File path to load from. Supports .gz compressed files. embedding_model : Optional[Any] Embedding model for DynamicFewShotPrompt. Required if the original used one.
Returns:
RankingMetric Loaded metric instance
Raises:
ValueError If file cannot be loaded or is not a RankingMetric
Source code in src/ragas/metrics/ranking.py
MetricResult
Class to hold the result of a metric evaluation.
This class behaves like its underlying result value but still provides access to additional metadata like reasoning.
Works with: - DiscreteMetrics (string results) - NumericMetrics (float/int results) - RankingMetrics (list results)
Source code in src/ragas/metrics/result.py
to_dict
validate
classmethod
Provide compatibility with older Pydantic versions.
discrete_metric
discrete_metric(*, name: Optional[str] = None, allowed_values: Optional[List[str]] = None, **metric_params) -> Callable[[Callable[..., Any]], DiscreteMetricProtocol]
Decorator for creating discrete metrics.
Args: name: Optional name for the metric (defaults to function name) allowed_values: List of allowed string values for the metric **metric_params: Additional parameters for the metric
Returns: A decorator that transforms a function into a DiscreteMetric instance
Source code in src/ragas/metrics/discrete.py
numeric_metric
numeric_metric(*, name: Optional[str] = None, allowed_values: Optional[Union[Tuple[float, float], range]] = None, **metric_params) -> Callable[[Callable[..., Any]], NumericMetricProtocol]
Decorator for creating numeric metrics.
Args: name: Optional name for the metric (defaults to function name) allowed_values: Tuple specifying (min, max) range or range object for valid values **metric_params: Additional parameters for the metric
Returns: A decorator that transforms a function into a NumericMetric instance
Source code in src/ragas/metrics/numeric.py
ranking_metric
ranking_metric(*, name: Optional[str] = None, allowed_values: Optional[int] = None, **metric_params) -> Callable[[Callable[..., Any]], RankingMetricProtocol]
Decorator for creating ranking metrics.
Args: name: Optional name for the metric (defaults to function name) allowed_values: Expected length of the returned ranking list **metric_params: Additional parameters for the metric
Returns: A decorator that transforms a function into a RankingMetric instance