1919
2020
2121class UnitTests (absltest .TestCase ):
22+
2223 def test_cache_create (self ):
2324 # [START cache_create]
2425 from google import genai
@@ -32,17 +33,15 @@ def test_cache_create(self):
3233 model = model_name ,
3334 config = types .CreateCachedContentConfig (
3435 contents = [document ],
35- system_instruction = "You are an expert analyzing transcripts."
36- )
36+ system_instruction = "You are an expert analyzing transcripts." ,
37+ ),
3738 )
3839 print (cache )
3940
4041 response = client .models .generate_content (
4142 model = model_name ,
4243 contents = "Please summarize this transcript" ,
43- config = types .GenerateContentConfig (
44- cached_content = cache .name
45- )
44+ config = types .GenerateContentConfig (cached_content = cache .name ),
4645 )
4746 print (response .text )
4847 # [END cache_create]
@@ -61,8 +60,8 @@ def test_cache_create_from_name(self):
6160 model = model_name ,
6261 config = types .CreateCachedContentConfig (
6362 contents = [document ],
64- system_instruction = "You are an expert analyzing transcripts."
65- )
63+ system_instruction = "You are an expert analyzing transcripts." ,
64+ ),
6665 )
6766 cache_name = cache .name # Save the name for later
6867
@@ -71,9 +70,7 @@ def test_cache_create_from_name(self):
7170 response = client .models .generate_content (
7271 model = model_name ,
7372 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 ),
7774 )
7875 print (response .text )
7976 # [END cache_create_from_name]
@@ -91,9 +88,7 @@ def test_cache_create_from_chat(self):
9188 # Create a chat session with the given system instruction.
9289 chat = client .chats .create (
9390 model = model_name ,
94- config = types .GenerateContentConfig (
95- system_instruction = system_instruction
96- )
91+ config = types .GenerateContentConfig (system_instruction = system_instruction ),
9792 )
9893 document = client .files .upload (file = media / "a11.txt" )
9994
@@ -110,16 +105,14 @@ def test_cache_create_from_chat(self):
110105 cache = client .caches .create (
111106 model = model_name ,
112107 config = {
113- ' contents' : chat .get_history (),
114- ' system_instruction' : system_instruction
115- }
108+ " contents" : chat .get_history (),
109+ " system_instruction" : system_instruction ,
110+ },
116111 )
117112 # Continue the conversation using the cached content.
118113 chat = client .chats .create (
119114 model = model_name ,
120- config = types .GenerateContentConfig (
121- cached_content = cache .name
122- )
115+ config = types .GenerateContentConfig (cached_content = cache .name ),
123116 )
124117 response = chat .send_message (
125118 message = "I didn't understand that last part, could you explain it in simpler language?"
@@ -139,9 +132,9 @@ def test_cache_delete(self):
139132 cache = client .caches .create (
140133 model = model_name ,
141134 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+ },
145138 )
146139 client .caches .delete (name = cache .name )
147140 # [END cache_delete]
@@ -157,9 +150,9 @@ def test_cache_get(self):
157150 cache = client .caches .create (
158151 model = model_name ,
159152 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+ },
163156 )
164157 print (client .caches .get (name = cache .name ))
165158 # [END cache_get]
@@ -176,9 +169,9 @@ def test_cache_list(self):
176169 cache = client .caches .create (
177170 model = model_name ,
178171 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+ },
182175 )
183176 print ("My caches:" )
184177 for c in client .caches .list ():
@@ -199,31 +192,31 @@ def test_cache_update(self):
199192 cache = client .caches .create (
200193 model = model_name ,
201194 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+ },
205198 )
206199
207200 # Update the cache's time-to-live (ttl)
208201 ttl = f"{ int (datetime .timedelta (hours = 2 ).total_seconds ())} s"
209202 client .caches .update (
210- name = cache .name ,
211- config = types .UpdateCachedContentConfig (
212- ttl = ttl
213- )
203+ name = cache .name , config = types .UpdateCachedContentConfig (ttl = ttl )
214204 )
215205 print (f"After update:\n { cache } " )
216206
217207 # Alternatively, update the expire_time directly
218208 # Update the expire_time directly in valid RFC 3339 format (UTC with a "Z" suffix)
219209 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+ )
222217 client .caches .update (
223218 name = cache .name ,
224- config = types .UpdateCachedContentConfig (
225- expire_time = expire_time
226- )
219+ config = types .UpdateCachedContentConfig (expire_time = expire_time ),
227220 )
228221 # [END cache_update]
229222 client .caches .delete (name = cache .name )
0 commit comments