Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# pylint: disable=protected-access,bad-continuation,missing-function-docstring

from tests.unit.vertexai.genai.replays import pytest_helper
from vertexai._genai import types

import pytest

METADATA_SCHEMA_URI = (
"gs://google-cloud-aiplatform/schema/dataset/metadata/multimodal_1.0.0.yaml"
)
BIGQUERY_TABLE_NAME = "vertex-sdk-dev.multimodal_dataset.test-table"
DATASET = "8810841321427173376"


def test_assemble_dataset(client):
operation = client.datasets._assemble_multimodal_dataset(
name=DATASET,
gemini_request_read_config={
"template_config": {
"field_mapping": {"question": "questionColumn"},
},
},
)
assert isinstance(operation, types.MultimodalDatasetOperation)


def test_assemble_dataset_public(client):
bigquery_destination = client.datasets.assemble(
name=DATASET,
template_config=types.GeminiTemplateConfig(
gemini_example=types.GeminiExample(
model="gemini-1.5-flash",
contents=[
{
"role": "user",
"parts": [{"text": "What is the capital of {name}?"}],
}
],
),
),
)
assert bigquery_destination.startswith(f"bq://{BIGQUERY_TABLE_NAME}")


pytestmark = pytest_helper.setup(
file=__file__,
globals_for_file=globals(),
)

pytest_plugins = ("pytest_asyncio",)


@pytest.mark.asyncio
async def test_assemble_dataset_async(client):
operation = await client.aio.datasets._assemble_multimodal_dataset(
name=DATASET,
gemini_request_read_config={
"template_config": {
"field_mapping": {"question": "questionColumn"},
},
},
)
assert isinstance(operation, types.MultimodalDatasetOperation)


@pytest.mark.asyncio
async def test_assemble_dataset_public_async(client):
bigquery_destination = await client.aio.datasets.assemble(
name=DATASET,
template_config=types.GeminiTemplateConfig(
gemini_example=types.GeminiExample(
model="gemini-1.5-flash",
contents=[
{
"role": "user",
"parts": [{"text": "What is the capital of {name}?"}],
}
],
),
),
)
assert bigquery_destination.startswith(f"bq://{BIGQUERY_TABLE_NAME}")
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def test_create_dataset_from_bigquery(client):
)
assert isinstance(dataset, types.MultimodalDataset)
assert dataset.display_name == "test-from-bigquery"
assert dataset.metadata.input_config.bigquery_source.uri == (
f"bq://{BIGQUERY_TABLE_NAME}"
)


def test_create_dataset_from_bigquery_without_bq_prefix(client):
Expand All @@ -70,6 +73,9 @@ def test_create_dataset_from_bigquery_without_bq_prefix(client):
)
assert isinstance(dataset, types.MultimodalDataset)
assert dataset.display_name == "test-from-bigquery"
assert dataset.metadata.input_config.bigquery_source.uri == (
f"bq://{BIGQUERY_TABLE_NAME}"
)


pytestmark = pytest_helper.setup(
Expand Down Expand Up @@ -111,6 +117,9 @@ async def test_create_dataset_from_bigquery_async(client):
)
assert isinstance(dataset, types.MultimodalDataset)
assert dataset.display_name == "test-from-bigquery"
assert dataset.metadata.input_config.bigquery_source.uri == (
f"bq://{BIGQUERY_TABLE_NAME}"
)


@pytest.mark.asyncio
Expand All @@ -129,6 +138,9 @@ async def test_create_dataset_from_bigquery_async_with_timeout(client):
)
assert isinstance(dataset, types.MultimodalDataset)
assert dataset.display_name == "test-from-bigquery"
assert dataset.metadata.input_config.bigquery_source.uri == (
f"bq://{BIGQUERY_TABLE_NAME}"
)


@pytest.mark.asyncio
Expand All @@ -146,3 +158,6 @@ async def test_create_dataset_from_bigquery_async_without_bq_prefix(client):
)
assert isinstance(dataset, types.MultimodalDataset)
assert dataset.display_name == "test-from-bigquery"
assert dataset.metadata.input_config.bigquery_source.uri == (
f"bq://{BIGQUERY_TABLE_NAME}"
)
16 changes: 16 additions & 0 deletions vertexai/_genai/_datasets_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,23 @@
#
"""Utility functions for multimodal dataset."""

from typing import Any, TypeVar, Type
from vertexai._genai.types import common
from pydantic import BaseModel

METADATA_SCHEMA_URI = (
"gs://google-cloud-aiplatform/schema/dataset/metadata/multimodal_1.0.0.yaml"
)

T = TypeVar("T", bound=BaseModel)


def create_from_response(model_type: Type[T], response: dict[str, Any]) -> T:
"""Creates a model from a response."""
model_field_names = model_type.model_fields.keys()
filtered_response = {}
for key, value in response.items():
snake_key = common.camel_to_snake(key)
if snake_key in model_field_names:
filtered_response[snake_key] = value
return model_type(**filtered_response)
Loading
Loading