Skip to content

Commit

Permalink
Update the evaluation method to pass the dataset item to the scoring …
Browse files Browse the repository at this point in the history
…method (#698)

* Updated evaluate function
  • Loading branch information
jverre authored Nov 22, 2024
1 parent 66f0985 commit 399b5cc
Show file tree
Hide file tree
Showing 83 changed files with 2,496 additions and 16,720 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install opik pyarrow fsspec huggingface_hub --upgrade"
"%pip install opik pyarrow fsspec huggingface_hub --upgrade --quiet"
]
},
{
Expand All @@ -51,7 +51,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -98,8 +98,8 @@
" {\n",
" \"input\": x[\"question\"],\n",
" \"context\": [x[\"passage\"]],\n",
" \"output\": x[\"answer\"],\n",
" \"expected_output\": x[\"label\"],\n",
" \"llm_output\": x[\"answer\"],\n",
" \"expected_hallucination_label\": x[\"label\"],\n",
" }\n",
" for x in df.to_dict(orient=\"records\")\n",
"]\n",
Expand Down Expand Up @@ -139,7 +139,7 @@
" metric = Hallucination()\n",
" try:\n",
" metric_score = metric.score(\n",
" input=x[\"input\"], context=x[\"context\"], output=x[\"output\"]\n",
" input=x[\"input\"], context=x[\"context\"], output=x[\"llm_output\"]\n",
" )\n",
" hallucination_score = metric_score.value\n",
" hallucination_reason = metric_score.reason\n",
Expand All @@ -149,9 +149,8 @@
" hallucination_reason = str(e)\n",
"\n",
" return {\n",
" \"output\": \"FAIL\" if hallucination_score == 1 else \"PASS\",\n",
" \"hallucination_score\": \"FAIL\" if hallucination_score == 1 else \"PASS\",\n",
" \"hallucination_reason\": hallucination_reason,\n",
" \"reference\": x[\"expected_output\"],\n",
" }\n",
"\n",
"\n",
Expand All @@ -174,6 +173,10 @@
" task=evaluation_task,\n",
" scoring_metrics=[check_hallucinated_metric],\n",
" experiment_config=experiment_config,\n",
" scoring_key_mapping={\n",
" \"reference\": \"expected_hallucination_label\",\n",
" \"output\": \"hallucination_score\",\n",
" },\n",
")"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ For this guide we will be evaluating the Hallucination metric included in the LL

```python
%pip install opik pyarrow fsspec huggingface_hub --upgrade
%pip install opik pyarrow fsspec huggingface_hub --upgrade --quiet
```


Expand Down Expand Up @@ -60,8 +60,8 @@ dataset_records = [
{
"input": x["question"],
"context": [x["passage"]],
"output": x["answer"],
"expected_output": x["label"],
"llm_output": x["answer"],
"expected_hallucination_label": x["label"],
}
for x in df.to_dict(orient="records")
]
Expand Down Expand Up @@ -92,7 +92,7 @@ def evaluation_task(x: Dict):
metric = Hallucination()
try:
metric_score = metric.score(
input=x["input"], context=x["context"], output=x["output"]
input=x["input"], context=x["context"], output=x["llm_output"]
)
hallucination_score = metric_score.value
hallucination_reason = metric_score.reason
Expand All @@ -102,9 +102,8 @@ def evaluation_task(x: Dict):
hallucination_reason = str(e)

return {
"output": "FAIL" if hallucination_score == 1 else "PASS",
"hallucination_reason": hallucination_reason,
"reference": x["expected_output"],
"hallucination_score": "FAIL" if hallucination_score == 1 else "PASS",
"hallucination_reason": hallucination_reason
}


Expand All @@ -127,6 +126,7 @@ res = evaluate(
task=evaluation_task,
scoring_metrics=[check_hallucinated_metric],
experiment_config=experiment_config,
scoring_key_mapping={"reference": "expected_hallucination_label", "output": "hallucination_score"},
)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -73,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -142,6 +142,7 @@
"from opik.evaluation.metrics.llm_judges.moderation.template import generate_query\n",
"from typing import Dict\n",
"\n",
"\n",
"# Define the evaluation task\n",
"def evaluation_task(x: Dict):\n",
" metric = Moderation()\n",
Expand All @@ -157,10 +158,8 @@
" moderation_score = \"moderated\" if metric_score.value > 0.5 else \"not_moderated\"\n",
"\n",
" return {\n",
" \"output\": moderation_score,\n",
" \"moderation_score\": moderation_score,\n",
" \"moderation_reason\": moderation_reason,\n",
" \"reference\": x[\"expected_output\"],\n",
" }\n",
"\n",
"\n",
Expand All @@ -181,6 +180,7 @@
" task=evaluation_task,\n",
" scoring_metrics=[moderation_metric],\n",
" experiment_config=experiment_config,\n",
" scoring_key_mapping={\"reference\": \"expected_output\", \"output\": \"moderation_score\"},\n",
")"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,8 @@ def evaluation_task(x: Dict):
moderation_score = "moderated" if metric_score.value > 0.5 else "not_moderated"

return {
"output": moderation_score,
"moderation_score": moderation_score,
"moderation_reason": moderation_reason,
"reference": x["expected_output"],
}


Expand All @@ -134,6 +132,7 @@ res = evaluate(
task=evaluation_task,
scoring_metrics=[moderation_metric],
experiment_config=experiment_config,
scoring_key_mapping={"reference": "expected_output", "output": "moderation_score"},
)
```

Expand Down
Loading

0 comments on commit 399b5cc

Please sign in to comment.