Skip to content

Commit 938ed40

Browse files
committed
Move imports into samples
Change-Id: I86550a09c6555a36679dad517ed4239f7ded90ff
1 parent 0e5c5f2 commit 938ed40

14 files changed

+146
-14
lines changed

samples/cache.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# limitations under the License.
1515
from absl.testing import absltest
1616

17-
import google.generativeai as genai
1817

1918
import pathlib
2019

@@ -24,6 +23,8 @@
2423
class UnitTests(absltest.TestCase):
2524
def test_cache_create(self):
2625
# [START cache_create]
26+
import google.generativeai as genai
27+
2728
document = genai.upload_file(path=media / "a11.txt")
2829
model_name = "gemini-1.5-flash-001"
2930
cache = genai.caching.CachedContent.create(
@@ -41,6 +42,8 @@ def test_cache_create(self):
4142

4243
def test_cache_create_from_name(self):
4344
# [START cache_create_from_name]
45+
import google.generativeai as genai
46+
4447
document = genai.upload_file(path=media / "a11.txt")
4548
model_name = "gemini-1.5-flash-001"
4649
cache = genai.caching.CachedContent.create(
@@ -60,6 +63,8 @@ def test_cache_create_from_name(self):
6063

6164
def test_cache_create_from_chat(self):
6265
# [START cache_create_from_chat]
66+
import google.generativeai as genai
67+
6368
model_name = "gemini-1.5-flash-001"
6469
system_instruction = "You are an expert analyzing transcripts."
6570

@@ -92,6 +97,8 @@ def test_cache_create_from_chat(self):
9297

9398
def test_cache_delete(self):
9499
# [START cache_delete]
100+
import google.generativeai as genai
101+
95102
document = genai.upload_file(path=media / "a11.txt")
96103
model_name = "gemini-1.5-flash-001"
97104
cache = genai.caching.CachedContent.create(
@@ -104,6 +111,8 @@ def test_cache_delete(self):
104111

105112
def test_cache_get(self):
106113
# [START cache_get]
114+
import google.generativeai as genai
115+
107116
document = genai.upload_file(path=media / "a11.txt")
108117
model_name = "gemini-1.5-flash-001"
109118
cache = genai.caching.CachedContent.create(
@@ -117,6 +126,8 @@ def test_cache_get(self):
117126

118127
def test_cache_list(self):
119128
# [START cache_list]
129+
import google.generativeai as genai
130+
120131
document = genai.upload_file(path=media / "a11.txt")
121132
model_name = "gemini-1.5-flash-001"
122133
cache = genai.caching.CachedContent.create(
@@ -132,6 +143,8 @@ def test_cache_list(self):
132143

133144
def test_cache_update(self):
134145
# [START cache_update]
146+
import google.generativeai as genai
147+
135148
import datetime
136149

137150
document = genai.upload_file(path=media / "a11.txt")

samples/chat.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# limitations under the License.
1515
from absl.testing import absltest
1616

17-
import google.generativeai as genai
1817
import pathlib
1918

2019
media = pathlib.Path(__file__).parents[1] / "third_party"
@@ -23,6 +22,8 @@
2322
class UnitTests(absltest.TestCase):
2423
def test_chat(self):
2524
# [START chat]
25+
import google.generativeai as genai
26+
2627
model = genai.GenerativeModel("gemini-1.5-flash")
2728
chat = model.start_chat(
2829
history=[
@@ -38,6 +39,8 @@ def test_chat(self):
3839

3940
def test_chat_streaming(self):
4041
# [START chat_streaming]
42+
import google.generativeai as genai
43+
4144
model = genai.GenerativeModel("gemini-1.5-flash")
4245
chat = model.start_chat(
4346
history=[
@@ -59,6 +62,8 @@ def test_chat_streaming(self):
5962

6063
def test_chat_streaming_with_images(self):
6164
# [START chat_streaming_with_images]
65+
import google.generativeai as genai
66+
6267
model = genai.GenerativeModel("gemini-1.5-flash")
6368
chat = model.start_chat()
6469

samples/code_execution.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
# limitations under the License.
1515
from absl.testing import absltest
1616

17-
import google.generativeai as genai
1817

1918

2019
class UnitTests(absltest.TestCase):
2120
def test_code_execution_basic(self):
2221
# [START code_execution_basic]
22+
import google.generativeai as genai
23+
2324
model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools="code_execution")
2425
response = model.generate_content(
2526
(
@@ -38,6 +39,8 @@ def test_code_execution_basic(self):
3839
# [END code_execution_basic]
3940

4041
# [START code_execution_basic_return]
42+
import google.generativeai as genai
43+
4144
# 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"
4245
#
4346
# executable_code {
@@ -92,6 +95,8 @@ def test_code_execution_basic(self):
9295

9396
def test_code_execution_request_override(self):
9497
# [START code_execution_request_override]
98+
import google.generativeai as genai
99+
95100
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
96101
response = model.generate_content(
97102
(
@@ -103,6 +108,8 @@ def test_code_execution_request_override(self):
103108
print(response.text)
104109
# [END code_execution_request_override]
105110
# [START code_execution_request_override_return]
111+
import google.generativeai as genai
112+
106113
# ``` python
107114
# def is_prime(n):
108115
# """
@@ -140,6 +147,8 @@ def test_code_execution_request_override(self):
140147

141148
def test_code_execution_chat(self):
142149
# [START code_execution_chat]
150+
import google.generativeai as genai
151+
143152
model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools="code_execution")
144153
chat = model.start_chat()
145154
response = chat.send_message('Can you print "Hello world!"?')
@@ -152,6 +161,8 @@ def test_code_execution_chat(self):
152161
print(response.text)
153162
# [END code_execution_chat]
154163
# [START code_execution_chat_return]
164+
import google.generativeai as genai
165+
155166
# ``` python
156167
# def is_prime(n):
157168
# """

samples/configure_model_parameters.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
# limitations under the License.
1515
from absl.testing import absltest
1616

17-
import google.generativeai as genai
1817

1918

2019
class UnitTests(absltest.TestCase):
2120
def test_configure_model(self):
2221
# [START configure_model_parameters]
22+
import google.generativeai as genai
23+
2324
model = genai.GenerativeModel("gemini-1.5-flash")
2425
response = model.generate_content(
2526
"Tell me a story about a magic backpack.",

samples/controlled_generation.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
from absl.testing import absltest
1414
import pathlib
1515

16-
import google.generativeai as genai
1716

1817
media = pathlib.Path(__file__).parents[1] / "third_party"
1918

2019

2120
class UnitTests(absltest.TestCase):
2221
def test_json_controlled_generation(self):
2322
# [START json_controlled_generation]
23+
import google.generativeai as genai
24+
2425
import typing_extensions as typing
2526

2627
class Recipe(typing.TypedDict):
@@ -39,6 +40,8 @@ class Recipe(typing.TypedDict):
3940

4041
def test_json_no_schema(self):
4142
# [START json_no_schema]
43+
import google.generativeai as genai
44+
4245
model = genai.GenerativeModel("gemini-1.5-pro-latest")
4346
prompt = """List a few popular cookie recipes in JSON format.
4447
@@ -52,6 +55,8 @@ def test_json_no_schema(self):
5255

5356
def test_json_enum(self):
5457
# [START json_enum]
58+
import google.generativeai as genai
59+
5560
import enum
5661

5762
class Choice(enum.Enum):
@@ -75,6 +80,8 @@ class Choice(enum.Enum):
7580

7681
def test_enum_in_json(self):
7782
# [START enum_in_json]
83+
import google.generativeai as genai
84+
7885
import enum
7986
from typing_extensions import TypedDict
8087

@@ -103,6 +110,8 @@ class Recipe(TypedDict):
103110

104111
def test_json_enum_raw(self):
105112
# [START json_enum_raw]
113+
import google.generativeai as genai
114+
106115
model = genai.GenerativeModel("gemini-1.5-pro-latest")
107116

108117
organ = genai.upload_file(media / "organ.jpg")
@@ -121,6 +130,8 @@ def test_json_enum_raw(self):
121130

122131
def test_x_enum(self):
123132
# [START x_enum]
133+
import google.generativeai as genai
134+
124135
import enum
125136

126137
class Choice(enum.Enum):
@@ -144,6 +155,8 @@ class Choice(enum.Enum):
144155

145156
def test_x_enum_raw(self):
146157
# [START x_enum_raw]
158+
import google.generativeai as genai
159+
147160
model = genai.GenerativeModel("gemini-1.5-pro-latest")
148161

149162
organ = genai.upload_file(media / "organ.jpg")

samples/count_tokens.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# limitations under the License.
1515
from absl.testing import absltest
1616

17-
import google.generativeai as genai
1817
import pathlib
1918

2019
media = pathlib.Path(__file__).parents[1] / "third_party"
@@ -23,6 +22,8 @@
2322
class UnitTests(absltest.TestCase):
2423
def test_tokens_context_window(self):
2524
# [START tokens_context_window]
25+
import google.generativeai as genai
26+
2627
model_info = genai.get_model("models/gemini-1.5-flash")
2728

2829
# Returns the "context window" for the model,
@@ -34,6 +35,8 @@ def test_tokens_context_window(self):
3435

3536
def test_tokens_text_only(self):
3637
# [START tokens_text_only]
38+
import google.generativeai as genai
39+
3740
model = genai.GenerativeModel("models/gemini-1.5-flash")
3841

3942
prompt = "The quick brown fox jumps over the lazy dog."
@@ -54,6 +57,8 @@ def test_tokens_text_only(self):
5457

5558
def test_tokens_chat(self):
5659
# [START tokens_chat]
60+
import google.generativeai as genai
61+
5762
model = genai.GenerativeModel("models/gemini-1.5-flash")
5863

5964
chat = model.start_chat(
@@ -86,6 +91,8 @@ def test_tokens_chat(self):
8691

8792
def test_tokens_multimodal_image_inline(self):
8893
# [START tokens_multimodal_image_inline]
94+
import google.generativeai as genai
95+
8996
import PIL.Image
9097

9198
model = genai.GenerativeModel("models/gemini-1.5-flash")
@@ -112,6 +119,8 @@ def test_tokens_multimodal_image_inline(self):
112119

113120
def test_tokens_multimodal_image_file_api(self):
114121
# [START tokens_multimodal_image_file_api]
122+
import google.generativeai as genai
123+
115124
model = genai.GenerativeModel("models/gemini-1.5-flash")
116125

117126
prompt = "Tell me about this image"
@@ -136,6 +145,8 @@ def test_tokens_multimodal_image_file_api(self):
136145

137146
def test_tokens_multimodal_video_audio_file_api(self):
138147
# [START tokens_multimodal_video_audio_file_api]
148+
import google.generativeai as genai
149+
139150
import time
140151

141152
model = genai.GenerativeModel("models/gemini-1.5-flash")
@@ -169,6 +180,8 @@ def test_tokens_multimodal_video_audio_file_api(self):
169180

170181
def test_tokens_multimodal_pdf_file_api(self):
171182
# [START tokens_multimodal_pdf_file_api]
183+
import google.generativeai as genai
184+
172185
model = genai.GenerativeModel("gemini-1.5-flash")
173186
sample_pdf = genai.upload_file(media / "test.pdf")
174187
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):
180193

181194
def test_tokens_cached_content(self):
182195
# [START tokens_cached_content]
196+
import google.generativeai as genai
197+
183198
import time
184199

185200
model = genai.GenerativeModel("models/gemini-1.5-flash")
@@ -220,6 +235,8 @@ def test_tokens_cached_content(self):
220235

221236
def test_tokens_system_instruction(self):
222237
# [START tokens_system_instruction]
238+
import google.generativeai as genai
239+
223240
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
224241

225242
prompt = "The quick brown fox jumps over the lazy dog."
@@ -239,6 +256,8 @@ def test_tokens_system_instruction(self):
239256

240257
def test_tokens_tools(self):
241258
# [START tokens_tools]
259+
import google.generativeai as genai
260+
242261
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
243262

244263
prompt = "I have 57 cats, each owns 44 mittens, how many mittens is that in total?"

samples/embed.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
from absl.testing import absltest
1616

1717

18-
import google.generativeai as genai
1918

2019

2120
class UnitTests(absltest.TestCase):
2221
def test_embed_content(self):
2322
# [START embed_content]
23+
import google.generativeai as genai
24+
2425

2526
text = "Hello World!"
2627
result = genai.embed_content(
@@ -31,6 +32,8 @@ def test_embed_content(self):
3132

3233
def batch_embed_contents(self):
3334
# [START batch_embed_contents]
35+
import google.generativeai as genai
36+
3437
texts = [
3538
"What is the meaning of life?",
3639
"How much wood would a woodchuck chuck?",

0 commit comments

Comments
 (0)