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/__init__.py +20 -0
- google/genai/_api_client.py +467 -0
- google/genai/_automatic_function_calling_util.py +341 -0
- google/genai/_common.py +256 -0
- google/genai/_extra_utils.py +295 -0
- google/genai/_replay_api_client.py +478 -0
- google/genai/_test_api_client.py +149 -0
- google/genai/_transformers.py +438 -0
- google/genai/batches.py +1041 -0
- google/genai/caches.py +1830 -0
- google/genai/chats.py +184 -0
- google/genai/client.py +277 -0
- google/genai/errors.py +110 -0
- google/genai/files.py +1211 -0
- google/genai/live.py +629 -0
- google/genai/models.py +5307 -0
- google/genai/pagers.py +245 -0
- google/genai/tunings.py +1366 -0
- google/genai/types.py +7639 -0
- google_genai-0.0.1.dist-info/LICENSE +202 -0
- google_genai-0.0.1.dist-info/METADATA +763 -0
- google_genai-0.0.1.dist-info/RECORD +24 -0
- google_genai-0.0.1.dist-info/WHEEL +5 -0
- google_genai-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,763 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: google-genai
|
3
|
+
Version: 0.0.1
|
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/googlapis/genai-python
|
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: Topic :: Internet
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
19
|
+
Requires-Python: >=3.9
|
20
|
+
Description-Content-Type: text/markdown
|
21
|
+
License-File: LICENSE
|
22
|
+
Requires-Dist: google-auth<3.0.0dev,>=2.14.1
|
23
|
+
Requires-Dist: pillow<12.0.0,>=10.0.0
|
24
|
+
Requires-Dist: pydantic<3.0.0dev,>=2.0.0
|
25
|
+
Requires-Dist: requests<3.0.0dev,>=2.28.1
|
26
|
+
Requires-Dist: websockets<15.0dev,>=13.0
|
27
|
+
|
28
|
+
# Google Gen AI SDK
|
29
|
+
|
30
|
+
## Imports
|
31
|
+
|
32
|
+
``` python
|
33
|
+
from google import genai
|
34
|
+
from google.genai import types
|
35
|
+
```
|
36
|
+
|
37
|
+
## Create a client
|
38
|
+
|
39
|
+
Please run one of the following code blocks to create a client for
|
40
|
+
different services (Google AI or Vertex). Feel free to switch the client
|
41
|
+
and run all the examples to see how it behaves under different APIs.
|
42
|
+
|
43
|
+
``` python
|
44
|
+
# Only run this block for Google AI API
|
45
|
+
client = genai.Client(api_key='YOUR_API_KEY')
|
46
|
+
```
|
47
|
+
|
48
|
+
``` python
|
49
|
+
# Only run this block for Vertex AI API
|
50
|
+
client = genai.Client(
|
51
|
+
vertexai=True, project='your-project-id', location='us-central1'
|
52
|
+
)
|
53
|
+
```
|
54
|
+
|
55
|
+
## Types
|
56
|
+
|
57
|
+
Parameter types can be specified as either dictionaries(TypedDict) or
|
58
|
+
pydantic Models. Pydantic model types are available in the `types`
|
59
|
+
module.
|
60
|
+
|
61
|
+
## Models
|
62
|
+
|
63
|
+
The `client.models` modules exposes model inferencing and model getters.
|
64
|
+
|
65
|
+
### Generate Content
|
66
|
+
|
67
|
+
``` python
|
68
|
+
response = client.models.generate_content(
|
69
|
+
model='gemini-1.5-pro-002', contents='What is your name?'
|
70
|
+
)
|
71
|
+
print(response.text)
|
72
|
+
```
|
73
|
+
|
74
|
+
### System Instructions and Other Configs
|
75
|
+
|
76
|
+
``` python
|
77
|
+
response = client.models.generate_content(
|
78
|
+
model='gemini-1.5-pro-002',
|
79
|
+
contents='high',
|
80
|
+
config=types.GenerateContentConfig(
|
81
|
+
system_instruction='I say high, you say low',
|
82
|
+
temperature= 0.3,
|
83
|
+
),
|
84
|
+
)
|
85
|
+
print(response.text)
|
86
|
+
```
|
87
|
+
|
88
|
+
### Typed Config
|
89
|
+
|
90
|
+
All API methods support pydantic types for parameters as well as
|
91
|
+
dictionaries. You can get the type from `google.genai.types`.
|
92
|
+
|
93
|
+
``` python
|
94
|
+
response = client.models.generate_content(
|
95
|
+
model='gemini-1.5-flash-002',
|
96
|
+
contents=types.Part.from_text('Why is sky blue?'),
|
97
|
+
config=types.GenerateContentConfig(
|
98
|
+
temperature=0,
|
99
|
+
top_p=0.95,
|
100
|
+
top_k=20,
|
101
|
+
candidate_count=1,
|
102
|
+
seed=5,
|
103
|
+
max_output_tokens=100,
|
104
|
+
stop_sequences=["STOP!"],
|
105
|
+
presence_penalty=0.0,
|
106
|
+
frequency_penalty=0.0,
|
107
|
+
)
|
108
|
+
)
|
109
|
+
|
110
|
+
response
|
111
|
+
```
|
112
|
+
|
113
|
+
### Safety Settings
|
114
|
+
|
115
|
+
``` python
|
116
|
+
response = client.models.generate_content(
|
117
|
+
model='gemini-1.5-flash',
|
118
|
+
contents='Say something bad.',
|
119
|
+
config=types.GenerateContentConfig(
|
120
|
+
safety_settings= [types.SafetySetting(
|
121
|
+
category='HARM_CATEGORY_HATE_SPEECH',
|
122
|
+
threshold='BLOCK_ONLY_HIGH',
|
123
|
+
)]
|
124
|
+
),
|
125
|
+
)
|
126
|
+
print(response.text)
|
127
|
+
```
|
128
|
+
|
129
|
+
### Function Calling
|
130
|
+
|
131
|
+
#### Automatic Python function Support
|
132
|
+
|
133
|
+
You can pass a python function directly and it will be automatically
|
134
|
+
called and responded.
|
135
|
+
|
136
|
+
``` python
|
137
|
+
def get_current_weather(location: str,) -> int:
|
138
|
+
"""Returns the current weather.
|
139
|
+
|
140
|
+
Args:
|
141
|
+
location: The city and state, e.g. San Francisco, CA
|
142
|
+
"""
|
143
|
+
return 'sunny'
|
144
|
+
|
145
|
+
response = client.models.generate_content(
|
146
|
+
model='gemini-1.5-pro-002',
|
147
|
+
contents="What is the weather like in Boston?",
|
148
|
+
config=types.GenerateContentConfig(tools=[get_current_weather],)
|
149
|
+
)
|
150
|
+
|
151
|
+
response.text
|
152
|
+
```
|
153
|
+
|
154
|
+
``` python
|
155
|
+
function = dict(
|
156
|
+
name="get_current_weather",
|
157
|
+
description="Get the current weather in a given location",
|
158
|
+
parameters={
|
159
|
+
"type": "OBJECT",
|
160
|
+
"properties": {
|
161
|
+
"location": {
|
162
|
+
"type": "STRING",
|
163
|
+
"description": "The city and state, e.g. San Francisco, CA",
|
164
|
+
},
|
165
|
+
},
|
166
|
+
"required": ["location"],
|
167
|
+
}
|
168
|
+
)
|
169
|
+
|
170
|
+
tool = types.Tool(function_declarations=[function])
|
171
|
+
|
172
|
+
|
173
|
+
response = client.models.generate_content(
|
174
|
+
model='gemini-1.5-pro-002',
|
175
|
+
contents="What is the weather like in Boston?",
|
176
|
+
config=types.GenerateContentConfig(tools=[tool],)
|
177
|
+
)
|
178
|
+
|
179
|
+
response.candidates[0].content.parts[0].function_call
|
180
|
+
```
|
181
|
+
|
182
|
+
``` python
|
183
|
+
function_call_part = response.candidates[0].content.parts[0]
|
184
|
+
|
185
|
+
function_response = get_current_weather(**function_call_part.function_call.args)
|
186
|
+
|
187
|
+
|
188
|
+
function_response_part = types.Part.from_function_response(
|
189
|
+
name=function_call_part.function_call.name,
|
190
|
+
response={'result': function_response}
|
191
|
+
)
|
192
|
+
|
193
|
+
response = client.models.generate_content(
|
194
|
+
model='gemini-1.5-pro-002',
|
195
|
+
contents=[
|
196
|
+
types.Part.from_text("What is the weather like in Boston?"),
|
197
|
+
function_call_part,
|
198
|
+
function_response_part,
|
199
|
+
])
|
200
|
+
|
201
|
+
response
|
202
|
+
```
|
203
|
+
|
204
|
+
### JSON Response Schema
|
205
|
+
|
206
|
+
#### Pydantic Model Schema support
|
207
|
+
|
208
|
+
Schemas can be provided as Pydantic Models.
|
209
|
+
|
210
|
+
``` python
|
211
|
+
from pydantic import BaseModel
|
212
|
+
|
213
|
+
class CountryInfo(BaseModel):
|
214
|
+
name: str
|
215
|
+
population: int
|
216
|
+
capital: str
|
217
|
+
continent: str
|
218
|
+
gdp: int
|
219
|
+
official_language: str
|
220
|
+
total_area_sq_mi: int
|
221
|
+
|
222
|
+
|
223
|
+
response = client.models.generate_content(
|
224
|
+
model='gemini-1.5-flash',
|
225
|
+
contents='Give me information of the United States.',
|
226
|
+
config=types.GenerateContentConfig(
|
227
|
+
response_mime_type= 'application/json',
|
228
|
+
response_schema= CountryInfo,
|
229
|
+
),
|
230
|
+
)
|
231
|
+
print(response.text)
|
232
|
+
```
|
233
|
+
|
234
|
+
``` python
|
235
|
+
response = client.models.generate_content(
|
236
|
+
model='gemini-1.5-flash',
|
237
|
+
contents='Give me information of the United States.',
|
238
|
+
config={
|
239
|
+
'response_mime_type': 'application/json',
|
240
|
+
'response_schema': {
|
241
|
+
'required': [
|
242
|
+
'name',
|
243
|
+
'population',
|
244
|
+
'capital',
|
245
|
+
'continent',
|
246
|
+
'gdp',
|
247
|
+
'official_language',
|
248
|
+
'total_area_sq_mi',
|
249
|
+
],
|
250
|
+
'properties': {
|
251
|
+
'name': {'type': 'STRING'},
|
252
|
+
'population': {'type': 'INTEGER'},
|
253
|
+
'capital': {'type': 'STRING'},
|
254
|
+
'continent': {'type': 'STRING'},
|
255
|
+
'gdp': {'type': 'INTEGER'},
|
256
|
+
'official_language': {'type': 'STRING'},
|
257
|
+
'total_area_sq_mi': {'type': 'INTEGER'},
|
258
|
+
},
|
259
|
+
'type': 'OBJECT',
|
260
|
+
},
|
261
|
+
},
|
262
|
+
)
|
263
|
+
print(response.text)
|
264
|
+
```
|
265
|
+
|
266
|
+
### Streaming
|
267
|
+
|
268
|
+
``` python
|
269
|
+
for chunk in client.models.generate_content_stream(
|
270
|
+
model='gemini-1.5-flash', contents='Tell me a story in 300 words.'
|
271
|
+
):
|
272
|
+
print(chunk.text)
|
273
|
+
```
|
274
|
+
|
275
|
+
### Async
|
276
|
+
|
277
|
+
`client.aio` exposes all the analogous `async` methods that are
|
278
|
+
available on `client`
|
279
|
+
|
280
|
+
For example, `client.aio.models.generate_content` is the async version
|
281
|
+
of `client.models.generate_content`
|
282
|
+
|
283
|
+
``` python
|
284
|
+
request = await client.aio.models.generate_content(
|
285
|
+
model='gemini-1.5-flash', contents='Tell me a story in 300 words.'
|
286
|
+
)
|
287
|
+
|
288
|
+
print(response.text)
|
289
|
+
```
|
290
|
+
|
291
|
+
### Streaming
|
292
|
+
|
293
|
+
``` python
|
294
|
+
async for response in client.aio.models.generate_content_stream(
|
295
|
+
model='gemini-1.5-flash', contents='Tell me a story in 300 words.'
|
296
|
+
):
|
297
|
+
print(response.text)
|
298
|
+
```
|
299
|
+
|
300
|
+
### Count Tokens and Compute Tokens
|
301
|
+
|
302
|
+
``` python
|
303
|
+
response = client.models.count_tokens(
|
304
|
+
model='gemini-1.5-flash',
|
305
|
+
contents='What is your name?',
|
306
|
+
)
|
307
|
+
print(response)
|
308
|
+
```
|
309
|
+
|
310
|
+
#### Compute Tokens
|
311
|
+
|
312
|
+
Compute tokens is not supported by Google AI.
|
313
|
+
|
314
|
+
``` python
|
315
|
+
response = client.models.compute_tokens(
|
316
|
+
model='gemini-1.5-flash',
|
317
|
+
contents='What is your name?',
|
318
|
+
)
|
319
|
+
print(response)
|
320
|
+
```
|
321
|
+
|
322
|
+
##### Async
|
323
|
+
|
324
|
+
``` python
|
325
|
+
response = await client.aio.models.count_tokens(
|
326
|
+
model='gemini-1.5-flash',
|
327
|
+
contents='What is your name?',
|
328
|
+
)
|
329
|
+
print(response)
|
330
|
+
```
|
331
|
+
|
332
|
+
### Embed Content
|
333
|
+
|
334
|
+
``` python
|
335
|
+
response = client.models.embed_content(
|
336
|
+
model='text-embedding-004',
|
337
|
+
contents='What is your name?',
|
338
|
+
)
|
339
|
+
response
|
340
|
+
```
|
341
|
+
|
342
|
+
``` python
|
343
|
+
# multiple contents with config
|
344
|
+
response = client.models.embed_content(
|
345
|
+
model='text-embedding-004',
|
346
|
+
contents=['What is your name?', 'What is your age?'],
|
347
|
+
config=types.EmbedContentConfig(output_dimensionality= 10)
|
348
|
+
)
|
349
|
+
|
350
|
+
response
|
351
|
+
```
|
352
|
+
|
353
|
+
### Imagen
|
354
|
+
|
355
|
+
#### Generate Image
|
356
|
+
|
357
|
+
Support for generate image in Google AI is behind an allowlist
|
358
|
+
|
359
|
+
``` python
|
360
|
+
# Generate Image
|
361
|
+
response1 = client.models.generate_image(
|
362
|
+
model='imagen-3.0-generate-001',
|
363
|
+
prompt='Robot holding a red skateboard',
|
364
|
+
config=types.GenerateImageConfig(
|
365
|
+
number_of_images= 1,
|
366
|
+
person_generation= "ALLOW_ADULT",
|
367
|
+
safety_filter_level= "BLOCK_LOW_AND_ABOVE",
|
368
|
+
include_rai_reason= True,
|
369
|
+
add_watermark= True,
|
370
|
+
aspect_ratio= "4:3"
|
371
|
+
)
|
372
|
+
)
|
373
|
+
response1.generated_images[0].image.show()
|
374
|
+
```
|
375
|
+
|
376
|
+
#### Upscale Image
|
377
|
+
|
378
|
+
``` python
|
379
|
+
# Upscale the generated image from the cell above
|
380
|
+
from google.genai.types import Image
|
381
|
+
response2 = client.models.upscale_image(
|
382
|
+
model='imagen-3.0-generate-001',
|
383
|
+
image=response1.generated_images[0].image,
|
384
|
+
config=types.UpscaleImageConfig(upscale_factor="x2")
|
385
|
+
)
|
386
|
+
response2.generated_images[0].image.show()
|
387
|
+
```
|
388
|
+
|
389
|
+
#### Edit Image
|
390
|
+
|
391
|
+
``` python
|
392
|
+
# Edit the generated image from the first cell
|
393
|
+
from google.genai.types import Image
|
394
|
+
response3 = client.models.edit_image(
|
395
|
+
model='imagegeneration@006',
|
396
|
+
prompt='holding yellow surfboard',
|
397
|
+
image=response1.generated_images[0].image,
|
398
|
+
config=types.EditImageConfig(
|
399
|
+
edit_mode="inpainting-insert",
|
400
|
+
mask_type="semantic",
|
401
|
+
segmentation_classes=[156],
|
402
|
+
number_of_images= 1,
|
403
|
+
include_rai_reason= True,
|
404
|
+
product_position="reposition"
|
405
|
+
)
|
406
|
+
)
|
407
|
+
response3.generated_images[0].image.show()
|
408
|
+
```
|
409
|
+
|
410
|
+
## Files (Only Google AI)
|
411
|
+
|
412
|
+
``` python
|
413
|
+
!gsutil cp gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf .
|
414
|
+
!gsutil cp gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf .
|
415
|
+
```
|
416
|
+
|
417
|
+
### Upload
|
418
|
+
|
419
|
+
``` python
|
420
|
+
file1 = client.files.upload(path='2312.11805v3.pdf')
|
421
|
+
file2 = client.files.upload(path='2403.05530.pdf')
|
422
|
+
|
423
|
+
print(file1)
|
424
|
+
print(file2)
|
425
|
+
```
|
426
|
+
|
427
|
+
### Delete
|
428
|
+
|
429
|
+
``` python
|
430
|
+
file3 = client.files.upload(path='2312.11805v3.pdf')
|
431
|
+
|
432
|
+
client.files.delete(name=file3.name)
|
433
|
+
```
|
434
|
+
|
435
|
+
## Caches
|
436
|
+
|
437
|
+
`client.caches` contains the control plane APIs for cached content
|
438
|
+
|
439
|
+
### Create
|
440
|
+
|
441
|
+
``` python
|
442
|
+
if client._api_client.vertexai:
|
443
|
+
file_uris = [
|
444
|
+
'gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf',
|
445
|
+
'gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf'
|
446
|
+
]
|
447
|
+
else:
|
448
|
+
file_uris = [file1.uri, file2.uri]
|
449
|
+
|
450
|
+
cached_content = client.caches.create(
|
451
|
+
model='gemini-1.5-pro-002',
|
452
|
+
contents=[
|
453
|
+
types.Content(
|
454
|
+
role='user',
|
455
|
+
parts=[
|
456
|
+
types.Part.from_uri(
|
457
|
+
file_uri=file_uris[0],
|
458
|
+
mime_type='application/pdf'),
|
459
|
+
types.Part.from_uri(
|
460
|
+
file_uri=file_uris[1],
|
461
|
+
mime_type='application/pdf',)])
|
462
|
+
],
|
463
|
+
config=types.CreateCachedContentConfig(
|
464
|
+
display_name='test cache',
|
465
|
+
system_instruction='What is the sum of the two pdfs?',
|
466
|
+
ttl='3600s',
|
467
|
+
),
|
468
|
+
)
|
469
|
+
```
|
470
|
+
|
471
|
+
### Get
|
472
|
+
|
473
|
+
``` python
|
474
|
+
client.caches.get(name=cached_content.name)
|
475
|
+
```
|
476
|
+
|
477
|
+
### Generate Content
|
478
|
+
|
479
|
+
``` python
|
480
|
+
client.models.generate_content(
|
481
|
+
model='gemini-1.5-pro-002',
|
482
|
+
contents='Summarize the pdfs',
|
483
|
+
config=types.GenerateContentConfig(
|
484
|
+
cached_content=cached_content.name,
|
485
|
+
)
|
486
|
+
)
|
487
|
+
```
|
488
|
+
|
489
|
+
## Tunings
|
490
|
+
|
491
|
+
`client.tunings` contains tuning job APIs and supports supervised fine
|
492
|
+
tuning through `tune` and distiallation through `distill`
|
493
|
+
|
494
|
+
### Tune
|
495
|
+
|
496
|
+
- Vertex supports tuning from GCS source
|
497
|
+
- Google AI supports tuning from inline examples
|
498
|
+
|
499
|
+
``` python
|
500
|
+
if client._api_client.vertexai:
|
501
|
+
model = 'gemini-1.5-pro-002'
|
502
|
+
training_dataset=types.TuningDataset(
|
503
|
+
gcs_uri='gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl',
|
504
|
+
)
|
505
|
+
else:
|
506
|
+
model = 'models/gemini-1.0-pro-001'
|
507
|
+
training_dataset=types.TuningDataset(
|
508
|
+
examples=[
|
509
|
+
types.TuningExample(
|
510
|
+
text_input=f"Input text {i}",
|
511
|
+
output=f"Output text {i}",
|
512
|
+
)
|
513
|
+
for i in range(5)
|
514
|
+
],
|
515
|
+
)
|
516
|
+
```
|
517
|
+
|
518
|
+
``` python
|
519
|
+
tuning_job = client.tunings.tune(
|
520
|
+
base_model=model,
|
521
|
+
training_dataset=training_dataset,
|
522
|
+
config=types.CreateTuningJobConfig(
|
523
|
+
epoch_count= 1,
|
524
|
+
tuned_model_display_name="test_dataset_examples model"
|
525
|
+
)
|
526
|
+
)
|
527
|
+
tuning_job
|
528
|
+
```
|
529
|
+
|
530
|
+
### Get Tuning Job
|
531
|
+
|
532
|
+
``` python
|
533
|
+
tuning_job = client.tunings.get(name=tuning_job.name)
|
534
|
+
tuning_job
|
535
|
+
```
|
536
|
+
|
537
|
+
``` python
|
538
|
+
import time
|
539
|
+
|
540
|
+
running_states = set([
|
541
|
+
"JOB_STATE_PENDING",
|
542
|
+
"JOB_STATE_RUNNING",
|
543
|
+
])
|
544
|
+
|
545
|
+
while tuning_job.state in running_states:
|
546
|
+
print(tuning_job.state)
|
547
|
+
tuning_job = client.tunings.get(name=tuning_job.name)
|
548
|
+
time.sleep(10)
|
549
|
+
```
|
550
|
+
|
551
|
+
#### Use Tuned Model
|
552
|
+
|
553
|
+
``` python
|
554
|
+
response = client.models.generate_content(
|
555
|
+
model=tuning_job.tuned_model.endpoint,
|
556
|
+
contents='What is your name?',
|
557
|
+
)
|
558
|
+
|
559
|
+
response.text
|
560
|
+
```
|
561
|
+
|
562
|
+
### Get Tuned Model
|
563
|
+
|
564
|
+
``` python
|
565
|
+
tuned_model = client.models.get(model=tuning_job.tuned_model.model)
|
566
|
+
tuned_model
|
567
|
+
```
|
568
|
+
|
569
|
+
### List Tuned Models
|
570
|
+
|
571
|
+
``` python
|
572
|
+
for model in client.models.list(config={'page_size': 10}):
|
573
|
+
print(model)
|
574
|
+
```
|
575
|
+
|
576
|
+
``` python
|
577
|
+
pager = client.models.list(config={'page_size': 10})
|
578
|
+
print(pager.page_size)
|
579
|
+
print(pager.page[0])
|
580
|
+
pager.next_page()
|
581
|
+
print(pager.page[0])
|
582
|
+
```
|
583
|
+
|
584
|
+
#### Async
|
585
|
+
|
586
|
+
``` python
|
587
|
+
async for job in await client.aio.models.list(config={'page_size': 10}):
|
588
|
+
print(job)
|
589
|
+
```
|
590
|
+
|
591
|
+
``` python
|
592
|
+
async_pager = await client.aio.models.list(config={'page_size': 10})
|
593
|
+
print(async_pager.page_size)
|
594
|
+
print(async_pager.page[0])
|
595
|
+
await async_pager.next_page()
|
596
|
+
print(async_pager.page[0])
|
597
|
+
```
|
598
|
+
|
599
|
+
### Update Tuned Model
|
600
|
+
|
601
|
+
``` python
|
602
|
+
model = pager.page[0]
|
603
|
+
|
604
|
+
model = client.models.update(
|
605
|
+
model=model.name,
|
606
|
+
config=types.UpdateModelConfig(
|
607
|
+
display_name='my tuned model',
|
608
|
+
description='my tuned model description'))
|
609
|
+
|
610
|
+
model
|
611
|
+
```
|
612
|
+
|
613
|
+
### Distillation
|
614
|
+
|
615
|
+
Only supported on Vertex. Requires allowlist.
|
616
|
+
|
617
|
+
``` python
|
618
|
+
distillation_job = client.tunings.distill(
|
619
|
+
student_model="gemma-2b-1.1-it",
|
620
|
+
teacher_model="gemini-1.5-pro-002",
|
621
|
+
training_dataset=genai.types.DistillationDataset(
|
622
|
+
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
|
623
|
+
),
|
624
|
+
config=genai.types.CreateDistillationJobConfig(
|
625
|
+
epoch_count=1,
|
626
|
+
pipeline_root_directory=(
|
627
|
+
"gs://vertex-sdk-dev-staging-us-central1/tmp/distillation_pipeline_root"
|
628
|
+
),
|
629
|
+
),
|
630
|
+
)
|
631
|
+
distillation_job
|
632
|
+
```
|
633
|
+
|
634
|
+
``` python
|
635
|
+
tcompleted_states = set([
|
636
|
+
"JOB_STATE_SUCCEEDED",
|
637
|
+
"JOB_STATE_FAILED",
|
638
|
+
"JOB_STATE_CANCELLED",
|
639
|
+
"JOB_STATE_PAUSED"
|
640
|
+
])
|
641
|
+
|
642
|
+
while distillation_job.state not in completed_states:
|
643
|
+
print(distillation_job.state)
|
644
|
+
distillation_job = client.tunings.get(name=distillation_job.name)
|
645
|
+
time.sleep(10)
|
646
|
+
```
|
647
|
+
|
648
|
+
``` python
|
649
|
+
distillation_job
|
650
|
+
```
|
651
|
+
|
652
|
+
### List Tuning Jobs
|
653
|
+
|
654
|
+
``` python
|
655
|
+
for job in client.tunings.list(config={'page_size': 10}):
|
656
|
+
print(job)
|
657
|
+
```
|
658
|
+
|
659
|
+
``` python
|
660
|
+
pager = client.tunings.list(config={'page_size': 10})
|
661
|
+
print(pager.page_size)
|
662
|
+
print(pager.page[0])
|
663
|
+
pager.next_page()
|
664
|
+
print(pager.page[0])
|
665
|
+
```
|
666
|
+
|
667
|
+
#### Async
|
668
|
+
|
669
|
+
``` python
|
670
|
+
async for job in await client.aio.tunings.list(config={'page_size': 10}):
|
671
|
+
print(job)
|
672
|
+
```
|
673
|
+
|
674
|
+
``` python
|
675
|
+
async_pager = await client.aio.tunings.list(config={'page_size': 10})
|
676
|
+
print(async_pager.page_size)
|
677
|
+
print(async_pager.page[0])
|
678
|
+
await async_pager.next_page()
|
679
|
+
print(async_pager.page[0])
|
680
|
+
```
|
681
|
+
|
682
|
+
## Batch Prediction
|
683
|
+
|
684
|
+
Only supported in Vertex AI.
|
685
|
+
|
686
|
+
### Create
|
687
|
+
|
688
|
+
``` python
|
689
|
+
# Specify model and source file only, destination and job display name will be auto-populated
|
690
|
+
job = client.batches.create(
|
691
|
+
model='gemini-1.5-flash-002',
|
692
|
+
src='bq://vertex-sdk-dev.unified_genai_tests_batches.generate_content_requests',
|
693
|
+
# # optionally specify destination and display_name by yourself
|
694
|
+
# config = {
|
695
|
+
# 'dest': 'bq://vertex-sdk-dev.unified_genai_tests_batches.generate_content_responses',
|
696
|
+
# 'display_name': 'create_batch_job_demo'
|
697
|
+
# }
|
698
|
+
)
|
699
|
+
|
700
|
+
job
|
701
|
+
```
|
702
|
+
|
703
|
+
``` python
|
704
|
+
# Get a job by name
|
705
|
+
job = client.batches.get(name=job.name)
|
706
|
+
|
707
|
+
job.state
|
708
|
+
```
|
709
|
+
|
710
|
+
``` python
|
711
|
+
completed_states = set([
|
712
|
+
"JOB_STATE_SUCCEEDED",
|
713
|
+
"JOB_STATE_FAILED",
|
714
|
+
"JOB_STATE_CANCELLED",
|
715
|
+
"JOB_STATE_PAUSED"
|
716
|
+
])
|
717
|
+
|
718
|
+
while job.state not in completed_states:
|
719
|
+
print(job.state)
|
720
|
+
job = client.batches.get(name=job.name)
|
721
|
+
time.sleep(30)
|
722
|
+
|
723
|
+
job
|
724
|
+
```
|
725
|
+
|
726
|
+
### List
|
727
|
+
|
728
|
+
``` python
|
729
|
+
for job in client.batches.list(config={'page_size': 10}):
|
730
|
+
print(job)
|
731
|
+
```
|
732
|
+
|
733
|
+
``` python
|
734
|
+
pager = client.batches.list(config={'page_size': 10})
|
735
|
+
print(pager.page_size)
|
736
|
+
print(pager.page[0])
|
737
|
+
pager.next_page()
|
738
|
+
print(pager.page[0])
|
739
|
+
```
|
740
|
+
|
741
|
+
#### Async
|
742
|
+
|
743
|
+
``` python
|
744
|
+
async for job in await client.aio.batches.list(config={'page_size': 10}):
|
745
|
+
print(job)
|
746
|
+
```
|
747
|
+
|
748
|
+
``` python
|
749
|
+
async_pager = await client.aio.tunings.list(config={'page_size': 10})
|
750
|
+
print(async_pager.page_size)
|
751
|
+
print(async_pager.page[0])
|
752
|
+
await async_pager.next_page()
|
753
|
+
print(async_pager.page[0])
|
754
|
+
```
|
755
|
+
|
756
|
+
### Delete
|
757
|
+
|
758
|
+
``` python
|
759
|
+
# Delete the job resource
|
760
|
+
delete_job = client.batches.delete(name=job.name)
|
761
|
+
|
762
|
+
delete_job
|
763
|
+
```
|