-
-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support user-defined prompt processing strategies for dpo (#1248)
* support user-defined prompt processing strategies for dpo * interpret dict dataset types as user-defined * fix lint errors * setup pydantic config for validation of User defined DPO --------- Co-authored-by: Wing Lian <[email protected]>
- Loading branch information
Showing
6 changed files
with
67 additions
and
9 deletions.
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
User-defined DPO strategies | ||
""" | ||
|
||
|
||
def default(cfg, dataset_idx=0, **kwargs): # pylint: disable=unused-argument | ||
ds_cfg = cfg["datasets"][dataset_idx]["type"] | ||
if not isinstance(ds_cfg, dict): | ||
raise ValueError( | ||
f"User-defined dataset type must be a dictionary. Got: {ds_cfg}" | ||
) | ||
field_prompt = ds_cfg.get("field_prompt", "prompt") | ||
field_system = ds_cfg.get("field_system", "system") | ||
field_chosen = ds_cfg.get("field_chosen", "chosen") | ||
field_rejected = ds_cfg.get("field_rejected", "rejected") | ||
prompt_format = ds_cfg.get("prompt_format") | ||
if not prompt_format: | ||
prompt_format = "{" + field_prompt + "}" | ||
chosen_format = ds_cfg.get("chosen_format") | ||
if not chosen_format: | ||
chosen_format = "{" + field_chosen + "}" | ||
rejected_format = ds_cfg.get("rejected_format") | ||
if not rejected_format: | ||
rejected_format = "{" + field_rejected + "}" | ||
|
||
def transform_fn(sample): | ||
if ( | ||
"{" + field_system + "}" in prompt_format | ||
and field_system in sample | ||
and sample[field_system] | ||
): | ||
sample["prompt"] = prompt_format.format( | ||
system=sample[field_system], prompt=sample[field_prompt] | ||
) | ||
else: | ||
sample["prompt"] = prompt_format.format(prompt=sample["prompt"]) | ||
sample["chosen"] = chosen_format.format(chosen=sample[field_chosen]) | ||
sample["rejected"] = rejected_format.format(rejected=sample[field_rejected]) | ||
return sample | ||
|
||
return transform_fn |
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