|
| 1 | +# Copyright 2024 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 | +# https://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 | +import os |
| 16 | + |
| 17 | +from vertexai.generative_models import GenerationResponse |
| 18 | + |
| 19 | +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") |
| 20 | + |
| 21 | + |
| 22 | +def generate_function_call() -> GenerationResponse: |
| 23 | + # [START generativeaionvertexai_gemini_function_calling] |
| 24 | + import vertexai |
| 25 | + from vertexai.generative_models import ( |
| 26 | + Content, |
| 27 | + FunctionDeclaration, |
| 28 | + GenerationConfig, |
| 29 | + GenerativeModel, |
| 30 | + Part, |
| 31 | + Tool, |
| 32 | + ) |
| 33 | + |
| 34 | + # TODO(developer): Update & uncomment below line |
| 35 | + # PROJECT_ID = "your-project-id" |
| 36 | + |
| 37 | + # Initialize Vertex AI |
| 38 | + vertexai.init(project=PROJECT_ID, location="us-central1") |
| 39 | + |
| 40 | + # Initialize Gemini model |
| 41 | + model = GenerativeModel("gemini-1.5-flash-001") |
| 42 | + |
| 43 | + # Define the user's prompt in a Content object that we can reuse in model calls |
| 44 | + user_prompt_content = Content( |
| 45 | + role="user", |
| 46 | + parts=[ |
| 47 | + Part.from_text("What is the weather like in Boston?"), |
| 48 | + ], |
| 49 | + ) |
| 50 | + |
| 51 | + # Specify a function declaration and parameters for an API request |
| 52 | + function_name = "get_current_weather" |
| 53 | + get_current_weather_func = FunctionDeclaration( |
| 54 | + name=function_name, |
| 55 | + description="Get the current weather in a given location", |
| 56 | + # Function parameters are specified in JSON schema format |
| 57 | + parameters={ |
| 58 | + "type": "object", |
| 59 | + "properties": {"location": {"type": "string", "description": "Location"}}, |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + # Define a tool that includes the above get_current_weather_func |
| 64 | + weather_tool = Tool( |
| 65 | + function_declarations=[get_current_weather_func], |
| 66 | + ) |
| 67 | + |
| 68 | + # Send the prompt and instruct the model to generate content using the Tool that you just created |
| 69 | + response = model.generate_content( |
| 70 | + user_prompt_content, |
| 71 | + generation_config=GenerationConfig(temperature=0), |
| 72 | + tools=[weather_tool], |
| 73 | + ) |
| 74 | + function_call = response.candidates[0].function_calls[0] |
| 75 | + print(function_call) |
| 76 | + |
| 77 | + # Check the function name that the model responded with, and make an API call to an external system |
| 78 | + if function_call.name == function_name: |
| 79 | + # Extract the arguments to use in your API call |
| 80 | + location = function_call.args["location"] # noqa: F841 |
| 81 | + |
| 82 | + # Here you can use your preferred method to make an API request to fetch the current weather, for example: |
| 83 | + # api_response = requests.post(weather_api_url, data={"location": location}) |
| 84 | + |
| 85 | + # In this example, we'll use synthetic data to simulate a response payload from an external API |
| 86 | + api_response = """{ "location": "Boston, MA", "temperature": 38, "description": "Partly Cloudy", |
| 87 | + "icon": "partly-cloudy", "humidity": 65, "wind": { "speed": 10, "direction": "NW" } }""" |
| 88 | + |
| 89 | + # Return the API response to Gemini so it can generate a model response or request another function call |
| 90 | + response = model.generate_content( |
| 91 | + [ |
| 92 | + user_prompt_content, # User prompt |
| 93 | + response.candidates[0].content, # Function call response |
| 94 | + Content( |
| 95 | + parts=[ |
| 96 | + Part.from_function_response( |
| 97 | + name=function_name, |
| 98 | + response={ |
| 99 | + "content": api_response, # Return the API response to Gemini |
| 100 | + }, |
| 101 | + ), |
| 102 | + ], |
| 103 | + ), |
| 104 | + ], |
| 105 | + tools=[weather_tool], |
| 106 | + ) |
| 107 | + |
| 108 | + # Get the model response |
| 109 | + print(response.text) |
| 110 | + # Example response: |
| 111 | + # The weather in Boston is partly cloudy with a temperature of 38 degrees Fahrenheit. |
| 112 | + # The humidity is 65% and the wind is blowing from the northwest at 10 mph. |
| 113 | + |
| 114 | + # [END generativeaionvertexai_gemini_function_calling] |
| 115 | + return response |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + generate_function_call() |
0 commit comments