|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | + |
| 16 | + |
| 17 | +"""Tests for batches.create() with Vertex dataset source.""" |
| 18 | + |
| 19 | +import re |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from .. import pytest_helper |
| 24 | +from ... import types |
| 25 | + |
| 26 | + |
| 27 | +_GEMINI_MODEL = 'gemini-2.5-flash' |
| 28 | +_GEMINI_MODEL_FULL_NAME = 'publishers/google/models/gemini-2.5-flash' |
| 29 | +_OUTPUT_VERTEX_DATASET_DISPLAY_NAME = 'test_batch_output' |
| 30 | +_VERTEX_DATASET_INPUT_NAME = ( |
| 31 | + 'projects/vertex-sdk-dev/locations/us-central1/datasets/7857316250517504000' |
| 32 | +) |
| 33 | + |
| 34 | +_BQ_OUTPUT_PREFIX = ( |
| 35 | + 'bq://vertex-sdk-dev.unified_genai_tests_batches.generate_content_output' |
| 36 | +) |
| 37 | +_VERTEX_DATASET_DESTINATION = types.VertexMultimodalDatasetDestination( |
| 38 | + bigquery_destination=_BQ_OUTPUT_PREFIX, |
| 39 | + display_name=_OUTPUT_VERTEX_DATASET_DISPLAY_NAME, |
| 40 | +) |
| 41 | + |
| 42 | + |
| 43 | +# All tests will be run for both Vertex and MLDev. |
| 44 | +test_table: list[pytest_helper.TestTableItem] = [ |
| 45 | + pytest_helper.TestTableItem( |
| 46 | + name='test_union_generate_content_with_vertex_dataset_name', |
| 47 | + parameters=types._CreateBatchJobParameters( |
| 48 | + model=_GEMINI_MODEL_FULL_NAME, |
| 49 | + src=_VERTEX_DATASET_INPUT_NAME, |
| 50 | + config={ |
| 51 | + 'dest': { |
| 52 | + 'vertex_dataset': _VERTEX_DATASET_DESTINATION, |
| 53 | + 'format': 'vertex-dataset', |
| 54 | + }, |
| 55 | + }, |
| 56 | + ), |
| 57 | + exception_if_mldev='not supported in Gemini API', |
| 58 | + has_union=True, |
| 59 | + ), |
| 60 | + pytest_helper.TestTableItem( |
| 61 | + name='test_generate_content_with_vertex_dataset_source', |
| 62 | + parameters=types._CreateBatchJobParameters( |
| 63 | + model=_GEMINI_MODEL_FULL_NAME, |
| 64 | + src=types.BatchJobSource( |
| 65 | + vertex_dataset_name=_VERTEX_DATASET_INPUT_NAME, |
| 66 | + format='vertex-dataset', |
| 67 | + ), |
| 68 | + config={ |
| 69 | + 'dest': { |
| 70 | + 'vertex_dataset': _VERTEX_DATASET_DESTINATION, |
| 71 | + 'format': 'vertex-dataset', |
| 72 | + }, |
| 73 | + }, |
| 74 | + ), |
| 75 | + exception_if_mldev='one of', |
| 76 | + ), |
| 77 | + pytest_helper.TestTableItem( |
| 78 | + name='test_generate_content_with_vertex_dataset_source_dict', |
| 79 | + parameters=types._CreateBatchJobParameters( |
| 80 | + model=_GEMINI_MODEL_FULL_NAME, |
| 81 | + src={ |
| 82 | + 'vertex_dataset_name': _VERTEX_DATASET_INPUT_NAME, |
| 83 | + 'format': 'vertex-dataset', |
| 84 | + }, |
| 85 | + config={ |
| 86 | + 'dest': { |
| 87 | + 'vertex_dataset': _VERTEX_DATASET_DESTINATION, |
| 88 | + 'format': 'vertex-dataset', |
| 89 | + }, |
| 90 | + }, |
| 91 | + ), |
| 92 | + exception_if_mldev='one of', |
| 93 | + ), |
| 94 | +] |
| 95 | + |
| 96 | +pytestmark = [ |
| 97 | + pytest.mark.usefixtures('mock_timestamped_unique_name'), |
| 98 | + pytest_helper.setup( |
| 99 | + file=__file__, |
| 100 | + globals_for_file=globals(), |
| 101 | + test_method='batches.create', |
| 102 | + test_table=test_table, |
| 103 | + ), |
| 104 | +] |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.asyncio |
| 108 | +async def test_async_create(client): |
| 109 | + with pytest_helper.exception_if_mldev(client, ValueError): |
| 110 | + batch_job = await client.aio.batches.create( |
| 111 | + model=_GEMINI_MODEL, |
| 112 | + src=_VERTEX_DATASET_INPUT_NAME, |
| 113 | + config={ |
| 114 | + 'dest': { |
| 115 | + 'vertex_dataset': _VERTEX_DATASET_DESTINATION, |
| 116 | + 'format': 'vertex-dataset', |
| 117 | + }, |
| 118 | + }, |
| 119 | + ) |
| 120 | + |
| 121 | + assert batch_job.name.startswith('projects/') |
| 122 | + assert ( |
| 123 | + batch_job.model == _GEMINI_MODEL_FULL_NAME |
| 124 | + ) # Converted to Vertex full name. |
| 125 | + assert batch_job.src.vertex_dataset_name == _VERTEX_DATASET_INPUT_NAME |
| 126 | + assert batch_job.src.format == 'vertex-dataset' |
0 commit comments