-
Notifications
You must be signed in to change notification settings - Fork 845
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
[Evals API][8/n] AnswerParsingScoringFn for MMLU #352
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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 |
---|---|---|
|
@@ -13,23 +13,20 @@ | |
from llama_stack.apis.datasets import * # noqa: F403 | ||
from llama_stack.apis.inference.inference import Inference | ||
from llama_stack.providers.datatypes import ScoringFunctionsProtocolPrivate | ||
from llama_stack.providers.impls.meta_reference.scoring.scoring_fn.equality_scoring_fn import ( | ||
EqualityScoringFn, | ||
) | ||
|
||
from llama_stack.providers.impls.meta_reference.scoring.scoring_fn.llm_as_judge_scoring_fn import ( | ||
LlmAsJudgeScoringFn, | ||
) | ||
|
||
from llama_stack.providers.impls.meta_reference.scoring.scoring_fn.subset_of_scoring_fn import ( | ||
SubsetOfScoringFn, | ||
) | ||
|
||
from .config import MetaReferenceScoringConfig | ||
from .scoring_fn.answer_parsing_scoring_fn import AnswerParsingScoringFn | ||
from .scoring_fn.equality_scoring_fn import EqualityScoringFn | ||
from .scoring_fn.llm_as_judge_scoring_fn import LlmAsJudgeScoringFn | ||
from .scoring_fn.subset_of_scoring_fn import SubsetOfScoringFn | ||
|
||
FIXED_FNS = [EqualityScoringFn, SubsetOfScoringFn] | ||
|
||
LLM_JUDGE_FNS = [LlmAsJudgeScoringFn] | ||
# Scoring functions with context that can be registered | ||
REGISTERABLE_SCORING_FNS = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each Registerable ScoringFn is mapped to a ScoringContextType, So that we are able to register a scoring function with custom judge_prompt/answer_extraction regex. |
||
ScoringContextType.llm_as_judge.value: LlmAsJudgeScoringFn, | ||
ScoringContextType.answer_parsing.value: AnswerParsingScoringFn, | ||
} | ||
|
||
|
||
class MetaReferenceScoringImpl(Scoring, ScoringFunctionsProtocolPrivate): | ||
|
@@ -44,18 +41,24 @@ def __init__( | |
self.datasetio_api = datasetio_api | ||
self.datasets_api = datasets_api | ||
self.inference_api = inference_api | ||
# keep track of scoring function id to impls | ||
self.scoring_fn_id_impls = {} | ||
# registerable scoring fn context to impls | ||
self.registerable_scoring_fn_impls = {} | ||
|
||
async def initialize(self) -> None: | ||
for x in FIXED_FNS: | ||
impl = x() | ||
for fn_defs in impl.get_supported_scoring_fn_defs(): | ||
self.scoring_fn_id_impls[fn_defs.identifier] = impl | ||
for x in LLM_JUDGE_FNS: | ||
impl = x(inference_api=self.inference_api) | ||
for context_type, impl_cls in REGISTERABLE_SCORING_FNS.items(): | ||
if context_type == ScoringContextType.llm_as_judge.value: | ||
impl = impl_cls(inference_api=self.inference_api) | ||
else: | ||
impl = impl_cls() | ||
for fn_defs in impl.get_supported_scoring_fn_defs(): | ||
self.scoring_fn_id_impls[fn_defs.identifier] = impl | ||
self.llm_as_judge_fn = impl | ||
self.registerable_scoring_fn_impls[context_type] = impl | ||
|
||
async def shutdown(self) -> None: ... | ||
|
||
|
@@ -74,8 +77,12 @@ async def list_scoring_functions(self) -> List[ScoringFnDef]: | |
return scoring_fn_defs_list | ||
|
||
async def register_scoring_function(self, function_def: ScoringFnDef) -> None: | ||
self.llm_as_judge_fn.register_scoring_fn_def(function_def) | ||
self.scoring_fn_id_impls[function_def.identifier] = self.llm_as_judge_fn | ||
assert ( | ||
function_def.context is not None | ||
), "Only ScoringFnDef with context set can be registered" | ||
fn_impl = self.registerable_scoring_fn_impls[function_def.context.type] | ||
fn_impl.register_scoring_fn_def(function_def) | ||
self.scoring_fn_id_impls[function_def.identifier] = fn_impl | ||
|
||
async def validate_scoring_input_dataset_schema(self, dataset_id: str) -> None: | ||
dataset_def = await self.datasets_api.get_dataset(dataset_identifier=dataset_id) | ||
|
61 changes: 61 additions & 0 deletions
61
llama_stack/providers/impls/meta_reference/scoring/scoring_fn/answer_parsing_scoring_fn.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,61 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the terms described in the LICENSE file in | ||
# the root directory of this source tree. | ||
import re | ||
|
||
from .base_scoring_fn import BaseScoringFn | ||
from llama_stack.apis.scoring_functions import * # noqa: F401, F403 | ||
from llama_stack.apis.scoring import * # noqa: F401, F403 | ||
from llama_stack.apis.common.type_system import * # noqa: F403 | ||
from .common import aggregate_accuracy | ||
|
||
from .fn_defs.answer_parsing_multiple_choice import answer_parsing_multiple_choice | ||
|
||
|
||
class AnswerParsingScoringFn(BaseScoringFn): | ||
""" | ||
A scoring_fn that parses answer from generated response according to context and check match with expected_answer. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
self.supported_fn_defs_registry = { | ||
answer_parsing_multiple_choice.identifier: answer_parsing_multiple_choice, | ||
} | ||
|
||
async def score_row( | ||
self, | ||
input_row: Dict[str, Any], | ||
scoring_fn_identifier: Optional[str] = None, | ||
) -> ScoringResultRow: | ||
assert ( | ||
scoring_fn_identifier is not None | ||
), "Scoring function identifier not found." | ||
fn_def = self.supported_fn_defs_registry[scoring_fn_identifier] | ||
assert ( | ||
fn_def.context is not None | ||
and fn_def.context.type == ScoringContextType.answer_parsing.value | ||
), f"AnswerParsingContext not found for {fn_def}." | ||
|
||
expected_answer = input_row["expected_answer"] | ||
generated_answer = input_row["generated_answer"] | ||
|
||
# parse answer according to regex | ||
parsed_answer = None | ||
for regex in fn_def.context.parsing_regex: | ||
match = re.search(regex, generated_answer) | ||
if match: | ||
parsed_answer = match.group(1) | ||
break | ||
|
||
score = 1.0 if parsed_answer and parsed_answer == expected_answer else 0.0 | ||
return { | ||
"score": score, | ||
} | ||
|
||
async def aggregate( | ||
self, scoring_results: List[ScoringResultRow] | ||
) -> Dict[str, Any]: | ||
return aggregate_accuracy(scoring_results) |
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
69 changes: 69 additions & 0 deletions
69
...oviders/impls/meta_reference/scoring/scoring_fn/fn_defs/answer_parsing_multiple_choice.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,69 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the terms described in the LICENSE file in | ||
# the root directory of this source tree. | ||
|
||
from llama_stack.apis.scoring_functions import * # noqa: F401, F403 | ||
from llama_stack.apis.scoring import * # noqa: F401, F403 | ||
from llama_stack.apis.common.type_system import NumberType | ||
|
||
MULTILINGUAL_ANSWER_REGEXES = [ | ||
r"Answer\s*:", | ||
r"Answer\s*:", # Korean invisible character | ||
r"উত্তর\s*:", | ||
r"उत्तर\s*:", | ||
r"উত্তরঃ", | ||
r"উত্তর\s*:", | ||
r"Antwort\s*:", | ||
r"답변\s*:", | ||
r"정답\s*:", | ||
r"답\s*:", | ||
r"答案\s*:", | ||
r"答案\s*:", | ||
r"答\s*:", | ||
r"答\s*:", | ||
r"答复\s*:", | ||
r"答曰\s*:", | ||
r"الإجابة:", | ||
r"الجواب:", | ||
r"إجابة:", | ||
r"الإجابة النهائية:", | ||
r"الإجابة الصحيحة:", | ||
r"الإجابة الصحيحة هي:", | ||
r"الإجابة هي:", | ||
r"Respuesta\s*:", | ||
r"Risposta\s*:", | ||
r"答え\s*:", | ||
r"答え\s*:", | ||
r"回答\s*:", | ||
r"回答\s*:", | ||
r"解答\s*:", | ||
r"Jawaban\s*:", | ||
r"Réponse\s*:", | ||
r"Resposta\s*:", | ||
r"Jibu\s*:", | ||
r"Idahun\s*:", | ||
r"Ìdáhùn\s*:", | ||
r"Idáhùn\s*:", | ||
r"Àmọ̀nà\s*:", | ||
r"Àdáhùn\s*:", | ||
r"Ànúgọ\s*:", | ||
r"Àṣàyàn\s*:", | ||
] | ||
|
||
MULTILINGUAL_ANSWER_PATTERN_TEMPLATE = ( | ||
r"(?i){}\s*([A-D]|[أ-د]|[অ]|[ব]|[ড]|[ঢ]|[A]|[B]|[C]|[D])" | ||
) | ||
|
||
answer_parsing_multiple_choice = ScoringFnDef( | ||
identifier="meta-reference::answer_parsing_multiple_choice", | ||
description="Extract answer from response matching Answer: [the_answer_letter], and compare with expected result", | ||
return_type=NumberType(), | ||
context=AnswerParsingContext( | ||
parsing_regex=[ | ||
MULTILINGUAL_ANSWER_PATTERN_TEMPLATE.format(x) | ||
for x in MULTILINGUAL_ANSWER_REGEXES | ||
], | ||
), | ||
) |
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
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
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
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
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
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
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.
Removing
parameters
field as we can just usecontext
for defining parameters associated with the scoring function.