|
| 1 | +# Copyright 2025 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 | + |
| 16 | +def generate_content() -> str: |
| 17 | + # [START googlegenaisdk_tools_func_desc_with_txt] |
| 18 | + from google import genai |
| 19 | + from google.genai.types import FunctionDeclaration, Tool, GenerateContentConfig |
| 20 | + |
| 21 | + client = genai.Client() |
| 22 | + model_id = "gemini-2.0-flash-exp" |
| 23 | + |
| 24 | + get_album_sales = FunctionDeclaration( |
| 25 | + name="get_album_sales", |
| 26 | + description="Gets the number of albums sold", |
| 27 | + # Function parameters are specified in JSON schema format |
| 28 | + parameters={ |
| 29 | + "type": "OBJECT", |
| 30 | + "properties": { |
| 31 | + "albums": { |
| 32 | + "type": "ARRAY", |
| 33 | + "description": "List of albums", |
| 34 | + "items": { |
| 35 | + "description": "Album and its sales", |
| 36 | + "type": "OBJECT", |
| 37 | + "properties": { |
| 38 | + "album_name": { |
| 39 | + "type": "STRING", |
| 40 | + "description": "Name of the music album", |
| 41 | + }, |
| 42 | + "copies_sold": { |
| 43 | + "type": "INTEGER", |
| 44 | + "description": "Number of copies sold", |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + ) |
| 52 | + |
| 53 | + sales_tool = Tool( |
| 54 | + function_declarations=[get_album_sales], |
| 55 | + ) |
| 56 | + |
| 57 | + response = client.models.generate_content( |
| 58 | + model=model_id, |
| 59 | + contents='At Stellar Sounds, a music label, 2024 was a rollercoaster. "Echoes of the Night," a debut synth-pop album, ' |
| 60 | + 'surprisingly sold 350,000 copies, while veteran rock band "Crimson Tide\'s" latest, "Reckless Hearts," ' |
| 61 | + 'lagged at 120,000. Their up-and-coming indie artist, "Luna Bloom\'s" EP, "Whispers of Dawn," ' |
| 62 | + 'secured 75,000 sales. The biggest disappointment was the highly-anticipated rap album "Street Symphony" ' |
| 63 | + "only reaching 100,000 units. Overall, Stellar Sounds moved over 645,000 units this year, revealing unexpected " |
| 64 | + "trends in music consumption.", |
| 65 | + config=GenerateContentConfig( |
| 66 | + tools=[sales_tool], |
| 67 | + temperature=0, |
| 68 | + ), |
| 69 | + ) |
| 70 | + |
| 71 | + print(response.candidates[0].content.parts[0].function_call) |
| 72 | + # Example response: |
| 73 | + # [FunctionCall( |
| 74 | + # id=None, |
| 75 | + # name="get_album_sales", |
| 76 | + # args={ |
| 77 | + # "albums": [ |
| 78 | + # {"album_name": "Echoes of the Night", "copies_sold": 350000}, |
| 79 | + # {"copies_sold": 120000, "album_name": "Reckless Hearts"}, |
| 80 | + # {"copies_sold": 75000, "album_name": "Whispers of Dawn"}, |
| 81 | + # {"copies_sold": 100000, "album_name": "Street Symphony"}, |
| 82 | + # ] |
| 83 | + # }, |
| 84 | + # )] |
| 85 | + |
| 86 | + # [END googlegenaisdk_tools_func_desc_with_txt] |
| 87 | + return str(response.candidates[0].content.parts[0].function_call) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + generate_content() |
0 commit comments