google-genai 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
google/genai/caches.py ADDED
@@ -0,0 +1,1830 @@
1
+ # Copyright 2024 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+
16
+ from typing import Optional, Union
17
+ from urllib.parse import urlencode
18
+ from . import _common
19
+ from . import _transformers as t
20
+ from . import types
21
+ from ._api_client import ApiClient
22
+ from ._common import get_value_by_path as getv
23
+ from ._common import set_value_by_path as setv
24
+ from .pagers import AsyncPager, Pager
25
+
26
+
27
+ def _Part_to_mldev(
28
+ api_client: ApiClient,
29
+ from_object: Union[dict, object],
30
+ parent_object: dict = None,
31
+ ) -> dict:
32
+ to_object = {}
33
+ if getv(from_object, ['video_metadata']):
34
+ raise ValueError('video_metadata parameter is not supported in Google AI.')
35
+
36
+ if getv(from_object, ['code_execution_result']) is not None:
37
+ setv(
38
+ to_object,
39
+ ['codeExecutionResult'],
40
+ getv(from_object, ['code_execution_result']),
41
+ )
42
+
43
+ if getv(from_object, ['executable_code']) is not None:
44
+ setv(to_object, ['executableCode'], getv(from_object, ['executable_code']))
45
+
46
+ if getv(from_object, ['file_data']) is not None:
47
+ setv(to_object, ['fileData'], getv(from_object, ['file_data']))
48
+
49
+ if getv(from_object, ['function_call']) is not None:
50
+ setv(to_object, ['functionCall'], getv(from_object, ['function_call']))
51
+
52
+ if getv(from_object, ['function_response']) is not None:
53
+ setv(
54
+ to_object,
55
+ ['functionResponse'],
56
+ getv(from_object, ['function_response']),
57
+ )
58
+
59
+ if getv(from_object, ['inline_data']) is not None:
60
+ setv(to_object, ['inlineData'], getv(from_object, ['inline_data']))
61
+
62
+ if getv(from_object, ['text']) is not None:
63
+ setv(to_object, ['text'], getv(from_object, ['text']))
64
+
65
+ return to_object
66
+
67
+
68
+ def _Part_to_vertex(
69
+ api_client: ApiClient,
70
+ from_object: Union[dict, object],
71
+ parent_object: dict = None,
72
+ ) -> dict:
73
+ to_object = {}
74
+ if getv(from_object, ['video_metadata']) is not None:
75
+ setv(to_object, ['videoMetadata'], getv(from_object, ['video_metadata']))
76
+
77
+ if getv(from_object, ['code_execution_result']) is not None:
78
+ setv(
79
+ to_object,
80
+ ['codeExecutionResult'],
81
+ getv(from_object, ['code_execution_result']),
82
+ )
83
+
84
+ if getv(from_object, ['executable_code']) is not None:
85
+ setv(to_object, ['executableCode'], getv(from_object, ['executable_code']))
86
+
87
+ if getv(from_object, ['file_data']) is not None:
88
+ setv(to_object, ['fileData'], getv(from_object, ['file_data']))
89
+
90
+ if getv(from_object, ['function_call']) is not None:
91
+ setv(to_object, ['functionCall'], getv(from_object, ['function_call']))
92
+
93
+ if getv(from_object, ['function_response']) is not None:
94
+ setv(
95
+ to_object,
96
+ ['functionResponse'],
97
+ getv(from_object, ['function_response']),
98
+ )
99
+
100
+ if getv(from_object, ['inline_data']) is not None:
101
+ setv(to_object, ['inlineData'], getv(from_object, ['inline_data']))
102
+
103
+ if getv(from_object, ['text']) is not None:
104
+ setv(to_object, ['text'], getv(from_object, ['text']))
105
+
106
+ return to_object
107
+
108
+
109
+ def _Content_to_mldev(
110
+ api_client: ApiClient,
111
+ from_object: Union[dict, object],
112
+ parent_object: dict = None,
113
+ ) -> dict:
114
+ to_object = {}
115
+ if getv(from_object, ['parts']) is not None:
116
+ setv(
117
+ to_object,
118
+ ['parts'],
119
+ [
120
+ _Part_to_mldev(api_client, item, to_object)
121
+ for item in getv(from_object, ['parts'])
122
+ ],
123
+ )
124
+
125
+ if getv(from_object, ['role']) is not None:
126
+ setv(to_object, ['role'], getv(from_object, ['role']))
127
+
128
+ return to_object
129
+
130
+
131
+ def _Content_to_vertex(
132
+ api_client: ApiClient,
133
+ from_object: Union[dict, object],
134
+ parent_object: dict = None,
135
+ ) -> dict:
136
+ to_object = {}
137
+ if getv(from_object, ['parts']) is not None:
138
+ setv(
139
+ to_object,
140
+ ['parts'],
141
+ [
142
+ _Part_to_vertex(api_client, item, to_object)
143
+ for item in getv(from_object, ['parts'])
144
+ ],
145
+ )
146
+
147
+ if getv(from_object, ['role']) is not None:
148
+ setv(to_object, ['role'], getv(from_object, ['role']))
149
+
150
+ return to_object
151
+
152
+
153
+ def _Schema_to_mldev(
154
+ api_client: ApiClient,
155
+ from_object: Union[dict, object],
156
+ parent_object: dict = None,
157
+ ) -> dict:
158
+ to_object = {}
159
+ if getv(from_object, ['min_items']):
160
+ raise ValueError('min_items parameter is not supported in Google AI.')
161
+
162
+ if getv(from_object, ['example']):
163
+ raise ValueError('example parameter is not supported in Google AI.')
164
+
165
+ if getv(from_object, ['property_ordering']):
166
+ raise ValueError(
167
+ 'property_ordering parameter is not supported in Google AI.'
168
+ )
169
+
170
+ if getv(from_object, ['pattern']):
171
+ raise ValueError('pattern parameter is not supported in Google AI.')
172
+
173
+ if getv(from_object, ['minimum']):
174
+ raise ValueError('minimum parameter is not supported in Google AI.')
175
+
176
+ if getv(from_object, ['default']):
177
+ raise ValueError('default parameter is not supported in Google AI.')
178
+
179
+ if getv(from_object, ['any_of']):
180
+ raise ValueError('any_of parameter is not supported in Google AI.')
181
+
182
+ if getv(from_object, ['max_length']):
183
+ raise ValueError('max_length parameter is not supported in Google AI.')
184
+
185
+ if getv(from_object, ['title']):
186
+ raise ValueError('title parameter is not supported in Google AI.')
187
+
188
+ if getv(from_object, ['min_length']):
189
+ raise ValueError('min_length parameter is not supported in Google AI.')
190
+
191
+ if getv(from_object, ['min_properties']):
192
+ raise ValueError('min_properties parameter is not supported in Google AI.')
193
+
194
+ if getv(from_object, ['max_items']):
195
+ raise ValueError('max_items parameter is not supported in Google AI.')
196
+
197
+ if getv(from_object, ['maximum']):
198
+ raise ValueError('maximum parameter is not supported in Google AI.')
199
+
200
+ if getv(from_object, ['nullable']):
201
+ raise ValueError('nullable parameter is not supported in Google AI.')
202
+
203
+ if getv(from_object, ['max_properties']):
204
+ raise ValueError('max_properties parameter is not supported in Google AI.')
205
+
206
+ if getv(from_object, ['type']) is not None:
207
+ setv(to_object, ['type'], getv(from_object, ['type']))
208
+
209
+ if getv(from_object, ['description']) is not None:
210
+ setv(to_object, ['description'], getv(from_object, ['description']))
211
+
212
+ if getv(from_object, ['enum']) is not None:
213
+ setv(to_object, ['enum'], getv(from_object, ['enum']))
214
+
215
+ if getv(from_object, ['format']) is not None:
216
+ setv(to_object, ['format'], getv(from_object, ['format']))
217
+
218
+ if getv(from_object, ['items']) is not None:
219
+ setv(to_object, ['items'], getv(from_object, ['items']))
220
+
221
+ if getv(from_object, ['properties']) is not None:
222
+ setv(to_object, ['properties'], getv(from_object, ['properties']))
223
+
224
+ if getv(from_object, ['required']) is not None:
225
+ setv(to_object, ['required'], getv(from_object, ['required']))
226
+
227
+ return to_object
228
+
229
+
230
+ def _Schema_to_vertex(
231
+ api_client: ApiClient,
232
+ from_object: Union[dict, object],
233
+ parent_object: dict = None,
234
+ ) -> dict:
235
+ to_object = {}
236
+ if getv(from_object, ['min_items']) is not None:
237
+ setv(to_object, ['minItems'], getv(from_object, ['min_items']))
238
+
239
+ if getv(from_object, ['example']) is not None:
240
+ setv(to_object, ['example'], getv(from_object, ['example']))
241
+
242
+ if getv(from_object, ['property_ordering']) is not None:
243
+ setv(
244
+ to_object,
245
+ ['propertyOrdering'],
246
+ getv(from_object, ['property_ordering']),
247
+ )
248
+
249
+ if getv(from_object, ['pattern']) is not None:
250
+ setv(to_object, ['pattern'], getv(from_object, ['pattern']))
251
+
252
+ if getv(from_object, ['minimum']) is not None:
253
+ setv(to_object, ['minimum'], getv(from_object, ['minimum']))
254
+
255
+ if getv(from_object, ['default']) is not None:
256
+ setv(to_object, ['default'], getv(from_object, ['default']))
257
+
258
+ if getv(from_object, ['any_of']) is not None:
259
+ setv(to_object, ['anyOf'], getv(from_object, ['any_of']))
260
+
261
+ if getv(from_object, ['max_length']) is not None:
262
+ setv(to_object, ['maxLength'], getv(from_object, ['max_length']))
263
+
264
+ if getv(from_object, ['title']) is not None:
265
+ setv(to_object, ['title'], getv(from_object, ['title']))
266
+
267
+ if getv(from_object, ['min_length']) is not None:
268
+ setv(to_object, ['minLength'], getv(from_object, ['min_length']))
269
+
270
+ if getv(from_object, ['min_properties']) is not None:
271
+ setv(to_object, ['minProperties'], getv(from_object, ['min_properties']))
272
+
273
+ if getv(from_object, ['max_items']) is not None:
274
+ setv(to_object, ['maxItems'], getv(from_object, ['max_items']))
275
+
276
+ if getv(from_object, ['maximum']) is not None:
277
+ setv(to_object, ['maximum'], getv(from_object, ['maximum']))
278
+
279
+ if getv(from_object, ['nullable']) is not None:
280
+ setv(to_object, ['nullable'], getv(from_object, ['nullable']))
281
+
282
+ if getv(from_object, ['max_properties']) is not None:
283
+ setv(to_object, ['maxProperties'], getv(from_object, ['max_properties']))
284
+
285
+ if getv(from_object, ['type']) is not None:
286
+ setv(to_object, ['type'], getv(from_object, ['type']))
287
+
288
+ if getv(from_object, ['description']) is not None:
289
+ setv(to_object, ['description'], getv(from_object, ['description']))
290
+
291
+ if getv(from_object, ['enum']) is not None:
292
+ setv(to_object, ['enum'], getv(from_object, ['enum']))
293
+
294
+ if getv(from_object, ['format']) is not None:
295
+ setv(to_object, ['format'], getv(from_object, ['format']))
296
+
297
+ if getv(from_object, ['items']) is not None:
298
+ setv(to_object, ['items'], getv(from_object, ['items']))
299
+
300
+ if getv(from_object, ['properties']) is not None:
301
+ setv(to_object, ['properties'], getv(from_object, ['properties']))
302
+
303
+ if getv(from_object, ['required']) is not None:
304
+ setv(to_object, ['required'], getv(from_object, ['required']))
305
+
306
+ return to_object
307
+
308
+
309
+ def _FunctionDeclaration_to_mldev(
310
+ api_client: ApiClient,
311
+ from_object: Union[dict, object],
312
+ parent_object: dict = None,
313
+ ) -> dict:
314
+ to_object = {}
315
+ if getv(from_object, ['response']):
316
+ raise ValueError('response parameter is not supported in Google AI.')
317
+
318
+ if getv(from_object, ['description']) is not None:
319
+ setv(to_object, ['description'], getv(from_object, ['description']))
320
+
321
+ if getv(from_object, ['name']) is not None:
322
+ setv(to_object, ['name'], getv(from_object, ['name']))
323
+
324
+ if getv(from_object, ['parameters']) is not None:
325
+ setv(to_object, ['parameters'], getv(from_object, ['parameters']))
326
+
327
+ return to_object
328
+
329
+
330
+ def _FunctionDeclaration_to_vertex(
331
+ api_client: ApiClient,
332
+ from_object: Union[dict, object],
333
+ parent_object: dict = None,
334
+ ) -> dict:
335
+ to_object = {}
336
+ if getv(from_object, ['response']) is not None:
337
+ setv(
338
+ to_object,
339
+ ['response'],
340
+ _Schema_to_vertex(
341
+ api_client, getv(from_object, ['response']), to_object
342
+ ),
343
+ )
344
+
345
+ if getv(from_object, ['description']) is not None:
346
+ setv(to_object, ['description'], getv(from_object, ['description']))
347
+
348
+ if getv(from_object, ['name']) is not None:
349
+ setv(to_object, ['name'], getv(from_object, ['name']))
350
+
351
+ if getv(from_object, ['parameters']) is not None:
352
+ setv(to_object, ['parameters'], getv(from_object, ['parameters']))
353
+
354
+ return to_object
355
+
356
+
357
+ def _GoogleSearch_to_mldev(
358
+ api_client: ApiClient,
359
+ from_object: Union[dict, object],
360
+ parent_object: dict = None,
361
+ ) -> dict:
362
+ to_object = {}
363
+
364
+ return to_object
365
+
366
+
367
+ def _GoogleSearch_to_vertex(
368
+ api_client: ApiClient,
369
+ from_object: Union[dict, object],
370
+ parent_object: dict = None,
371
+ ) -> dict:
372
+ to_object = {}
373
+
374
+ return to_object
375
+
376
+
377
+ def _DynamicRetrievalConfig_to_mldev(
378
+ api_client: ApiClient,
379
+ from_object: Union[dict, object],
380
+ parent_object: dict = None,
381
+ ) -> dict:
382
+ to_object = {}
383
+ if getv(from_object, ['mode']) is not None:
384
+ setv(to_object, ['mode'], getv(from_object, ['mode']))
385
+
386
+ if getv(from_object, ['dynamic_threshold']) is not None:
387
+ setv(
388
+ to_object,
389
+ ['dynamicThreshold'],
390
+ getv(from_object, ['dynamic_threshold']),
391
+ )
392
+
393
+ return to_object
394
+
395
+
396
+ def _DynamicRetrievalConfig_to_vertex(
397
+ api_client: ApiClient,
398
+ from_object: Union[dict, object],
399
+ parent_object: dict = None,
400
+ ) -> dict:
401
+ to_object = {}
402
+ if getv(from_object, ['mode']) is not None:
403
+ setv(to_object, ['mode'], getv(from_object, ['mode']))
404
+
405
+ if getv(from_object, ['dynamic_threshold']) is not None:
406
+ setv(
407
+ to_object,
408
+ ['dynamicThreshold'],
409
+ getv(from_object, ['dynamic_threshold']),
410
+ )
411
+
412
+ return to_object
413
+
414
+
415
+ def _GoogleSearchRetrieval_to_mldev(
416
+ api_client: ApiClient,
417
+ from_object: Union[dict, object],
418
+ parent_object: dict = None,
419
+ ) -> dict:
420
+ to_object = {}
421
+ if getv(from_object, ['dynamic_retrieval_config']) is not None:
422
+ setv(
423
+ to_object,
424
+ ['dynamicRetrievalConfig'],
425
+ _DynamicRetrievalConfig_to_mldev(
426
+ api_client,
427
+ getv(from_object, ['dynamic_retrieval_config']),
428
+ to_object,
429
+ ),
430
+ )
431
+
432
+ return to_object
433
+
434
+
435
+ def _GoogleSearchRetrieval_to_vertex(
436
+ api_client: ApiClient,
437
+ from_object: Union[dict, object],
438
+ parent_object: dict = None,
439
+ ) -> dict:
440
+ to_object = {}
441
+ if getv(from_object, ['dynamic_retrieval_config']) is not None:
442
+ setv(
443
+ to_object,
444
+ ['dynamicRetrievalConfig'],
445
+ _DynamicRetrievalConfig_to_vertex(
446
+ api_client,
447
+ getv(from_object, ['dynamic_retrieval_config']),
448
+ to_object,
449
+ ),
450
+ )
451
+
452
+ return to_object
453
+
454
+
455
+ def _Tool_to_mldev(
456
+ api_client: ApiClient,
457
+ from_object: Union[dict, object],
458
+ parent_object: dict = None,
459
+ ) -> dict:
460
+ to_object = {}
461
+ if getv(from_object, ['function_declarations']) is not None:
462
+ setv(
463
+ to_object,
464
+ ['functionDeclarations'],
465
+ [
466
+ _FunctionDeclaration_to_mldev(api_client, item, to_object)
467
+ for item in getv(from_object, ['function_declarations'])
468
+ ],
469
+ )
470
+
471
+ if getv(from_object, ['retrieval']):
472
+ raise ValueError('retrieval parameter is not supported in Google AI.')
473
+
474
+ if getv(from_object, ['google_search']) is not None:
475
+ setv(
476
+ to_object,
477
+ ['googleSearch'],
478
+ _GoogleSearch_to_mldev(
479
+ api_client, getv(from_object, ['google_search']), to_object
480
+ ),
481
+ )
482
+
483
+ if getv(from_object, ['google_search_retrieval']) is not None:
484
+ setv(
485
+ to_object,
486
+ ['googleSearchRetrieval'],
487
+ _GoogleSearchRetrieval_to_mldev(
488
+ api_client,
489
+ getv(from_object, ['google_search_retrieval']),
490
+ to_object,
491
+ ),
492
+ )
493
+
494
+ if getv(from_object, ['code_execution']) is not None:
495
+ setv(to_object, ['codeExecution'], getv(from_object, ['code_execution']))
496
+
497
+ return to_object
498
+
499
+
500
+ def _Tool_to_vertex(
501
+ api_client: ApiClient,
502
+ from_object: Union[dict, object],
503
+ parent_object: dict = None,
504
+ ) -> dict:
505
+ to_object = {}
506
+ if getv(from_object, ['function_declarations']) is not None:
507
+ setv(
508
+ to_object,
509
+ ['functionDeclarations'],
510
+ [
511
+ _FunctionDeclaration_to_vertex(api_client, item, to_object)
512
+ for item in getv(from_object, ['function_declarations'])
513
+ ],
514
+ )
515
+
516
+ if getv(from_object, ['retrieval']) is not None:
517
+ setv(to_object, ['retrieval'], getv(from_object, ['retrieval']))
518
+
519
+ if getv(from_object, ['google_search']) is not None:
520
+ setv(
521
+ to_object,
522
+ ['googleSearch'],
523
+ _GoogleSearch_to_vertex(
524
+ api_client, getv(from_object, ['google_search']), to_object
525
+ ),
526
+ )
527
+
528
+ if getv(from_object, ['google_search_retrieval']) is not None:
529
+ setv(
530
+ to_object,
531
+ ['googleSearchRetrieval'],
532
+ _GoogleSearchRetrieval_to_vertex(
533
+ api_client,
534
+ getv(from_object, ['google_search_retrieval']),
535
+ to_object,
536
+ ),
537
+ )
538
+
539
+ if getv(from_object, ['code_execution']) is not None:
540
+ setv(to_object, ['codeExecution'], getv(from_object, ['code_execution']))
541
+
542
+ return to_object
543
+
544
+
545
+ def _FunctionCallingConfig_to_mldev(
546
+ api_client: ApiClient,
547
+ from_object: Union[dict, object],
548
+ parent_object: dict = None,
549
+ ) -> dict:
550
+ to_object = {}
551
+ if getv(from_object, ['mode']) is not None:
552
+ setv(to_object, ['mode'], getv(from_object, ['mode']))
553
+
554
+ if getv(from_object, ['allowed_function_names']) is not None:
555
+ setv(
556
+ to_object,
557
+ ['allowedFunctionNames'],
558
+ getv(from_object, ['allowed_function_names']),
559
+ )
560
+
561
+ return to_object
562
+
563
+
564
+ def _FunctionCallingConfig_to_vertex(
565
+ api_client: ApiClient,
566
+ from_object: Union[dict, object],
567
+ parent_object: dict = None,
568
+ ) -> dict:
569
+ to_object = {}
570
+ if getv(from_object, ['mode']) is not None:
571
+ setv(to_object, ['mode'], getv(from_object, ['mode']))
572
+
573
+ if getv(from_object, ['allowed_function_names']) is not None:
574
+ setv(
575
+ to_object,
576
+ ['allowedFunctionNames'],
577
+ getv(from_object, ['allowed_function_names']),
578
+ )
579
+
580
+ return to_object
581
+
582
+
583
+ def _ToolConfig_to_mldev(
584
+ api_client: ApiClient,
585
+ from_object: Union[dict, object],
586
+ parent_object: dict = None,
587
+ ) -> dict:
588
+ to_object = {}
589
+ if getv(from_object, ['function_calling_config']) is not None:
590
+ setv(
591
+ to_object,
592
+ ['functionCallingConfig'],
593
+ _FunctionCallingConfig_to_mldev(
594
+ api_client,
595
+ getv(from_object, ['function_calling_config']),
596
+ to_object,
597
+ ),
598
+ )
599
+
600
+ return to_object
601
+
602
+
603
+ def _ToolConfig_to_vertex(
604
+ api_client: ApiClient,
605
+ from_object: Union[dict, object],
606
+ parent_object: dict = None,
607
+ ) -> dict:
608
+ to_object = {}
609
+ if getv(from_object, ['function_calling_config']) is not None:
610
+ setv(
611
+ to_object,
612
+ ['functionCallingConfig'],
613
+ _FunctionCallingConfig_to_vertex(
614
+ api_client,
615
+ getv(from_object, ['function_calling_config']),
616
+ to_object,
617
+ ),
618
+ )
619
+
620
+ return to_object
621
+
622
+
623
+ def _CreateCachedContentConfig_to_mldev(
624
+ api_client: ApiClient,
625
+ from_object: Union[dict, object],
626
+ parent_object: dict = None,
627
+ ) -> dict:
628
+ to_object = {}
629
+ if getv(from_object, ['http_options']) is not None:
630
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
631
+
632
+ if getv(from_object, ['ttl']) is not None:
633
+ setv(parent_object, ['ttl'], getv(from_object, ['ttl']))
634
+
635
+ if getv(from_object, ['expire_time']) is not None:
636
+ setv(parent_object, ['expireTime'], getv(from_object, ['expire_time']))
637
+
638
+ if getv(from_object, ['display_name']) is not None:
639
+ setv(parent_object, ['displayName'], getv(from_object, ['display_name']))
640
+
641
+ if getv(from_object, ['system_instruction']) is not None:
642
+ setv(
643
+ parent_object,
644
+ ['systemInstruction'],
645
+ _Content_to_mldev(
646
+ api_client,
647
+ t.t_content(api_client, getv(from_object, ['system_instruction'])),
648
+ to_object,
649
+ ),
650
+ )
651
+
652
+ if getv(from_object, ['tools']) is not None:
653
+ setv(
654
+ parent_object,
655
+ ['tools'],
656
+ [
657
+ _Tool_to_mldev(api_client, item, to_object)
658
+ for item in getv(from_object, ['tools'])
659
+ ],
660
+ )
661
+
662
+ if getv(from_object, ['tool_config']) is not None:
663
+ setv(
664
+ parent_object,
665
+ ['toolConfig'],
666
+ _ToolConfig_to_mldev(
667
+ api_client, getv(from_object, ['tool_config']), to_object
668
+ ),
669
+ )
670
+
671
+ return to_object
672
+
673
+
674
+ def _CreateCachedContentConfig_to_vertex(
675
+ api_client: ApiClient,
676
+ from_object: Union[dict, object],
677
+ parent_object: dict = None,
678
+ ) -> dict:
679
+ to_object = {}
680
+ if getv(from_object, ['http_options']) is not None:
681
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
682
+
683
+ if getv(from_object, ['ttl']) is not None:
684
+ setv(parent_object, ['ttl'], getv(from_object, ['ttl']))
685
+
686
+ if getv(from_object, ['expire_time']) is not None:
687
+ setv(parent_object, ['expireTime'], getv(from_object, ['expire_time']))
688
+
689
+ if getv(from_object, ['display_name']) is not None:
690
+ setv(parent_object, ['displayName'], getv(from_object, ['display_name']))
691
+
692
+ if getv(from_object, ['system_instruction']) is not None:
693
+ setv(
694
+ parent_object,
695
+ ['systemInstruction'],
696
+ _Content_to_vertex(
697
+ api_client,
698
+ t.t_content(api_client, getv(from_object, ['system_instruction'])),
699
+ to_object,
700
+ ),
701
+ )
702
+
703
+ if getv(from_object, ['tools']) is not None:
704
+ setv(
705
+ parent_object,
706
+ ['tools'],
707
+ [
708
+ _Tool_to_vertex(api_client, item, to_object)
709
+ for item in getv(from_object, ['tools'])
710
+ ],
711
+ )
712
+
713
+ if getv(from_object, ['tool_config']) is not None:
714
+ setv(
715
+ parent_object,
716
+ ['toolConfig'],
717
+ _ToolConfig_to_vertex(
718
+ api_client, getv(from_object, ['tool_config']), to_object
719
+ ),
720
+ )
721
+
722
+ return to_object
723
+
724
+
725
+ def _CreateCachedContentParameters_to_mldev(
726
+ api_client: ApiClient,
727
+ from_object: Union[dict, object],
728
+ parent_object: dict = None,
729
+ ) -> dict:
730
+ to_object = {}
731
+ if getv(from_object, ['model']) is not None:
732
+ setv(
733
+ to_object,
734
+ ['model'],
735
+ t.t_caches_model(api_client, getv(from_object, ['model'])),
736
+ )
737
+
738
+ if getv(from_object, ['contents']) is not None:
739
+ setv(
740
+ to_object,
741
+ ['contents'],
742
+ [
743
+ _Content_to_mldev(api_client, item, to_object)
744
+ for item in t.t_contents(
745
+ api_client, getv(from_object, ['contents'])
746
+ )
747
+ ],
748
+ )
749
+
750
+ if getv(from_object, ['config']) is not None:
751
+ setv(
752
+ to_object,
753
+ ['config'],
754
+ _CreateCachedContentConfig_to_mldev(
755
+ api_client, getv(from_object, ['config']), to_object
756
+ ),
757
+ )
758
+
759
+ return to_object
760
+
761
+
762
+ def _CreateCachedContentParameters_to_vertex(
763
+ api_client: ApiClient,
764
+ from_object: Union[dict, object],
765
+ parent_object: dict = None,
766
+ ) -> dict:
767
+ to_object = {}
768
+ if getv(from_object, ['model']) is not None:
769
+ setv(
770
+ to_object,
771
+ ['model'],
772
+ t.t_caches_model(api_client, getv(from_object, ['model'])),
773
+ )
774
+
775
+ if getv(from_object, ['contents']) is not None:
776
+ setv(
777
+ to_object,
778
+ ['contents'],
779
+ [
780
+ _Content_to_vertex(api_client, item, to_object)
781
+ for item in t.t_contents(
782
+ api_client, getv(from_object, ['contents'])
783
+ )
784
+ ],
785
+ )
786
+
787
+ if getv(from_object, ['config']) is not None:
788
+ setv(
789
+ to_object,
790
+ ['config'],
791
+ _CreateCachedContentConfig_to_vertex(
792
+ api_client, getv(from_object, ['config']), to_object
793
+ ),
794
+ )
795
+
796
+ return to_object
797
+
798
+
799
+ def _GetCachedContentConfig_to_mldev(
800
+ api_client: ApiClient,
801
+ from_object: Union[dict, object],
802
+ parent_object: dict = None,
803
+ ) -> dict:
804
+ to_object = {}
805
+ if getv(from_object, ['http_options']) is not None:
806
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
807
+
808
+ return to_object
809
+
810
+
811
+ def _GetCachedContentConfig_to_vertex(
812
+ api_client: ApiClient,
813
+ from_object: Union[dict, object],
814
+ parent_object: dict = None,
815
+ ) -> dict:
816
+ to_object = {}
817
+ if getv(from_object, ['http_options']) is not None:
818
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
819
+
820
+ return to_object
821
+
822
+
823
+ def _GetCachedContentParameters_to_mldev(
824
+ api_client: ApiClient,
825
+ from_object: Union[dict, object],
826
+ parent_object: dict = None,
827
+ ) -> dict:
828
+ to_object = {}
829
+ if getv(from_object, ['name']) is not None:
830
+ setv(
831
+ to_object,
832
+ ['_url', 'name'],
833
+ t.t_cached_content_name(api_client, getv(from_object, ['name'])),
834
+ )
835
+
836
+ if getv(from_object, ['config']) is not None:
837
+ setv(
838
+ to_object,
839
+ ['config'],
840
+ _GetCachedContentConfig_to_mldev(
841
+ api_client, getv(from_object, ['config']), to_object
842
+ ),
843
+ )
844
+
845
+ return to_object
846
+
847
+
848
+ def _GetCachedContentParameters_to_vertex(
849
+ api_client: ApiClient,
850
+ from_object: Union[dict, object],
851
+ parent_object: dict = None,
852
+ ) -> dict:
853
+ to_object = {}
854
+ if getv(from_object, ['name']) is not None:
855
+ setv(
856
+ to_object,
857
+ ['_url', 'name'],
858
+ t.t_cached_content_name(api_client, getv(from_object, ['name'])),
859
+ )
860
+
861
+ if getv(from_object, ['config']) is not None:
862
+ setv(
863
+ to_object,
864
+ ['config'],
865
+ _GetCachedContentConfig_to_vertex(
866
+ api_client, getv(from_object, ['config']), to_object
867
+ ),
868
+ )
869
+
870
+ return to_object
871
+
872
+
873
+ def _DeleteCachedContentConfig_to_mldev(
874
+ api_client: ApiClient,
875
+ from_object: Union[dict, object],
876
+ parent_object: dict = None,
877
+ ) -> dict:
878
+ to_object = {}
879
+ if getv(from_object, ['http_options']) is not None:
880
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
881
+
882
+ return to_object
883
+
884
+
885
+ def _DeleteCachedContentConfig_to_vertex(
886
+ api_client: ApiClient,
887
+ from_object: Union[dict, object],
888
+ parent_object: dict = None,
889
+ ) -> dict:
890
+ to_object = {}
891
+ if getv(from_object, ['http_options']) is not None:
892
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
893
+
894
+ return to_object
895
+
896
+
897
+ def _DeleteCachedContentParameters_to_mldev(
898
+ api_client: ApiClient,
899
+ from_object: Union[dict, object],
900
+ parent_object: dict = None,
901
+ ) -> dict:
902
+ to_object = {}
903
+ if getv(from_object, ['name']) is not None:
904
+ setv(
905
+ to_object,
906
+ ['_url', 'name'],
907
+ t.t_cached_content_name(api_client, getv(from_object, ['name'])),
908
+ )
909
+
910
+ if getv(from_object, ['config']) is not None:
911
+ setv(
912
+ to_object,
913
+ ['config'],
914
+ _DeleteCachedContentConfig_to_mldev(
915
+ api_client, getv(from_object, ['config']), to_object
916
+ ),
917
+ )
918
+
919
+ return to_object
920
+
921
+
922
+ def _DeleteCachedContentParameters_to_vertex(
923
+ api_client: ApiClient,
924
+ from_object: Union[dict, object],
925
+ parent_object: dict = None,
926
+ ) -> dict:
927
+ to_object = {}
928
+ if getv(from_object, ['name']) is not None:
929
+ setv(
930
+ to_object,
931
+ ['_url', 'name'],
932
+ t.t_cached_content_name(api_client, getv(from_object, ['name'])),
933
+ )
934
+
935
+ if getv(from_object, ['config']) is not None:
936
+ setv(
937
+ to_object,
938
+ ['config'],
939
+ _DeleteCachedContentConfig_to_vertex(
940
+ api_client, getv(from_object, ['config']), to_object
941
+ ),
942
+ )
943
+
944
+ return to_object
945
+
946
+
947
+ def _UpdateCachedContentConfig_to_mldev(
948
+ api_client: ApiClient,
949
+ from_object: Union[dict, object],
950
+ parent_object: dict = None,
951
+ ) -> dict:
952
+ to_object = {}
953
+ if getv(from_object, ['http_options']) is not None:
954
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
955
+
956
+ if getv(from_object, ['ttl']) is not None:
957
+ setv(parent_object, ['ttl'], getv(from_object, ['ttl']))
958
+
959
+ if getv(from_object, ['expire_time']) is not None:
960
+ setv(parent_object, ['expireTime'], getv(from_object, ['expire_time']))
961
+
962
+ return to_object
963
+
964
+
965
+ def _UpdateCachedContentConfig_to_vertex(
966
+ api_client: ApiClient,
967
+ from_object: Union[dict, object],
968
+ parent_object: dict = None,
969
+ ) -> dict:
970
+ to_object = {}
971
+ if getv(from_object, ['http_options']) is not None:
972
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
973
+
974
+ if getv(from_object, ['ttl']) is not None:
975
+ setv(parent_object, ['ttl'], getv(from_object, ['ttl']))
976
+
977
+ if getv(from_object, ['expire_time']) is not None:
978
+ setv(parent_object, ['expireTime'], getv(from_object, ['expire_time']))
979
+
980
+ return to_object
981
+
982
+
983
+ def _UpdateCachedContentParameters_to_mldev(
984
+ api_client: ApiClient,
985
+ from_object: Union[dict, object],
986
+ parent_object: dict = None,
987
+ ) -> dict:
988
+ to_object = {}
989
+ if getv(from_object, ['name']) is not None:
990
+ setv(
991
+ to_object,
992
+ ['_url', 'name'],
993
+ t.t_cached_content_name(api_client, getv(from_object, ['name'])),
994
+ )
995
+
996
+ if getv(from_object, ['config']) is not None:
997
+ setv(
998
+ to_object,
999
+ ['config'],
1000
+ _UpdateCachedContentConfig_to_mldev(
1001
+ api_client, getv(from_object, ['config']), to_object
1002
+ ),
1003
+ )
1004
+
1005
+ return to_object
1006
+
1007
+
1008
+ def _UpdateCachedContentParameters_to_vertex(
1009
+ api_client: ApiClient,
1010
+ from_object: Union[dict, object],
1011
+ parent_object: dict = None,
1012
+ ) -> dict:
1013
+ to_object = {}
1014
+ if getv(from_object, ['name']) is not None:
1015
+ setv(
1016
+ to_object,
1017
+ ['_url', 'name'],
1018
+ t.t_cached_content_name(api_client, getv(from_object, ['name'])),
1019
+ )
1020
+
1021
+ if getv(from_object, ['config']) is not None:
1022
+ setv(
1023
+ to_object,
1024
+ ['config'],
1025
+ _UpdateCachedContentConfig_to_vertex(
1026
+ api_client, getv(from_object, ['config']), to_object
1027
+ ),
1028
+ )
1029
+
1030
+ return to_object
1031
+
1032
+
1033
+ def _ListCachedContentsConfig_to_mldev(
1034
+ api_client: ApiClient,
1035
+ from_object: Union[dict, object],
1036
+ parent_object: dict = None,
1037
+ ) -> dict:
1038
+ to_object = {}
1039
+ if getv(from_object, ['page_size']) is not None:
1040
+ setv(
1041
+ parent_object, ['_query', 'pageSize'], getv(from_object, ['page_size'])
1042
+ )
1043
+
1044
+ if getv(from_object, ['page_token']) is not None:
1045
+ setv(
1046
+ parent_object,
1047
+ ['_query', 'pageToken'],
1048
+ getv(from_object, ['page_token']),
1049
+ )
1050
+
1051
+ return to_object
1052
+
1053
+
1054
+ def _ListCachedContentsConfig_to_vertex(
1055
+ api_client: ApiClient,
1056
+ from_object: Union[dict, object],
1057
+ parent_object: dict = None,
1058
+ ) -> dict:
1059
+ to_object = {}
1060
+ if getv(from_object, ['page_size']) is not None:
1061
+ setv(
1062
+ parent_object, ['_query', 'pageSize'], getv(from_object, ['page_size'])
1063
+ )
1064
+
1065
+ if getv(from_object, ['page_token']) is not None:
1066
+ setv(
1067
+ parent_object,
1068
+ ['_query', 'pageToken'],
1069
+ getv(from_object, ['page_token']),
1070
+ )
1071
+
1072
+ return to_object
1073
+
1074
+
1075
+ def _ListCachedContentsParameters_to_mldev(
1076
+ api_client: ApiClient,
1077
+ from_object: Union[dict, object],
1078
+ parent_object: dict = None,
1079
+ ) -> dict:
1080
+ to_object = {}
1081
+ if getv(from_object, ['config']) is not None:
1082
+ setv(
1083
+ to_object,
1084
+ ['config'],
1085
+ _ListCachedContentsConfig_to_mldev(
1086
+ api_client, getv(from_object, ['config']), to_object
1087
+ ),
1088
+ )
1089
+
1090
+ return to_object
1091
+
1092
+
1093
+ def _ListCachedContentsParameters_to_vertex(
1094
+ api_client: ApiClient,
1095
+ from_object: Union[dict, object],
1096
+ parent_object: dict = None,
1097
+ ) -> dict:
1098
+ to_object = {}
1099
+ if getv(from_object, ['config']) is not None:
1100
+ setv(
1101
+ to_object,
1102
+ ['config'],
1103
+ _ListCachedContentsConfig_to_vertex(
1104
+ api_client, getv(from_object, ['config']), to_object
1105
+ ),
1106
+ )
1107
+
1108
+ return to_object
1109
+
1110
+
1111
+ def _CachedContent_from_mldev(
1112
+ api_client: ApiClient,
1113
+ from_object: Union[dict, object],
1114
+ parent_object: dict = None,
1115
+ ) -> dict:
1116
+ to_object = {}
1117
+ if getv(from_object, ['name']) is not None:
1118
+ setv(to_object, ['name'], getv(from_object, ['name']))
1119
+
1120
+ if getv(from_object, ['displayName']) is not None:
1121
+ setv(to_object, ['display_name'], getv(from_object, ['displayName']))
1122
+
1123
+ if getv(from_object, ['model']) is not None:
1124
+ setv(to_object, ['model'], getv(from_object, ['model']))
1125
+
1126
+ if getv(from_object, ['createTime']) is not None:
1127
+ setv(to_object, ['create_time'], getv(from_object, ['createTime']))
1128
+
1129
+ if getv(from_object, ['updateTime']) is not None:
1130
+ setv(to_object, ['update_time'], getv(from_object, ['updateTime']))
1131
+
1132
+ if getv(from_object, ['expireTime']) is not None:
1133
+ setv(to_object, ['expire_time'], getv(from_object, ['expireTime']))
1134
+
1135
+ if getv(from_object, ['usageMetadata']) is not None:
1136
+ setv(to_object, ['usage_metadata'], getv(from_object, ['usageMetadata']))
1137
+
1138
+ return to_object
1139
+
1140
+
1141
+ def _CachedContent_from_vertex(
1142
+ api_client: ApiClient,
1143
+ from_object: Union[dict, object],
1144
+ parent_object: dict = None,
1145
+ ) -> dict:
1146
+ to_object = {}
1147
+ if getv(from_object, ['name']) is not None:
1148
+ setv(to_object, ['name'], getv(from_object, ['name']))
1149
+
1150
+ if getv(from_object, ['displayName']) is not None:
1151
+ setv(to_object, ['display_name'], getv(from_object, ['displayName']))
1152
+
1153
+ if getv(from_object, ['model']) is not None:
1154
+ setv(to_object, ['model'], getv(from_object, ['model']))
1155
+
1156
+ if getv(from_object, ['createTime']) is not None:
1157
+ setv(to_object, ['create_time'], getv(from_object, ['createTime']))
1158
+
1159
+ if getv(from_object, ['updateTime']) is not None:
1160
+ setv(to_object, ['update_time'], getv(from_object, ['updateTime']))
1161
+
1162
+ if getv(from_object, ['expireTime']) is not None:
1163
+ setv(to_object, ['expire_time'], getv(from_object, ['expireTime']))
1164
+
1165
+ if getv(from_object, ['usageMetadata']) is not None:
1166
+ setv(to_object, ['usage_metadata'], getv(from_object, ['usageMetadata']))
1167
+
1168
+ return to_object
1169
+
1170
+
1171
+ def _DeleteCachedContentResponse_from_mldev(
1172
+ api_client: ApiClient,
1173
+ from_object: Union[dict, object],
1174
+ parent_object: dict = None,
1175
+ ) -> dict:
1176
+ to_object = {}
1177
+
1178
+ return to_object
1179
+
1180
+
1181
+ def _DeleteCachedContentResponse_from_vertex(
1182
+ api_client: ApiClient,
1183
+ from_object: Union[dict, object],
1184
+ parent_object: dict = None,
1185
+ ) -> dict:
1186
+ to_object = {}
1187
+
1188
+ return to_object
1189
+
1190
+
1191
+ def _ListCachedContentsResponse_from_mldev(
1192
+ api_client: ApiClient,
1193
+ from_object: Union[dict, object],
1194
+ parent_object: dict = None,
1195
+ ) -> dict:
1196
+ to_object = {}
1197
+ if getv(from_object, ['nextPageToken']) is not None:
1198
+ setv(to_object, ['next_page_token'], getv(from_object, ['nextPageToken']))
1199
+
1200
+ if getv(from_object, ['cachedContents']) is not None:
1201
+ setv(
1202
+ to_object,
1203
+ ['cached_contents'],
1204
+ [
1205
+ _CachedContent_from_mldev(api_client, item, to_object)
1206
+ for item in getv(from_object, ['cachedContents'])
1207
+ ],
1208
+ )
1209
+
1210
+ return to_object
1211
+
1212
+
1213
+ def _ListCachedContentsResponse_from_vertex(
1214
+ api_client: ApiClient,
1215
+ from_object: Union[dict, object],
1216
+ parent_object: dict = None,
1217
+ ) -> dict:
1218
+ to_object = {}
1219
+ if getv(from_object, ['nextPageToken']) is not None:
1220
+ setv(to_object, ['next_page_token'], getv(from_object, ['nextPageToken']))
1221
+
1222
+ if getv(from_object, ['cachedContents']) is not None:
1223
+ setv(
1224
+ to_object,
1225
+ ['cached_contents'],
1226
+ [
1227
+ _CachedContent_from_vertex(api_client, item, to_object)
1228
+ for item in getv(from_object, ['cachedContents'])
1229
+ ],
1230
+ )
1231
+
1232
+ return to_object
1233
+
1234
+
1235
+ class Caches(_common.BaseModule):
1236
+
1237
+ def create(
1238
+ self,
1239
+ *,
1240
+ model: str,
1241
+ contents: Union[types.ContentListUnion, types.ContentListUnionDict],
1242
+ config: Optional[types.CreateCachedContentConfigOrDict] = None,
1243
+ ) -> types.CachedContent:
1244
+ """Creates cached content, this call will initialize the cached
1245
+
1246
+ content in the data storage, and users need to pay for the cache data
1247
+ storage.
1248
+
1249
+ Usage:
1250
+
1251
+ .. code-block:: python
1252
+ contents = ... // Initialize the content to cache.
1253
+ response = await client.aio.caches.create(
1254
+ model= ... // The publisher model id
1255
+ contents=contents,
1256
+ config={
1257
+ 'display_name': 'test cache',
1258
+ 'system_instruction': 'What is the sum of the two pdfs?',
1259
+ 'ttl': '86400s',
1260
+ },
1261
+ )
1262
+ """
1263
+
1264
+ parameter_model = types._CreateCachedContentParameters(
1265
+ model=model,
1266
+ contents=contents,
1267
+ config=config,
1268
+ )
1269
+
1270
+ if self.api_client.vertexai:
1271
+ request_dict = _CreateCachedContentParameters_to_vertex(
1272
+ self.api_client, parameter_model
1273
+ )
1274
+ path = 'cachedContents'.format_map(request_dict.get('_url'))
1275
+ else:
1276
+ request_dict = _CreateCachedContentParameters_to_mldev(
1277
+ self.api_client, parameter_model
1278
+ )
1279
+ path = 'cachedContents'.format_map(request_dict.get('_url'))
1280
+ query_params = request_dict.get('_query')
1281
+ if query_params:
1282
+ path = f'{path}?{urlencode(query_params)}'
1283
+ # TODO: remove the hack that pops config.
1284
+ config = request_dict.pop('config', None)
1285
+ http_options = config.pop('httpOptions', None) if config else None
1286
+ request_dict = _common.convert_to_dict(request_dict)
1287
+ request_dict = _common.apply_base64_encoding(request_dict)
1288
+
1289
+ response_dict = self.api_client.request(
1290
+ 'post', path, request_dict, http_options
1291
+ )
1292
+
1293
+ if self.api_client.vertexai:
1294
+ response_dict = _CachedContent_from_vertex(self.api_client, response_dict)
1295
+ else:
1296
+ response_dict = _CachedContent_from_mldev(self.api_client, response_dict)
1297
+
1298
+ return_value = types.CachedContent._from_response(
1299
+ response_dict, parameter_model
1300
+ )
1301
+ self.api_client._verify_response(return_value)
1302
+ return return_value
1303
+
1304
+ def get(
1305
+ self,
1306
+ *,
1307
+ name: str,
1308
+ config: Optional[types.GetCachedContentConfigOrDict] = None,
1309
+ ) -> types.CachedContent:
1310
+ """Gets cached content configurations.
1311
+
1312
+ .. code-block:: python
1313
+ await client.aio.caches.get(name= ... ) // The server-generated resource
1314
+ name.
1315
+ """
1316
+
1317
+ parameter_model = types._GetCachedContentParameters(
1318
+ name=name,
1319
+ config=config,
1320
+ )
1321
+
1322
+ if self.api_client.vertexai:
1323
+ request_dict = _GetCachedContentParameters_to_vertex(
1324
+ self.api_client, parameter_model
1325
+ )
1326
+ path = '{name}'.format_map(request_dict.get('_url'))
1327
+ else:
1328
+ request_dict = _GetCachedContentParameters_to_mldev(
1329
+ self.api_client, parameter_model
1330
+ )
1331
+ path = '{name}'.format_map(request_dict.get('_url'))
1332
+ query_params = request_dict.get('_query')
1333
+ if query_params:
1334
+ path = f'{path}?{urlencode(query_params)}'
1335
+ # TODO: remove the hack that pops config.
1336
+ config = request_dict.pop('config', None)
1337
+ http_options = config.pop('httpOptions', None) if config else None
1338
+ request_dict = _common.convert_to_dict(request_dict)
1339
+ request_dict = _common.apply_base64_encoding(request_dict)
1340
+
1341
+ response_dict = self.api_client.request(
1342
+ 'get', path, request_dict, http_options
1343
+ )
1344
+
1345
+ if self.api_client.vertexai:
1346
+ response_dict = _CachedContent_from_vertex(self.api_client, response_dict)
1347
+ else:
1348
+ response_dict = _CachedContent_from_mldev(self.api_client, response_dict)
1349
+
1350
+ return_value = types.CachedContent._from_response(
1351
+ response_dict, parameter_model
1352
+ )
1353
+ self.api_client._verify_response(return_value)
1354
+ return return_value
1355
+
1356
+ def delete(
1357
+ self,
1358
+ *,
1359
+ name: str,
1360
+ config: Optional[types.DeleteCachedContentConfigOrDict] = None,
1361
+ ) -> types.DeleteCachedContentResponse:
1362
+ """Deletes cached content.
1363
+
1364
+ Usage:
1365
+
1366
+ .. code-block:: python
1367
+ await client.aio.caches.delete(name= ... ) // The server-generated
1368
+ resource name.
1369
+ """
1370
+
1371
+ parameter_model = types._DeleteCachedContentParameters(
1372
+ name=name,
1373
+ config=config,
1374
+ )
1375
+
1376
+ if self.api_client.vertexai:
1377
+ request_dict = _DeleteCachedContentParameters_to_vertex(
1378
+ self.api_client, parameter_model
1379
+ )
1380
+ path = '{name}'.format_map(request_dict.get('_url'))
1381
+ else:
1382
+ request_dict = _DeleteCachedContentParameters_to_mldev(
1383
+ self.api_client, parameter_model
1384
+ )
1385
+ path = '{name}'.format_map(request_dict.get('_url'))
1386
+ query_params = request_dict.get('_query')
1387
+ if query_params:
1388
+ path = f'{path}?{urlencode(query_params)}'
1389
+ # TODO: remove the hack that pops config.
1390
+ config = request_dict.pop('config', None)
1391
+ http_options = config.pop('httpOptions', None) if config else None
1392
+ request_dict = _common.convert_to_dict(request_dict)
1393
+ request_dict = _common.apply_base64_encoding(request_dict)
1394
+
1395
+ response_dict = self.api_client.request(
1396
+ 'delete', path, request_dict, http_options
1397
+ )
1398
+
1399
+ if self.api_client.vertexai:
1400
+ response_dict = _DeleteCachedContentResponse_from_vertex(
1401
+ self.api_client, response_dict
1402
+ )
1403
+ else:
1404
+ response_dict = _DeleteCachedContentResponse_from_mldev(
1405
+ self.api_client, response_dict
1406
+ )
1407
+
1408
+ return_value = types.DeleteCachedContentResponse._from_response(
1409
+ response_dict, parameter_model
1410
+ )
1411
+ self.api_client._verify_response(return_value)
1412
+ return return_value
1413
+
1414
+ def update(
1415
+ self,
1416
+ *,
1417
+ name: str,
1418
+ config: Optional[types.UpdateCachedContentConfigOrDict] = None,
1419
+ ) -> types.CachedContent:
1420
+ """Updates cached content configurations.
1421
+
1422
+ .. code-block:: python
1423
+ response = await client.aio.caches.update(
1424
+ name= ... // The server-generated resource name.
1425
+ config={
1426
+ 'ttl': '7600s',
1427
+ },
1428
+ )
1429
+ """
1430
+
1431
+ parameter_model = types._UpdateCachedContentParameters(
1432
+ name=name,
1433
+ config=config,
1434
+ )
1435
+
1436
+ if self.api_client.vertexai:
1437
+ request_dict = _UpdateCachedContentParameters_to_vertex(
1438
+ self.api_client, parameter_model
1439
+ )
1440
+ path = '{name}'.format_map(request_dict.get('_url'))
1441
+ else:
1442
+ request_dict = _UpdateCachedContentParameters_to_mldev(
1443
+ self.api_client, parameter_model
1444
+ )
1445
+ path = '{name}'.format_map(request_dict.get('_url'))
1446
+ query_params = request_dict.get('_query')
1447
+ if query_params:
1448
+ path = f'{path}?{urlencode(query_params)}'
1449
+ # TODO: remove the hack that pops config.
1450
+ config = request_dict.pop('config', None)
1451
+ http_options = config.pop('httpOptions', None) if config else None
1452
+ request_dict = _common.convert_to_dict(request_dict)
1453
+ request_dict = _common.apply_base64_encoding(request_dict)
1454
+
1455
+ response_dict = self.api_client.request(
1456
+ 'patch', path, request_dict, http_options
1457
+ )
1458
+
1459
+ if self.api_client.vertexai:
1460
+ response_dict = _CachedContent_from_vertex(self.api_client, response_dict)
1461
+ else:
1462
+ response_dict = _CachedContent_from_mldev(self.api_client, response_dict)
1463
+
1464
+ return_value = types.CachedContent._from_response(
1465
+ response_dict, parameter_model
1466
+ )
1467
+ self.api_client._verify_response(return_value)
1468
+ return return_value
1469
+
1470
+ def _list(
1471
+ self, *, config: Optional[types.ListCachedContentsConfigOrDict] = None
1472
+ ) -> types.ListCachedContentsResponse:
1473
+ """Lists cached content configurations.
1474
+
1475
+ .. code-block:: python
1476
+ cached_contents = await client.aio.caches.list(config={'page_size': 2})
1477
+ async for cached_content in cached_contents:
1478
+ print(cached_content)
1479
+ """
1480
+
1481
+ parameter_model = types._ListCachedContentsParameters(
1482
+ config=config,
1483
+ )
1484
+
1485
+ if self.api_client.vertexai:
1486
+ request_dict = _ListCachedContentsParameters_to_vertex(
1487
+ self.api_client, parameter_model
1488
+ )
1489
+ path = 'cachedContents'.format_map(request_dict.get('_url'))
1490
+ else:
1491
+ request_dict = _ListCachedContentsParameters_to_mldev(
1492
+ self.api_client, parameter_model
1493
+ )
1494
+ path = 'cachedContents'.format_map(request_dict.get('_url'))
1495
+ query_params = request_dict.get('_query')
1496
+ if query_params:
1497
+ path = f'{path}?{urlencode(query_params)}'
1498
+ # TODO: remove the hack that pops config.
1499
+ config = request_dict.pop('config', None)
1500
+ http_options = config.pop('httpOptions', None) if config else None
1501
+ request_dict = _common.convert_to_dict(request_dict)
1502
+ request_dict = _common.apply_base64_encoding(request_dict)
1503
+
1504
+ response_dict = self.api_client.request(
1505
+ 'get', path, request_dict, http_options
1506
+ )
1507
+
1508
+ if self.api_client.vertexai:
1509
+ response_dict = _ListCachedContentsResponse_from_vertex(
1510
+ self.api_client, response_dict
1511
+ )
1512
+ else:
1513
+ response_dict = _ListCachedContentsResponse_from_mldev(
1514
+ self.api_client, response_dict
1515
+ )
1516
+
1517
+ return_value = types.ListCachedContentsResponse._from_response(
1518
+ response_dict, parameter_model
1519
+ )
1520
+ self.api_client._verify_response(return_value)
1521
+ return return_value
1522
+
1523
+ def list(
1524
+ self, *, config: Optional[types.ListCachedContentsConfigOrDict] = None
1525
+ ) -> Pager[types.CachedContent]:
1526
+ return Pager(
1527
+ 'cached_contents',
1528
+ self._list,
1529
+ self._list(config=config),
1530
+ config,
1531
+ )
1532
+
1533
+
1534
+ class AsyncCaches(_common.BaseModule):
1535
+
1536
+ async def create(
1537
+ self,
1538
+ *,
1539
+ model: str,
1540
+ contents: Union[types.ContentListUnion, types.ContentListUnionDict],
1541
+ config: Optional[types.CreateCachedContentConfigOrDict] = None,
1542
+ ) -> types.CachedContent:
1543
+ """Creates cached content, this call will initialize the cached
1544
+
1545
+ content in the data storage, and users need to pay for the cache data
1546
+ storage.
1547
+
1548
+ Usage:
1549
+
1550
+ .. code-block:: python
1551
+ contents = ... // Initialize the content to cache.
1552
+ response = await client.aio.caches.create(
1553
+ model= ... // The publisher model id
1554
+ contents=contents,
1555
+ config={
1556
+ 'display_name': 'test cache',
1557
+ 'system_instruction': 'What is the sum of the two pdfs?',
1558
+ 'ttl': '86400s',
1559
+ },
1560
+ )
1561
+ """
1562
+
1563
+ parameter_model = types._CreateCachedContentParameters(
1564
+ model=model,
1565
+ contents=contents,
1566
+ config=config,
1567
+ )
1568
+
1569
+ if self.api_client.vertexai:
1570
+ request_dict = _CreateCachedContentParameters_to_vertex(
1571
+ self.api_client, parameter_model
1572
+ )
1573
+ path = 'cachedContents'.format_map(request_dict.get('_url'))
1574
+ else:
1575
+ request_dict = _CreateCachedContentParameters_to_mldev(
1576
+ self.api_client, parameter_model
1577
+ )
1578
+ path = 'cachedContents'.format_map(request_dict.get('_url'))
1579
+ query_params = request_dict.get('_query')
1580
+ if query_params:
1581
+ path = f'{path}?{urlencode(query_params)}'
1582
+ # TODO: remove the hack that pops config.
1583
+ config = request_dict.pop('config', None)
1584
+ http_options = config.pop('httpOptions', None) if config else None
1585
+ request_dict = _common.convert_to_dict(request_dict)
1586
+ request_dict = _common.apply_base64_encoding(request_dict)
1587
+
1588
+ response_dict = await self.api_client.async_request(
1589
+ 'post', path, request_dict, http_options
1590
+ )
1591
+
1592
+ if self.api_client.vertexai:
1593
+ response_dict = _CachedContent_from_vertex(self.api_client, response_dict)
1594
+ else:
1595
+ response_dict = _CachedContent_from_mldev(self.api_client, response_dict)
1596
+
1597
+ return_value = types.CachedContent._from_response(
1598
+ response_dict, parameter_model
1599
+ )
1600
+ self.api_client._verify_response(return_value)
1601
+ return return_value
1602
+
1603
+ async def get(
1604
+ self,
1605
+ *,
1606
+ name: str,
1607
+ config: Optional[types.GetCachedContentConfigOrDict] = None,
1608
+ ) -> types.CachedContent:
1609
+ """Gets cached content configurations.
1610
+
1611
+ .. code-block:: python
1612
+ await client.aio.caches.get(name= ... ) // The server-generated resource
1613
+ name.
1614
+ """
1615
+
1616
+ parameter_model = types._GetCachedContentParameters(
1617
+ name=name,
1618
+ config=config,
1619
+ )
1620
+
1621
+ if self.api_client.vertexai:
1622
+ request_dict = _GetCachedContentParameters_to_vertex(
1623
+ self.api_client, parameter_model
1624
+ )
1625
+ path = '{name}'.format_map(request_dict.get('_url'))
1626
+ else:
1627
+ request_dict = _GetCachedContentParameters_to_mldev(
1628
+ self.api_client, parameter_model
1629
+ )
1630
+ path = '{name}'.format_map(request_dict.get('_url'))
1631
+ query_params = request_dict.get('_query')
1632
+ if query_params:
1633
+ path = f'{path}?{urlencode(query_params)}'
1634
+ # TODO: remove the hack that pops config.
1635
+ config = request_dict.pop('config', None)
1636
+ http_options = config.pop('httpOptions', None) if config else None
1637
+ request_dict = _common.convert_to_dict(request_dict)
1638
+ request_dict = _common.apply_base64_encoding(request_dict)
1639
+
1640
+ response_dict = await self.api_client.async_request(
1641
+ 'get', path, request_dict, http_options
1642
+ )
1643
+
1644
+ if self.api_client.vertexai:
1645
+ response_dict = _CachedContent_from_vertex(self.api_client, response_dict)
1646
+ else:
1647
+ response_dict = _CachedContent_from_mldev(self.api_client, response_dict)
1648
+
1649
+ return_value = types.CachedContent._from_response(
1650
+ response_dict, parameter_model
1651
+ )
1652
+ self.api_client._verify_response(return_value)
1653
+ return return_value
1654
+
1655
+ async def delete(
1656
+ self,
1657
+ *,
1658
+ name: str,
1659
+ config: Optional[types.DeleteCachedContentConfigOrDict] = None,
1660
+ ) -> types.DeleteCachedContentResponse:
1661
+ """Deletes cached content.
1662
+
1663
+ Usage:
1664
+
1665
+ .. code-block:: python
1666
+ await client.aio.caches.delete(name= ... ) // The server-generated
1667
+ resource name.
1668
+ """
1669
+
1670
+ parameter_model = types._DeleteCachedContentParameters(
1671
+ name=name,
1672
+ config=config,
1673
+ )
1674
+
1675
+ if self.api_client.vertexai:
1676
+ request_dict = _DeleteCachedContentParameters_to_vertex(
1677
+ self.api_client, parameter_model
1678
+ )
1679
+ path = '{name}'.format_map(request_dict.get('_url'))
1680
+ else:
1681
+ request_dict = _DeleteCachedContentParameters_to_mldev(
1682
+ self.api_client, parameter_model
1683
+ )
1684
+ path = '{name}'.format_map(request_dict.get('_url'))
1685
+ query_params = request_dict.get('_query')
1686
+ if query_params:
1687
+ path = f'{path}?{urlencode(query_params)}'
1688
+ # TODO: remove the hack that pops config.
1689
+ config = request_dict.pop('config', None)
1690
+ http_options = config.pop('httpOptions', None) if config else None
1691
+ request_dict = _common.convert_to_dict(request_dict)
1692
+ request_dict = _common.apply_base64_encoding(request_dict)
1693
+
1694
+ response_dict = await self.api_client.async_request(
1695
+ 'delete', path, request_dict, http_options
1696
+ )
1697
+
1698
+ if self.api_client.vertexai:
1699
+ response_dict = _DeleteCachedContentResponse_from_vertex(
1700
+ self.api_client, response_dict
1701
+ )
1702
+ else:
1703
+ response_dict = _DeleteCachedContentResponse_from_mldev(
1704
+ self.api_client, response_dict
1705
+ )
1706
+
1707
+ return_value = types.DeleteCachedContentResponse._from_response(
1708
+ response_dict, parameter_model
1709
+ )
1710
+ self.api_client._verify_response(return_value)
1711
+ return return_value
1712
+
1713
+ async def update(
1714
+ self,
1715
+ *,
1716
+ name: str,
1717
+ config: Optional[types.UpdateCachedContentConfigOrDict] = None,
1718
+ ) -> types.CachedContent:
1719
+ """Updates cached content configurations.
1720
+
1721
+ .. code-block:: python
1722
+ response = await client.aio.caches.update(
1723
+ name= ... // The server-generated resource name.
1724
+ config={
1725
+ 'ttl': '7600s',
1726
+ },
1727
+ )
1728
+ """
1729
+
1730
+ parameter_model = types._UpdateCachedContentParameters(
1731
+ name=name,
1732
+ config=config,
1733
+ )
1734
+
1735
+ if self.api_client.vertexai:
1736
+ request_dict = _UpdateCachedContentParameters_to_vertex(
1737
+ self.api_client, parameter_model
1738
+ )
1739
+ path = '{name}'.format_map(request_dict.get('_url'))
1740
+ else:
1741
+ request_dict = _UpdateCachedContentParameters_to_mldev(
1742
+ self.api_client, parameter_model
1743
+ )
1744
+ path = '{name}'.format_map(request_dict.get('_url'))
1745
+ query_params = request_dict.get('_query')
1746
+ if query_params:
1747
+ path = f'{path}?{urlencode(query_params)}'
1748
+ # TODO: remove the hack that pops config.
1749
+ config = request_dict.pop('config', None)
1750
+ http_options = config.pop('httpOptions', None) if config else None
1751
+ request_dict = _common.convert_to_dict(request_dict)
1752
+ request_dict = _common.apply_base64_encoding(request_dict)
1753
+
1754
+ response_dict = await self.api_client.async_request(
1755
+ 'patch', path, request_dict, http_options
1756
+ )
1757
+
1758
+ if self.api_client.vertexai:
1759
+ response_dict = _CachedContent_from_vertex(self.api_client, response_dict)
1760
+ else:
1761
+ response_dict = _CachedContent_from_mldev(self.api_client, response_dict)
1762
+
1763
+ return_value = types.CachedContent._from_response(
1764
+ response_dict, parameter_model
1765
+ )
1766
+ self.api_client._verify_response(return_value)
1767
+ return return_value
1768
+
1769
+ async def _list(
1770
+ self, *, config: Optional[types.ListCachedContentsConfigOrDict] = None
1771
+ ) -> types.ListCachedContentsResponse:
1772
+ """Lists cached content configurations.
1773
+
1774
+ .. code-block:: python
1775
+ cached_contents = await client.aio.caches.list(config={'page_size': 2})
1776
+ async for cached_content in cached_contents:
1777
+ print(cached_content)
1778
+ """
1779
+
1780
+ parameter_model = types._ListCachedContentsParameters(
1781
+ config=config,
1782
+ )
1783
+
1784
+ if self.api_client.vertexai:
1785
+ request_dict = _ListCachedContentsParameters_to_vertex(
1786
+ self.api_client, parameter_model
1787
+ )
1788
+ path = 'cachedContents'.format_map(request_dict.get('_url'))
1789
+ else:
1790
+ request_dict = _ListCachedContentsParameters_to_mldev(
1791
+ self.api_client, parameter_model
1792
+ )
1793
+ path = 'cachedContents'.format_map(request_dict.get('_url'))
1794
+ query_params = request_dict.get('_query')
1795
+ if query_params:
1796
+ path = f'{path}?{urlencode(query_params)}'
1797
+ # TODO: remove the hack that pops config.
1798
+ config = request_dict.pop('config', None)
1799
+ http_options = config.pop('httpOptions', None) if config else None
1800
+ request_dict = _common.convert_to_dict(request_dict)
1801
+ request_dict = _common.apply_base64_encoding(request_dict)
1802
+
1803
+ response_dict = await self.api_client.async_request(
1804
+ 'get', path, request_dict, http_options
1805
+ )
1806
+
1807
+ if self.api_client.vertexai:
1808
+ response_dict = _ListCachedContentsResponse_from_vertex(
1809
+ self.api_client, response_dict
1810
+ )
1811
+ else:
1812
+ response_dict = _ListCachedContentsResponse_from_mldev(
1813
+ self.api_client, response_dict
1814
+ )
1815
+
1816
+ return_value = types.ListCachedContentsResponse._from_response(
1817
+ response_dict, parameter_model
1818
+ )
1819
+ self.api_client._verify_response(return_value)
1820
+ return return_value
1821
+
1822
+ async def list(
1823
+ self, *, config: Optional[types.ListCachedContentsConfigOrDict] = None
1824
+ ) -> AsyncPager[types.CachedContent]:
1825
+ return AsyncPager(
1826
+ 'cached_contents',
1827
+ self._list,
1828
+ await self._list(config=config),
1829
+ config,
1830
+ )