1+ """
2+ This file defines google SUTs that use Google's generativeai python SDK.
3+ This SDK is older and does not include the latest features e.g. reasoning configuration. generativeai will be deprecated in the future.
4+ The SUTs defined in this file should be migrated to use google's newer SDK `genai` as implemented in google_genai.py.
5+ """
6+
17from abc import abstractmethod
28from typing import Dict , List , Optional
39
@@ -49,8 +55,8 @@ def description(cls) -> SecretDescription:
4955 )
5056
5157
52- class GoogleGenAiConfig (BaseModel ):
53- """Generation config for Google Gen AI requests.
58+ class GenerativeAiConfig (BaseModel ):
59+ """Generation config for Google Generative AI requests.
5460
5561 Based on https://ai.google.dev/api/generate-content#v1beta.GenerationConfig
5662 """
@@ -64,13 +70,13 @@ class GoogleGenAiConfig(BaseModel):
6470 frequency_penalty : Optional [float ] = None
6571
6672
67- class GoogleGenAiRequest (BaseModel ):
73+ class GenerativeAiRequest (BaseModel ):
6874 contents : str
69- generation_config : GoogleGenAiConfig
75+ generation_config : GenerativeAiConfig
7076 safety_settings : Optional [Dict [HarmCategory , HarmBlockThreshold ]] = None
7177
7278
73- class GoogleGenAiResponse (BaseModel ):
79+ class GenerativeAiResponse (BaseModel ):
7480 class Candidate (BaseModel ):
7581 content : Optional [Dict ] = None
7682 finish_reason : int
@@ -79,7 +85,7 @@ class Candidate(BaseModel):
7985 usage_metadata : Dict
8086
8187
82- class GoogleGenAiBaseSUT (PromptResponseSUT [GoogleGenAiRequest , GoogleGenAiResponse ]):
88+ class GoogleGenerativeAiBaseSUT (PromptResponseSUT [GenerativeAiRequest , GenerativeAiResponse ]):
8389 def __init__ (self , uid : str , model_name : str , api_key : GoogleAiApiKey ):
8490 super ().__init__ (uid )
8591 self .model_name = model_name
@@ -101,8 +107,8 @@ def safety_settings(self) -> Optional[Dict[HarmCategory, HarmBlockThreshold]]:
101107 def _load_client (self ) -> genai .GenerativeModel :
102108 return genai .GenerativeModel (self .model_name )
103109
104- def translate_text_prompt (self , prompt : TextPrompt , options : SUTOptions ) -> GoogleGenAiRequest :
105- generation_config = GoogleGenAiConfig (
110+ def translate_text_prompt (self , prompt : TextPrompt , options : SUTOptions ) -> GenerativeAiRequest :
111+ generation_config = GenerativeAiConfig (
106112 stop_sequences = options .stop_sequences ,
107113 max_output_tokens = options .max_tokens ,
108114 temperature = options .temperature ,
@@ -111,20 +117,20 @@ def translate_text_prompt(self, prompt: TextPrompt, options: SUTOptions) -> Goog
111117 presence_penalty = options .presence_penalty ,
112118 frequency_penalty = options .frequency_penalty ,
113119 )
114- return GoogleGenAiRequest (
120+ return GenerativeAiRequest (
115121 contents = prompt .text , generation_config = generation_config , safety_settings = self .safety_settings
116122 )
117123
118124 @retry (transient_exceptions = [InternalServerError , ResourceExhausted , RetryError , TooManyRequests ])
119- def evaluate (self , request : GoogleGenAiRequest ) -> GoogleGenAiResponse :
125+ def evaluate (self , request : GenerativeAiRequest ) -> GenerativeAiResponse :
120126 if self .model is None :
121127 # Handle lazy init.
122128 self .model = self ._load_client ()
123129 response = self .model .generate_content (** request .model_dump (exclude_none = True ))
124130 # Convert to pydantic model
125- return GoogleGenAiResponse (** response .to_dict ())
131+ return GenerativeAiResponse (** response .to_dict ())
126132
127- def translate_response (self , request : GoogleGenAiRequest , response : GoogleGenAiResponse ) -> SUTResponse :
133+ def translate_response (self , request : GenerativeAiRequest , response : GenerativeAiResponse ) -> SUTResponse :
128134 assert (
129135 len (response .candidates ) <= 1
130136 ), f"Expected a single candidate in the response, got { len (response .candidates )} ."
@@ -140,7 +146,7 @@ def translate_response(self, request: GoogleGenAiRequest, response: GoogleGenAiR
140146 response_text = candidate .content ["parts" ][0 ]["text" ]
141147 else :
142148 raise APIException (
143- f"Unexpected candidate in response from GoogleGenAiSUT { self .uid } : { candidate } . "
149+ f"Unexpected candidate in response from GoogleGenerativeAiSUT { self .uid } : { candidate } . "
144150 f"The candidate does not have any content,"
145151 f" but it's finish reason { candidate .finish_reason } does not qualify as a refusal."
146152 )
@@ -149,7 +155,7 @@ def translate_response(self, request: GoogleGenAiRequest, response: GoogleGenAiR
149155
150156
151157@modelgauge_sut (capabilities = [AcceptsTextPrompt ])
152- class GoogleGenAiDefaultSUT ( GoogleGenAiBaseSUT ):
158+ class GoogleGenerativeAiDefaultSUT ( GoogleGenerativeAiBaseSUT ):
153159 """SUT for Google Generative AI model with the model's default safety settings.
154160 As of 11/20/2024: The default settings are:
155161 "Block most (for gemini-1.5-pro-002 and gemini-1.5-flash-002 only) or Block some (in all other models)
@@ -168,7 +174,7 @@ def safety_settings(self) -> Optional[Dict[HarmCategory, HarmBlockThreshold]]:
168174
169175
170176@modelgauge_sut (capabilities = [AcceptsTextPrompt ])
171- class GoogleGeminiDisabledSafetySettingsSUT (GoogleGenAiBaseSUT ):
177+ class GoogleGeminiDisabledSafetySettingsSUT (GoogleGenerativeAiBaseSUT ):
172178 """SUT for Google Gemini model that removes that harm block threshold for all Gemini-specific harm categories."""
173179
174180 @property
@@ -182,7 +188,7 @@ def safety_settings(self) -> Optional[Dict[HarmCategory, HarmBlockThreshold]]:
182188
183189
184190@modelgauge_sut (capabilities = [AcceptsTextPrompt ])
185- class GoogleGenAiSafetyOnSUT ( GoogleGenAiBaseSUT ):
191+ class GoogleGenerativeAiSafetyOnSUT ( GoogleGenerativeAiBaseSUT ):
186192 """SUT for Google Generative AI model with the explicit safety settings turned on (ie BLOCK_LOW_AND_ABOVE).
187193
188194 Finish reasons related to safety are treated as refusal responses."""
@@ -207,8 +213,8 @@ def safety_settings(self) -> Optional[Dict[HarmCategory, HarmBlockThreshold]]:
207213 "gemini-2.5-pro-preview-05-06" ,
208214]
209215for model in gemini_models :
210- SUTS .register (GoogleGenAiDefaultSUT , model , model , InjectSecret (GoogleAiApiKey ))
216+ SUTS .register (GoogleGenerativeAiDefaultSUT , model , model , InjectSecret (GoogleAiApiKey ))
211217 SUTS .register (
212218 GoogleGeminiDisabledSafetySettingsSUT , f"{ model } -safety_block_none" , model , InjectSecret (GoogleAiApiKey )
213219 )
214- SUTS .register (GoogleGenAiSafetyOnSUT , f"{ model } -safety_block_most" , model , InjectSecret (GoogleAiApiKey ))
220+ SUTS .register (GoogleGenerativeAiSafetyOnSUT , f"{ model } -safety_block_most" , model , InjectSecret (GoogleAiApiKey ))
0 commit comments