diff --git a/samples/cache.py b/samples/cache.py index 82c4c1d7d..1d4dd6e47 100644 --- a/samples/cache.py +++ b/samples/cache.py @@ -14,7 +14,6 @@ # limitations under the License. from absl.testing import absltest -import google.generativeai as genai import pathlib @@ -24,6 +23,8 @@ class UnitTests(absltest.TestCase): def test_cache_create(self): # [START cache_create] + import google.generativeai as genai + document = genai.upload_file(path=media / "a11.txt") model_name = "gemini-1.5-flash-001" cache = genai.caching.CachedContent.create( @@ -41,6 +42,8 @@ def test_cache_create(self): def test_cache_create_from_name(self): # [START cache_create_from_name] + import google.generativeai as genai + document = genai.upload_file(path=media / "a11.txt") model_name = "gemini-1.5-flash-001" cache = genai.caching.CachedContent.create( @@ -60,6 +63,8 @@ def test_cache_create_from_name(self): def test_cache_create_from_chat(self): # [START cache_create_from_chat] + import google.generativeai as genai + model_name = "gemini-1.5-flash-001" system_instruction = "You are an expert analyzing transcripts." @@ -92,6 +97,8 @@ def test_cache_create_from_chat(self): def test_cache_delete(self): # [START cache_delete] + import google.generativeai as genai + document = genai.upload_file(path=media / "a11.txt") model_name = "gemini-1.5-flash-001" cache = genai.caching.CachedContent.create( @@ -104,6 +111,8 @@ def test_cache_delete(self): def test_cache_get(self): # [START cache_get] + import google.generativeai as genai + document = genai.upload_file(path=media / "a11.txt") model_name = "gemini-1.5-flash-001" cache = genai.caching.CachedContent.create( @@ -117,6 +126,8 @@ def test_cache_get(self): def test_cache_list(self): # [START cache_list] + import google.generativeai as genai + document = genai.upload_file(path=media / "a11.txt") model_name = "gemini-1.5-flash-001" cache = genai.caching.CachedContent.create( @@ -132,6 +143,8 @@ def test_cache_list(self): def test_cache_update(self): # [START cache_update] + import google.generativeai as genai + import datetime document = genai.upload_file(path=media / "a11.txt") diff --git a/samples/chat.py b/samples/chat.py index 5089450d9..d4d77eeca 100644 --- a/samples/chat.py +++ b/samples/chat.py @@ -14,7 +14,6 @@ # limitations under the License. from absl.testing import absltest -import google.generativeai as genai import pathlib media = pathlib.Path(__file__).parents[1] / "third_party" @@ -23,6 +22,8 @@ class UnitTests(absltest.TestCase): def test_chat(self): # [START chat] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") chat = model.start_chat( history=[ @@ -38,6 +39,8 @@ def test_chat(self): def test_chat_streaming(self): # [START chat_streaming] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") chat = model.start_chat( history=[ @@ -59,6 +62,8 @@ def test_chat_streaming(self): def test_chat_streaming_with_images(self): # [START chat_streaming_with_images] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") chat = model.start_chat() diff --git a/samples/code_execution.py b/samples/code_execution.py index 9b5ad3638..dfc2ce84e 100644 --- a/samples/code_execution.py +++ b/samples/code_execution.py @@ -14,12 +14,12 @@ # 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] + import google.generativeai as genai + model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools="code_execution") response = model.generate_content( ( @@ -38,6 +38,8 @@ def test_code_execution_basic(self): # [END code_execution_basic] # [START code_execution_basic_return] + import google.generativeai as genai + # text: "I can help with that! To calculate the sum of the first 50 prime numbers, we\'ll need to first identify all the prime numbers up to the 50th prime number. \n\nHere is the code to find and sum the first 50 prime numbers:\n\n" # # executable_code { @@ -92,6 +94,8 @@ def test_code_execution_basic(self): def test_code_execution_request_override(self): # [START code_execution_request_override] + import google.generativeai as genai + model = genai.GenerativeModel(model_name="gemini-1.5-flash") response = model.generate_content( ( @@ -103,6 +107,8 @@ def test_code_execution_request_override(self): print(response.text) # [END code_execution_request_override] # [START code_execution_request_override_return] + import google.generativeai as genai + # ``` python # def is_prime(n): # """ @@ -140,6 +146,8 @@ def test_code_execution_request_override(self): def test_code_execution_chat(self): # [START code_execution_chat] + import google.generativeai as genai + model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools="code_execution") chat = model.start_chat() response = chat.send_message('Can you print "Hello world!"?') @@ -152,6 +160,8 @@ def test_code_execution_chat(self): print(response.text) # [END code_execution_chat] # [START code_execution_chat_return] + import google.generativeai as genai + # ``` python # def is_prime(n): # """ diff --git a/samples/configure_model_parameters.py b/samples/configure_model_parameters.py index ecd39d312..3fe28a5a8 100644 --- a/samples/configure_model_parameters.py +++ b/samples/configure_model_parameters.py @@ -14,12 +14,12 @@ # limitations under the License. from absl.testing import absltest -import google.generativeai as genai - class UnitTests(absltest.TestCase): def test_configure_model(self): # [START configure_model_parameters] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") response = model.generate_content( "Tell me a story about a magic backpack.", diff --git a/samples/controlled_generation.py b/samples/controlled_generation.py index 042209a72..5caa9b7d4 100644 --- a/samples/controlled_generation.py +++ b/samples/controlled_generation.py @@ -13,7 +13,6 @@ from absl.testing import absltest import pathlib -import google.generativeai as genai media = pathlib.Path(__file__).parents[1] / "third_party" @@ -21,6 +20,8 @@ class UnitTests(absltest.TestCase): def test_json_controlled_generation(self): # [START json_controlled_generation] + import google.generativeai as genai + import typing_extensions as typing class Recipe(typing.TypedDict): @@ -39,6 +40,8 @@ class Recipe(typing.TypedDict): def test_json_no_schema(self): # [START json_no_schema] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-pro-latest") prompt = """List a few popular cookie recipes in JSON format. @@ -52,6 +55,8 @@ def test_json_no_schema(self): def test_json_enum(self): # [START json_enum] + import google.generativeai as genai + import enum class Choice(enum.Enum): @@ -75,6 +80,8 @@ class Choice(enum.Enum): def test_enum_in_json(self): # [START enum_in_json] + import google.generativeai as genai + import enum from typing_extensions import TypedDict @@ -103,6 +110,8 @@ class Recipe(TypedDict): def test_json_enum_raw(self): # [START json_enum_raw] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-pro-latest") organ = genai.upload_file(media / "organ.jpg") @@ -121,6 +130,8 @@ def test_json_enum_raw(self): def test_x_enum(self): # [START x_enum] + import google.generativeai as genai + import enum class Choice(enum.Enum): @@ -144,6 +155,8 @@ class Choice(enum.Enum): def test_x_enum_raw(self): # [START x_enum_raw] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-pro-latest") organ = genai.upload_file(media / "organ.jpg") diff --git a/samples/count_tokens.py b/samples/count_tokens.py index 4d52684d9..33431dde8 100644 --- a/samples/count_tokens.py +++ b/samples/count_tokens.py @@ -14,7 +14,6 @@ # limitations under the License. from absl.testing import absltest -import google.generativeai as genai import pathlib media = pathlib.Path(__file__).parents[1] / "third_party" @@ -23,6 +22,8 @@ class UnitTests(absltest.TestCase): def test_tokens_context_window(self): # [START tokens_context_window] + import google.generativeai as genai + model_info = genai.get_model("models/gemini-1.5-flash") # Returns the "context window" for the model, @@ -34,6 +35,8 @@ def test_tokens_context_window(self): def test_tokens_text_only(self): # [START tokens_text_only] + import google.generativeai as genai + model = genai.GenerativeModel("models/gemini-1.5-flash") prompt = "The quick brown fox jumps over the lazy dog." @@ -54,6 +57,8 @@ def test_tokens_text_only(self): def test_tokens_chat(self): # [START tokens_chat] + import google.generativeai as genai + model = genai.GenerativeModel("models/gemini-1.5-flash") chat = model.start_chat( @@ -86,6 +91,8 @@ def test_tokens_chat(self): def test_tokens_multimodal_image_inline(self): # [START tokens_multimodal_image_inline] + import google.generativeai as genai + import PIL.Image model = genai.GenerativeModel("models/gemini-1.5-flash") @@ -112,6 +119,8 @@ def test_tokens_multimodal_image_inline(self): def test_tokens_multimodal_image_file_api(self): # [START tokens_multimodal_image_file_api] + import google.generativeai as genai + model = genai.GenerativeModel("models/gemini-1.5-flash") prompt = "Tell me about this image" @@ -136,6 +145,8 @@ def test_tokens_multimodal_image_file_api(self): def test_tokens_multimodal_video_audio_file_api(self): # [START tokens_multimodal_video_audio_file_api] + import google.generativeai as genai + import time model = genai.GenerativeModel("models/gemini-1.5-flash") @@ -169,6 +180,8 @@ def test_tokens_multimodal_video_audio_file_api(self): def test_tokens_multimodal_pdf_file_api(self): # [START tokens_multimodal_pdf_file_api] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") sample_pdf = genai.upload_file(media / "test.pdf") token_count = model.count_tokens(["Give me a summary of this document.", sample_pdf]) @@ -180,6 +193,8 @@ def test_tokens_multimodal_pdf_file_api(self): def test_tokens_cached_content(self): # [START tokens_cached_content] + import google.generativeai as genai + import time model = genai.GenerativeModel("models/gemini-1.5-flash") @@ -220,6 +235,8 @@ def test_tokens_cached_content(self): def test_tokens_system_instruction(self): # [START tokens_system_instruction] + import google.generativeai as genai + model = genai.GenerativeModel(model_name="gemini-1.5-flash") prompt = "The quick brown fox jumps over the lazy dog." @@ -239,6 +256,8 @@ def test_tokens_system_instruction(self): def test_tokens_tools(self): # [START tokens_tools] + import google.generativeai as genai + model = genai.GenerativeModel(model_name="gemini-1.5-flash") prompt = "I have 57 cats, each owns 44 mittens, how many mittens is that in total?" diff --git a/samples/embed.py b/samples/embed.py index 2ee4997ed..a5897639b 100644 --- a/samples/embed.py +++ b/samples/embed.py @@ -15,12 +15,10 @@ from absl.testing import absltest -import google.generativeai as genai - - class UnitTests(absltest.TestCase): def test_embed_content(self): # [START embed_content] + import google.generativeai as genai text = "Hello World!" result = genai.embed_content( @@ -31,6 +29,8 @@ def test_embed_content(self): def batch_embed_contents(self): # [START batch_embed_contents] + import google.generativeai as genai + texts = [ "What is the meaning of life?", "How much wood would a woodchuck chuck?", diff --git a/samples/files.py b/samples/files.py index 8f98365aa..0011f4da2 100644 --- a/samples/files.py +++ b/samples/files.py @@ -15,7 +15,6 @@ from absl.testing import absltest import google -import google.generativeai as genai import pathlib media = pathlib.Path(__file__).parents[1] / "third_party" @@ -24,6 +23,8 @@ class UnitTests(absltest.TestCase): def test_files_create_text(self): # [START files_create_text] + import google.generativeai as genai + myfile = genai.upload_file(media / "poem.txt") print(f"{myfile=}") @@ -36,6 +37,8 @@ def test_files_create_text(self): def test_files_create_image(self): # [START files_create_image] + import google.generativeai as genai + myfile = genai.upload_file(media / "Cajun_instruments.jpg") print(f"{myfile=}") @@ -48,6 +51,8 @@ def test_files_create_image(self): def test_files_create_audio(self): # [START files_create_audio] + import google.generativeai as genai + myfile = genai.upload_file(media / "sample.mp3") print(f"{myfile=}") @@ -58,6 +63,8 @@ def test_files_create_audio(self): def test_files_create_video(self): # [START files_create_video] + import google.generativeai as genai + import time # Video clip (CC BY 3.0) from https://peach.blender.org/download/ @@ -77,6 +84,8 @@ def test_files_create_video(self): def test_files_create_pdf(self): # [START files_create_pdf] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") sample_pdf = genai.upload_file(media / "test.pdf") response = model.generate_content(["Give me a summary of this pdf file.", sample_pdf]) @@ -85,6 +94,8 @@ def test_files_create_pdf(self): def test_files_create_from_IO(self): # [START files_create_io] + import google.generativeai as genai + # You can pass a file-like object, instead of a path. # Useful for streaming. model = genai.GenerativeModel("gemini-1.5-flash") @@ -97,6 +108,8 @@ def test_files_create_from_IO(self): def test_files_list(self): # [START files_list] + import google.generativeai as genai + print("My files:") for f in genai.list_files(): print(" ", f.name) @@ -104,6 +117,8 @@ def test_files_list(self): def test_files_get(self): # [START files_get] + import google.generativeai as genai + myfile = genai.upload_file(media / "poem.txt") file_name = myfile.name print(file_name) # "files/*" @@ -114,6 +129,8 @@ def test_files_get(self): def test_files_delete(self): # [START files_delete] + import google.generativeai as genai + myfile = genai.upload_file(media / "poem.txt") myfile.delete() diff --git a/samples/function_calling.py b/samples/function_calling.py index 8832408cf..829b97742 100644 --- a/samples/function_calling.py +++ b/samples/function_calling.py @@ -14,12 +14,12 @@ # 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] + import google.generativeai as genai + def add(a: float, b: float): """returns a + b.""" return a + b diff --git a/samples/models.py b/samples/models.py index c1758792b..1eea4b31e 100644 --- a/samples/models.py +++ b/samples/models.py @@ -14,12 +14,12 @@ # limitations under the License. from absl.testing import absltest -import google.generativeai as genai - class UnitTests(absltest.TestCase): def test_models_list(self): # [START models_list] + import google.generativeai as genai + print("List of models that support generateContent:\n") for m in genai.list_models(): if "generateContent" in m.supported_generation_methods: @@ -33,6 +33,8 @@ def test_models_list(self): def test_models_get(self): # [START models_get] + import google.generativeai as genai + model_info = genai.get_model("models/gemini-1.5-flash-latest") print(model_info) # [END models_get] diff --git a/samples/safety_settings.py b/samples/safety_settings.py index 84c70d980..49b099dad 100644 --- a/samples/safety_settings.py +++ b/samples/safety_settings.py @@ -14,12 +14,12 @@ # limitations under the License. from absl.testing import absltest -import google.generativeai as genai - class UnitTests(absltest.TestCase): def test_safety_settings(self): # [START safety_settings] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") unsafe_prompt = "I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them." response = model.generate_content( @@ -33,6 +33,8 @@ def test_safety_settings(self): def test_safety_settings_multi(self): # [START safety_settings_multi] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") unsafe_prompt = "I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them." response = model.generate_content( diff --git a/samples/system_instruction.py b/samples/system_instruction.py index a87932c95..bf486ec45 100644 --- a/samples/system_instruction.py +++ b/samples/system_instruction.py @@ -14,12 +14,12 @@ # limitations under the License. from absl.testing import absltest -import google.generativeai as genai - class UnitTests(absltest.TestCase): def test_system_instructions(self): # [START system_instruction] + import google.generativeai as genai + model = genai.GenerativeModel( "models/gemini-1.5-flash", system_instruction="You are a cat. Your name is Neko.", diff --git a/samples/text_generation.py b/samples/text_generation.py index aad0916f7..ab57149b2 100644 --- a/samples/text_generation.py +++ b/samples/text_generation.py @@ -14,7 +14,6 @@ # limitations under the License. from absl.testing import absltest -import google.generativeai as genai import pathlib media = pathlib.Path(__file__).parents[1] / "third_party" @@ -23,6 +22,8 @@ class UnitTests(absltest.TestCase): def test_text_gen_text_only_prompt(self): # [START text_gen_text_only_prompt] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") response = model.generate_content("Write a story about a magic backpack.") print(response.text) @@ -30,6 +31,8 @@ def test_text_gen_text_only_prompt(self): def test_text_gen_text_only_prompt_streaming(self): # [START text_gen_text_only_prompt_streaming] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") response = model.generate_content("Write a story about a magic backpack.", stream=True) for chunk in response: @@ -39,6 +42,8 @@ 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 google.generativeai as genai + import PIL.Image model = genai.GenerativeModel("gemini-1.5-flash") @@ -49,6 +54,8 @@ 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 google.generativeai as genai + import PIL.Image model = genai.GenerativeModel("gemini-1.5-flash") @@ -61,6 +68,8 @@ 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 google.generativeai as genai + import PIL.Image model = genai.GenerativeModel("gemini-1.5-flash") @@ -74,6 +83,8 @@ 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 google.generativeai as genai + import PIL.Image model = genai.GenerativeModel("gemini-1.5-flash") @@ -90,6 +101,8 @@ def test_text_gen_multimodal_multi_image_prompt_streaming(self): def test_text_gen_multimodal_audio(self): # [START text_gen_multimodal_audio] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") sample_audio = genai.upload_file(media / "sample.mp3") response = model.generate_content(["Give me a summary of this audio file.", sample_audio]) @@ -98,6 +111,8 @@ def test_text_gen_multimodal_audio(self): def test_text_gen_multimodal_audio_streaming(self): # [START text_gen_multimodal_audio_streaming] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") sample_audio = genai.upload_file(media / "sample.mp3") response = model.generate_content(["Give me a summary of this audio file.", sample_audio]) @@ -109,6 +124,8 @@ def test_text_gen_multimodal_audio_streaming(self): def test_text_gen_multimodal_video_prompt(self): # [START text_gen_multimodal_video_prompt] + import google.generativeai as genai + import time # Video clip (CC BY 3.0) from https://peach.blender.org/download/ @@ -128,6 +145,8 @@ def test_text_gen_multimodal_video_prompt(self): def test_text_gen_multimodal_video_prompt_streaming(self): # [START text_gen_multimodal_video_prompt_streaming] + import google.generativeai as genai + import time # Video clip (CC BY 3.0) from https://peach.blender.org/download/ @@ -150,6 +169,8 @@ def test_text_gen_multimodal_video_prompt_streaming(self): def test_text_gen_multimodal_pdf(self): # [START text_gen_multimodal_pdf] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") sample_pdf = genai.upload_file(media / "test.pdf") response = model.generate_content(["Give me a summary of this document:", sample_pdf]) @@ -158,6 +179,8 @@ def test_text_gen_multimodal_pdf(self): def test_text_gen_multimodal_pdf_streaming(self): # [START text_gen_multimodal_pdf_streaming] + import google.generativeai as genai + model = genai.GenerativeModel("gemini-1.5-flash") sample_pdf = genai.upload_file(media / "test.pdf") response = model.generate_content(["Give me a summary of this document:", sample_pdf]) diff --git a/samples/tuned_models.py b/samples/tuned_models.py index 8ecad73de..970919115 100644 --- a/samples/tuned_models.py +++ b/samples/tuned_models.py @@ -15,7 +15,6 @@ from absl.testing import absltest import google -import google.generativeai as genai import pathlib @@ -25,6 +24,8 @@ class UnitTests(absltest.TestCase): def test_tuned_models_create(self): # [START tuned_models_create] + import google.generativeai as genai + import time base_model = "models/gemini-1.5-flash-001-tuning" @@ -74,6 +75,8 @@ def test_tuned_models_create(self): def test_tuned_models_generate_content(self): # [START tuned_models_generate_content] + import google.generativeai as genai + model = genai.GenerativeModel(model_name="tunedModels/my-increment-model") result = model.generate_content("III") print(result.text) # "IV" @@ -81,12 +84,16 @@ def test_tuned_models_generate_content(self): def test_tuned_models_get(self): # [START tuned_models_get] + import google.generativeai as genai + model_info = genai.get_model("tunedModels/my-increment-model") print(model_info) # [END tuned_models_get] def test_tuned_models_list(self): # [START tuned_models_list] + import google.generativeai as genai + for model_info in genai.list_tuned_models(): print(model_info.name) # [END tuned_models_list] @@ -114,6 +121,8 @@ def test_tuned_models_delete(self): time.sleep(10) # [START tuned_models_delete] + import google.generativeai as genai + model_name = "tunedModels/delete-this-model" model_info = genai.get_model(model_name) print(model_info) @@ -124,6 +133,8 @@ def test_tuned_models_delete(self): def test_tuned_models_permissions_create(self): # [START tuned_models_permissions_create] + import google.generativeai as genai + model_info = genai.get_model("tunedModels/my-increment-model") # [START_EXCLUDE] for p in model_info.permissions.list(): @@ -148,6 +159,8 @@ def test_tuned_models_permissions_create(self): def test_tuned_models_permissions_list(self): # [START tuned_models_permissions_list] + import google.generativeai as genai + model_info = genai.get_model("tunedModels/my-increment-model") # [START_EXCLUDE] @@ -175,6 +188,8 @@ def test_tuned_models_permissions_list(self): def test_tuned_models_permissions_get(self): # [START tuned_models_permissions_get] + import google.generativeai as genai + model_info = genai.get_model("tunedModels/my-increment-model") # [START_EXCLUDE] @@ -197,6 +212,8 @@ def test_tuned_models_permissions_get(self): def test_tuned_models_permissions_update(self): # [START tuned_models_permissions_update] + import google.generativeai as genai + model_info = genai.get_model("tunedModels/my-increment-model") # [START_EXCLUDE] @@ -216,6 +233,8 @@ def test_tuned_models_permissions_update(self): def test_tuned_models_permission_delete(self): # [START tuned_models_permissions_delete] + import google.generativeai as genai + model_info = genai.get_model("tunedModels/my-increment-model") # [START_EXCLUDE] for p in model_info.permissions.list():