google-genai 0.5.0__tar.gz → 0.6.0__tar.gz

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.
Files changed (33) hide show
  1. google_genai-0.6.0/PKG-INFO +973 -0
  2. google_genai-0.6.0/README.md +945 -0
  3. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/_api_client.py +164 -55
  4. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/_common.py +37 -6
  5. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/_extra_utils.py +3 -3
  6. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/_replay_api_client.py +44 -32
  7. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/_transformers.py +167 -38
  8. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/batches.py +10 -10
  9. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/caches.py +10 -10
  10. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/client.py +2 -1
  11. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/errors.py +1 -1
  12. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/files.py +239 -40
  13. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/live.py +5 -1
  14. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/models.py +102 -30
  15. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/tunings.py +8 -8
  16. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/types.py +546 -348
  17. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/version.py +1 -1
  18. google_genai-0.6.0/google_genai.egg-info/PKG-INFO +973 -0
  19. {google_genai-0.5.0 → google_genai-0.6.0}/pyproject.toml +1 -1
  20. google_genai-0.5.0/PKG-INFO +0 -888
  21. google_genai-0.5.0/README.md +0 -860
  22. google_genai-0.5.0/google_genai.egg-info/PKG-INFO +0 -888
  23. {google_genai-0.5.0 → google_genai-0.6.0}/LICENSE +0 -0
  24. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/__init__.py +0 -0
  25. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/_automatic_function_calling_util.py +0 -0
  26. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/_test_api_client.py +0 -0
  27. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/chats.py +0 -0
  28. {google_genai-0.5.0 → google_genai-0.6.0}/google/genai/pagers.py +0 -0
  29. {google_genai-0.5.0 → google_genai-0.6.0}/google_genai.egg-info/SOURCES.txt +0 -0
  30. {google_genai-0.5.0 → google_genai-0.6.0}/google_genai.egg-info/dependency_links.txt +0 -0
  31. {google_genai-0.5.0 → google_genai-0.6.0}/google_genai.egg-info/requires.txt +0 -0
  32. {google_genai-0.5.0 → google_genai-0.6.0}/google_genai.egg-info/top_level.txt +0 -0
  33. {google_genai-0.5.0 → google_genai-0.6.0}/setup.cfg +0 -0
