Skip to content

Commit

Permalink
Fix handling of place_correct_choice_position (#1511)
Browse files Browse the repository at this point in the history
Fix: Improved handling of `place_correct_choice_position` for flexible input sizes

- Added logic to ensure `place_correct_choice_position` works correctly for varying numbers of choices.
- Implemented two approaches:
  1. Clamping the position to ensure it remains within valid bounds.
  2. Supporting negative indexing to allow placing the correct choice at the end with `-1`.
- Ensured the implementation avoids crashes and provides robust handling for edge cases.

Co-authored-by: Elron Bandel <[email protected]>
  • Loading branch information
eliyahabba and elronbandel authored Jan 16, 2025
1 parent 9777799 commit 347f6e1
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/unitxt/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,21 @@ def preprocess_input_and_reference_fields(
)
random_generator.shuffle(choices)
if self.place_correct_choice_position is not None:
fix_pos = self.place_correct_choice_position

# Option 1: Clamping the position to the valid range
# Force the position to be within the valid range [0, len(choices)-1].
# If fix_pos is less than 0, it becomes 0.
# If fix_pos is greater than len(choices)-1, it becomes len(choices)-1.
fix_pos = max(0, min(fix_pos, len(choices) - 1))

# Option 2: Supporting negative indexes similar to Python lists
# If fix_pos is negative, convert it to a valid positive index by adding len(choices).
# For example, -1 becomes the last index, -2 becomes the one before last, etc.
if fix_pos < 0:
fix_pos += len(choices)
self.place_correct_choice_position = fix_pos
# Remove the original label choice from the list
if not 0 <= self.place_correct_choice_position < len(choices):
raise ValueError(
f"fix_correct_choice_position={self.place_correct_choice_position} out of range (0..{len(choices) - 1})."
Expand Down

0 comments on commit 347f6e1

Please sign in to comment.