|
| 1 | +"""Scorer resource class for asynchronous operations.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing_extensions import Unpack, override |
| 6 | + |
| 7 | +from ._types import ( |
| 8 | + BaseRequestOptions, |
| 9 | + SDKScorerListParams, |
| 10 | + SDKScorerCreateParams, |
| 11 | + SDKScorerUpdateParams, |
| 12 | + SDKScorerValidateParams, |
| 13 | +) |
| 14 | +from .._client import AsyncRunloop |
| 15 | +from ..types.scenarios import ScorerUpdateResponse, ScorerRetrieveResponse, ScorerValidateResponse |
| 16 | + |
| 17 | + |
| 18 | +class AsyncScorer: |
| 19 | + """Asynchronous wrapper around a scenario scorer resource.""" |
| 20 | + |
| 21 | + def __init__( |
| 22 | + self, |
| 23 | + client: AsyncRunloop, |
| 24 | + scorer_id: str, |
| 25 | + ) -> None: |
| 26 | + """Initialize the wrapper. |
| 27 | +
|
| 28 | + :param client: Generated AsyncRunloop client |
| 29 | + :type client: AsyncRunloop |
| 30 | + :param scorer_id: Scorer ID returned by the API |
| 31 | + :type scorer_id: str |
| 32 | + """ |
| 33 | + self._client = client |
| 34 | + self._id = scorer_id |
| 35 | + |
| 36 | + @override |
| 37 | + def __repr__(self) -> str: |
| 38 | + return f"<AsyncScorer id={self._id!r}>" |
| 39 | + |
| 40 | + @property |
| 41 | + def id(self) -> str: |
| 42 | + """Return the scorer ID. |
| 43 | +
|
| 44 | + :return: Unique scorer ID |
| 45 | + :rtype: str |
| 46 | + """ |
| 47 | + return self._id |
| 48 | + |
| 49 | + # TODO: replace static method once we have a proper client |
| 50 | + @staticmethod |
| 51 | + async def create( |
| 52 | + client: AsyncRunloop, |
| 53 | + **params: Unpack[SDKScorerCreateParams], |
| 54 | + ) -> "AsyncScorer": |
| 55 | + """Create a new scenario scorer. |
| 56 | +
|
| 57 | + :param client: Generated AsyncRunloop client |
| 58 | + :type client: AsyncRunloop |
| 59 | + :param params: See :typeddict:`~runloop_api_client.sdk._types.SDKScorerCreateParams` for available parameters |
| 60 | + :return: Wrapper bound to the newly created scorer |
| 61 | + :rtype: AsyncScorer |
| 62 | + """ |
| 63 | + response = await client.scenarios.scorers.create( |
| 64 | + **params, |
| 65 | + ) |
| 66 | + return AsyncScorer(client, response.id) |
| 67 | + |
| 68 | + # TODO: replace static method once we have a proper client |
| 69 | + @staticmethod |
| 70 | + async def list( |
| 71 | + client: AsyncRunloop, |
| 72 | + **params: Unpack[SDKScorerListParams], |
| 73 | + ) -> list["AsyncScorer"]: |
| 74 | + """List all scenario scorers. |
| 75 | +
|
| 76 | + :param client: Generated AsyncRunloop client |
| 77 | + :type client: AsyncRunloop |
| 78 | + :param params: See :typeddict:`~runloop_api_client.sdk._types.SDKScorerListParams` for available parameters |
| 79 | + :return: List of scorer wrappers |
| 80 | + :rtype: list[AsyncScorer] |
| 81 | + """ |
| 82 | + page = await client.scenarios.scorers.list( |
| 83 | + **params, |
| 84 | + ) |
| 85 | + return [AsyncScorer(client, item.id) async for item in page] |
| 86 | + |
| 87 | + async def get_info( |
| 88 | + self, |
| 89 | + **options: Unpack[BaseRequestOptions], |
| 90 | + ) -> ScorerRetrieveResponse: |
| 91 | + """Retrieve the latest scorer details. |
| 92 | +
|
| 93 | + :param options: Optional request configuration |
| 94 | + :return: API response describing the scorer |
| 95 | + :rtype: ScorerRetrieveResponse |
| 96 | + """ |
| 97 | + return await self._client.scenarios.scorers.retrieve( |
| 98 | + self._id, |
| 99 | + **options, |
| 100 | + ) |
| 101 | + |
| 102 | + async def update( |
| 103 | + self, |
| 104 | + **params: Unpack[SDKScorerUpdateParams], |
| 105 | + ) -> ScorerUpdateResponse: |
| 106 | + """Update the scorer. |
| 107 | +
|
| 108 | + :param params: See :typeddict:`~runloop_api_client.sdk._types.SDKScorerUpdateParams` for available parameters |
| 109 | + :return: API response with updated scorer details |
| 110 | + :rtype: ScorerUpdateResponse |
| 111 | + """ |
| 112 | + return await self._client.scenarios.scorers.update( |
| 113 | + self._id, |
| 114 | + **params, |
| 115 | + ) |
| 116 | + |
| 117 | + async def validate( |
| 118 | + self, |
| 119 | + **params: Unpack[SDKScorerValidateParams], |
| 120 | + ) -> ScorerValidateResponse: |
| 121 | + """Validate the scorer with a given context. |
| 122 | +
|
| 123 | + :param params: See :typeddict:`~runloop_api_client.sdk._types.SDKScorerValidateParams` for available parameters |
| 124 | + :return: API response with validation results |
| 125 | + :rtype: ScorerValidateResponse |
| 126 | + """ |
| 127 | + return await self._client.scenarios.scorers.validate( |
| 128 | + self._id, |
| 129 | + **params, |
| 130 | + ) |
0 commit comments