Skip to content

Rename json_mode.py and create function calling sample #406

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion google/generativeai/types/content_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
File renamed without changes.
27 changes: 14 additions & 13 deletions samples/count_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]

Expand All @@ -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]


Expand Down
51 changes: 51 additions & 0 deletions samples/function_calling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- 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


class UnitTests(absltest.TestCase):
def test_function_calling(self):
# [START function_calling]
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(
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?"
)
print(response.text)
# [END function_calling]


if __name__ == "__main__":
absltest.main()
4 changes: 4 additions & 0 deletions samples/text_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,15 @@ 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] # 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)

@parameterized.named_parameters(
["string", "code_execution"],
["proto_object", protos.CodeExecution()],
Expand Down