-
Notifications
You must be signed in to change notification settings - Fork 120
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
Adds ability to use functions for prompt definition #207
Closed
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
58fee65
add function prompt assigment
hynky1999 57d40f9
add json casting
hynky1999 cb8be21
fix ruff setting + fmt
hynky1999 f275f60
Merge branch 'main' into function_prompts
clefourrier cde6c04
Merge branch 'main' into function_prompts
clefourrier c656d64
Merge branch 'main' into function_prompts
clefourrier 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
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 |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
from dataclasses import dataclass | ||
from multiprocessing import Pool | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING, List, Optional, Tuple, Union | ||
from typing import TYPE_CHECKING, Callable, List, Optional, Tuple, Union | ||
|
||
from datasets import load_dataset | ||
|
||
|
@@ -61,6 +61,8 @@ | |
if TYPE_CHECKING: | ||
from lighteval.logging.evaluation_tracker import EvaluationTracker | ||
|
||
FormatterType = Callable[[dict, str], Doc] | ||
|
||
|
||
@dataclass | ||
class LightevalTaskConfig: | ||
|
@@ -89,7 +91,7 @@ class LightevalTaskConfig: | |
""" | ||
|
||
name: str | ||
prompt_function: str | ||
prompt_function: FormatterType | str | ||
hf_repo: str | ||
hf_subset: str | ||
metric: Tuple[Union[str, Metrics]] | ||
|
@@ -149,6 +151,38 @@ def __post_init__(self): | |
self.stop_sequence = tuple(self.stop_sequence) if self.stop_sequence is not None else None | ||
|
||
|
||
def load_prompt_function(prompt_function: str, custom_tasks_module: list | None) -> FormatterType: | ||
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. I personally find it less readable with a custom type, we'll see if we keep it through time |
||
""" | ||
Tries to load the prompt function defined as string. | ||
Arguments: | ||
prompt_function (str): Name of the prompt function to load. | ||
custom_tasks_module (list): List of custom modules to search for the prompt function. | ||
Returns: | ||
FormatterType: The prompt function. | ||
""" | ||
|
||
if custom_tasks_module is None: | ||
return getattr(tasks_prompt_formatting, prompt_function) | ||
|
||
formatter = [] | ||
for module in custom_tasks_module: | ||
if hasattr(module, prompt_function): | ||
formatter.append(getattr(module, prompt_function)) | ||
|
||
if len(formatter) == 0: # Default version | ||
return getattr(tasks_prompt_formatting, prompt_function) | ||
elif len(formatter) == 1: | ||
# If we have a prompt in both the module and our tasks_prompt_formatting | ||
# We take the prompt from the module | ||
if hasattr(tasks_prompt_formatting, prompt_function): | ||
hlog_warn(f"Be careful you are using custom prompt function {prompt_function} and not the default one.") | ||
return formatter[0] | ||
else: | ||
raise Exception( | ||
f"You defined the prompt function {prompt_function} several times in the different custom modules you are loading." | ||
) | ||
|
||
|
||
class LightevalTask: | ||
def __init__( # noqa: C901 | ||
self, name: str, cfg: LightevalTaskConfig, cache_dir: Optional[str] = None, custom_tasks_module: list = None | ||
|
@@ -209,31 +243,12 @@ def __init__( # noqa: C901 | |
self.num_samples = [1] + [ | ||
int(metric.replace("maj_at_", "").split("_")[0]) for metric in self.metrics if "maj_at_" in metric | ||
] | ||
|
||
# Data processing | ||
# to use once prompt formatting is managed as a module | ||
if custom_tasks_module is None: | ||
self.formatter = getattr(tasks_prompt_formatting, cfg.prompt_function) | ||
self.formatter: FormatterType | ||
if isinstance(cfg.prompt_function, str): | ||
self.formatter = load_prompt_function(cfg.prompt_function, custom_tasks_module) | ||
else: | ||
formatter = [] | ||
for module in custom_tasks_module: | ||
if hasattr(module, cfg.prompt_function): | ||
formatter.append(getattr(module, cfg.prompt_function)) | ||
|
||
if len(formatter) == 0: # Default version | ||
self.formatter = getattr(tasks_prompt_formatting, cfg.prompt_function) | ||
elif len(formatter) == 1: | ||
# If we have a prompt in both the module and our tasks_prompt_formatting | ||
# We take the prompt from the module | ||
if hasattr(tasks_prompt_formatting, cfg.prompt_function): | ||
hlog_warn( | ||
f"Be careful you are using custom prompt function {cfg.prompt_function} and not the default one." | ||
) | ||
self.formatter = formatter[0] | ||
else: | ||
raise Exception( | ||
f"You defined the prompt function {cfg.prompt_function} several times in the different custom modules you are loading." | ||
) | ||
self.formatter = cfg.prompt_function | ||
|
||
self.generation_size = cfg.generation_size | ||
self.stop_sequence = cfg.stop_sequence | ||
self.output_regex = cfg.output_regex | ||
|
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.
Why do you include a string?
I don't see which prompt formater takes a string as input