Skip to content

Commit 7bd2daa

Browse files
Caching samples (#399)
* Add samples for caching * Add samples for caching functions * Update importing for json and embeddings * Add updates to each sample file * Resolve caching comments. Change-Id: I4b7eac67b6e5999b0d256c47f05dd4c112eae45e * format Change-Id: I54437b359a2bd75972376423fd30361f39e799ce --------- Co-authored-by: Mark Daoust <[email protected]>
1 parent 4960f78 commit 7bd2daa

File tree

6 files changed

+28841
-13
lines changed

6 files changed

+28841
-13
lines changed

samples/cache.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
from absl.testing import absltest
16+
17+
import google.generativeai as genai
18+
19+
import pathlib
20+
21+
media = pathlib.Path(__file__).parents[1] / "third_party"
22+
23+
24+
class UnitTests(absltest.TestCase):
25+
def test_cache_create(self):
26+
# [START cache_create]
27+
document = genai.upload_file(path=media / "a11.txt")
28+
model_name = "gemini-1.5-flash-001"
29+
cache = genai.caching.CachedContent.create(
30+
model=model_name,
31+
system_instruction="You are an expert analyzing transcripts.",
32+
contents=[document],
33+
)
34+
print(cache)
35+
# [END cache_create]
36+
cache.delete()
37+
38+
def test_cache_delete(self):
39+
# [START cache_delete]
40+
document = genai.upload_file(path=media / "a11.txt")
41+
model_name = "gemini-1.5-flash-001"
42+
cache = genai.caching.CachedContent.create(
43+
model=model_name,
44+
system_instruction="You are an expert analyzing transcripts.",
45+
contents=[document],
46+
)
47+
cache.delete()
48+
# [END cache_delete]
49+
50+
def test_cache_get(self):
51+
# [START cache_get]
52+
document = genai.upload_file(path=media / "a11.txt")
53+
model_name = "gemini-1.5-flash-001"
54+
cache = genai.caching.CachedContent.create(
55+
model=model_name,
56+
system_instruction="You are an expert analyzing transcripts.",
57+
contents=[document],
58+
)
59+
print(genai.caching.CachedContent.get(name=cache.name))
60+
# [END cache_get]
61+
cache.delete()
62+
63+
def test_cache_list(self):
64+
# [START cache_list]
65+
document = genai.upload_file(path=media / "a11.txt")
66+
model_name = "gemini-1.5-flash-001"
67+
cache = genai.caching.CachedContent.create(
68+
model=model_name,
69+
system_instruction="You are an expert analyzing transcripts.",
70+
contents=[document],
71+
)
72+
print("My caches:")
73+
for c in genai.caching.CachedContent.list():
74+
print(" ", c.name)
75+
# [END cache_list]
76+
cache.delete()
77+
78+
def test_cache_update(self):
79+
# [START cache_update]
80+
import datetime
81+
82+
document = genai.upload_file(path=media / "a11.txt")
83+
model_name = "gemini-1.5-flash-001"
84+
cache = genai.caching.CachedContent.create(
85+
model=model_name,
86+
system_instruction="You are an expert analyzing transcripts.",
87+
contents=[document],
88+
)
89+
90+
# You can update the ttl
91+
cache.update(ttl=datetime.timedelta(hours=2))
92+
print(f"After update:\n {cache}")
93+
94+
# Or you can update the expire_time
95+
cache.update(expire_time=datetime.now() + datetime.timedelta(minutes=15))
96+
# [END cache_update]
97+
cache.delete()
98+
99+
100+
if __name__ == "__main__":
101+
absltest.main()

samples/embed.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def test_embed_content(self):
2727
model="models/text-embedding-004", content=text, output_dimensionality=10
2828
)
2929
print(result["embedding"])
30-
print()
3130
# [END embed_content]
3231

3332
def batch_embed_content(self):
@@ -41,7 +40,6 @@ def batch_embed_content(self):
4140
model="models/text-embedding-004", content=texts, output_dimensionality=10
4241
)
4342
print(result)
44-
print()
4543
# [END batch_embed_content]
4644

4745

samples/files.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,31 @@ def test_files_create(self):
2626
# [START files_create]
2727
myfile = genai.upload_file(media / "poem.txt")
2828
print(f"{myfile=}")
29-
print()
3029

3130
model = genai.GenerativeModel("gemini-1.5-flash")
3231
result = model.generate_content(
3332
[myfile, "\n\n", "Can you add a few more lines to this poem?"]
3433
)
3534
print(f"{result.text=}")
36-
print()
3735
# [END files_create]
3836

3937
def test_files_create_video(self):
4038
# [START files_create_video]
4139
import time
4240

43-
# Video clip from https://peach.blender.org/download/
41+
# Video clip (CC BY 3.0) from https://peach.blender.org/download/
4442
myfile = genai.upload_file(media / "Big_Buck_Bunny.mp4")
4543
print(f"{myfile=}")
46-
print()
4744

45+
# Videos need to be processed before you can use them.
4846
while myfile.state.name == "PROCESSING":
4947
print("processing video...")
5048
time.sleep(5)
5149
myfile = genai.get_file(myfile.name)
5250

5351
model = genai.GenerativeModel("gemini-1.5-flash")
5452
result = model.generate_content([myfile, "Describe this video clip"])
55-
print()
5653
print(f"{result.text=}")
57-
print()
5854
# [END files_create_video]
5955

6056
def test_files_list(self):

samples/json_mode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
from absl.testing import absltest
1414

1515
import google.generativeai as genai
16-
import typing_extensions as typing
1716

1817

1918
class UnitTests(absltest.TestCase):
2019
def test_controlled_generation(self):
2120
# [START controlled_generation]
21+
import typing_extensions as typing
22+
2223
class Recipe(typing.TypedDict):
2324
recipe_name: str
2425

@@ -30,7 +31,6 @@ class Recipe(typing.TypedDict):
3031
),
3132
)
3233
print(result)
33-
print()
3434
# [END controlled_generation]
3535

3636

third_party/LICENSE.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
* poem.txt:
2-
* This is the first paragraph from shakespear's spring, no copyright.
1+
* a11.txt
2+
* This is an apollo 11 transcript.
3+
* This file is in the public domain in the United States because it was solely created by NASA
34
* Big_Buck_Bunny.mp4:
45
* This is a clip from https://peach.blender.org/download/
56
* License CC-BY 3.0 (Attribution)
6-
7+
* poem.txt:
8+
* This is the first paragraph from Shakespeare's "spring", public domain.

0 commit comments

Comments
 (0)