Skip to content

Commit 97a3494

Browse files
yinghsienwucopybara-github
authored andcommitted
docs: update batches and tunings doc
PiperOrigin-RevId: 784288318
1 parent df52660 commit 97a3494

5 files changed

Lines changed: 149 additions & 3610 deletions

File tree

README.md

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,35 +1370,21 @@ print(response.text)
13701370
## Tunings
13711371

13721372
`client.tunings` contains tuning job APIs and supports supervised fine
1373-
tuning through `tune`. See the 'Create a client' section above to initialize a
1374-
client.
1373+
tuning through `tune`. Only supported in Vertex AI. See the 'Create a client'
1374+
section above to initialize a client.
13751375

13761376
### Tune
13771377

13781378
- Vertex AI supports tuning from GCS source or from a Vertex Multimodal Dataset
1379-
- Gemini Developer API supports tuning from inline examples
13801379

13811380
```python
13821381
from google.genai import types
13831382

1384-
if client.vertexai:
1385-
model = 'gemini-2.0-flash-001'
1386-
training_dataset = types.TuningDataset(
1387-
# or gcs_uri=my_vertex_multimodal_dataset
1388-
gcs_uri='gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl',
1389-
)
1390-
else:
1391-
model = 'models/gemini-2.0-flash-001'
1392-
# or gcs_uri=my_vertex_multimodal_dataset.resource_name
1393-
training_dataset = types.TuningDataset(
1394-
examples=[
1395-
types.TuningExample(
1396-
text_input=f'Input text {i}',
1397-
output=f'Output text {i}',
1398-
)
1399-
for i in range(5)
1400-
],
1401-
)
1383+
model = 'gemini-2.0-flash-001'
1384+
training_dataset = types.TuningDataset(
1385+
# or gcs_uri=my_vertex_multimodal_dataset
1386+
gcs_uri='gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl',
1387+
)
14021388
```
14031389

14041390
```python
@@ -1424,14 +1410,15 @@ print(tuning_job)
14241410
```python
14251411
import time
14261412

