|
| 1 | +"""Tests for exemplar selectors.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | +from unittest.mock import MagicMock |
| 6 | + |
| 7 | +from promptolution.exemplar_selectors.random_search_selector import RandomSearchSelector |
| 8 | +from promptolution.tasks.base_task import EvalResult |
| 9 | +from promptolution.utils.prompt import Prompt |
| 10 | +from tests.mocks.mock_predictor import MockPredictor |
| 11 | + |
| 12 | + |
| 13 | +def make_eval_result(sequences, score): |
| 14 | + n = len(sequences) |
| 15 | + return EvalResult( |
| 16 | + scores=np.array([[score] * n], dtype=float), |
| 17 | + agg_scores=np.array([score], dtype=float), |
| 18 | + sequences=np.array([sequences], dtype=object), |
| 19 | + input_tokens=np.array([[1.0] * n], dtype=float), |
| 20 | + output_tokens=np.array([[1.0] * n], dtype=float), |
| 21 | + agg_input_tokens=np.array([1.0], dtype=float), |
| 22 | + agg_output_tokens=np.array([1.0], dtype=float), |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def task_and_predictor(): |
| 28 | + task = MagicMock() |
| 29 | + pred = MockPredictor() |
| 30 | + return task, pred |
| 31 | + |
| 32 | + |
| 33 | +def test_random_search_selector_respects_n_examples(task_and_predictor): |
| 34 | + task, pred = task_and_predictor |
| 35 | + sequences = [f"ex_{i}" for i in range(10)] |
| 36 | + task.evaluate.return_value = make_eval_result(sequences, score=0.8) |
| 37 | + |
| 38 | + selector = RandomSearchSelector(task, pred) |
| 39 | + result = selector.select_exemplars(Prompt("Classify:"), n_examples=3, n_trials=1) |
| 40 | + |
| 41 | + assert len(result.few_shots) == 3 |
| 42 | + assert all(ex in sequences for ex in result.few_shots) |
| 43 | + |
| 44 | + |
| 45 | +def test_random_search_selector_returns_best_trial(task_and_predictor): |
| 46 | + task, pred = task_and_predictor |
| 47 | + sequences = [f"ex_{i}" for i in range(5)] |
| 48 | + |
| 49 | + # First trial scores low, second scores high |
| 50 | + task.evaluate.side_effect = [ |
| 51 | + make_eval_result(sequences, score=0.3), # zero-shot eval trial 1 |
| 52 | + make_eval_result(sequences, score=0.3), # few-shot eval trial 1 |
| 53 | + make_eval_result(sequences, score=0.9), # zero-shot eval trial 2 |
| 54 | + make_eval_result(sequences, score=0.9), # few-shot eval trial 2 |
| 55 | + ] |
| 56 | + |
| 57 | + selector = RandomSearchSelector(task, pred) |
| 58 | + result = selector.select_exemplars(Prompt("Classify:"), n_examples=2, n_trials=2) |
| 59 | + |
| 60 | + assert len(result.few_shots) == 2 |
| 61 | + assert result.few_shots != [] |
| 62 | + |
| 63 | + |
| 64 | +def test_random_search_selector_n_examples_kwarg(task_and_predictor): |
| 65 | + """Regression test: calling with n_examples as keyword arg must not raise TypeError.""" |
| 66 | + task, pred = task_and_predictor |
| 67 | + sequences = [f"ex_{i}" for i in range(5)] |
| 68 | + task.evaluate.return_value = make_eval_result(sequences, score=0.5) |
| 69 | + |
| 70 | + selector = RandomSearchSelector(task, pred) |
| 71 | + # This call pattern is what triggered the original bug report |
| 72 | + result = selector.select_exemplars(prompt=Prompt("Classify:"), n_examples=2) |
| 73 | + |
| 74 | + assert len(result.few_shots) == 2 |
0 commit comments