|
| 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 | +import enum |
| 19 | +from typing import Optional |
| 20 | + |
| 21 | +from google.genai import types as genai_types |
| 22 | +from pydantic import alias_generators |
| 23 | +from pydantic import BaseModel |
| 24 | +from pydantic import ConfigDict |
| 25 | +from pydantic import Field |
| 26 | +from pydantic import ValidationError |
| 27 | + |
| 28 | +from ..events.event import Event |
| 29 | +from ..utils.feature_decorator import experimental |
| 30 | +from .common import EvalBaseModel |
| 31 | +from .evaluator import Evaluator |
| 32 | + |
| 33 | + |
| 34 | +class BaseUserSimulatorConfig(BaseModel): |
| 35 | + """Base class for configurations pertaining to User Simulator.""" |
| 36 | + |
| 37 | + model_config = ConfigDict( |
| 38 | + alias_generator=alias_generators.to_camel, |
| 39 | + populate_by_name=True, |
| 40 | + extra="allow", |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +class Status(enum.Enum): |
| 45 | + """The resulting status of get_next_user_message().""" |
| 46 | + |
| 47 | + SUCCESS = "success" |
| 48 | + TURN_LIMIT_REACHED = "turn_limit_reached" |
| 49 | + STOP_SIGNAL_DETECTED = "stop_signal_detected" |
| 50 | + NO_MESSAGE_GENERATED = "no_message_generated" |
| 51 | + |
| 52 | + |
| 53 | +class NextUserMessage(EvalBaseModel): |
| 54 | + status: Status = Field( |
| 55 | + description="""The resulting status of `get_next_user_message()`. |
| 56 | +
|
| 57 | +The caller of `get_next_user_message()` should inspect this field to determine |
| 58 | +if the user simulator was able to successfully generate a message or why it was |
| 59 | +not able to do so.""" |
| 60 | + ) |
| 61 | + |
| 62 | + user_message: Optional[genai_types.Content] = Field( |
| 63 | + description="""The next user message.""" |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +@experimental |
| 68 | +class UserSimulator(ABC): |
| 69 | + """A user simulator for the purposes of automating interaction with an Agent.""" |
| 70 | + |
| 71 | + def __init__( |
| 72 | + self, |
| 73 | + config: BaseUserSimulatorConfig, |
| 74 | + config_type: type[BaseUserSimulatorConfig], |
| 75 | + ): |
| 76 | + # Unpack the config to a specific type needed by the class implementing this |
| 77 | + # interface. |
| 78 | + try: |
| 79 | + self._config = config_type.model_validate(config.model_dump()) |
| 80 | + except ValidationError as e: |
| 81 | + raise ValueError(f"Expect config of type `{config_type}`.") from e |
| 82 | + |
| 83 | + async def get_next_user_message( |
| 84 | + self, |
| 85 | + conversation_plan: str, |
| 86 | + events: list[Event], |
| 87 | + ) -> NextUserMessage: |
| 88 | + """Returns the next user message to send to the agent. |
| 89 | +
|
| 90 | + Args: |
| 91 | + conversation_plan: A plan that user simulation system needs to follow as |
| 92 | + it plays out the conversation. |
| 93 | + events: The unaltered conversation history between the user and the |
| 94 | + agent(s) under evaluation. |
| 95 | + """ |
| 96 | + raise NotImplementedError() |
| 97 | + |
| 98 | + def get_simulation_evaluator( |
| 99 | + self, |
| 100 | + ) -> Evaluator: |
| 101 | + """Returns an instnace of an Evaluator that evaluates if the simulation was successful or not.""" |
| 102 | + raise NotImplementedError() |
0 commit comments