1427-
running_states = set(
1413+
completed_states = set(
14281414
[
1429-
'JOB_STATE_PENDING',
1430-
'JOB_STATE_RUNNING',
1415+
'JOB_STATE_SUCCEEDED',
1416+
'JOB_STATE_FAILED',
1417+
'JOB_STATE_CANCELLED',
14311418
]
14321419
)
14331420

1434-
while tuning_job.state in running_states:
1421+
while tuning_job.state not in completed_states:
14351422
print(tuning_job.state)
14361423
tuning_job = client.tunings.get(name=tuning_job.name)
14371424
time.sleep(10)
@@ -1542,16 +1529,63 @@ initialize a client.
15421529

15431530
### Create
15441531

1532+
Vertex AI:
1533+
15451534
```python
15461535
# Specify model and source file only, destination and job display name will be auto-populated
15471536
job = client.batches.create(
15481537
model='gemini-2.0-flash-001',
1549-
src='bq://my-project.my-dataset.my-table',
1538+
src='bq://my-project.my-dataset.my-table', # or "gs://path/to/input/data"
15501539
)
15511540

15521541
job
15531542
```
15541543

1544+
Gemini Developer API:
1545+
1546+
```python
1547+
# Create a batch job with inlined requests
1548+
batch_job = client.batches.create(
1549+
model="gemini-2.0-flash",
1550+
src=[{
1551+
"contents": [{
1552+
"parts": [{
1553+
"text": "Hello!",
1554+
}],
1555+
"role": "user",
1556+
}],
1557+
"config:": {"response_modalities": ["text"]},
1558+
}],
1559+
)
1560+
1561+
job
1562+
```
1563+
1564+
In order to create a batch job with file name. Need to upload a jsonl file.
1565+
For example myrequests.json:
1566+
1567+
```
1568+
{"key":"request_1", "request": {"contents": [{"parts": [{"text":
1569+
"Explain how AI works in a few words"}]}], "generation_config": {"response_modalities": ["TEXT"]}}}
1570+
{"key":"request_2", "request": {"contents": [{"parts": [{"text": "Explain how Crypto works in a few words"}]}]}}
1571+
```
1572+
Then upload the file.
1573+
1574+
```python
1575+
# Upload the file
1576+
file = client.files.upload(
1577+
file='myrequest.json',
1578+
config=types.UploadFileConfig(display_name='test_json')
1579+
)
1580+
1581+
# Create a batch job with file name
1582+
batch_job = client.batches.create(
1583+
model="gemini-2.0-flash",
1584+
src="files/file_name",
1585+
)
1586+
```
1587+
1588+
15551589
```python
15561590
# Get a job by name
15571591
job = client.batches.get(name=job.name)

docs/_sources/index.rst.txt

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,35 +1291,23 @@ Tunings
12911291
=======
12921292

12931293
``client.tunings`` contains tuning job APIs and supports supervised fine
1294-
tuning through ``tune``. See the 'Create a client' section above to initialize a
1295-
client.
1294+
tuning through ``tune``. Only supported in Vertex AI. See the 'Create a client'
1295+
section above to initialize a client.
12961296

12971297
Tune
12981298
----
12991299

13001300
- Vertex AI supports tuning from GCS source
1301-
- Gemini Developer API supports tuning from inline examples
13021301

13031302
.. code:: python
13041303
13051304
from google.genai import types
13061305
1307-
if client.vertexai:
1308-
model = 'gemini-2.0-flash-001'
1309-
training_dataset = types.TuningDataset(
1310-
gcs_uri='gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl',
1311-
)
1312-
else:
1313-
model = 'models/gemini-2.0-flash-001'
1314-
training_dataset = types.TuningDataset(
1315-
examples=[
1316-
types.TuningExample(
1317-
text_input=f'Input text {i}',
1318-
output=f'Output text {i}',
1319-
)
1320-
for i in range(5)
1321-
],
1322-
)
1306+
model = 'gemini-2.0-flash-001'
1307+
training_dataset = types.TuningDataset(
1308+
gcs_uri='gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl',
1309+
)
1310+
13231311
13241312
.. code:: python
13251313
@@ -1346,14 +1334,15 @@ Get Tuning Job
13461334
13471335
import time
13481336
1349-
running_states = set(
1337+
completed_states = set(
13501338
[
1351-
'JOB_STATE_PENDING',
1352-
'JOB_STATE_RUNNING',
1339+
'JOB_STATE_SUCCEEDED',
1340+
'JOB_STATE_FAILED',
1341+
'JOB_STATE_CANCELLED',
13531342
]
13541343
)
13551344
1356-
while tuning_job.state in running_states:
1345+
while tuning_job.state not in completed_states:
13571346
print(tuning_job.state)
13581347
tuning_job = client.tunings.get(name=tuning_job.name)
13591348
time.sleep(10)
@@ -1478,21 +1467,61 @@ List Tuning Jobs (Asynchronous):
14781467
Batch Prediction
14791468
================
14801469

1481-
Only supported in Vertex AI. See the 'Create a client' section above to
1482-
initialize a client.
1470+
Create a batch job. See the 'Create a client' section above to initialize a client.
14831471

14841472
Create
14851473
------
14861474

1475+
Vertex AI client support using a BigQuery table or a GCS file as the source.
1476+
14871477
.. code:: python
14881478
14891479
# Specify model and source file only, destination and job display name will be auto-populated
14901480
job = client.batches.create(
14911481
model='gemini-2.0-flash-001',
1492-
src='bq://my-project.my-dataset.my-table',
1482+
src='bq://my-project.my-dataset.my-table', # or gcs://my-bucket/my-file.jsonl
1483+
)
1484+
1485+
1486+
Gemini Developer API client:
1487+
1488+
.. code:: python
1489+
1490+
# Create a batch job with inlined requests
1491+
batch_job = client.batches.create(
1492+
model="gemini-2.0-flash",
1493+
src=[{
1494+
"contents": [{
1495+
"parts": [{
1496+
"text": "Hello!",
1497+
}],
1498+
"role": "user",
1499+
}],
1500+
"config:": {"response_modalities": ["text"]},
1501+
}],
1502+
)
1503+
1504+
1505+
In order to create a batch job with a file. Need to upload a jsonl file.
1506+
For example myrequests.json:
1507+
1508+
.. code:: json
1509+
{"key":"request_1", "request": {"contents": [{"parts": [{"text": "Explain how AI works in a few words"}]}], "generation_config": {"response_modalities": ["TEXT"]}}}
1510+
{"key":"request_2", "request": {"contents": [{"parts": [{"text": "Explain how Crypto works in a few words"}]}]}}
1511+
1512+
1513+
.. code:: python
1514+
# Upload a file to Gemini Developer API
1515+
file_name = client.files.upload(
1516+
file='myrequest.json',
1517+
config=types.UploadFileConfig(display_name='test_json'),
1518+
)
1519+
# Create a batch job with file name
1520+
batch_job = client.batches.create(
1521+
model="gemini-2.0-flash",
1522+
src="files/file_name",
14931523
)
14941524
1495-
job
14961525
14971526
.. code:: python
14981527

0 commit comments

Comments
 (0)