|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from abc import ABC |
| 18 | +from abc import abstractmethod |
| 19 | +from typing import AsyncGenerator |
| 20 | +from typing import Optional |
| 21 | + |
| 22 | +from pydantic import alias_generators |
| 23 | +from pydantic import BaseModel |
| 24 | +from pydantic import ConfigDict |
| 25 | +from pydantic import Field |
| 26 | + |
| 27 | +from .eval_case import Invocation |
| 28 | +from .eval_metrics import EvalMetric |
| 29 | +from .eval_result import EvalCaseResult |
| 30 | + |
| 31 | + |
| 32 | +class EvaluateConfig(BaseModel): |
| 33 | + """Contains configurations need to run an evaluations.""" |
| 34 | + |
| 35 | + model_config = ConfigDict( |
| 36 | + alias_generator=alias_generators.to_camel, |
| 37 | + populate_by_name=True, |
| 38 | + ) |
| 39 | + |
| 40 | + eval_metrics: list[EvalMetric] = Field( |
| 41 | + description="""The list of metrics to be used in Eval.""", |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +class InferenceConfig(BaseModel): |
| 46 | + """Contains configurations need to run inferences.""" |
| 47 | + |
| 48 | + model_config = ConfigDict( |
| 49 | + alias_generator=alias_generators.to_camel, |
| 50 | + populate_by_name=True, |
| 51 | + ) |
| 52 | + |
| 53 | + labels: Optional[dict[str, str]] = Field( |
| 54 | + default=None, |
| 55 | + description="""Labels with user-defined metadata to break down billed |
| 56 | +charges.""", |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +class InferenceRequest(BaseModel): |
| 61 | + """Represent a request to perform inferences for the eval cases in an eval set.""" |
| 62 | + |
| 63 | + model_config = ConfigDict( |
| 64 | + alias_generator=alias_generators.to_camel, |
| 65 | + populate_by_name=True, |
| 66 | + ) |
| 67 | + |
| 68 | + app_name: str = Field( |
| 69 | + description="""The name of the app to which the eval case belongs to.""" |
| 70 | + ) |
| 71 | + |
| 72 | + eval_set_id: str = Field(description="""Id of the eval set.""") |
| 73 | + |
| 74 | + eval_case_ids: Optional[list[str]] = Field( |
| 75 | + default=None, |
| 76 | + description="""Id of the eval cases for which inferences need to be |
| 77 | +generated. |
| 78 | +
|
| 79 | +All the eval case ids should belong to the EvalSet. |
| 80 | +
|
| 81 | +If the list of eval case ids are empty or not specified, then all the eval cases |
| 82 | +in an eval set are evaluated. |
| 83 | + """, |
| 84 | + ) |
| 85 | + |
| 86 | + inference_config: InferenceConfig = Field( |
| 87 | + description="""The config to use for inferencing.""", |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +class InferenceResult(BaseModel): |
| 92 | + """Contains inference results for a single eval case.""" |
| 93 | + |
| 94 | + model_config = ConfigDict( |
| 95 | + alias_generator=alias_generators.to_camel, |
| 96 | + populate_by_name=True, |
| 97 | + ) |
| 98 | + |
| 99 | + app_name: str = Field( |
| 100 | + description="""The name of the app to which the eval case belongs to.""" |
| 101 | + ) |
| 102 | + |
| 103 | + eval_set_id: str = Field(description="""Id of the eval set.""") |
| 104 | + |
| 105 | + eval_case_id: str = Field( |
| 106 | + description="""Id of the eval case for which inferences were generated.""", |
| 107 | + ) |
| 108 | + |
| 109 | + inferences: list[Invocation] = Field( |
| 110 | + description="""Inferences obtained from the Agent for the eval case.""" |
| 111 | + ) |
| 112 | + |
| 113 | + session_id: Optional[str] = Field( |
| 114 | + description="""Id of the inference session.""" |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +class EvaluateRequest(BaseModel): |
| 119 | + model_config = ConfigDict( |
| 120 | + alias_generator=alias_generators.to_camel, |
| 121 | + populate_by_name=True, |
| 122 | + ) |
| 123 | + |
| 124 | + inference_results: list[InferenceResult] = Field( |
| 125 | + description="""A list of inferences that need to be evaluated.""", |
| 126 | + ) |
| 127 | + |
| 128 | + evaluate_config: EvaluateConfig = Field( |
| 129 | + description="""The config to use for evaluations.""", |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +class BaseEvalService(ABC): |
| 134 | + """A service to run Evals for an ADK agent.""" |
| 135 | + |
| 136 | + @abstractmethod |
| 137 | + async def perform_inference( |
| 138 | + self, |
| 139 | + inference_request: InferenceRequest, |
| 140 | + ) -> AsyncGenerator[InferenceResult, None]: |
| 141 | + """Returns InferenceResult obtained from the Agent as and when they are available. |
| 142 | +
|
| 143 | + Args: |
| 144 | + inference_request: The request for generating inferences. |
| 145 | + """ |
| 146 | + |
| 147 | + @abstractmethod |
| 148 | + async def evaluate( |
| 149 | + self, |
| 150 | + evaluate_request: EvaluateRequest, |
| 151 | + ) -> AsyncGenerator[EvalCaseResult, None]: |
| 152 | + """Returns EvalCaseResult for each item as and when they are available. |
| 153 | +
|
| 154 | + Args: |
| 155 | + evaluate_request: The request to perform metric evaluations on the |
| 156 | + inferences. |
| 157 | + """ |
0 commit comments