-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add two criteria based direct llm judges #1527
Merged
+315
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2401573
Add criteria based direct judges
lilacheden e54c70e
Merge branch 'main' into criterias
yoavkatz e496c0f
Add criteria to catalog
lilacheden 56eba42
Move judges under rag.response_generation
lilacheden 7159e52
Merge branch 'main' into criterias
yoavkatz 9b946ee
fix typo in prompt
lilacheden 9759621
Merge remote-tracking branch 'origin/criterias' into criterias
lilacheden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
prepare/metrics/llm_as_judge/direct/llama_3_3_70b_instruct_adherence_completeness.py
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,129 @@ | ||
from unitxt import add_to_catalog | ||
from unitxt.inference import CrossProviderInferenceEngine | ||
from unitxt.llm_as_judge import LLMJudgeDirect | ||
from unitxt.llm_as_judge_constants import ( | ||
CriteriaWithOptions, | ||
) | ||
|
||
option_map = { | ||
"Excellent": 1.0, | ||
"Good": 0.75, | ||
"mediocre": 0.5, | ||
"Bad": 0.25, | ||
"Very Bad": 0, | ||
} | ||
|
||
# First, describe a judgement criteria | ||
adherence_criteria = CriteriaWithOptions.from_obj( | ||
{ | ||
"name": "adherence_with_format", | ||
"description": "The response aligns with the requested structure, style, or format (e.g., bullet points, headings, specific phrasing).", | ||
"options": [ | ||
{ | ||
"name": "Excellent", | ||
"description": "The response perfectly aligns with the requested structure, style, or format, with no deviations.", | ||
}, | ||
{ | ||
"name": "Good", | ||
"description": "The response aligns well with the requested structure, style, or format, with minor deviations that do not affect clarity or usability.", | ||
}, | ||
{ | ||
"name": "mediocre", | ||
"description": "The response generally follows the requested structure, style, or format, but noticeable inconsistencies or omissions are present.", | ||
}, | ||
{ | ||
"name": "Bad", | ||
"description": "The response only partially aligns with the requested structure, style, or format, with significant inconsistencies or a lack of adherence.", | ||
}, | ||
{ | ||
"name": "Very Bad", | ||
"description": "The response fails to align with the requested structure, style, or format.", | ||
}, | ||
], | ||
"option_map": option_map, | ||
} | ||
) | ||
add_to_catalog( | ||
adherence_criteria, | ||
f"metrics.llm_as_judge.direct.criterias.{adherence_criteria.name}", | ||
overwrite=True, | ||
) | ||
|
||
completeness_criteria = CriteriaWithOptions.from_obj( | ||
{ | ||
"name": "answer_completeness", | ||
"description": "The response is complete: all the aspects of the reference answer are addressed in the response. The " | ||
"response might use different phrasing or wording from the reference answer.", | ||
"options": [ | ||
{ | ||
"name": "Excellent", | ||
"description": "The response addresses all aspects of the reference answer.", | ||
}, | ||
{ | ||
"name": "Good", | ||
"description": "The response addresses most aspects of the reference answer, with minor omissions.", | ||
}, | ||
{ | ||
"name": "mediocre", | ||
"description": "The response covers the essential aspects of the reference answer but has notable omissions.", | ||
}, | ||
{ | ||
"name": "Bad", | ||
"description": "The response covers only a few aspects of the reference answer, with significant omissions.", | ||
}, | ||
{ | ||
"name": "Very Bad", | ||
"description": "The response fails to address the reference answer meaningfully, with most aspects omitted.", | ||
}, | ||
], | ||
"option_map": option_map, | ||
} | ||
) | ||
add_to_catalog( | ||
completeness_criteria, | ||
f"metrics.llm_as_judge.direct.criterias.{completeness_criteria.name}", | ||
overwrite=True, | ||
) | ||
|
||
|
||
# now = define the judge metric using the criteria | ||
adherence_metric = LLMJudgeDirect( | ||
inference_engine=CrossProviderInferenceEngine( # or your favorite inference model | ||
model="llama-3-3-70b-instruct", max_tokens=1024 | ||
), | ||
criteria=adherence_criteria, | ||
# the fields from the generation task to be presented to the judge. Those fields must be present | ||
# in the generation task so they can be embedded here | ||
context_fields={ | ||
"question": "question", | ||
"instructions": "metadata/template/instruction", | ||
}, | ||
criteria_field="criteria", | ||
generate_summaries=False, | ||
check_positional_bias=False, | ||
) | ||
add_to_catalog( | ||
adherence_metric, | ||
"metrics.rag.response_generation.adherence_with_format.llama_3_3_70b_instruct_judge", | ||
overwrite=True, | ||
) | ||
|
||
# now = define the judge metric using the criteria | ||
completeness_metric = LLMJudgeDirect( | ||
inference_engine=CrossProviderInferenceEngine( # or your favorite inference model | ||
model="llama-3-3-70b-instruct", max_tokens=1024 | ||
), | ||
criteria=completeness_criteria, | ||
# the fields from the generation task to be presented to the judge. Those fields must be present | ||
# in the generation task so they can be embedded here | ||
context_fields={"question": "question", "reference_answers": "reference_answers"}, | ||
criteria_field="criteria", | ||
generate_summaries=False, | ||
check_positional_bias=False, | ||
) | ||
|
||
add_to_catalog( | ||
completeness_metric, | ||
"metrics.rag.response_generation.answer_completeness.llama_3_3_70b_instruct_judge", | ||
overwrite=True, | ||
) |
39 changes: 39 additions & 0 deletions
39
src/unitxt/catalog/metrics/llm_as_judge/direct/criterias/adherence_with_format.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,39 @@ | ||
{ | ||
"__type__": "criteria_with_options", | ||
"name": "adherence_with_format", | ||
"description": "The response aligns with the requested structure, style, or format (e.g., bullet points, headings, specific phrasing).", | ||
"options": [ | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Excellent", | ||
"description": "The response perfectly aligns with the requested structure, style, or format, with no deviations." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Good", | ||
"description": "The response aligns well with the requested structure, style, or format, with minor deviations that do not affect clarity or usability." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "mediocre", | ||
"description": "The response generally follows the requested structure, style, or format, but noticeable inconsistencies or omissions are present." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Bad", | ||
"description": "The response only partially aligns with the requested structure, style, or format, with significant inconsistencies or a lack of adherence." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Very Bad", | ||
"description": "The response fails to align with the requested structure, style, or format." | ||
} | ||
], | ||
"option_map": { | ||
"Excellent": 1.0, | ||
"Good": 0.75, | ||
"mediocre": 0.5, | ||
"Bad": 0.25, | ||
"Very Bad": 0 | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/unitxt/catalog/metrics/llm_as_judge/direct/criterias/answer_completeness.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,39 @@ | ||
{ | ||
"__type__": "criteria_with_options", | ||
"name": "answer_completeness", | ||
"description": "The response is complete: all the aspects of the reference answer are addressed in the response. The response might use different phrasing or wording from the reference answer.", | ||
"options": [ | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Excellent", | ||
"description": "The response addresses all aspects of the reference answer." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Good", | ||
"description": "The response addresses most aspects of the reference answer, with minor omissions." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "mediocre", | ||
"description": "The response covers the essential aspects of the reference answer but has notable omissions." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Bad", | ||
"description": "The response covers only a few aspects of the reference answer, with significant omissions." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Very Bad", | ||
"description": "The response fails to address the reference answer meaningfully, with most aspects omitted." | ||
} | ||
], | ||
"option_map": { | ||
"Excellent": 1.0, | ||
"Good": 0.75, | ||
"mediocre": 0.5, | ||
"Bad": 0.25, | ||
"Very Bad": 0 | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...g/metrics/rag/response_generation/adherence_with_format/llama_3_3_70b_instruct_judge.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,54 @@ | ||
{ | ||
"__type__": "llm_judge_direct", | ||
"inference_engine": { | ||
"__type__": "cross_provider_inference_engine", | ||
"model": "llama-3-3-70b-instruct", | ||
"max_tokens": 1024 | ||
}, | ||
"criteria": { | ||
"__type__": "criteria_with_options", | ||
"name": "adherence_with_format", | ||
"description": "The response aligns with the requested structure, style, or format (e.g., bullet points, headings, specific phrasing).", | ||
"options": [ | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Excellent", | ||
"description": "The response perfectly aligns with the requested structure, style, or format, with no deviations." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Good", | ||
"description": "The response aligns well with the requested structure, style, or format, with minor deviations that do not affect clarity or usability." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "mediocre", | ||
"description": "The response generally follows the requested structure, style, or format, but noticeable inconsistencies or omissions are present." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Bad", | ||
"description": "The response only partially aligns with the requested structure, style, or format, with significant inconsistencies or a lack of adherence." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Very Bad", | ||
"description": "The response fails to align with the requested structure, style, or format." | ||
} | ||
], | ||
"option_map": { | ||
"Excellent": 1.0, | ||
"Good": 0.75, | ||
"mediocre": 0.5, | ||
"Bad": 0.25, | ||
"Very Bad": 0 | ||
} | ||
}, | ||
"context_fields": { | ||
"question": "question", | ||
"instructions": "metadata/template/instruction" | ||
}, | ||
"criteria_field": "criteria", | ||
"generate_summaries": false, | ||
"check_positional_bias": false | ||
} |
54 changes: 54 additions & 0 deletions
54
...log/metrics/rag/response_generation/answer_completeness/llama_3_3_70b_instruct_judge.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,54 @@ | ||
{ | ||
"__type__": "llm_judge_direct", | ||
"inference_engine": { | ||
"__type__": "cross_provider_inference_engine", | ||
"model": "llama-3-3-70b-instruct", | ||
"max_tokens": 1024 | ||
}, | ||
"criteria": { | ||
"__type__": "criteria_with_options", | ||
"name": "answer_completeness", | ||
"description": "The response is complete: all the aspects of the reference answer are addressed in the response. The response might use different phrasing or wording from the reference answer.", | ||
"options": [ | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Excellent", | ||
"description": "The response addresses all aspects of the reference answer." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Good", | ||
"description": "The response addresses most aspects of the reference answer, with minor omissions." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "mediocre", | ||
"description": "The response covers the essential aspects of the reference answer but has notable omissions." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Bad", | ||
"description": "The response covers only a few aspects of the reference answer, with significant omissions." | ||
}, | ||
{ | ||
"__type__": "criteria_option", | ||
"name": "Very Bad", | ||
"description": "The response fails to address the reference answer meaningfully, with most aspects omitted." | ||
} | ||
], | ||
"option_map": { | ||
"Excellent": 1.0, | ||
"Good": 0.75, | ||
"mediocre": 0.5, | ||
"Bad": 0.25, | ||
"Very Bad": 0 | ||
} | ||
}, | ||
"context_fields": { | ||
"question": "question", | ||
"reference_answers": "reference_answers" | ||
}, | ||
"criteria_field": "criteria", | ||
"generate_summaries": false, | ||
"check_positional_bias": false | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Criteria should be added here as catalog entries:
https://www.unitxt.ai/en/latest/catalog/catalog.metrics.llm_as_judge.direct.criterias.__dir__.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So they can be reused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yoavkatz - ok, added the criteria to the catalog