|
| 1 | +"""AsyncBenchmarkRun resource class for asynchronous operations.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import List |
| 6 | +from typing_extensions import Unpack, override |
| 7 | + |
| 8 | +from ..types import ScenarioRunView, BenchmarkRunView |
| 9 | +from ._types import BaseRequestOptions, LongRequestOptions, SDKBenchmarkRunListScenarioRunsParams |
| 10 | +from .._client import AsyncRunloop |
| 11 | + |
| 12 | + |
| 13 | +class AsyncBenchmarkRun: |
| 14 | + """A benchmark run for evaluating agent performance across scenarios (async). |
| 15 | +
|
| 16 | + Provides async methods for monitoring run status, managing the run lifecycle, |
| 17 | + and accessing scenario run results. Obtain instances via |
| 18 | + ``benchmark.run()`` or ``benchmark.list_runs()``. |
| 19 | +
|
| 20 | + Example: |
| 21 | + >>> benchmark = runloop.benchmark.from_id("bench-xxx") |
| 22 | + >>> run = await benchmark.run(run_name="evaluation-v1") |
| 23 | + >>> info = await run.get_info() |
| 24 | + >>> scenario_runs = await run.list_scenario_runs() |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, client: AsyncRunloop, run_id: str, benchmark_id: str) -> None: |
| 28 | + """Create an AsyncBenchmarkRun instance. |
| 29 | +
|
| 30 | + :param client: AsyncRunloop client instance |
| 31 | + :type client: AsyncRunloop |
| 32 | + :param run_id: Benchmark run ID |
| 33 | + :type run_id: str |
| 34 | + :param benchmark_id: Parent benchmark ID |
| 35 | + :type benchmark_id: str |
| 36 | + """ |
| 37 | + self._client = client |
| 38 | + self._id = run_id |
| 39 | + self._benchmark_id = benchmark_id |
| 40 | + |
| 41 | + @override |
| 42 | + def __repr__(self) -> str: |
| 43 | + return f"<AsyncBenchmarkRun id={self._id!r}>" |
| 44 | + |
| 45 | + @property |
| 46 | + def id(self) -> str: |
| 47 | + """Return the benchmark run ID. |
| 48 | +
|
| 49 | + :return: Unique benchmark run ID |
| 50 | + :rtype: str |
| 51 | + """ |
| 52 | + return self._id |
| 53 | + |
| 54 | + @property |
| 55 | + def benchmark_id(self) -> str: |
| 56 | + """Return the parent benchmark ID. |
| 57 | +
|
| 58 | + :return: Parent benchmark ID |
| 59 | + :rtype: str |
| 60 | + """ |
| 61 | + return self._benchmark_id |
| 62 | + |
| 63 | + async def get_info( |
| 64 | + self, |
| 65 | + **options: Unpack[BaseRequestOptions], |
| 66 | + ) -> BenchmarkRunView: |
| 67 | + """Retrieve current benchmark run status and metadata. |
| 68 | +
|
| 69 | + :param options: See :typeddict:`~runloop_api_client.sdk._types.BaseRequestOptions` for available options |
| 70 | + :return: Current benchmark run state info |
| 71 | + :rtype: BenchmarkRunView |
| 72 | + """ |
| 73 | + return await self._client.benchmarks.runs.retrieve( |
| 74 | + self._id, |
| 75 | + **options, |
| 76 | + ) |
| 77 | + |
| 78 | + async def cancel( |
| 79 | + self, |
| 80 | + **options: Unpack[LongRequestOptions], |
| 81 | + ) -> BenchmarkRunView: |
| 82 | + """Cancel the benchmark run. |
| 83 | +
|
| 84 | + Stops all running scenarios and marks the run as canceled. |
| 85 | +
|
| 86 | + :param options: See :typeddict:`~runloop_api_client.sdk._types.LongRequestOptions` for available options |
| 87 | + :return: Updated benchmark run state |
| 88 | + :rtype: BenchmarkRunView |
| 89 | + """ |
| 90 | + return await self._client.benchmarks.runs.cancel( |
| 91 | + self._id, |
| 92 | + **options, |
| 93 | + ) |
| 94 | + |
| 95 | + async def complete( |
| 96 | + self, |
| 97 | + **options: Unpack[LongRequestOptions], |
| 98 | + ) -> BenchmarkRunView: |
| 99 | + """Complete the benchmark run. |
| 100 | +
|
| 101 | + Marks the run as completed. Call this after all scenarios have finished. |
| 102 | +
|
| 103 | + :param options: See :typeddict:`~runloop_api_client.sdk._types.LongRequestOptions` for available options |
| 104 | + :return: Completed benchmark run state |
| 105 | + :rtype: BenchmarkRunView |
| 106 | + """ |
| 107 | + return await self._client.benchmarks.runs.complete( |
| 108 | + self._id, |
| 109 | + **options, |
| 110 | + ) |
| 111 | + |
| 112 | + async def list_scenario_runs( |
| 113 | + self, |
| 114 | + **params: Unpack[SDKBenchmarkRunListScenarioRunsParams], |
| 115 | + ) -> List[ScenarioRunView]: |
| 116 | + """List all scenario runs for this benchmark run. |
| 117 | +
|
| 118 | + :param params: See :typeddict:`~runloop_api_client.sdk._types.SDKBenchmarkRunListScenarioRunsParams` for available parameters |
| 119 | + :return: List of scenario run views |
| 120 | + :rtype: List[ScenarioRunView] |
| 121 | + """ |
| 122 | + page = self._client.benchmarks.runs.list_scenario_runs( |
| 123 | + self._id, |
| 124 | + **params, |
| 125 | + ) |
| 126 | + return [item async for item in page] |
0 commit comments