Skip to content

Commit ac8aaaa

Browse files
feat: convert markdown sample for function calling to python
1 parent d9b2f9c commit ac8aaaa

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 typing import List
18+
19+
from vertexai.generative_models import FunctionDeclaration
20+
21+
22+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
23+
24+
25+
def prototype():
26+
# [START TBD]
27+
# Define a function. Could be a local function or you can import the requests library to call an API
28+
def multiply_numbers(numbers: List[int]) -> int:
29+
"""
30+
Calculates the product of all numbers in an array.
31+
32+
Args:
33+
numbers: An array of numbers to be multiplied.
34+
35+
Returns:
36+
The product of all the numbers. If the array is empty, returns 1.
37+
"""
38+
39+
if not numbers: # Handle empty array
40+
return 1
41+
42+
product = 1
43+
for num in numbers:
44+
product *= num
45+
46+
return product
47+
48+
multiply_number_func = FunctionDeclaration.from_func(multiply_numbers)
49+
50+
'''
51+
multiply_number_func contains the following schema:
52+
53+
name: "multiply_numbers"
54+
description: "Calculates the product of all numbers in an array."
55+
parameters {
56+
type_: OBJECT
57+
properties {
58+
key: "numbers"
59+
value {
60+
description: "An array of numbers to be multiplied."
61+
title: "Numbers"
62+
}
63+
}
64+
required: "numbers"
65+
description: "Calculates the product of all numbers in an array."
66+
title: "multiply_numbers"
67+
}
68+
'''
69+
# [END TBD]
70+
return multiply_number_func
71+
72+
73+
if __name__ == "__main__":
74+
prototype()

generative_ai/function_calling/test_function_calling.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
import backoff
1616

1717
from google.api_core.exceptions import ResourceExhausted
18+
from vertexai.generative_models import GenerativeModel, Tool
1819

1920
import advanced_example
2021
import basic_example
2122
import chat_example
2223
import chat_function_calling_basic
2324
import chat_function_calling_config
2425
import parallel_function_calling_example
26+
import declare_function_from_function
2527

2628

2729
@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
@@ -85,3 +87,13 @@ def test_function_calling_chat() -> None:
8587
def test_parallel_function_calling() -> None:
8688
response = parallel_function_calling_example.parallel_function_calling_example()
8789
assert response is not None
90+
91+
92+
def test_prototype() -> None:
93+
func_declaration = declare_function_from_function.prototype()
94+
tools = Tool(function_declarations=[func_declaration])
95+
model = GenerativeModel(model_name="gemini-1.5-pro-002", tools=[tools])
96+
chat_session = model.start_chat()
97+
response = chat_session.send_message("What will be 1 multiplied by 2?")
98+
99+
assert response is not None

0 commit comments

Comments
 (0)