@@ -68,11 +68,11 @@ with text content
68
68
.. code :: python
69
69
70
70
response = client.models.generate_content(
71
- model = " gemini-2.0-flash-exp" , contents = " What is your name ?"
71
+ model = " gemini-2.0-flash-exp" , contents = " Why is the sky blue ?"
72
72
)
73
73
print (response.text)
74
74
75
- with uploaded file (Google AI only)
75
+ with uploaded file (Gemini API only)
76
76
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
77
77
78
78
download the file in console.
@@ -85,9 +85,10 @@ python code.
85
85
86
86
.. code :: python
87
87
88
- file = client.files.upload(path = ' a11.text ' )
88
+ file = client.files.upload(path = ' a11.txt ' )
89
89
response = client.models.generate_content(
90
- model = ' gemini-2.0-flash-exp' , contents = [' Summarize this file' , file ]
90
+ model = ' gemini-2.0-flash-exp' ,
91
+ contents = [' Could you summarize this file?' , file ]
91
92
)
92
93
print (response.text)
93
94
@@ -117,7 +118,7 @@ dictionaries. You can get the type from ``google.genai.types``.
117
118
118
119
response = client.models.generate_content(
119
120
model = " gemini-2.0-flash-exp" ,
120
- contents = types.Part.from_text(" Why is the sky blue?" ),
121
+ contents = types.Part.from_text(text = " Why is the sky blue?" ),
121
122
config = types.GenerateContentConfig(
122
123
temperature = 0 ,
123
124
top_p = 0.95 ,
@@ -131,7 +132,46 @@ dictionaries. You can get the type from ``google.genai.types``.
131
132
),
132
133
)
133
134
134
- response
135
+ print (response.text)
136
+
137
+ Thinking Model
138
+ ---------------
139
+
140
+ The Gemini 2.0 Flash Thinking model is an experimental model that could return
141
+ "thoughts" as part of its response.
142
+
143
+ Gemini Developer API
144
+ ~~~~~~~~~~~~~~~~~~~~
145
+
146
+ Thinking config is only available in v1alpha for Gemini AI API.
147
+
148
+ .. code :: python
149
+
150
+ response = client.models.generate_content(
151
+ model = ' gemini-2.0-flash-thinking-exp' ,
152
+ contents = ' What is the sum of natural numbers from 1 to 100?' ,
153
+ config = types.GenerateContentConfig(
154
+ thinking_config = types.ThinkingConfig(include_thoughts = True ),
155
+ http_options = types.HttpOptions(api_version = ' v1alpha' ),
156
+ )
157
+ )
158
+ for part in response.candidates[0 ].content.parts:
159
+ print (part)
160
+
161
+ Vertex AI API
162
+ ~~~~~~~~~~~~~~
163
+
164
+ .. code :: python
165
+
166
+ response = client.models.generate_content(
167
+ model = ' gemini-2.0-flash-thinking-exp-01-21' ,
168
+ contents = ' What is the sum of natural numbers from 1 to 100?' ,
169
+ config = types.GenerateContentConfig(
170
+ thinking_config = types.ThinkingConfig(include_thoughts = True ),
171
+ )
172
+ )
173
+ for part in response.candidates[0 ].content.parts:
174
+ print (part)
135
175
136
176
List Base Models
137
177
----------------
@@ -230,10 +270,10 @@ Then you will receive a function call part in the response.
230
270
function = types.FunctionDeclaration(
231
271
name = " get_current_weather" ,
232
272
description = " Get the current weather in a given location" ,
233
- parameters = types.FunctionParameters (
273
+ parameters = types.Schema (
234
274
type = " OBJECT" ,
235
275
properties = {
236
- " location" : types.ParameterType (
276
+ " location" : types.Schema (
237
277
type = " STRING" ,
238
278
description = " The city and state, e.g. San Francisco, CA" ,
239
279
),
@@ -262,10 +302,9 @@ The following example shows how to do it for a simple function invocation.
262
302
263
303
user_prompt_content = types.Content(
264
304
role = " user" ,
265
- parts = [types.Part.from_text(" What is the weather like in Boston?" )],
305
+ parts = [types.Part.from_text(text = " What is the weather like in Boston?" )],
266
306
)
267
- function_call_content = response.candidates[0 ].content
268
- function_call_part = function_call_content.parts[0 ]
307
+ function_call_part = response.function_calls[0 ]
269
308
270
309
271
310
try :
@@ -366,6 +405,60 @@ Schemas can be provided as Pydantic Models.
366
405
)
367
406
print (response.text)
368
407
408
+ Enum Response Schema
409
+ --------------------
410
+
411
+ Text Response
412
+ ~~~~~~~~~~~~~
413
+
414
+ You can set response_mime_type to 'text/x.enum' to return one of those enum
415
+ values as the response.
416
+
417
+ .. code :: python
418
+
419
+ from enum import Enum
420
+
421
+ class InstrumentEnum (Enum ):
422
+ PERCUSSION = ' Percussion'
423
+ STRING = ' String'
424
+ WOODWIND = ' Woodwind'
425
+ BRASS = ' Brass'
426
+ KEYBOARD = ' Keyboard'
427
+
428
+ response = client.models.generate_content(
429
+ model = ' gemini-2.0-flash-exp' ,
430
+ contents = ' What instrument plays multiple notes at once?' ,
431
+ config = {
432
+ ' response_mime_type' : ' text/x.enum' ,
433
+ ' response_schema' : InstrumentEnum,
434
+ },
435
+ )
436
+ print (response.text)
437
+
438
+ JSON Response
439
+ ~~~~~~~~~~~~~
440
+
441
+ You can also set response_mime_type to 'application/json', the response will be
442
+ identical but in quotes.
443
+
444
+ .. code :: python
445
+
446
+ class InstrumentEnum (Enum ):
447
+ PERCUSSION = ' Percussion'
448
+ STRING = ' String'
449
+ WOODWIND = ' Woodwind'
450
+ BRASS = ' Brass'
451
+ KEYBOARD = ' Keyboard'
452
+
453
+ response = client.models.generate_content(
454
+ model = ' gemini-2.0-flash-exp' ,
455
+ contents = ' What instrument plays multiple notes at once?' ,
456
+ config = {
457
+ ' response_mime_type' : ' application/json' ,
458
+ ' response_schema' : InstrumentEnum,
459
+ },
460
+ )
461
+ print (response.text)
369
462
370
463
Streaming
371
464
---------
@@ -439,10 +532,10 @@ Streaming
439
532
440
533
.. code :: python
441
534
442
- async for response in client.aio.models.generate_content_stream(
535
+ async for chunk in await client.aio.models.generate_content_stream(
443
536
model = " gemini-2.0-flash-exp" , contents = " Tell me a story in 300 words."
444
537
):
445
- print (response .text, end = " " )
538
+ print (chunk .text, end = " " )
446
539
447
540
Count Tokens and Compute Tokens
448
541
-------------------------------
@@ -451,7 +544,7 @@ Count Tokens and Compute Tokens
451
544
452
545
response = client.models.count_tokens(
453
546
model = " gemini-2.0-flash-exp" ,
454
- contents = " What is your name ?" ,
547
+ contents = " why is the sky blue ?" ,
455
548
)
456
549
print (response)
457
550
@@ -464,7 +557,7 @@ Compute tokens is only supported in Vertex AI.
464
557
465
558
response = client.models.compute_tokens(
466
559
model = " gemini-2.0-flash-exp" ,
467
- contents = " What is your name ?" ,
560
+ contents = " why is the sky blue ?" ,
468
561
)
469
562
print (response)
470
563
@@ -475,7 +568,7 @@ Async
475
568
476
569
response = await client.aio.models.count_tokens(
477
570
model = " gemini-2.0-flash-exp" ,
478
- contents = " What is your name ?" ,
571
+ contents = " why is the sky blue ?" ,
479
572
)
480
573
print (response)
481
574
@@ -486,7 +579,7 @@ Embed Content
486
579
487
580
response = client.models.embed_content(
488
581
model = " text-embedding-004" ,
489
- contents = " What is your name ?" ,
582
+ contents = " why is the sky blue ?" ,
490
583
)
491
584
print (response)
492
585
@@ -495,7 +588,7 @@ Embed Content
495
588
# multiple contents with config
496
589
response = client.models.embed_content(
497
590
model = " text-embedding-004" ,
498
- contents = [" What is your name ?" , " What is your age?" ],
591
+ contents = [" why is the sky blue ?" , " What is your age?" ],
499
592
config = types.EmbedContentConfig(output_dimensionality = 10 ),
500
593
)
501
594
@@ -512,10 +605,10 @@ Support for generate image in Gemini Developer API is behind an allowlist
512
605
.. code :: python
513
606
514
607
# Generate Image
515
- response1 = client.models.generate_image (
516
- model = " imagen-3.0-generate-001 " ,
608
+ response1 = client.models.generate_images (
609
+ model = " imagen-3.0-generate-002 " ,
517
610
prompt = " An umbrella in the foreground, and a rainy night sky in the background" ,
518
- config = types.GenerateImageConfig (
611
+ config = types.GenerateImagesConfig (
519
612
negative_prompt = " human" ,
520
613
number_of_images = 1 ,
521
614
include_rai_reason = True ,
@@ -533,7 +626,7 @@ Upscale image is only supported in Vertex AI.
533
626
534
627
# Upscale the generated image from above
535
628
response2 = client.models.upscale_image(
536
- model = " imagen-3.0-generate-001 " ,
629
+ model = " imagen-3.0-generate-002 " ,
537
630
image = response1.generated_images[0 ].image,
538
631
upscale_factor = " x2" ,
539
632
config = types.UpscaleImageConfig(
@@ -621,7 +714,7 @@ Async Streaming
621
714
.. code :: python
622
715
623
716
chat = client.aio.chats.create(model = " gemini-2.0-flash-exp" )
624
- async for chunk in chat.send_message_stream(" tell me a story" ):
717
+ async for chunk in await chat.send_message_stream(" tell me a story" ):
625
718
print (chunk.text, end = " " )
626
719
627
720
Files
@@ -720,7 +813,7 @@ Tunings
720
813
=======
721
814
722
815
``client.tunings `` contains tuning job APIs and supports supervised fine
723
- tuning through ``tune `` and distillation through `` distill ``
816
+ tuning through ``tune ``.
724
817
725
818
Tune
726
819
----
@@ -789,7 +882,7 @@ Use Tuned Model
789
882
790
883
response = client.models.generate_content(
791
884
model = tuning_job.tuned_model.endpoint,
792
- contents = " What is your name ?" ,
885
+ contents = " why is the sky blue ?" ,
793
886
)
794
887
795
888
print (response.text)
@@ -852,49 +945,6 @@ Update Tuned Model
852
945
853
946
print (model)
854
947
855
- Distillation
856
- ------------
857
-
858
- Only supported in Vertex AI. Requires allowlist.
859
-
860
- .. code :: python
861
-
862
- distillation_job = client.tunings.distill(
863
- student_model = " gemma-2b-1.1-it" ,
864
- teacher_model = " gemini-1.5-pro-002" ,
865
- training_dataset = genai.types.DistillationDataset(
866
- gcs_uri = " gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl" ,
867
- ),
868
- config = genai.types.CreateDistillationJobConfig(
869
- epoch_count = 1 ,
870
- pipeline_root_directory = (
871
- " gs://vertex-sdk-dev-staging-us-central1/tmp/distillation_pipeline_root"
872
- ),
873
- ),
874
- )
875
- print (distillation_job)
876
-
877
- .. code :: python
878
-
879
- completed_states = set (
880
- [
881
- " JOB_STATE_SUCCEEDED" ,
882
- " JOB_STATE_FAILED" ,
883
- " JOB_STATE_CANCELLED" ,
884
- " JOB_STATE_PAUSED" ,
885
- ]
886
- )
887
-
888
- while distillation_job.state not in completed_states:
889
- print (distillation_job.state)
890
- distillation_job = client.tunings.get(name = distillation_job.name)
891
- time.sleep(10 )
892
-
893
- print (distillation_job)
894
-
895
- .. code :: python
896
-
897
- distillation_job
898
948
899
949
List Tuning Jobs
900
950
----------------
@@ -976,12 +1026,12 @@ List
976
1026
977
1027
.. code :: python
978
1028
979
- for job in client.batches.list(config = types.ListBatchJobConfig (page_size = 10 )):
1029
+ for job in client.batches.list(config = types.ListBatchJobsConfig (page_size = 10 )):
980
1030
print (job)
981
1031
982
1032
.. code :: python
983
1033
984
- pager = client.batches.list(config = types.ListBatchJobConfig (page_size = 10 ))
1034
+ pager = client.batches.list(config = types.ListBatchJobsConfig (page_size = 10 ))
985
1035
print (pager.page_size)
986
1036
print (pager[0 ])
987
1037
pager.next_page()
@@ -993,14 +1043,14 @@ Async
993
1043
.. code :: python
994
1044
995
1045
async for job in await client.aio.batches.list(
996
- config = types.ListBatchJobConfig (page_size = 10 )
1046
+ config = types.ListBatchJobsConfig (page_size = 10 )
997
1047
):
998
1048
print (job)
999
1049
1000
1050
.. code :: python
1001
1051
1002
1052
async_pager = await client.aio.batches.list(
1003
- config = types.ListBatchJobConfig (page_size = 10 )
1053
+ config = types.ListBatchJobsConfig (page_size = 10 )
1004
1054
)
1005
1055
print (async_pager.page_size)
1006
1056
print (async_pager[0 ])
0 commit comments