|
28 | 28 | generative_models as preview_generative_models,
|
29 | 29 | )
|
30 | 30 |
|
| 31 | +_REQUEST_FUNCTION_PARAMETER_SCHEMA_STRUCT = { |
| 32 | + "type": "object", |
| 33 | + "properties": { |
| 34 | + "location": { |
| 35 | + "type": "string", |
| 36 | + "description": "The city and state, e.g. San Francisco, CA", |
| 37 | + }, |
| 38 | + "unit": { |
| 39 | + "type": "string", |
| 40 | + "enum": [ |
| 41 | + "celsius", |
| 42 | + "fahrenheit", |
| 43 | + ], |
| 44 | + }, |
| 45 | + }, |
| 46 | + "required": ["location"], |
| 47 | +} |
| 48 | + |
31 | 49 |
|
32 | 50 | class TestGenerativeModels(e2e_base.TestEndToEnd):
|
33 | 51 | """System tests for generative models."""
|
@@ -188,3 +206,111 @@ def test_send_message_from_text(self):
|
188 | 206 | )
|
189 | 207 | assert response2.text
|
190 | 208 | assert len(chat.history) == 4
|
| 209 | + |
| 210 | + def test_chat_function_calling(self): |
| 211 | + get_current_weather_func = generative_models.FunctionDeclaration( |
| 212 | + name="get_current_weather", |
| 213 | + description="Get the current weather in a given location", |
| 214 | + parameters=_REQUEST_FUNCTION_PARAMETER_SCHEMA_STRUCT, |
| 215 | + ) |
| 216 | + |
| 217 | + weather_tool = generative_models.Tool( |
| 218 | + function_declarations=[get_current_weather_func], |
| 219 | + ) |
| 220 | + |
| 221 | + model = generative_models.GenerativeModel( |
| 222 | + "gemini-1.0-pro", |
| 223 | + # Specifying the tools once to avoid specifying them in every request |
| 224 | + tools=[weather_tool], |
| 225 | + ) |
| 226 | + |
| 227 | + chat = model.start_chat() |
| 228 | + |
| 229 | + response1 = chat.send_message("What is the weather like in Boston?") |
| 230 | + assert ( |
| 231 | + response1.candidates[0].content.parts[0].function_call.name |
| 232 | + == "get_current_weather" |
| 233 | + ) |
| 234 | + response2 = chat.send_message( |
| 235 | + generative_models.Part.from_function_response( |
| 236 | + name="get_current_weather", |
| 237 | + response={ |
| 238 | + "content": {"weather": "super nice"}, |
| 239 | + }, |
| 240 | + ), |
| 241 | + ) |
| 242 | + assert response2.text |
| 243 | + |
| 244 | + def test_generate_content_function_calling(self): |
| 245 | + get_current_weather_func = generative_models.FunctionDeclaration( |
| 246 | + name="get_current_weather", |
| 247 | + description="Get the current weather in a given location", |
| 248 | + parameters=_REQUEST_FUNCTION_PARAMETER_SCHEMA_STRUCT, |
| 249 | + ) |
| 250 | + |
| 251 | + weather_tool = generative_models.Tool( |
| 252 | + function_declarations=[get_current_weather_func], |
| 253 | + ) |
| 254 | + |
| 255 | + model = generative_models.GenerativeModel( |
| 256 | + "gemini-1.0-pro", |
| 257 | + # Specifying the tools once to avoid specifying them in every request |
| 258 | + tools=[weather_tool], |
| 259 | + ) |
| 260 | + |
| 261 | + # Define the user's prompt in a Content object that we can reuse in model calls |
| 262 | + prompt = "What is the weather like in Boston?" |
| 263 | + user_prompt_content = generative_models.Content( |
| 264 | + role="user", |
| 265 | + parts=[ |
| 266 | + generative_models.Part.from_text(prompt), |
| 267 | + ], |
| 268 | + ) |
| 269 | + |
| 270 | + # Send the prompt and instruct the model to generate content using the Tool |
| 271 | + response = model.generate_content( |
| 272 | + user_prompt_content, |
| 273 | + generation_config={"temperature": 0}, |
| 274 | + tools=[weather_tool], |
| 275 | + ) |
| 276 | + response_function_call_content = response.candidates[0].content |
| 277 | + |
| 278 | + assert ( |
| 279 | + response.candidates[0].content.parts[0].function_call.name |
| 280 | + == "get_current_weather" |
| 281 | + ) |
| 282 | + |
| 283 | + assert response.candidates[0].content.parts[0].function_call.args["location"] |
| 284 | + |
| 285 | + # fake api_response data |
| 286 | + api_response = { |
| 287 | + "location": "Boston, MA", |
| 288 | + "temperature": 38, |
| 289 | + "description": "Partly Cloudy", |
| 290 | + "icon": "partly-cloudy", |
| 291 | + "humidity": 65, |
| 292 | + "wind": {"speed": 10, "direction": "NW"}, |
| 293 | + } |
| 294 | + |
| 295 | + response = model.generate_content( |
| 296 | + [ |
| 297 | + user_prompt_content, |
| 298 | + response_function_call_content, |
| 299 | + generative_models.Content( |
| 300 | + role="user", |
| 301 | + parts=[ |
| 302 | + generative_models.Part.from_function_response( |
| 303 | + name="get_current_weather", |
| 304 | + response=api_response, |
| 305 | + ) |
| 306 | + ], |
| 307 | + ), |
| 308 | + ], |
| 309 | + tools=[weather_tool], |
| 310 | + ) |
| 311 | + assert response |
| 312 | + |
| 313 | + # Get the model summary response |
| 314 | + summary = response.candidates[0].content.parts[0].text |
| 315 | + |
| 316 | + assert summary |
0 commit comments