|
| 1 | +"""Shared test helpers for Llama Stack eval provider tests. |
| 2 | +
|
| 3 | +Provides ``SmokeTester`` and ``EvalTester``, plain helper classes that |
| 4 | +encapsulate common assertions (model/dataset/benchmark registration) and |
| 5 | +eval-job execution logic (run, poll, verify scores). Test modules |
| 6 | +instantiate them via fixtures, supplying the appropriate client and |
| 7 | +configuration for each environment (in-process library client or remote |
| 8 | +``LlamaStackClient``). |
| 9 | +""" |
| 10 | + |
| 11 | +import time |
| 12 | + |
| 13 | +from rich import print as pprint |
| 14 | + |
| 15 | + |
| 16 | +class SmokeTester: |
| 17 | + def __init__(self, client, dataset_id, inline_benchmark_id, remote_benchmark_id): |
| 18 | + self.client = client |
| 19 | + self.dataset_id = dataset_id |
| 20 | + self.inline_benchmark_id = inline_benchmark_id |
| 21 | + self.remote_benchmark_id = remote_benchmark_id |
| 22 | + |
| 23 | + def test_providers_registered(self): |
| 24 | + providers = self.client.providers.list() |
| 25 | + assert len(providers) > 0 |
| 26 | + assert any(p.api == "eval" for p in providers) |
| 27 | + pprint("Providers:", providers) |
| 28 | + |
| 29 | + def test_models_registered(self): |
| 30 | + models = self.client.models.list() |
| 31 | + pprint("Models:", models) |
| 32 | + assert len(models) > 0, "No models registered" |
| 33 | + |
| 34 | + def test_datasets_registered(self): |
| 35 | + datasets = self.client.beta.datasets.list() |
| 36 | + pprint("Datasets:", datasets) |
| 37 | + dataset_ids = [d.identifier for d in datasets] |
| 38 | + assert self.dataset_id in dataset_ids, ( |
| 39 | + f"Dataset '{self.dataset_id}' not found. Available: {dataset_ids}" |
| 40 | + ) |
| 41 | + |
| 42 | + def test_benchmarks_registered(self): |
| 43 | + benchmarks = self.client.alpha.benchmarks.list() |
| 44 | + pprint("Benchmarks:", benchmarks) |
| 45 | + benchmark_ids = [b.identifier for b in benchmarks] |
| 46 | + assert self.inline_benchmark_id in benchmark_ids, ( |
| 47 | + f"Benchmark '{self.inline_benchmark_id}' not found. Available: {benchmark_ids}" |
| 48 | + ) |
| 49 | + assert self.remote_benchmark_id in benchmark_ids, ( |
| 50 | + f"Benchmark '{self.remote_benchmark_id}' not found. Available: {benchmark_ids}" |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +class EvalTester: |
| 55 | + """Base evaluation test class.""" |
| 56 | + |
| 57 | + def __init__( |
| 58 | + self, |
| 59 | + client, |
| 60 | + inference_model, |
| 61 | + dataset_id, |
| 62 | + inline_benchmark_id, |
| 63 | + remote_benchmark_id, |
| 64 | + poll_interval: int = 5, |
| 65 | + poll_timeout: int = 300, |
| 66 | + ): |
| 67 | + self.client = client |
| 68 | + self.inference_model = inference_model |
| 69 | + self.dataset_id = dataset_id |
| 70 | + self.inline_benchmark_id = inline_benchmark_id |
| 71 | + self.remote_benchmark_id = remote_benchmark_id |
| 72 | + self.poll_interval = poll_interval |
| 73 | + self.poll_timeout = poll_timeout |
| 74 | + |
| 75 | + def run_eval( |
| 76 | + self, |
| 77 | + benchmark_id: str, |
| 78 | + inference_model: str, |
| 79 | + num_examples: int | None = None, |
| 80 | + ): |
| 81 | + """Run an evaluation job and verify it completes with scores.""" |
| 82 | + benchmark_config = self._build_benchmark_config( |
| 83 | + inference_model, num_examples=num_examples |
| 84 | + ) |
| 85 | + job = self.client.alpha.eval.run_eval( |
| 86 | + benchmark_id=benchmark_id, |
| 87 | + benchmark_config=benchmark_config, |
| 88 | + ) |
| 89 | + assert job.job_id is not None |
| 90 | + assert job.status == "in_progress" |
| 91 | + |
| 92 | + completed = self._wait_for_job(self.client, benchmark_id, job.job_id) |
| 93 | + assert completed.status == "completed", ( |
| 94 | + f"Job finished with status '{completed.status}'" |
| 95 | + ) |
| 96 | + |
| 97 | + results = self.client.alpha.eval.jobs.retrieve( |
| 98 | + benchmark_id=benchmark_id, job_id=job.job_id |
| 99 | + ) |
| 100 | + pprint(f"[{self.__class__.__name__}] Results:", results) |
| 101 | + assert results.scores, "Expected non-empty scores" |
| 102 | + |
| 103 | + # -- helpers -------------------------------------------------------- |
| 104 | + |
| 105 | + def _build_benchmark_config( |
| 106 | + self, inference_model: str, num_examples: int | None = None |
| 107 | + ) -> dict: |
| 108 | + """Build the ``benchmark_config`` dict for ``run_eval``.""" |
| 109 | + config: dict = { |
| 110 | + "eval_candidate": { |
| 111 | + "type": "model", |
| 112 | + "model": inference_model, |
| 113 | + "sampling_params": { |
| 114 | + "temperature": 0.1, |
| 115 | + "max_tokens": 100, |
| 116 | + }, |
| 117 | + }, |
| 118 | + "scoring_params": {}, |
| 119 | + } |
| 120 | + if num_examples is not None: |
| 121 | + config["num_examples"] = num_examples |
| 122 | + return config |
| 123 | + |
| 124 | + def _wait_for_job( |
| 125 | + self, client, benchmark_id: str, job_id: str, timeout: int | None = None |
| 126 | + ): |
| 127 | + """Poll until the eval job reaches a terminal state.""" |
| 128 | + timeout = timeout if timeout is not None else self.poll_timeout |
| 129 | + deadline = time.time() + timeout |
| 130 | + while time.time() < deadline: |
| 131 | + job = client.alpha.eval.jobs.status( |
| 132 | + benchmark_id=benchmark_id, job_id=job_id |
| 133 | + ) |
| 134 | + pprint(f"[{self.__class__.__name__}] Job status:", job) |
| 135 | + if job.status in ("completed", "failed"): |
| 136 | + return job |
| 137 | + time.sleep(self.poll_interval) |
| 138 | + raise TimeoutError( |
| 139 | + f"Job {job_id} for benchmark {benchmark_id} " |
| 140 | + f"did not complete within {timeout}s" |
| 141 | + ) |
0 commit comments