|
| 1 | +# Copyright 2026 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 typing import Any |
| 18 | +from typing import Generic |
| 19 | +from typing import Optional |
| 20 | +from typing import TypeVar |
| 21 | + |
| 22 | +from google.adk.agents.llm_agent import Agent |
| 23 | +from pydantic import BaseModel |
| 24 | +from pydantic import Field |
| 25 | + |
| 26 | + |
| 27 | +class BaseSamplingResult(BaseModel): |
| 28 | + """Base class for evaluation results of the candidate agent on the batch of examples.""" |
| 29 | + |
| 30 | + scores: dict[str, float] = Field( |
| 31 | + required=True, |
| 32 | + description=( |
| 33 | + "A map from example UID to the agent's overall score on that example." |
| 34 | + " (higher is better)." |
| 35 | + ), |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +# SamplingResult: the per-component evaluation results for a batch of examples. |
| 40 | +# Should at least include per-example scores and may also contain other data |
| 41 | +# required for optimizing the agent (e.g., outputs, trajectories, and metrics). |
| 42 | +SamplingResult = TypeVar("SamplingResult", bound=BaseSamplingResult) |
| 43 | + |
| 44 | + |
| 45 | +class BaseAgentWithScores(BaseModel): |
| 46 | + """An optimized agent with its scores. |
| 47 | +
|
| 48 | + Optimizers may use the overall_score field and can return custom metrics by |
| 49 | + sub-classing this class. |
| 50 | + """ |
| 51 | + |
| 52 | + optimized_agent: Agent = Field( |
| 53 | + required=True, |
| 54 | + description="The optimized agent.", |
| 55 | + ) |
| 56 | + |
| 57 | + overall_score: Optional[float] = Field( |
| 58 | + default=None, |
| 59 | + description="The overall score of the optimized agent.", |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +AgentWithScores = TypeVar("AgentWithScores", bound=BaseAgentWithScores) |
| 64 | + |
| 65 | + |
| 66 | +class OptimizerResult(BaseModel, Generic[AgentWithScores]): |
| 67 | + """Base class for optimizer final results.""" |
| 68 | + |
| 69 | + optimized_agents: list[AgentWithScores] = Field( |
| 70 | + required=True, |
| 71 | + description=( |
| 72 | + "A list of optimized agents which cannot be considered strictly" |
| 73 | + " better than one another (see" |
| 74 | + " https://en.wikipedia.org/wiki/Pareto_front), along with scores." |
| 75 | + ), |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +class UnstructuredSamplingResult(BaseSamplingResult): |
| 80 | + """Evaluation result providing per-example unstructured evaluation data.""" |
| 81 | + |
| 82 | + data: Optional[dict[str, dict[str, Any]]] = Field( |
| 83 | + default=None, |
| 84 | + description=( |
| 85 | + "A map from example UID to JSON-serializable evaluation data useful" |
| 86 | + " for agent optimization. Recommended contents include inputs," |
| 87 | + " trajectories, and metrics. Must be provided if requested by the" |
| 88 | + " optimizer." |
| 89 | + ), |
| 90 | + ) |
0 commit comments