|
| 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() |
0 commit comments