19
19
20
20
21
21
class UnitTests (absltest .TestCase ):
22
+
22
23
def test_cache_create (self ):
23
24
# [START cache_create]
24
25
from google import genai
@@ -32,17 +33,15 @@ def test_cache_create(self):
32
33
model = model_name ,
33
34
config = types .CreateCachedContentConfig (
34
35
contents = [document ],
35
- system_instruction = "You are an expert analyzing transcripts."
36
- )
36
+ system_instruction = "You are an expert analyzing transcripts." ,
37
+ ),
37
38
)
38
39
print (cache )
39
40
40
41
response = client .models .generate_content (
41
42
model = model_name ,
42
43
contents = "Please summarize this transcript" ,
43
- config = types .GenerateContentConfig (
44
- cached_content = cache .name
45
- )
44
+ config = types .GenerateContentConfig (cached_content = cache .name ),
46
45
)
47
46
print (response .text )
48
47
# [END cache_create]
@@ -61,8 +60,8 @@ def test_cache_create_from_name(self):
61
60
model = model_name ,
62
61
config = types .CreateCachedContentConfig (
63
62
contents = [document ],
64
- system_instruction = "You are an expert analyzing transcripts."
65
- )
63
+ system_instruction = "You are an expert analyzing transcripts." ,
64
+ ),
66
65
)
67
66
cache_name = cache .name # Save the name for later
68
67
@@ -71,9 +70,7 @@ def test_cache_create_from_name(self):
71
70
response = client .models .generate_content (
72
71
model = model_name ,
73
72
contents = "Find a lighthearted moment from this transcript" ,
74
- config = types .GenerateContentConfig (
75
- cached_content = cache .name
76
- )
73
+ config = types .GenerateContentConfig (cached_content = cache .name ),
77
74
)
78
75
print (response .text )
79
76
# [END cache_create_from_name]
@@ -91,9 +88,7 @@ def test_cache_create_from_chat(self):
91
88
# Create a chat session with the given system instruction.
92
89
chat = client .chats .create (
93
90
model = model_name ,
94
- config = types .GenerateContentConfig (
95
- system_instruction = system_instruction
96
- )
91
+ config = types .GenerateContentConfig (system_instruction = system_instruction ),
97
92
)
98
93
document = client .files .upload (file = media / "a11.txt" )
99
94
@@ -110,16 +105,14 @@ def test_cache_create_from_chat(self):
110
105
cache = client .caches .create (
111
106
model = model_name ,
112
107
config = {
113
- ' contents' : chat .get_history (),
114
- ' system_instruction' : system_instruction
115
- }
108
+ " contents" : chat .get_history (),
109
+ " system_instruction" : system_instruction ,
110
+ },
116
111
)
117
112
# Continue the conversation using the cached content.
118
113
chat = client .chats .create (
119
114
model = model_name ,
120
- config = types .GenerateContentConfig (
121
- cached_content = cache .name
122
- )
115
+ config = types .GenerateContentConfig (cached_content = cache .name ),
123
116
)
124
117
response = chat .send_message (
125
118
message = "I didn't understand that last part, could you explain it in simpler language?"
@@ -139,9 +132,9 @@ def test_cache_delete(self):
139
132
cache = client .caches .create (
140
133
model = model_name ,
141
134
config = {
142
- ' contents' : [document ],
143
- ' system_instruction' : "You are an expert analyzing transcripts." ,
144
- }
135
+ " contents" : [document ],
136
+ " system_instruction" : "You are an expert analyzing transcripts." ,
137
+ },
145
138
)
146
139
client .caches .delete (name = cache .name )
147
140
# [END cache_delete]
@@ -157,9 +150,9 @@ def test_cache_get(self):
157
150
cache = client .caches .create (
158
151
model = model_name ,
159
152
config = {
160
- ' contents' : [document ],
161
- ' system_instruction' : "You are an expert analyzing transcripts." ,
162
- }
153
+ " contents" : [document ],
154
+ " system_instruction" : "You are an expert analyzing transcripts." ,
155
+ },
163
156
)
164
157
print (client .caches .get (name = cache .name ))
165
158
# [END cache_get]
@@ -176,9 +169,9 @@ def test_cache_list(self):
176
169
cache = client .caches .create (
177
170
model = model_name ,
178
171
config = {
179
- ' contents' : [document ],
180
- ' system_instruction' : "You are an expert analyzing transcripts." ,
181
- }
172
+ " contents" : [document ],
173
+ " system_instruction" : "You are an expert analyzing transcripts." ,
174
+ },
182
175
)
183
176
print ("My caches:" )
184
177
for c in client .caches .list ():
@@ -199,31 +192,31 @@ def test_cache_update(self):
199
192
cache = client .caches .create (
200
193
model = model_name ,
201
194
config = {
202
- ' contents' : [document ],
203
- ' system_instruction' : "You are an expert analyzing transcripts." ,
204
- }
195
+ " contents" : [document ],
196
+ " system_instruction" : "You are an expert analyzing transcripts." ,
197
+ },
205
198
)
206
199
207
200
# Update the cache's time-to-live (ttl)
208
201
ttl = f"{ int (datetime .timedelta (hours = 2 ).total_seconds ())} s"
209
202
client .caches .update (
210
- name = cache .name ,
211
- config = types .UpdateCachedContentConfig (
212
- ttl = ttl
213
- )
203
+ name = cache .name , config = types .UpdateCachedContentConfig (ttl = ttl )
214
204
)
215
205
print (f"After update:\n { cache } " )
216
206
217
207
# Alternatively, update the expire_time directly
218
208
# Update the expire_time directly in valid RFC 3339 format (UTC with a "Z" suffix)
219
209
expire_time = (
220
- datetime .datetime .now (datetime .timezone .utc ) + datetime .timedelta (minutes = 15 )
221
- ).isoformat ().replace ('+00:00' , 'Z' )
210
+ (
211
+ datetime .datetime .now (datetime .timezone .utc )
212
+ + datetime .timedelta (minutes = 15 )
213
+ )
214
+ .isoformat ()
215
+ .replace ("+00:00" , "Z" )
216
+ )
222
217
client .caches .update (
223
218
name = cache .name ,
224
- config = types .UpdateCachedContentConfig (
225
- expire_time = expire_time
226
- )
219
+ config = types .UpdateCachedContentConfig (expire_time = expire_time ),
227
220
)
228
221
# [END cache_update]
229
222
client .caches .delete (name = cache .name )
0 commit comments