Skip to content

Commit 343afbc

Browse files
cleop-googlecopybara-github
authored andcommitted
feat: GenAI SDK client(multimodal) - Allow passing dataset ID in addition to full resource name in dataset methods.
PiperOrigin-RevId: 899573600
1 parent f2d73fd commit 343afbc

3 files changed

Lines changed: 111 additions & 29 deletions

File tree

tests/unit/vertexai/genai/replays/test_get_multimodal_datasets.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ def test_get_dataset_from_public_method(client):
4141
assert dataset.display_name == "test-display-name"
4242

4343

44+
def test_get_dataset_by_id(client):
45+
dataset = client.datasets.get_multimodal_dataset(
46+
name="8810841321427173376",
47+
)
48+
assert isinstance(dataset, types.MultimodalDataset)
49+
assert dataset.name == DATASET
50+
assert dataset.display_name == "test-display-name"
51+
52+
4453
pytestmark = pytest_helper.setup(
4554
file=__file__,
4655
globals_for_file=globals(),
@@ -67,3 +76,13 @@ async def test_get_dataset_from_public_method_async(client):
6776
assert isinstance(dataset, types.MultimodalDataset)
6877
assert dataset.name == DATASET
6978
assert dataset.display_name == "test-display-name"
79+
80+
81+
@pytest.mark.asyncio
82+
async def test_get_dataset_by_id_async(client):
83+
dataset = await client.aio.datasets.get_multimodal_dataset(
84+
name="8810841321427173376",
85+
)
86+
assert isinstance(dataset, types.MultimodalDataset)
87+
assert dataset.name == DATASET
88+
assert dataset.display_name == "test-display-name"

vertexai/_genai/_datasets_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,10 @@ async def save_dataframe_to_bigquery_async(
262262
)
263263
await asyncio.to_thread(copy_job.result)
264264
await asyncio.to_thread(bq_client.delete_table, temp_table_id)
265+
266+
267+
def resolve_dataset_name(resource_name_or_id: str, project: str, location: str) -> str:
268+
"""Resolves a dataset name or ID to a full resource name."""
269+
if not resource_name_or_id.startswith("projects/"):
270+
return f"projects/{project}/locations/{location}/datasets/{resource_name_or_id}"
271+
return resource_name_or_id

vertexai/_genai/datasets.py

Lines changed: 85 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,8 +1130,8 @@ def get_multimodal_dataset(
11301130
11311131
Args:
11321132
name:
1133-
Required. name of a multimodal dataset. The name should be in
1134-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1133+
Required. A fully-qualified resource name or ID of the dataset.
1134+
Example: "projects/.../locations/.../datasets/123" or "123".
11351135
config:
11361136
Optional. A configuration for getting the multimodal dataset. If not
11371137
provided, the default configuration will be used.
@@ -1145,6 +1145,10 @@ def get_multimodal_dataset(
11451145
elif not config:
11461146
config = types.VertexBaseConfig()
11471147

1148+
name = _datasets_utils.resolve_dataset_name(
1149+
name, self._api_client.project, self._api_client.location
1150+
)
1151+
11481152
return self._get_multimodal_dataset(config=config, name=name)
11491153

11501154
def delete_multimodal_dataset(
@@ -1157,8 +1161,8 @@ def delete_multimodal_dataset(
11571161
11581162
Args:
11591163
name:
1160-
Required. name of a multimodal dataset. The name should be in
1161-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1164+
Required. A fully-qualified resource name or ID of the dataset.
1165+
Example: "projects/.../locations/.../datasets/123" or "123".
11621166
config:
11631167
Optional. A configuration for deleting the multimodal dataset. If not
11641168
provided, the default configuration will be used.
@@ -1172,6 +1176,10 @@ def delete_multimodal_dataset(
11721176
elif not config:
11731177
config = types.VertexBaseConfig()
11741178

1179+
name = _datasets_utils.resolve_dataset_name(
1180+
name, self._api_client.project, self._api_client.location
1181+
)
1182+
11751183
return self._delete_multimodal_dataset(config=config, name=name)
11761184

11771185
def assemble(
@@ -1189,8 +1197,8 @@ def assemble(
11891197
11901198
Args:
11911199
name:
1192-
Required. The name of the dataset to assemble. The name should be in
1193-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1200+
Required. A fully-qualified resource name or ID of the dataset.
1201+
Example: "projects/.../locations/.../datasets/123" or "123".
11941202
gemini_request_read_config:
11951203
Optional. The read config to use to assemble the dataset. If
11961204
not provided, the read config attached to the dataset will be
@@ -1207,6 +1215,10 @@ def assemble(
12071215
elif not config:
12081216
config = types.AssembleDatasetConfig()
12091217

1218+
name = _datasets_utils.resolve_dataset_name(
1219+
name, self._api_client.project, self._api_client.location
1220+
)
1221+
12101222
operation = self._assemble_multimodal_dataset(
12111223
name=name,
12121224
gemini_request_read_config=gemini_request_read_config,
@@ -1232,8 +1244,8 @@ def assess_tuning_resources(
12321244
12331245
Args:
12341246
dataset_name:
1235-
Required. The name of the dataset to assess the tuning resources
1236-
for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1247+
Required. A fully-qualified resource name or ID of the dataset.
1248+
Example: "projects/.../locations/.../datasets/123" or "123".
12371249
model_name:
12381250
Required. The name of the model to assess the tuning resources
12391251
for.
@@ -1255,6 +1267,10 @@ def assess_tuning_resources(
12551267
elif not config:
12561268
config = types.AssessDatasetConfig()
12571269

1270+
dataset_name = _datasets_utils.resolve_dataset_name(
1271+
dataset_name, self._api_client.project, self._api_client.location
1272+
)
1273+
12581274
operation = self._assess_multimodal_dataset(
12591275
name=dataset_name,
12601276
tuning_resource_usage_assessment_config=types.TuningResourceUsageAssessmentConfig(
@@ -1288,8 +1304,8 @@ def assess_tuning_validity(
12881304
12891305
Args:
12901306
dataset_name:
1291-
Required. The name of the dataset to assess the tuning validity
1292-
for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1307+
Required. A fully-qualified resource name or ID of the dataset.
1308+
Example: "projects/.../locations/.../datasets/123" or "123".
12931309
model_name:
12941310
Required. The name of the model to assess the tuning validity
12951311
for.
@@ -1316,6 +1332,10 @@ def assess_tuning_validity(
13161332
elif not config:
13171333
config = types.AssessDatasetConfig()
13181334

1335+
dataset_name = _datasets_utils.resolve_dataset_name(
1336+
dataset_name, self._api_client.project, self._api_client.location
1337+
)
1338+
13191339
operation = self._assess_multimodal_dataset(
13201340
name=dataset_name,
13211341
tuning_validation_assessment_config=types.TuningValidationAssessmentConfig(
@@ -1348,8 +1368,8 @@ def assess_batch_prediction_resources(
13481368
13491369
Args:
13501370
dataset_name:
1351-
Required. The name of the dataset to assess the batch prediction
1352-
resources. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1371+
Required. A fully-qualified resource name or ID of the dataset.
1372+
Example: "projects/.../locations/.../datasets/123" or "123".
13531373
model_name:
13541374
Required. The name of the model to assess the batch prediction
13551375
resources.
@@ -1376,6 +1396,10 @@ def assess_batch_prediction_resources(
13761396
elif not config:
13771397
config = types.AssessDatasetConfig()
13781398

1399+
dataset_name = _datasets_utils.resolve_dataset_name(
1400+
dataset_name, self._api_client.project, self._api_client.location
1401+
)
1402+
13791403
operation = self._assess_multimodal_dataset(
13801404
name=dataset_name,
13811405
batch_prediction_resource_usage_assessment_config=types.BatchPredictionResourceUsageAssessmentConfig(
@@ -1409,8 +1433,8 @@ def assess_batch_prediction_validity(
14091433
14101434
Args:
14111435
dataset_name:
1412-
Required. The name of the dataset to assess the batch prediction
1413-
validity for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
1436+
Required. A fully-qualified resource name or ID of the dataset.
1437+
Example: "projects/.../locations/.../datasets/123" or "123".
14141438
model_name:
14151439
Required. The name of the model to assess the batch prediction
14161440
validity for.
@@ -1435,6 +1459,10 @@ def assess_batch_prediction_validity(
14351459
elif not config:
14361460
config = types.AssessDatasetConfig()
14371461

1462+
dataset_name = _datasets_utils.resolve_dataset_name(
1463+
dataset_name, self._api_client.project, self._api_client.location
1464+
)
1465+
14381466
operation = self._assess_multimodal_dataset(
14391467
name=dataset_name,
14401468
batch_prediction_validation_assessment_config=types.BatchPredictionValidationAssessmentConfig(
@@ -2352,21 +2380,25 @@ async def get_multimodal_dataset(
23522380
23532381
Args:
23542382
name:
2355-
Required. name of a multimodal dataset. The name should be in
2356-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2383+
Required. A fully-qualified resource name or ID of the dataset.
2384+
Example: "projects/.../locations/.../datasets/123" or "123".
23572385
config:
23582386
Optional. A configuration for getting the multimodal dataset. If not
23592387
provided, the default configuration will be used.
23602388
23612389
Returns:
2362-
A types.MultimodalDataset object representing the updated multimodal
2390+
A types.MultimodalDataset object representing the retrieved multimodal
23632391
dataset.
23642392
"""
23652393
if isinstance(config, dict):
23662394
config = types.VertexBaseConfig(**config)
23672395
elif not config:
23682396
config = types.VertexBaseConfig()
23692397

2398+
name = _datasets_utils.resolve_dataset_name(
2399+
name, self._api_client.project, self._api_client.location
2400+
)
2401+
23702402
return await self._get_multimodal_dataset(config=config, name=name)
23712403

23722404
async def delete_multimodal_dataset(
@@ -2379,8 +2411,8 @@ async def delete_multimodal_dataset(
23792411
23802412
Args:
23812413
name:
2382-
Required. name of a multimodal dataset. The name should be in
2383-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2414+
Required. A fully-qualified resource name or ID of the dataset.
2415+
Example: "projects/.../locations/.../datasets/123" or "123".
23842416
config:
23852417
Optional. A configuration for deleting the multimodal dataset. If not
23862418
provided, the default configuration will be used.
@@ -2394,6 +2426,10 @@ async def delete_multimodal_dataset(
23942426
elif not config:
23952427
config = types.VertexBaseConfig()
23962428

2429+
name = _datasets_utils.resolve_dataset_name(
2430+
name, self._api_client.project, self._api_client.location
2431+
)
2432+
23972433
return await self._delete_multimodal_dataset(config=config, name=name)
23982434

23992435
async def assemble(
@@ -2411,8 +2447,8 @@ async def assemble(
24112447
24122448
Args:
24132449
name:
2414-
Required. The name of the dataset to assemble. The name should be in
2415-
the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2450+
Required. A fully-qualified resource name or ID of the dataset.
2451+
Example: "projects/.../locations/.../datasets/123" or "123".
24162452
gemini_request_read_config:
24172453
Optional. The read config to use to assemble the dataset. If
24182454
not provided, the read config attached to the dataset will be
@@ -2429,6 +2465,10 @@ async def assemble(
24292465
elif not config:
24302466
config = types.AssembleDatasetConfig()
24312467

2468+
name = _datasets_utils.resolve_dataset_name(
2469+
name, self._api_client.project, self._api_client.location
2470+
)
2471+
24322472
operation = await self._assemble_multimodal_dataset(
24332473
name=name,
24342474
gemini_request_read_config=gemini_request_read_config,
@@ -2454,8 +2494,8 @@ async def assess_tuning_resources(
24542494
24552495
Args:
24562496
dataset_name:
2457-
Required. The name of the dataset to assess the tuning resources
2458-
for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2497+
Required. A fully-qualified resource name or ID of the dataset.
2498+
Example: "projects/.../locations/.../datasets/123" or "123".
24592499
model_name:
24602500
Required. The name of the model to assess the tuning resources
24612501
for.
@@ -2477,6 +2517,10 @@ async def assess_tuning_resources(
24772517
elif not config:
24782518
config = types.AssessDatasetConfig()
24792519

2520+
dataset_name = _datasets_utils.resolve_dataset_name(
2521+
dataset_name, self._api_client.project, self._api_client.location
2522+
)
2523+
24802524
operation = await self._assess_multimodal_dataset(
24812525
name=dataset_name,
24822526
tuning_resource_usage_assessment_config=types.TuningResourceUsageAssessmentConfig(
@@ -2510,8 +2554,8 @@ async def assess_tuning_validity(
25102554
25112555
Args:
25122556
dataset_name:
2513-
Required. The name of the dataset to assess the tuning validity
2514-
for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2557+
Required. A fully-qualified resource name or ID of the dataset.
2558+
Example: "projects/.../locations/.../datasets/123" or "123".
25152559
model_name:
25162560
Required. The name of the model to assess the tuning validity
25172561
for.
@@ -2538,6 +2582,10 @@ async def assess_tuning_validity(
25382582
elif not config:
25392583
config = types.AssessDatasetConfig()
25402584

2585+
dataset_name = _datasets_utils.resolve_dataset_name(
2586+
dataset_name, self._api_client.project, self._api_client.location
2587+
)
2588+
25412589
operation = await self._assess_multimodal_dataset(
25422590
name=dataset_name,
25432591
tuning_validation_assessment_config=types.TuningValidationAssessmentConfig(
@@ -2570,8 +2618,8 @@ async def assess_batch_prediction_resources(
25702618
25712619
Args:
25722620
dataset_name:
2573-
Required. The name of the dataset to assess the batch prediction
2574-
resources. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2621+
Required. A fully-qualified resource name or ID of the dataset.
2622+
Example: "projects/.../locations/.../datasets/123" or "123".
25752623
model_name:
25762624
Required. The name of the model to assess the batch prediction
25772625
resources.
@@ -2598,6 +2646,10 @@ async def assess_batch_prediction_resources(
25982646
elif not config:
25992647
config = types.AssessDatasetConfig()
26002648

2649+
dataset_name = _datasets_utils.resolve_dataset_name(
2650+
dataset_name, self._api_client.project, self._api_client.location
2651+
)
2652+
26012653
operation = await self._assess_multimodal_dataset(
26022654
name=dataset_name,
26032655
batch_prediction_resource_usage_assessment_config=types.BatchPredictionResourceUsageAssessmentConfig(
@@ -2631,8 +2683,8 @@ async def assess_batch_prediction_validity(
26312683
26322684
Args:
26332685
dataset_name:
2634-
Required. The name of the dataset to assess the batch prediction
2635-
validity for. The name should be in the format of "projects/{project}/locations/{location}/datasets/{dataset}".
2686+
Required. A fully-qualified resource name or ID of the dataset.
2687+
Example: "projects/.../locations/.../datasets/123" or "123".
26362688
model_name:
26372689
Required. The name of the model to assess the batch prediction
26382690
validity for.
@@ -2657,6 +2709,10 @@ async def assess_batch_prediction_validity(
26572709
elif not config:
26582710
config = types.AssessDatasetConfig()
26592711

2712+
dataset_name = _datasets_utils.resolve_dataset_name(
2713+
dataset_name, self._api_client.project, self._api_client.location
2714+
)
2715+
26602716
operation = await self._assess_multimodal_dataset(
26612717
name=dataset_name,
26622718
batch_prediction_validation_assessment_config=types.BatchPredictionValidationAssessmentConfig(

0 commit comments

Comments
 (0)