-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add LlamaIndex faithfulness metric (#971)
* add LlamaIndex faithfulness metric Signed-off-by: Ariel Gera <ariel.gera1@ibm.com> * share code between LlamaIndex metrics Signed-off-by: Ariel Gera <ariel.gera1@ibm.com> * use existing 'score_prefix' field Signed-off-by: Ariel Gera <ariel.gera1@ibm.com> * remove unused field Signed-off-by: Ariel Gera <ariel.gera1@ibm.com> --------- Signed-off-by: Ariel Gera <ariel.gera1@ibm.com>
- Loading branch information
Showing
6 changed files
with
143 additions
and
94 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from unitxt import add_to_catalog | ||
from unitxt.metrics import LlamaIndexCorrectness, LlamaIndexFaithfulness | ||
from unitxt.test_utils.metrics import test_metric | ||
|
||
# Test with mock | ||
model_name = "mock" | ||
model_name_normalized = model_name.replace(".", "_").replace("-", "_") | ||
|
||
predictions = ["The right answer"] | ||
references = [["The right answer", "The wrong answer"]] | ||
task_data = [ | ||
{ | ||
"question": "question number 1", | ||
"contexts": ["context number 1"], | ||
# "reference_answers": ["The right answer", "The wrong answer"], | ||
}, | ||
] | ||
|
||
metric_classes = { | ||
"correctness": LlamaIndexCorrectness, | ||
"faithfulness": LlamaIndexFaithfulness, | ||
} | ||
|
||
for metric_name, metric_class in metric_classes.items(): | ||
metric = metric_class(model_name=model_name) | ||
|
||
score_name = f"{metric_name}_llama_index_by_{model_name_normalized}_judge" | ||
|
||
instance_targets = [ | ||
{ | ||
"score": 1.0, | ||
"score_name": score_name, | ||
score_name: 1.0, | ||
# "feedback": "The generated answer is fully correct and relevant to the user query, matching the reference answer exactly.", | ||
} | ||
] * len(predictions) | ||
|
||
global_target = { | ||
"score": 1.0, | ||
"score_name": score_name, | ||
score_name: 1.0, | ||
} | ||
|
||
outputs = test_metric( | ||
metric=metric, | ||
predictions=predictions, | ||
references=references, | ||
task_data=task_data, | ||
instance_targets=instance_targets, | ||
global_target=global_target, | ||
) | ||
|
||
# GPT model to catalog | ||
model_names = ["gpt-3.5-turbo", "mock"] | ||
for model_name in model_names: | ||
model_name_normalized = model_name.replace(".", "_").replace("-", "_") | ||
|
||
metric = ( | ||
metric_class(model_name=model_name, data_classification_policy=["public"]) | ||
if model_name != "mock" | ||
else metric_class(model_name=model_name) | ||
) | ||
|
||
add_to_catalog( | ||
metric, | ||
f"metrics.rag.{metric_name}.llama_index_by_{model_name_normalized}", | ||
overwrite=True, | ||
) |
5 changes: 4 additions & 1 deletion
5
src/unitxt/catalog/metrics/rag/correctness/llama_index_by_gpt_3_5_turbo.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"__type__": "llama_index_correctness", | ||
"model_name": "gpt-3.5-turbo" | ||
"model_name": "gpt-3.5-turbo", | ||
"data_classification_policy": [ | ||
"public" | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
src/unitxt/catalog/metrics/rag/faithfulness/llama_index_by_gpt_3_5_turbo.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"__type__": "llama_index_faithfulness", | ||
"model_name": "gpt-3.5-turbo", | ||
"data_classification_policy": [ | ||
"public" | ||
] | ||
} |
4 changes: 4 additions & 0 deletions
4
src/unitxt/catalog/metrics/rag/faithfulness/llama_index_by_mock.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"__type__": "llama_index_faithfulness", | ||
"model_name": "mock" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters