-
Notifications
You must be signed in to change notification settings - Fork 409
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
Add code execution python sample #451
Merged
MarkDaoust
merged 3 commits into
google-gemini:main
from
shilpakancharla:code_exec_python
Jul 10, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
# -*- 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") | ||
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-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-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." | ||
) | ||
) | ||
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__": | ||
absltest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this use
executableCode
andexecutableResult
?