From eeebbe7842378c9d850e64ca7694380a16edd9b3 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 27 Jun 2024 15:33:16 -0700 Subject: [PATCH 1/7] Rename json_mode.py and create function calling sample --- ...{json_mode.py => controlled_generation.py} | 0 samples/function_calling.py | 49 +++++++++++++++++++ 2 files changed, 49 insertions(+) rename samples/{json_mode.py => controlled_generation.py} (100%) create mode 100644 samples/function_calling.py diff --git a/samples/json_mode.py b/samples/controlled_generation.py similarity index 100% rename from samples/json_mode.py rename to samples/controlled_generation.py diff --git a/samples/function_calling.py b/samples/function_calling.py new file mode 100644 index 000000000..c2998fd74 --- /dev/null +++ b/samples/function_calling.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# Copyright 2023 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 +# +# http://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. +from absl.testing import absltest + +import google.generativeai as genai + + +def enable_lights(): + """Turn on the lighting system.""" + print("LIGHTBOT: Lights enabled.") + + +def set_light_color(rgb_hex: str): + """Set the light color. Lights must be enabled for this to work.""" + print(f"LIGHTBOT: Lights set to {rgb_hex}.") + + +def stop_lights(): + """Stop flashing lights.""" + print("LIGHTBOT: Lights turned off.") + + +class UnitTests(absltest.TestCase): + def test_function_calling(self): + # [START function_calling] + light_controls = [enable_lights, set_light_color, stop_lights] + instruction = "You are a helpful lighting system bot. You can turn lights on and off, and you can set the color. Do not perform any other tasks." + model = genai.GenerativeModel( + "models/gemini-1.5-pro", tools=light_controls, system_instruction=instruction + ) + response = model.generate_content(contents="Hello light-bot, what can you do?") + print(response.text) + # [END function_calling] + + +if __name__ == "__main__": + absltest.main() From 953e6dcd6ac2733e5194e4f3a7ef92cb1e113d39 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Fri, 28 Jun 2024 10:05:24 -0700 Subject: [PATCH 2/7] Move functions inside test case --- samples/function_calling.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/samples/function_calling.py b/samples/function_calling.py index c2998fd74..21ad71f0b 100644 --- a/samples/function_calling.py +++ b/samples/function_calling.py @@ -17,24 +17,21 @@ import google.generativeai as genai -def enable_lights(): - """Turn on the lighting system.""" - print("LIGHTBOT: Lights enabled.") - - -def set_light_color(rgb_hex: str): - """Set the light color. Lights must be enabled for this to work.""" - print(f"LIGHTBOT: Lights set to {rgb_hex}.") - - -def stop_lights(): - """Stop flashing lights.""" - print("LIGHTBOT: Lights turned off.") - - class UnitTests(absltest.TestCase): def test_function_calling(self): # [START function_calling] + def enable_lights(): + """Turn on the lighting system.""" + print("LIGHTBOT: Lights enabled.") + + def set_light_color(rgb_hex: str): + """Set the light color. Lights must be enabled for this to work.""" + print(f"LIGHTBOT: Lights set to {rgb_hex}.") + + def stop_lights(): + """Stop flashing lights.""" + print("LIGHTBOT: Lights turned off.") + light_controls = [enable_lights, set_light_color, stop_lights] instruction = "You are a helpful lighting system bot. You can turn lights on and off, and you can set the color. Do not perform any other tasks." model = genai.GenerativeModel( From 67eefd2dfdb1adfdf6837fbdbb20088a3b076afd Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Fri, 28 Jun 2024 15:32:57 -0700 Subject: [PATCH 3/7] fix lightbot Change-Id: If4201ef6e0d0282aec685ed36b935b376f907856 --- google/generativeai/types/content_types.py | 4 +++- samples/function_calling.py | 8 +++++--- tests/test_content.py | 11 +++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/google/generativeai/types/content_types.py b/google/generativeai/types/content_types.py index 74f03029c..64de045ed 100644 --- a/google/generativeai/types/content_types.py +++ b/google/generativeai/types/content_types.py @@ -369,7 +369,9 @@ def _schema_for_function( ) ) ] - schema = dict(name=f.__name__, description=f.__doc__, parameters=parameters) + schema = dict(name=f.__name__, description=f.__doc__) + if parameters['properties']: + schema['parameters'] = parameters return schema diff --git a/samples/function_calling.py b/samples/function_calling.py index 21ad71f0b..54dd1be94 100644 --- a/samples/function_calling.py +++ b/samples/function_calling.py @@ -33,12 +33,14 @@ def stop_lights(): print("LIGHTBOT: Lights turned off.") light_controls = [enable_lights, set_light_color, stop_lights] - instruction = "You are a helpful lighting system bot. You can turn lights on and off, and you can set the color. Do not perform any other tasks." + instruction = ( + "You are a helpful lighting system bot. You can turn lights on and off, " + "and you can set the color. Do not perform any other tasks.") model = genai.GenerativeModel( "models/gemini-1.5-pro", tools=light_controls, system_instruction=instruction ) - response = model.generate_content(contents="Hello light-bot, what can you do?") - print(response.text) + response = model.generate_content(contents="Red Alert!") + print(response.candidates[0].content.parts) # [END function_calling] diff --git a/tests/test_content.py b/tests/test_content.py index 5b7aa9781..97a9aeaac 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -378,6 +378,17 @@ def test_to_tools(self, tools): self.assertEqual(tools, expected) + def test_empty_function(self): + def no_args(): + print('hello') + + fd = content_types.to_function_library(no_args).to_proto()[0] + fd = type(fd).to_dict(fd, including_default_value_fields=False) + # parameters are not set. + self.assertEqual({'function_declarations': [{'name': 'no_args'}]}, fd) + + + @parameterized.named_parameters( ["string", "code_execution"], ["proto_object", protos.CodeExecution()], From 98e465ddb61eb35ade8c852f4fcfb484295a2c6f Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Fri, 28 Jun 2024 15:35:11 -0700 Subject: [PATCH 4/7] format Change-Id: Ib052095b489a28ca5a2fc7338c8fefd5ac0adbc5 --- google/generativeai/types/content_types.py | 4 ++-- samples/function_calling.py | 3 ++- tests/test_content.py | 6 ++---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/google/generativeai/types/content_types.py b/google/generativeai/types/content_types.py index 64de045ed..82d36f7c5 100644 --- a/google/generativeai/types/content_types.py +++ b/google/generativeai/types/content_types.py @@ -370,8 +370,8 @@ def _schema_for_function( ) ] schema = dict(name=f.__name__, description=f.__doc__) - if parameters['properties']: - schema['parameters'] = parameters + if parameters["properties"]: + schema["parameters"] = parameters return schema diff --git a/samples/function_calling.py b/samples/function_calling.py index 54dd1be94..5ccebb68b 100644 --- a/samples/function_calling.py +++ b/samples/function_calling.py @@ -35,7 +35,8 @@ def stop_lights(): light_controls = [enable_lights, set_light_color, stop_lights] instruction = ( "You are a helpful lighting system bot. You can turn lights on and off, " - "and you can set the color. Do not perform any other tasks.") + "and you can set the color. Do not perform any other tasks." + ) model = genai.GenerativeModel( "models/gemini-1.5-pro", tools=light_controls, system_instruction=instruction ) diff --git a/tests/test_content.py b/tests/test_content.py index 97a9aeaac..8d4c06255 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -380,14 +380,12 @@ def test_to_tools(self, tools): def test_empty_function(self): def no_args(): - print('hello') + print("hello") fd = content_types.to_function_library(no_args).to_proto()[0] fd = type(fd).to_dict(fd, including_default_value_fields=False) # parameters are not set. - self.assertEqual({'function_declarations': [{'name': 'no_args'}]}, fd) - - + self.assertEqual({"function_declarations": [{"name": "no_args"}]}, fd) @parameterized.named_parameters( ["string", "code_execution"], From ca7d86ecbf9aad15dfc1fb31ac357324cf6f25ad Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Fri, 28 Jun 2024 15:39:09 -0700 Subject: [PATCH 5/7] Function calling now passing tests --- samples/function_calling.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/samples/function_calling.py b/samples/function_calling.py index 21ad71f0b..8832408cf 100644 --- a/samples/function_calling.py +++ b/samples/function_calling.py @@ -20,24 +20,29 @@ class UnitTests(absltest.TestCase): def test_function_calling(self): # [START function_calling] - def enable_lights(): - """Turn on the lighting system.""" - print("LIGHTBOT: Lights enabled.") + def add(a: float, b: float): + """returns a + b.""" + return a + b - def set_light_color(rgb_hex: str): - """Set the light color. Lights must be enabled for this to work.""" - print(f"LIGHTBOT: Lights set to {rgb_hex}.") + def subtract(a: float, b: float): + """returns a - b.""" + return a - b - def stop_lights(): - """Stop flashing lights.""" - print("LIGHTBOT: Lights turned off.") + def multiply(a: float, b: float): + """returns a * b.""" + return a * b + + def divide(a: float, b: float): + """returns a / b.""" + return a / b - light_controls = [enable_lights, set_light_color, stop_lights] - instruction = "You are a helpful lighting system bot. You can turn lights on and off, and you can set the color. Do not perform any other tasks." model = genai.GenerativeModel( - "models/gemini-1.5-pro", tools=light_controls, system_instruction=instruction + model_name="gemini-1.5-flash", tools=[add, subtract, multiply, divide] + ) + chat = model.start_chat(enable_automatic_function_calling=True) + response = chat.send_message( + "I have 57 cats, each owns 44 mittens, how many mittens is that in total?" ) - response = model.generate_content(contents="Hello light-bot, what can you do?") print(response.text) # [END function_calling] From 827767229324fc9d70e534184d2449b132c06232 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Fri, 28 Jun 2024 16:19:14 -0700 Subject: [PATCH 6/7] type:ignore --- tests/test_content.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_content.py b/tests/test_content.py index 8d4c06255..54d8ea15d 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -382,7 +382,7 @@ def test_empty_function(self): def no_args(): print("hello") - fd = content_types.to_function_library(no_args).to_proto()[0] + fd = content_types.to_function_library(no_args).to_proto()[0] # type: ignore fd = type(fd).to_dict(fd, including_default_value_fields=False) # parameters are not set. self.assertEqual({"function_declarations": [{"name": "no_args"}]}, fd) From ab4cd55b39be0f8b6d8a886b42a8a83780b2a227 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Fri, 28 Jun 2024 16:27:22 -0700 Subject: [PATCH 7/7] format Change-Id: I1fea8ebb0e7c7874fdac6743a35e9be37a67301c --- samples/count_tokens.py | 27 ++++++++++++++------------- samples/text_generation.py | 4 ++++ tests/test_content.py | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/samples/count_tokens.py b/samples/count_tokens.py index 81bedeb4b..42c40d8e1 100644 --- a/samples/count_tokens.py +++ b/samples/count_tokens.py @@ -20,9 +20,6 @@ media = pathlib.Path(__file__).parents[1] / "third_party" - - - class UnitTests(absltest.TestCase): def test_tokens_text_only(self): # [START tokens_text_only] @@ -84,8 +81,10 @@ def test_tokens_cached_content(self): def test_tokens_system_instruction(self): # [START tokens_system_instruction] document = genai.upload_file(path=media / "a11.txt") - model = genai.GenerativeModel("models/gemini-1.5-flash-001", - system_instruction="You are an expert analyzing transcripts. Give a summary of this document.") + model = genai.GenerativeModel( + "models/gemini-1.5-flash-001", + system_instruction="You are an expert analyzing transcripts. Give a summary of this document.", + ) print(model.count_tokens(document)) # [END tokens_system_instruction] @@ -95,25 +94,27 @@ def add(a: float, b: float): """returns a + b.""" return a + b - def subtract(a: float, b: float): """returns a - b.""" return a - b - def multiply(a: float, b: float): """returns a * b.""" return a * b - def divide(a: float, b: float): """returns a / b.""" return a / b - - model = genai.GenerativeModel("models/gemini-1.5-flash-001", - tools=[add, subtract, multiply, divide]) - - print(model.count_tokens("I have 57 cats, each owns 44 mittens, how many mittens is that in total?")) + + model = genai.GenerativeModel( + "models/gemini-1.5-flash-001", tools=[add, subtract, multiply, divide] + ) + + print( + model.count_tokens( + "I have 57 cats, each owns 44 mittens, how many mittens is that in total?" + ) + ) # [END tokens_tools] diff --git a/samples/text_generation.py b/samples/text_generation.py index 015a00e1f..6ba793dfa 100644 --- a/samples/text_generation.py +++ b/samples/text_generation.py @@ -41,6 +41,7 @@ def test_text_gen_text_only_prompt_streaming(self): def test_text_gen_multimodal_one_image_prompt(self): # [START text_gen_multimodal_one_image_prompt] import PIL + model = genai.GenerativeModel("gemini-1.5-flash") organ = PIL.Image.open(media / "organ.jpg") response = model.generate_content(["Tell me about this instrument", organ]) @@ -50,6 +51,7 @@ def test_text_gen_multimodal_one_image_prompt(self): def test_text_gen_multimodal_one_image_prompt_streaming(self): # [START text_gen_multimodal_one_image_prompt_streaming] import PIL + model = genai.GenerativeModel("gemini-1.5-flash") organ = PIL.Image.open(media / "organ.jpg") response = model.generate_content(["Tell me about this instrument", organ], stream=True) @@ -61,6 +63,7 @@ def test_text_gen_multimodal_one_image_prompt_streaming(self): def test_text_gen_multimodal_multi_image_prompt(self): # [START text_gen_multimodal_multi_image_prompt] import PIL + model = genai.GenerativeModel("gemini-1.5-flash") organ = PIL.Image.open(media / "organ.jpg") cajun_instrument = PIL.Image.open(media / "Cajun_instruments.jpg") @@ -73,6 +76,7 @@ def test_text_gen_multimodal_multi_image_prompt(self): def test_text_gen_multimodal_multi_image_prompt_streaming(self): # [START text_gen_multimodal_multi_image_prompt_streaming] import PIL + model = genai.GenerativeModel("gemini-1.5-flash") organ = PIL.Image.open(media / "organ.jpg") cajun_instrument = PIL.Image.open(media / "Cajun_instruments.jpg") diff --git a/tests/test_content.py b/tests/test_content.py index 54d8ea15d..b52858bb8 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -382,7 +382,7 @@ def test_empty_function(self): def no_args(): print("hello") - fd = content_types.to_function_library(no_args).to_proto()[0] # type: ignore + fd = content_types.to_function_library(no_args).to_proto()[0] # type: ignore fd = type(fd).to_dict(fd, including_default_value_fields=False) # parameters are not set. self.assertEqual({"function_declarations": [{"name": "no_args"}]}, fd)