From 5cb21c19c6c1be75ab0802a75372f0b273ad4b58 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Wed, 10 Jul 2024 13:11:17 -0700 Subject: [PATCH 1/3] Add code execution python sample --- samples/code_execution.py | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 samples/code_execution.py diff --git a/samples/code_execution.py b/samples/code_execution.py new file mode 100644 index 000000000..6b10e74cd --- /dev/null +++ b/samples/code_execution.py @@ -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_code_execution_basic(self): + # [START code_execution_basic] + model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools="code_execution") + result = model.generate_content( + "What's the sum of the sum of first 200 prime numbers? Make sure you get all 200." + ) + print(result.text) + # [END code_execution_basic] + + def test_code_execution_request_override(self): + # [START code_execution_request_override] + model = genai.GenerativeModel(model_name="gemini-1.5-flash") + result = model.generate_content( + "Write code to count how many letter r in the word strawberry", tools="code_execution" + ) + print(result.text) + # [END code_execution_request_override] + + def test_code_execution_chat(self): + # [START code_execution_chat] + model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools="code_execution") + chat = model.start_chat() + result = chat.send_message( + "Can you run some code to bogo-sort this list of numbers?: [2,34,1,65,4]" + ) + print(result.text) + # [END code_execution_chat] + + +if __name__ == "__main__": + absltest.main() From 1e45e494959fc87c3fd6082ae88a997f092e71da Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Wed, 10 Jul 2024 13:32:12 -0700 Subject: [PATCH 2/3] Sync with docs. --- samples/code_execution.py | 142 +++++++++++++++++++++++++++++++++----- 1 file changed, 126 insertions(+), 16 deletions(-) diff --git a/samples/code_execution.py b/samples/code_execution.py index 6b10e74cd..6538326cf 100644 --- a/samples/code_execution.py +++ b/samples/code_execution.py @@ -20,31 +20,141 @@ class UnitTests(absltest.TestCase): def test_code_execution_basic(self): # [START code_execution_basic] - model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools="code_execution") - result = model.generate_content( - "What's the sum of the sum of first 200 prime numbers? Make sure you get all 200." - ) - print(result.text) + model = genai.GenerativeModel( + model_name='gemini-1.5-flash', + tools='code_execution') + response = model.generate_content(( + 'What is the sum of the first 50 prime numbers? ' + 'Generate and run code for the calculation, and make sure you get all 50.')) + print(response.text) # [END code_execution_basic] + # [START code_execution_basic_return] + # ``` python + # def is_prime(n): + # """ + # Checks if a number is prime. + # """ + # if n <= 1: + # return False + # for i in range(2, int(n**0.5) + 1): + # if n % i == 0: + # return False + # return True + # + # primes = [] + # num = 2 + # count = 0 + # while count < 50: + # if is_prime(num): + # primes.append(num) + # count += 1 + # num += 1 + # + # print(f'The first 50 prime numbers are: {primes}') + # print(f'The sum of the first 50 prime numbers is: {sum(primes)}') + # + # ``` + # ``` + # The first 50 prime numbers are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229] + # The sum of the first 50 prime numbers is: 5117 + # + # ``` + # The code generated a list of the first 50 prime numbers, then sums the list to find the answer. + # + # The sum of the first 50 prime numbers is **5117**. + # [END code_execution_basic_return] + def test_code_execution_request_override(self): # [START code_execution_request_override] - model = genai.GenerativeModel(model_name="gemini-1.5-flash") - result = model.generate_content( - "Write code to count how many letter r in the word strawberry", tools="code_execution" - ) - print(result.text) + model = genai.GenerativeModel(model_name='gemini-1.5-pro') + response = model.generate_content( + ('What is the sum of the first 50 prime numbers? ' + 'Generate and run code for the calculation, and make sure you get all 50.'), + tools='code_execution') + print(response.text) # [END code_execution_request_override] - + # [START code_execution_request_override_return] + # ``` python + # def is_prime(n): + # """ + # Checks if a number is prime. + # """ + # if n <= 1: + # return False + # for i in range(2, int(n**0.5) + 1): + # if n % i == 0: + # return False + # return True + # + # primes = [] + # num = 2 + # count = 0 + # while count < 50: + # if is_prime(num): + # primes.append(num) + # count += 1 + # num += 1 + # + # print(f'The first 50 prime numbers are: {primes}') + # print(f'The sum of the first 50 prime numbers is: {sum(primes)}') + # + # ``` + # ``` + # The first 50 prime numbers are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229] + # The sum of the first 50 prime numbers is: 5117 + # + # ``` + # The code generated a list of the first 50 prime numbers, then sums the list to find the answer. + # + # The sum of the first 50 prime numbers is **5117**. + # [END code_execution_request_override_return] + def test_code_execution_chat(self): # [START code_execution_chat] - model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools="code_execution") + model = genai.GenerativeModel(model_name='gemini-1.5-pro', + tools='code_execution') chat = model.start_chat() - result = chat.send_message( - "Can you run some code to bogo-sort this list of numbers?: [2,34,1,65,4]" - ) - print(result.text) + response = chat.send_message(( + 'What is the sum of the first 50 prime numbers? ' + 'Generate and run code for the calculation, and make sure you get all 50.')) + print(response.text) # [END code_execution_chat] + # [START code_execution_chat_return] + # ``` python + # def is_prime(n): + # """ + # Checks if a number is prime. + # """ + # if n <= 1: + # return False + # for i in range(2, int(n**0.5) + 1): + # if n % i == 0: + # return False + # return True + # + # primes = [] + # num = 2 + # count = 0 + # while count < 50: + # if is_prime(num): + # primes.append(num) + # count += 1 + # num += 1 + # + # print(f'The first 50 prime numbers are: {primes}') + # print(f'The sum of the first 50 prime numbers is: {sum(primes)}') + # + # ``` + # ``` + # The first 50 prime numbers are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229] + # The sum of the first 50 prime numbers is: 5117 + # + # ``` + # The code generated a list of the first 50 prime numbers, then sums the list to find the answer. + # + # The sum of the first 50 prime numbers is **5117**. + # [END code_execution_chat_return] if __name__ == "__main__": From 586648ae57aac5b84dff6a252c1b8d281cd62dad Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Wed, 10 Jul 2024 13:33:20 -0700 Subject: [PATCH 3/3] format Change-Id: Id75a4c1936a13a63f1e22f36d5b7011c24e31233 --- samples/code_execution.py | 45 ++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/samples/code_execution.py b/samples/code_execution.py index 6538326cf..cd82d676d 100644 --- a/samples/code_execution.py +++ b/samples/code_execution.py @@ -20,12 +20,13 @@ class UnitTests(absltest.TestCase): def test_code_execution_basic(self): # [START code_execution_basic] - model = genai.GenerativeModel( - model_name='gemini-1.5-flash', - tools='code_execution') - response = model.generate_content(( - 'What is the sum of the first 50 prime numbers? ' - 'Generate and run code for the calculation, and make sure you get all 50.')) + model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools="code_execution") + response = model.generate_content( + ( + "What is the sum of the first 50 prime numbers? " + "Generate and run code for the calculation, and make sure you get all 50." + ) + ) print(response.text) # [END code_execution_basic] # [START code_execution_basic_return] @@ -61,17 +62,19 @@ def test_code_execution_basic(self): # ``` # The code generated a list of the first 50 prime numbers, then sums the list to find the answer. # - # The sum of the first 50 prime numbers is **5117**. + # The sum of the first 50 prime numbers is **5117**. # [END code_execution_basic_return] - def test_code_execution_request_override(self): # [START code_execution_request_override] - model = genai.GenerativeModel(model_name='gemini-1.5-pro') + model = genai.GenerativeModel(model_name="gemini-1.5-pro") response = model.generate_content( - ('What is the sum of the first 50 prime numbers? ' - 'Generate and run code for the calculation, and make sure you get all 50.'), - tools='code_execution') + ( + "What is the sum of the first 50 prime numbers? " + "Generate and run code for the calculation, and make sure you get all 50." + ), + tools="code_execution", + ) print(response.text) # [END code_execution_request_override] # [START code_execution_request_override_return] @@ -107,17 +110,19 @@ def test_code_execution_request_override(self): # ``` # The code generated a list of the first 50 prime numbers, then sums the list to find the answer. # - # The sum of the first 50 prime numbers is **5117**. + # The sum of the first 50 prime numbers is **5117**. # [END code_execution_request_override_return] - + def test_code_execution_chat(self): # [START code_execution_chat] - model = genai.GenerativeModel(model_name='gemini-1.5-pro', - tools='code_execution') + model = genai.GenerativeModel(model_name="gemini-1.5-pro", tools="code_execution") chat = model.start_chat() - response = chat.send_message(( - 'What is the sum of the first 50 prime numbers? ' - 'Generate and run code for the calculation, and make sure you get all 50.')) + response = chat.send_message( + ( + "What is the sum of the first 50 prime numbers? " + "Generate and run code for the calculation, and make sure you get all 50." + ) + ) print(response.text) # [END code_execution_chat] # [START code_execution_chat_return] @@ -153,7 +158,7 @@ def test_code_execution_chat(self): # ``` # The code generated a list of the first 50 prime numbers, then sums the list to find the answer. # - # The sum of the first 50 prime numbers is **5117**. + # The sum of the first 50 prime numbers is **5117**. # [END code_execution_chat_return]