|
| 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 typing import Optional |
| 18 | + |
| 19 | +from google.genai import types |
| 20 | +from typing_extensions import override |
| 21 | + |
| 22 | +from ..agents.callback_context import CallbackContext |
| 23 | +from ..models.llm_request import LlmRequest |
| 24 | +from ..models.llm_response import LlmResponse |
| 25 | +from ..plugins.base_plugin import BasePlugin |
| 26 | + |
| 27 | +_RETRY_HTTP_STATUS_CODES = ( |
| 28 | + 408, # Request timeout. |
| 29 | + 429, # Too many requests. |
| 30 | + 500, # Internal server error. |
| 31 | + 502, # Bad gateway. |
| 32 | + 503, # Service unavailable. |
| 33 | + 504, # Gateway timeout |
| 34 | +) |
| 35 | +_DEFAULT_HTTP_RETRY_OPTIONS = types.HttpRetryOptions( |
| 36 | + attempts=7, |
| 37 | + initial_delay=5.0, |
| 38 | + max_delay=120, |
| 39 | + exp_base=2.0, |
| 40 | + http_status_codes=_RETRY_HTTP_STATUS_CODES, |
| 41 | +) |
| 42 | + |
| 43 | + |
| 44 | +def add_default_retry_options_if_not_present(llm_request: LlmRequest): |
| 45 | + """Adds default HTTP Retry Options, if they are not present on the llm_request. |
| 46 | +
|
| 47 | + NOTE: This implementation is intended for eval systems internal usage. Do not |
| 48 | + take direct dependency on it. |
| 49 | + """ |
| 50 | + llm_request.config = llm_request.config or types.GenerateContentConfig() |
| 51 | + |
| 52 | + llm_request.config.http_options = ( |
| 53 | + llm_request.config.http_options or types.HttpOptions() |
| 54 | + ) |
| 55 | + llm_request.config.http_options.retry_options = ( |
| 56 | + llm_request.config.http_options.retry_options |
| 57 | + or _DEFAULT_HTTP_RETRY_OPTIONS |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +class EnsureRetryOptionsPlugin(BasePlugin): |
| 62 | + """This plugin adds retry options to llm_request, if they are not present. |
| 63 | +
|
| 64 | + This is done to ensure that temporary outages with the model provider don't |
| 65 | + affect eval runs. |
| 66 | +
|
| 67 | + NOTE: This implementation is intended for eval systems internal usage. Do not |
| 68 | + take direct dependency on it. |
| 69 | + """ |
| 70 | + |
| 71 | + @override |
| 72 | + async def before_model_callback( |
| 73 | + self, *, callback_context: CallbackContext, llm_request: LlmRequest |
| 74 | + ) -> Optional[LlmResponse]: |
| 75 | + add_default_retry_options_if_not_present(llm_request) |
0 commit comments