@@ -0,0 +1,973 @@
1
+ Metadata-Version: 2.2
2
+ Name: google-genai
3
+ Version: 0.6.0
4
+ Summary: GenAI Python SDK
5
+ Author-email: Google LLC <googleapis-packages@google.com>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/googleapis/python-genai
8
+ Classifier: Intended Audience :: Developers
9
+ Classifier: License :: OSI Approved :: Apache Software License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Internet
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Requires-Python: >=3.9
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: google-auth<3.0.0dev,>=2.14.1
24
+ Requires-Dist: pillow<12.0.0,>=10.0.0
25
+ Requires-Dist: pydantic<3.0.0dev,>=2.0.0
26
+ Requires-Dist: requests<3.0.0dev,>=2.28.1
27
+ Requires-Dist: websockets<15.0dev,>=13.0
28
+
29
+ # Google Gen AI SDK
30
+
31
+ [![PyPI version](https://img.shields.io/pypi/v/google-genai.svg)](https://pypi.org/project/google-genai/)
32
+
33
+ --------
34
+ **Documentation:** https://googleapis.github.io/python-genai/
35
+
36
+ -----
37
+
38
+ Google Gen AI Python SDK provides an interface for developers to integrate Google's generative models into their Python applications. It supports the [Gemini Developer API](https://ai.google.dev/gemini-api/docs) and [Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview) APIs. This is an early release. API is subject to change. Please do not use this SDK in production environments at this stage.
39
+
40
+ ## Installation
41
+
42
+ ```cmd
43
+ pip install google-genai
44
+ ```
45
+
46
+ ## Imports
47
+
48
+ ```python
49
+ from google import genai
50
+ from google.genai import types
51
+ ```
52
+
53
+ ## Create a client
54
+
55
+ Please run one of the following code blocks to create a client for
56
+ different services ([Gemini Developer API](https://ai.google.dev/gemini-api/docs) or [Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview)).
57
+
58
+ ```python
59
+ # Only run this block for Gemini Developer API
60
+ client = genai.Client(api_key="GEMINI_API_KEY")
61
+ ```
62
+
63
+ ```python
64
+ # Only run this block for Vertex AI API
65
+ client = genai.Client(
66
+ vertexai=True, project="your-project-id", location="us-central1"
67
+ )
68
+ ```
69
+
70
+ ## Types
71
+
72
+ Parameter types can be specified as either dictionaries(`TypedDict`) or
73
+ [Pydantic Models](https://pydantic.readthedocs.io/en/stable/model.html).
74
+ Pydantic model types are available in the `types` module.
75
+
76
+ ## Models
77
+
78
+ The `client.models` modules exposes model inferencing and model getters.
79
+
80
+ ### Generate Content
81
+
82
+ #### with text content
83
+
84
+ ```python
85
+ response = client.models.generate_content(
86
+ model="gemini-2.0-flash-exp", contents="What is your name?"
87
+ )
88
+ print(response.text)
89
+ ```
90
+
91
+ #### with uploaded file (Google AI only)
92
+ download the file in console.
93
+
94
+ ```cmd
95
+ !wget -q https://storage.googleapis.com/generativeai-downloads/data/a11.txt
96
+ ```
97
+
98
+ python code.
99
+
100
+ ```python
101
+ file = client.files.upload(path="a11.text")
102
+ response = client.models.generate_content(
103
+ model="gemini-2.0-flash-exp", contents=["Summarize this file", file]
104
+ )
105
+ print(response.text)
106
+ ```
107
+
108
+ ### System Instructions and Other Configs
109
+
110
+ ```python
111
+ response = client.models.generate_content(
112
+ model="gemini-2.0-flash-exp",
113
+ contents="high",
114
+ config=types.GenerateContentConfig(
115
+ system_instruction="I say high, you say low",
116
+ temperature=0.3,
117
+ ),
118
+ )
119
+ print(response.text)
120
+ ```
121
+
122
+ ### Typed Config
123
+
124
+ All API methods support Pydantic types for parameters as well as
125
+ dictionaries. You can get the type from `google.genai.types`.
126
+
127
+ ```python
128
+ response = client.models.generate_content(
129
+ model="gemini-2.0-flash-exp",
130
+ contents=types.Part.from_text("Why is the sky blue?"),
131
+ config=types.GenerateContentConfig(
132
+ temperature=0,
133
+ top_p=0.95,
134
+ top_k=20,
135
+ candidate_count=1,
136
+ seed=5,
137
+ max_output_tokens=100,
138
+ stop_sequences=["STOP!"],
139
+ presence_penalty=0.0,
140
+ frequency_penalty=0.0,
141
+ ),
142
+ )
143
+
144
+ response
145
+ ```
146
+
147
+ ### List Base Models
148
+
149
+ To retrieve tuned models, see [list tuned models](#list-tuned-models).
150
+
151
+ ```python
152
+ for model in client.models.list(config={'query_base':True}):
153
+ print(model)
154
+ ```
155
+
156
+ ```python
157
+ pager = client.models.list(config={"page_size": 10, 'query_base':True})
158
+ print(pager.page_size)
159
+ print(pager[0])
160
+ pager.next_page()
161
+ print(pager[0])
162
+ ```
163
+
164
+ #### Async
165
+
166
+ ```python
167
+ async for job in await client.aio.models.list(config={'query_base':True}):
168
+ print(job)
169
+ ```
170
+
171
+ ```python
172
+ async_pager = await client.aio.models.list(config={"page_size": 10, 'query_base':True})
173
+ print(async_pager.page_size)
174
+ print(async_pager[0])
175
+ await async_pager.next_page()
176
+ print(async_pager[0])
177
+ ```
178
+
179
+ ### Safety Settings
180
+
181
+ ```python
182
+ response = client.models.generate_content(
183
+ model="gemini-2.0-flash-exp",
184
+ contents="Say something bad.",
185
+ config=types.GenerateContentConfig(
186
+ safety_settings=[
187
+ types.SafetySetting(
188
+ category="HARM_CATEGORY_HATE_SPEECH",
189
+ threshold="BLOCK_ONLY_HIGH",
190
+ )
191
+ ]
192
+ ),
193
+ )
194
+ print(response.text)
195
+ ```
196
+
197
+ ### Function Calling
198
+
199
+ #### Automatic Python function Support
200
+
201
+ You can pass a Python function directly and it will be automatically
202
+ called and responded.
203
+
204
+ ```python
205
+ def get_current_weather(location: str) -> str:
206
+ """Returns the current weather.
207
+
208
+ Args:
209
+ location: The city and state, e.g. San Francisco, CA
210
+ """
211
+ return "sunny"
212
+
213
+
214
+ response = client.models.generate_content(
215
+ model="gemini-2.0-flash-exp",
216
+ contents="What is the weather like in Boston?",
217
+ config=types.GenerateContentConfig(tools=[get_current_weather]),
218
+ )
219
+
220
+ print(response.text)
221
+ ```
222
+
223
+ #### Manually declare and invoke a function for function calling
224
+
225
+ If you don't want to use the automatic function support, you can manually
226
+ declare the function and invoke it.
227
+
228
+ The following example shows how to declare a function and pass it as a tool.
229
+ Then you will receive a function call part in the response.
230
+
231
+ ```python
232
+ function = types.FunctionDeclaration(
233
+ name="get_current_weather",
234
+ description="Get the current weather in a given location",
235
+ parameters=types.FunctionParameters(
236
+ type="OBJECT",
237
+ properties={
238
+ "location": types.ParameterType(
239
+ type="STRING",
240
+ description="The city and state, e.g. San Francisco, CA",
241
+ ),
242
+ },
243
+ required=["location"],
244
+ ),
245
+ )
246
+
247
+ tool = types.Tool(function_declarations=[function])
248
+
249
+ response = client.models.generate_content(
250
+ model="gemini-2.0-flash-exp",
251
+ contents="What is the weather like in Boston?",
252
+ config=types.GenerateContentConfig(tools=[tool]),
253
+ )
254
+
255
+ print(response.function_calls[0])
256
+ ```
257
+
258
+ After you receive the function call part from the model, you can invoke the function
259
+ and get the function response. And then you can pass the function response to
260
+ the model.
261
+ The following example shows how to do it for a simple function invocation.
262
+
263
+ ```python
264
+ user_prompt_content = types.Content(
265
+ role="user",
266
+ parts=[types.Part.from_text("What is the weather like in Boston?")],
267
+ )
268
+ function_call_content = response.candidates[0].content
269
+ function_call_part = function_call_content.parts[0]
270
+
271
+
272
+ try:
273
+ function_result = get_current_weather(
274
+ **function_call_part.function_call.args
275
+ )
276
+ function_response = {"result": function_result}
277
+ except (
278
+ Exception
279
+ ) as e: # instead of raising the exception, you can let the model handle it
280
+ function_response = {"error": str(e)}
281
+
282
+
283
+ function_response_part = types.Part.from_function_response(
284
+ name=function_call_part.function_call.name,
285
+ response=function_response,
286
+ )
287
+ function_response_content = types.Content(
288
+ role="tool", parts=[function_response_part]
289
+ )
290
+
291
+ response = client.models.generate_content(
292
+ model="gemini-2.0-flash-exp",
293
+ contents=[
294
+ user_prompt_content,
295
+ function_call_content,
296
+ function_response_content,
297
+ ],
298
+ config=types.GenerateContentConfig(
299
+ tools=[tool],
300
+ ),
301
+ )
302
+
303
+ print(response.text)
304
+ ```
305
+
306
+ ### JSON Response Schema
307
+
308
+ #### Pydantic Model Schema support
309
+
310
+ Schemas can be provided as Pydantic Models.
311
+
312
+ ```python
313
+ from pydantic import BaseModel
314
+
315
+
316
+ class CountryInfo(BaseModel):
317
+ name: str
318
+ population: int
319
+ capital: str
320
+ continent: str
321
+ gdp: int
322
+ official_language: str
323
+ total_area_sq_mi: int
324
+
325
+
326
+ response = client.models.generate_content(
327
+ model="gemini-2.0-flash-exp",
328
+ contents="Give me information for the United States.",
329
+ config=types.GenerateContentConfig(
330
+ response_mime_type="application/json",
331
+ response_schema=CountryInfo,
332
+ ),
333
+ )
334
+ print(response.text)
335
+ ```
336
+
337
+ ```python
338
+ response = client.models.generate_content(
339
+ model="gemini-2.0-flash-exp",
340
+ contents="Give me information for the United States.",
341
+ config=types.GenerateContentConfig(
342
+ response_mime_type="application/json",
343
+ response_schema={
344
+ "required": [
345
+ "name",
346
+ "population",
347
+ "capital",
348
+ "continent",
349
+ "gdp",
350
+ "official_language",
351
+ "total_area_sq_mi",
352
+ ],
353
+ "properties": {
354
+ "name": {"type": "STRING"},
355
+ "population": {"type": "INTEGER"},
356
+ "capital": {"type": "STRING"},
357
+ "continent": {"type": "STRING"},
358
+ "gdp": {"type": "INTEGER"},
359
+ "official_language": {"type": "STRING"},
360
+ "total_area_sq_mi": {"type": "INTEGER"},
361
+ },
362
+ "type": "OBJECT",
363
+ },
364
+ ),
365
+ )
366
+ print(response.text)
367
+ ```
368
+
369
+ ### Streaming
370
+
371
+ #### Streaming for text content
372
+
373
+ ```python
374
+ for chunk in client.models.generate_content_stream(
375
+ model="gemini-2.0-flash-exp", contents="Tell me a story in 300 words."
376
+ ):
377
+ print(chunk.text, end="")
378
+ ```
379
+
380
+ #### Streaming for image content
381
+
382
+ If your image is stored in [Google Cloud Storage](https://cloud.google.com/storage),
383
+ you can use the `from_uri` class method to create a `Part` object.
384
+
385
+ ```python
386
+ for chunk in client.models.generate_content_stream(
387
+ model="gemini-2.0-flash-exp",
388
+ contents=[
389
+ "What is this image about?",
390
+ types.Part.from_uri(
391
+ file_uri="gs://generativeai-downloads/images/scones.jpg",
392
+ mime_type="image/jpeg",
393
+ ),
394
+ ],
395
+ ):
396
+ print(chunk.text, end="")
397
+ ```
398
+
399
+ If your image is stored in your local file system, you can read it in as bytes
400
+ data and use the `from_bytes` class method to create a `Part` object.
401
+
402
+ ```python
403
+ YOUR_IMAGE_PATH = "your_image_path"
404
+ YOUR_IMAGE_MIME_TYPE = "your_image_mime_type"
405
+ with open(YOUR_IMAGE_PATH, "rb") as f:
406
+ image_bytes = f.read()
407
+
408
+ for chunk in client.models.generate_content_stream(
409
+ model="gemini-2.0-flash-exp",
410
+ contents=[
411
+ "What is this image about?",
412
+ types.Part.from_bytes(data=image_bytes, mime_type=YOUR_IMAGE_MIME_TYPE),
413
+ ],
414
+ ):
415
+ print(chunk.text, end="")
416
+ ```
417
+
418
+ ### Async
419
+
420
+ `client.aio` exposes all the analogous [`async` methods](https://docs.python.org/3/library/asyncio.html)
421
+ that are available on `client`
422
+
423
+ For example, `client.aio.models.generate_content` is the `async` version
424
+ of `client.models.generate_content`
425
+
426
+ ```python
427
+ response = await client.aio.models.generate_content(
428
+ model="gemini-2.0-flash-exp", contents="Tell me a story in 300 words."
429
+ )
430
+
431
+ print(response.text)
432
+ ```
433
+
434
+ ### Streaming
435
+
436
+ ```python
437
+ async for response in client.aio.models.generate_content_stream(
438
+ model="gemini-2.0-flash-exp", contents="Tell me a story in 300 words."
439
+ ):
440
+ print(response.text, end="")
441
+ ```
442
+
443
+ ### Count Tokens and Compute Tokens
444
+
445
+ ```python
446
+ response = client.models.count_tokens(
447
+ model="gemini-2.0-flash-exp",
448
+ contents="What is your name?",
449
+ )
450
+ print(response)
451
+ ```
452
+
453
+ #### Compute Tokens
454
+
455
+ Compute tokens is only supported in Vertex AI.
456
+
457
+ ```python
458
+ response = client.models.compute_tokens(
459
+ model="gemini-2.0-flash-exp",
460
+ contents="What is your name?",
461
+ )
462
+ print(response)
463
+ ```
464
+
465
+ ##### Async
466
+
467
+ ```python
468
+ response = await client.aio.models.count_tokens(
469
+ model="gemini-2.0-flash-exp",
470
+ contents="What is your name?",
471
+ )
472
+ print(response)
473
+ ```
474
+
475
+ ### Embed Content
476
+
477
+ ```python
478
+ response = client.models.embed_content(
479
+ model="text-embedding-004",
480
+ contents="What is your name?",
481
+ )
482
+ print(response)
483
+ ```
484
+
485
+ ```python
486
+ # multiple contents with config
487
+ response = client.models.embed_content(
488
+ model="text-embedding-004",
489
+ contents=["What is your name?", "What is your age?"],
490
+ config=types.EmbedContentConfig(output_dimensionality=10),
491
+ )
492
+
493
+ print(response)
494
+ ```
495
+
496
+ ### Imagen
497
+
498
+ #### Generate Image
499
+
500
+ Support for generate image in Gemini Developer API is behind an allowlist
501
+
502
+ ```python
503
+ # Generate Image
504
+ response1 = client.models.generate_image(
505
+ model="imagen-3.0-generate-001",
506
+ prompt="An umbrella in the foreground, and a rainy night sky in the background",
507
+ config=types.GenerateImageConfig(
508
+ negative_prompt="human",
509
+ number_of_images=1,
510
+ include_rai_reason=True,
511
+ output_mime_type="image/jpeg",
512
+ ),
513
+ )
514
+ response1.generated_images[0].image.show()
515
+ ```
516
+
517
+ #### Upscale Image
518
+
519
+ Upscale image is only supported in Vertex AI.
520
+
521
+ ```python
522
+ # Upscale the generated image from above
523
+ response2 = client.models.upscale_image(
524
+ model="imagen-3.0-generate-001",
525
+ image=response1.generated_images[0].image,
526
+ upscale_factor="x2",
527
+ config=types.UpscaleImageConfig(
528
+ include_rai_reason=True,
529
+ output_mime_type="image/jpeg",
530
+ ),
531
+ )
532
+ response2.generated_images[0].image.show()
533
+ ```
534
+
535
+ #### Edit Image
536
+
537
+ Edit image uses a separate model from generate and upscale.
538
+
539
+ Edit image is only supported in Vertex AI.
540
+
541
+ ```python
542
+ # Edit the generated image from above
543
+ from google.genai.types import RawReferenceImage, MaskReferenceImage
544
+
545
+ raw_ref_image = RawReferenceImage(
546
+ reference_id=1,
547
+ reference_image=response1.generated_images[0].image,
548
+ )
549
+
550
+ # Model computes a mask of the background
551
+ mask_ref_image = MaskReferenceImage(
552
+ reference_id=2,
553
+ config=types.MaskReferenceConfig(
554
+ mask_mode="MASK_MODE_BACKGROUND",
555
+ mask_dilation=0,
556
+ ),
557
+ )
558
+
559
+ response3 = client.models.edit_image(
560
+ model="imagen-3.0-capability-001",
561
+ prompt="Sunlight and clear sky",
562
+ reference_images=[raw_ref_image, mask_ref_image],
563
+ config=types.EditImageConfig(
564
+ edit_mode="EDIT_MODE_INPAINT_INSERTION",
565
+ number_of_images=1,
566
+ negative_prompt="human",
567
+ include_rai_reason=True,
568
+ output_mime_type="image/jpeg",
569
+ ),
570
+ )
571
+ response3.generated_images[0].image.show()
572
+ ```
573
+
574
+ ## Chats
575
+
576
+ Create a chat session to start a multi-turn conversations with the model.
577
+
578
+ ### Send Message
579
+
580
+ ```python
581
+ chat = client.chats.create(model="gemini-2.0-flash-exp")
582
+ response = chat.send_message("tell me a story")
583
+ print(response.text)
584
+ ```
585
+
586
+ ### Streaming
587
+
588
+ ```python
589
+ chat = client.chats.create(model="gemini-2.0-flash-exp")
590
+ for chunk in chat.send_message_stream("tell me a story"):
591
+ print(chunk.text)
592
+ ```
593
+
594
+ ### Async
595
+
596
+ ```python
597
+ chat = client.aio.chats.create(model="gemini-2.0-flash-exp")
598
+ response = await chat.send_message("tell me a story")
599
+ print(response.text)
600
+ ```
601
+
602
+ ### Async Streaming
603
+
604
+ ```python
605
+ chat = client.aio.chats.create(model="gemini-2.0-flash-exp")
606
+ async for chunk in chat.send_message_stream("tell me a story"):
607
+ print(chunk.text)
608
+ ```
609
+
610
+ ## Files
611
+
612
+ Files are only supported in Gemini Developer API.
613
+
614
+ ```cmd
615
+ !gsutil cp gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf .
616
+ !gsutil cp gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf .
617
+ ```
618
+
619
+ ### Upload
620
+
621
+ ```python
622
+ file1 = client.files.upload(path="2312.11805v3.pdf")
623
+ file2 = client.files.upload(path="2403.05530.pdf")
624
+
625
+ print(file1)
626
+ print(file2)
627
+ ```
628
+
629
+ ### Delete
630
+
631
+ ```python
632
+ file3 = client.files.upload(path="2312.11805v3.pdf")
633
+
634
+ client.files.delete(name=file3.name)
635
+ ```
636
+
637
+ ## Caches
638
+
639
+ `client.caches` contains the control plane APIs for cached content
640
+
641
+ ### Create
642
+
643
+ ```python
644
+ if client.vertexai:
645
+ file_uris = [
646
+ "gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf",
647
+ "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf",
648
+ ]
649
+ else:
650
+ file_uris = [file1.uri, file2.uri]
651
+
652
+ cached_content = client.caches.create(
653
+ model="gemini-1.5-pro-002",
654
+ config=types.CreateCachedContentConfig(
655
+ contents=[
656
+ types.Content(
657
+ role="user",
658
+ parts=[
659
+ types.Part.from_uri(
660
+ file_uri=file_uris[0], mime_type="application/pdf"
661
+ ),
662
+ types.Part.from_uri(
663
+ file_uri=file_uris[1],
664
+ mime_type="application/pdf",
665
+ ),
666
+ ],
667
+ )
668
+ ],
669
+ system_instruction="What is the sum of the two pdfs?",
670
+ display_name="test cache",
671
+ ttl="3600s",
672
+ ),
673
+ )
674
+ ```
675
+
676
+ ### Get
677
+
678
+ ```python
679
+ cached_content = client.caches.get(name=cached_content.name)
680
+ ```
681
+
682
+ ### Generate Content
683
+
684
+ ```python
685
+ response = client.models.generate_content(
686
+ model="gemini-1.5-pro-002",
687
+ contents="Summarize the pdfs",
688
+ config=types.GenerateContentConfig(
689
+ cached_content=cached_content.name,
690
+ ),
691
+ )
692
+ print(response.text)
693
+ ```
694
+
695
+ ## Tunings
696
+
697
+ `client.tunings` contains tuning job APIs and supports supervised fine
698
+ tuning through `tune` and distillation through `distill`
699
+
700
+ ### Tune
701
+
702
+ - Vertex AI supports tuning from GCS source
703
+ - Gemini Developer API supports tuning from inline examples
704
+
705
+ ```python
706
+ if client.vertexai:
707
+ model = "gemini-1.5-pro-002"
708
+ training_dataset = types.TuningDataset(
709
+ gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
710
+ )
711
+ else:
712
+ model = "models/gemini-1.0-pro-001"
713
+ training_dataset = types.TuningDataset(
714
+ examples=[
715
+ types.TuningExample(
716
+ text_input=f"Input text {i}",
717
+ output=f"Output text {i}",
718
+ )
719
+ for i in range(5)
720
+ ],
721
+ )
722
+ ```
723
+
724
+ ```python
725
+ tuning_job = client.tunings.tune(
726
+ base_model=model,
727
+ training_dataset=training_dataset,
728
+ config=types.CreateTuningJobConfig(
729
+ epoch_count=1, tuned_model_display_name="test_dataset_examples model"
730
+ ),
731
+ )
732
+ print(tuning_job)
733
+ ```
734
+
735
+ ### Get Tuning Job
736
+
737
+ ```python
738
+ tuning_job = client.tunings.get(name=tuning_job.name)
739
+ print(tuning_job)
740
+ ```
741
+
742
+ ```python
743
+ import time
744
+
745
+ running_states = set(
746
+ [
747
+ "JOB_STATE_PENDING",
748
+ "JOB_STATE_RUNNING",
749
+ ]
750
+ )
751
+
752
+ while tuning_job.state in running_states:
753
+ print(tuning_job.state)
754
+ tuning_job = client.tunings.get(name=tuning_job.name)
755
+ time.sleep(10)
756
+ ```
757
+
758
+ #### Use Tuned Model
759
+
760
+ ```python
761
+ response = client.models.generate_content(
762
+ model=tuning_job.tuned_model.endpoint,
763
+ contents="What is your name?",
764
+ )
765
+
766
+ print(response.text)
767
+ ```
768
+
769
+ ### Get Tuned Model
770
+
771
+ ```python
772
+ tuned_model = client.models.get(model=tuning_job.tuned_model.model)
773
+ print(tuned_model)
774
+ ```
775
+
776
+ ### List Tuned Models
777
+
778
+ To retrieve base models, see [list base models](#list-base-models).
779
+
780
+ ```python
781
+ for model in client.models.list(config={"page_size": 10}):
782
+ print(model)
783
+ ```
784
+
785
+ ```python
786
+ pager = client.models.list(config={"page_size": 10})
787
+ print(pager.page_size)
788
+ print(pager[0])
789
+ pager.next_page()
790
+ print(pager[0])
791
+ ```
792
+
793
+ #### Async
794
+
795
+ ```python
796
+ async for job in await client.aio.models.list(config={"page_size": 10}):
797
+ print(job)
798
+ ```
799
+
800
+ ```python
801
+ async_pager = await client.aio.models.list(config={"page_size": 10})
802
+ print(async_pager.page_size)
803
+ print(async_pager[0])
804
+ await async_pager.next_page()
805
+ print(async_pager[0])
806
+ ```
807
+
808
+ ### Update Tuned Model
809
+
810
+ ```python
811
+ model = pager[0]
812
+
813
+ model = client.models.update(
814
+ model=model.name,
815
+ config=types.UpdateModelConfig(
816
+ display_name="my tuned model", description="my tuned model description"
817
+ ),
818
+ )
819
+
820
+ print(model)
821
+ ```
822
+
823
+ ### Distillation
824
+
825
+ Only supported in Vertex AI. Requires allowlist.
826
+
827
+ ```python
828
+ distillation_job = client.tunings.distill(
829
+ student_model="gemma-2b-1.1-it",
830
+ teacher_model="gemini-1.5-pro-002",
831
+ training_dataset=genai.types.DistillationDataset(
832
+ gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
833
+ ),
834
+ config=genai.types.CreateDistillationJobConfig(
835
+ epoch_count=1,
836
+ pipeline_root_directory=("gs://my-bucket"),
837
+ ),
838
+ )
839
+ print(distillation_job)
840
+ ```
841
+
842
+ ```python
843
+ completed_states = set(
844
+ [
845
+ "JOB_STATE_SUCCEEDED",
846
+ "JOB_STATE_FAILED",
847
+ "JOB_STATE_CANCELLED",
848
+ "JOB_STATE_PAUSED",
849
+ ]
850
+ )
851
+
852
+ while distillation_job.state not in completed_states:
853
+ print(distillation_job.state)
854
+ distillation_job = client.tunings.get(name=distillation_job.name)
855
+ time.sleep(10)
856
+
857
+ print(distillation_job)
858
+ ```
859
+
860
+
861
+ ### List Tuning Jobs
862
+
863
+ ```python
864
+ for job in client.tunings.list(config={"page_size": 10}):
865
+ print(job)
866
+ ```
867
+
868
+ ```python
869
+ pager = client.tunings.list(config={"page_size": 10})
870
+ print(pager.page_size)
871
+ print(pager[0])
872
+ pager.next_page()
873
+ print(pager[0])
874
+ ```
875
+
876
+ #### Async
877
+
878
+ ```python
879
+ async for job in await client.aio.tunings.list(config={"page_size": 10}):
880
+ print(job)
881
+ ```
882
+
883
+ ```python
884
+ async_pager = await client.aio.tunings.list(config={"page_size": 10})
885
+ print(async_pager.page_size)
886
+ print(async_pager[0])
887
+ await async_pager.next_page()
888
+ print(async_pager[0])
889
+ ```
890
+
891
+ ## Batch Prediction
892
+
893
+ Only supported in Vertex AI.
894
+
895
+ ### Create
896
+
897
+ ```python
898
+ # Specify model and source file only, destination and job display name will be auto-populated
899
+ job = client.batches.create(
900
+ model="gemini-1.5-flash-002",
901
+ src="bq://my-project.my-dataset.my-table",
902
+ )
903
+
904
+ job
905
+ ```
906
+
907
+ ```python
908
+ # Get a job by name
909
+ job = client.batches.get(name=job.name)
910
+
911
+ job.state
912
+ ```
913
+
914
+ ```python
915
+ completed_states = set(
916
+ [
917
+ "JOB_STATE_SUCCEEDED",
918
+ "JOB_STATE_FAILED",
919
+ "JOB_STATE_CANCELLED",
920
+ "JOB_STATE_PAUSED",
921
+ ]
922
+ )
923
+
924
+ while job.state not in completed_states:
925
+ print(job.state)
926
+ job = client.batches.get(name=job.name)
927
+ time.sleep(30)
928
+
929
+ job
930
+ ```
931
+
932
+ ### List
933
+
934
+ ```python
935
+ for job in client.batches.list(config=types.ListBatchJobConfig(page_size=10)):
936
+ print(job)
937
+ ```
938
+
939
+ ```python
940
+ pager = client.batches.list(config=types.ListBatchJobConfig(page_size=10))
941
+ print(pager.page_size)
942
+ print(pager[0])
943
+ pager.next_page()
944
+ print(pager[0])
945
+ ```
946
+
947
+ #### Async
948
+
949
+ ```python
950
+ async for job in await client.aio.batches.list(
951
+ config=types.ListBatchJobConfig(page_size=10)
952
+ ):
953
+ print(job)
954
+ ```
955
+
956
+ ```python
957
+ async_pager = await client.aio.batches.list(
958
+ config=types.ListBatchJobConfig(page_size=10)
959
+ )
960
+ print(async_pager.page_size)
961
+ print(async_pager[0])
962
+ await async_pager.next_page()
963
+ print(async_pager[0])
964
+ ```
965
+
966
+ ### Delete
967
+
968
+ ```python
969
+ # Delete the job resource
970
+ delete_job = client.batches.delete(name=job.name)
971
+
972
+ delete_job
973
+ ```