You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: make general purpose metrics more general (#1666)
## Metrics Converted
- [x] Aspect Critic
- [x] Simple Criteria
- [x] Rubric Based - both Instance and Domain specific
a few different examples
### Aspect Critic
```py
from ragas.metrics import AspectCritic
from ragas.dataset_schema import SingleTurnSample
only_response = SingleTurnSample(
response="The Eiffel Tower is located in Paris."
)
grammar_critic = AspectCritic(
name="grammar",
definition="Is the response grammatically correct?",
llm=evaluator_llm
)
await grammar_critic.single_turn_ascore(only_response)
```
with reference
```py
answer_correctness_critic = AspectCritic(
name="answer_correctness",
definition="Is the response and reference answer are the same?",
llm=evaluator_llm
)
# data row
sample = SingleTurnSample(
user_input="Where is the Eiffel Tower located?",
response="The Eiffel Tower is located in Paris.",
reference="London"
)
await answer_correctness_critic.single_turn_ascore(sample)
```
**Note:** this only works for multi-turn metrics for now
@@ -6,7 +6,6 @@ General purpose evaluation metrics are used to evaluate any given task.
6
6
7
7
`AspectCritic` is an evaluation metric that can be used to evaluate responses based on predefined aspects in free form natural language. The output of aspect critiques is binary, indicating whether the submission aligns with the defined aspect or not.
8
8
9
-
**Without reference**
10
9
11
10
### Example
12
11
@@ -28,32 +27,6 @@ scorer = AspectCritic(
28
27
await scorer.single_turn_ascore(sample)
29
28
```
30
29
31
-
**With reference**
32
-
33
-
### Example
34
-
35
-
```python
36
-
from ragas.dataset_schema import SingleTurnSample
37
-
from ragas.metrics import AspectCriticWithReference
38
-
39
-
40
-
sample = SingleTurnSample(
41
-
user_input="Where is the Eiffel Tower located?",
42
-
response="The Eiffel Tower is located in Paris.",
43
-
reference="The Eiffel Tower is located in Paris.",
44
-
)
45
-
46
-
scorer = AspectCritic(
47
-
name="correctness",
48
-
definition="Is the response factually similar to the reference?",
49
-
llm=evaluator_llm
50
-
51
-
)
52
-
53
-
await scorer.single_turn_ascore(sample)
54
-
55
-
```
56
-
57
30
### How it works
58
31
59
32
Critics are essentially basic LLM calls using the defined criteria. For example, let's see how the harmfulness critic works:
@@ -74,41 +47,22 @@ Critics are essentially basic LLM calls using the defined criteria. For example,
74
47
75
48
Course graned evaluation method is an evaluation metric that can be used to score (integer) responses based on predefined single free form scoring criteria. The output of course grained evaluation is a integer score between the range specified in the criteria.
76
49
77
-
**Without Reference**
78
-
79
-
```python
80
-
from ragas.dataset_schema import SingleTurnSample
81
-
from ragas.metrics import SimpleCriteriaScoreWithoutReference
Domain specific evaluation metric is a rubric-based evaluation metric that is used to evaluate responses on a specific domain. The rubric consists of descriptions for each score, typically ranging from 1 to 5. The response here is evaluation and scored using the LLM using description specified in the rubric. This metric also have reference free and reference based variations.
119
73
120
-
### With Reference
121
-
122
-
Used when you have reference answer to evaluate the responses against.
123
-
124
74
#### Example
125
75
```python
126
76
from ragas.dataset_schema import SingleTurnSample
127
-
from ragas.metrics importRubricsScoreWithReference
77
+
from ragas.metrics importRubricsScore
128
78
sample = SingleTurnSample(
129
79
user_input="Where is the Eiffel Tower located?",
130
80
response="The Eiffel Tower is located in Paris.",
@@ -137,67 +87,18 @@ rubrics = {
137
87
"score4_description": "The response is mostly accurate and aligns well with the ground truth, with only minor issues or missing details.",
138
88
"score5_description": "The response is fully accurate, aligns completely with the ground truth, and is clear and detailed.",
Instance specific evaluation metric is a rubric-based evaluation metric that is used to evaluate responses on a specific instance, ie each instance to be evaluated is annotated with a rubric based evaluation criteria. The rubric consists of descriptions for each score, typically ranging from 1 to 5. The response here is evaluation and scored using the LLM using description specified in the rubric. This metric also have reference free and reference based variations. This scoring method is useful when evaluating each instance in your dataset required high amount of customized evaluation criteria.
165
97
166
-
### With Reference
167
-
168
-
Used when you have reference answer to evaluate the responses against.
169
-
170
-
#### Example
171
-
```python
172
-
from ragas.dataset_schema import SingleTurnSample
173
-
from ragas.metrics import InstanceRubricsWithReference
174
-
175
-
176
-
SingleTurnSample(
177
-
user_input="Where is the Eiffel Tower located?",
178
-
response="The Eiffel Tower is located in Paris.",
179
-
reference="The Eiffel Tower is located in Paris.",
180
-
rubrics= {
181
-
"score1": "The response is completely incorrect or irrelevant (e.g., 'The Eiffel Tower is in London.' or no mention of the Eiffel Tower).",
182
-
"score2": "The response mentions the Eiffel Tower but gives the wrong location or vague information (e.g., 'The Eiffel Tower is in Europe.' or 'It is in France.' without specifying Paris).",
183
-
"score3": "The response provides the correct city but with minor factual or grammatical issues (e.g., 'The Eiffel Tower is in Paris, Germany.' or 'The tower is located at Paris.').",
184
-
"score4": "The response is correct but lacks some clarity or extra detail (e.g., 'The Eiffel Tower is in Paris, France.' without other useful context or slightly awkward phrasing).",
185
-
"score5": "The response is fully correct and matches the reference exactly (e.g., 'The Eiffel Tower is located in Paris.' with no errors or unnecessary details)."
Copy file name to clipboardExpand all lines: docs/howtos/customizations/testgenerator/_persona_generator.md
+12-4Lines changed: 12 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,9 +14,18 @@ Which we can define as follows:
14
14
```python
15
15
from ragas.testset.persona import Persona
16
16
17
-
persona_new_joinee = Persona(name="New Joinee", role_description="Don't know much about the company and is looking for information on how to get started.")
18
-
persona_manager = Persona(name="Manager", role_description="Wants to know about the different teams and how they collaborate with each other.")
19
-
persona_senior_manager = Persona(name="Senior Manager", role_description="Wants to know about the company vision and how it is executed.")
17
+
persona_new_joinee = Persona(
18
+
name="New Joinee",
19
+
role_description="Don't know much about the company and is looking for information on how to get started.",
20
+
)
21
+
persona_manager = Persona(
22
+
name="Manager",
23
+
role_description="Wants to know about the different teams and how they collaborate with each other.",
24
+
)
25
+
persona_senior_manager = Persona(
26
+
name="Senior Manager",
27
+
role_description="Wants to know about the company vision and how it is executed.",
28
+
)
20
29
21
30
personas = [persona_new_joinee, persona_manager, persona_senior_manager]
Copy file name to clipboardExpand all lines: docs/howtos/customizations/testgenerator/persona_generator.ipynb
+13-4Lines changed: 13 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -38,9 +38,18 @@
38
38
"source": [
39
39
"from ragas.testset.persona import Persona\n",
40
40
"\n",
41
-
"persona_new_joinee = Persona(name=\"New Joinee\", role_description=\"Don't know much about the company and is looking for information on how to get started.\")\n",
42
-
"persona_manager = Persona(name=\"Manager\", role_description=\"Wants to know about the different teams and how they collaborate with each other.\")\n",
43
-
"persona_senior_manager = Persona(name=\"Senior Manager\", role_description=\"Wants to know about the company vision and how it is executed.\")\n",
41
+
"persona_new_joinee = Persona(\n",
42
+
" name=\"New Joinee\",\n",
43
+
" role_description=\"Don't know much about the company and is looking for information on how to get started.\",\n",
44
+
")\n",
45
+
"persona_manager = Persona(\n",
46
+
" name=\"Manager\",\n",
47
+
" role_description=\"Wants to know about the different teams and how they collaborate with each other.\",\n",
48
+
")\n",
49
+
"persona_senior_manager = Persona(\n",
50
+
" name=\"Senior Manager\",\n",
51
+
" role_description=\"Wants to know about the company vision and how it is executed.\",\n",
Copy file name to clipboardExpand all lines: docs/howtos/integrations/langchain.ipynb
+25-4Lines changed: 25 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@
25
25
},
26
26
{
27
27
"cell_type": "code",
28
-
"execution_count": null,
28
+
"execution_count": 1,
29
29
"id": "fb5deb25",
30
30
"metadata": {},
31
31
"outputs": [],
@@ -59,10 +59,31 @@
59
59
},
60
60
{
61
61
"cell_type": "code",
62
-
"execution_count": null,
62
+
"execution_count": 2,
63
63
"id": "4aa9a986",
64
64
"metadata": {},
65
-
"outputs": [],
65
+
"outputs": [
66
+
{
67
+
"name": "stderr",
68
+
"output_type": "stream",
69
+
"text": [
70
+
"/home/jjmachan/.pyenv/versions/ragas/lib/python3.10/site-packages/langchain/indexes/vectorstore.py:128: UserWarning: Using InMemoryVectorStore as the default vectorstore.This memory store won't persist data. You should explicitlyspecify a vectorstore when using VectorstoreIndexCreator\n",
71
+
" warnings.warn(\n"
72
+
]
73
+
},
74
+
{
75
+
"ename": "ValidationError",
76
+
"evalue": "1 validation error for VectorstoreIndexCreator\nembedding\n Field required [type=missing, input_value={}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.9/v/missing",
"File \u001b[0;32m~/.pyenv/versions/ragas/lib/python3.10/site-packages/pydantic/main.py:212\u001b[0m, in \u001b[0;36mBaseModel.__init__\u001b[0;34m(self, **data)\u001b[0m\n\u001b[1;32m 210\u001b[0m \u001b[38;5;66;03m# `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks\u001b[39;00m\n\u001b[1;32m 211\u001b[0m __tracebackhide__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m--> 212\u001b[0m validated_self \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__pydantic_validator__\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalidate_python\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mself_instance\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 213\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m validated_self:\n\u001b[1;32m 214\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(\n\u001b[1;32m 215\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA custom validator is returning a value other than `self`.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 216\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mReturning anything other than `self` from a top level model validator isn\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt supported when validating via `__init__`.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 217\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mSee the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m 218\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 219\u001b[0m )\n",
83
+
"\u001b[0;31mValidationError\u001b[0m: 1 validation error for VectorstoreIndexCreator\nembedding\n Field required [type=missing, input_value={}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.9/v/missing"
0 commit comments