Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: (Gen AI): Add sample for Gemini parallel function calling example #12637

109 changes: 109 additions & 0 deletions generative_ai/function_calling/parallel_function_calling_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os

from vertexai.generative_models import ChatSession

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")


def generate_parallel_function_calls() -> ChatSession:
# [START generativeaionvertexai_gemini_parallel_function_calling_example]
msampathkumar marked this conversation as resolved.
Show resolved Hide resolved
import vertexai
from vertexai.generative_models import (
FunctionDeclaration,
GenerationConfig,
GenerativeModel,
Part,
Tool,
)

# TODO(developer): Update & uncomment below line
# PROJECT_ID = "your-project-id"

# Initialize Vertex AI
vertexai.init(project=PROJECT_ID, location="us-central1")

# Specify a function declaration and parameters for an API request
function_name = "get_current_weather"
get_current_weather_func = FunctionDeclaration(
name=function_name,
description="Get the current weather in a given location",
parameters={
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The location to whih to get the weather. Can be a city name, a city name and state, or a zip code. Examples: 'San Francisco', 'San Francisco, CA', '95616', etc."
},
},
},
)

# In this example, we'll use synthetic data to simulate a response payload from an external API
def mock_weather_api_service(location: str) -> str:
temperature = 25 if location == "San Francisco" else 35
return f"""{{ "location": "{location}", "temperature": {temperature}, "unit": "C" }}"""

# Define a tool that includes the above function
tools = Tool(
function_declarations=[get_current_weather_func],
)

# Initialize Gemini model
model = GenerativeModel(
model_name="gemini-1.5-pro-001",
Valeriy-Burlaka marked this conversation as resolved.
Show resolved Hide resolved
generation_config=GenerationConfig(temperature=0),
tools=[tools],
)

# Start a chat session
chat = model.start_chat()
response = chat.send_message("Compare the weather in New Delhi and San Francisco")

function_calls = response.candidates[0].function_calls
print("Suggested finction calls:\n", function_calls)

if function_calls:
api_responses = []
for func in function_calls:
if func.name == function_name:
api_responses.append({
"content": mock_weather_api_service(location=func.args["location"])
})

# Return the API response to Gemini
response = chat.send_message(
[
Part.from_function_response(
name="get_current_weather",
response=api_responses[0],
),
Part.from_function_response(
name="get_current_weather",
response=api_responses[1],
),
],
)

print(response.text)
# Example response:
# The weather in New Delhi is 35 degrees Celsius and the weather in San Francisco is 25 degrees Celsius. New Delhi is warmer than San Francisco.

# [END generativeaionvertexai_gemini_parallel_function_calling_example]
return response


if __name__ == "__main__":
generate_parallel_function_calls()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import backoff
Valeriy-Burlaka marked this conversation as resolved.
Show resolved Hide resolved

from google.api_core.exceptions import ResourceExhausted

import parallel_function_calling_example


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def test_parallel_function_calling() -> None:
response = parallel_function_calling_example.generate_parallel_function_calls()
assert response is not None