first vommit

This commit is contained in:
Till-Immer committed 2026-07-04 14:31:50 +02:00
commit a04e68bc95
4926 files changed
+764440

No files matched your search

@@ -0,0 +1,16 @@
# 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.
#
@@ -0,0 +1,39 @@
# 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.
#
"""Tests for tunings.cancel()."""
from ... import types as genai_types
from .. import pytest_helper
test_table: list[pytest_helper.TestTableItem] = [
pytest_helper.TestTableItem(
name="test_cancel_vertex",
parameters=genai_types._CancelTuningJobParameters(
name="projects/801452371447/locations/us-central1/tuningJobs/2470687341547618304"
),
exception_if_mldev="Not Found",
),
]
pytestmark = pytest_helper.setup(
file=__file__,
globals_for_file=globals(),
test_method="tunings.cancel",
test_table=test_table,
)
pytest_plugins = ("pytest_asyncio",)
@@ -0,0 +1,106 @@
# 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.
#
"""Tests for tunings.tune()."""
import time
from ... import _replay_api_client
from ... import types as genai_types
from .. import pytest_helper
test_table: list[pytest_helper.TestTableItem] = []
pytestmark = pytest_helper.setup(
file=__file__,
globals_for_file=globals(),
test_method="tunings.tune",
test_table=test_table,
)
pytest_plugins = ("pytest_asyncio",)
def test_tune_until_success(client):
if client._api_client.vertexai:
job = client.tunings.tune(
base_model="gemini-2.5-flash-lite",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl",
),
config=genai_types.CreateTuningJobConfig(
epoch_count=1,
),
)
else:
# Remove GenAI SDK test since it is deprecated:
# https://ai.google.dev/gemini-api/docs/model-tuning
return
while not job.has_ended:
# Skipping the sleep for when in replay mode.
if client._api_client._mode not in ("replay", "auto"):
time.sleep(300)
job = client.tunings.get(name=job.name)
assert job.has_ended
assert job.has_succeeded
def test_continuous_tuning(client):
if not client._api_client.vertexai:
return
job = client.tunings.tune(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl",
),
config=genai_types.CreateTuningJobConfig(
epoch_count=1,
),
)
while not job.has_ended:
# Skipping the sleep for when in replay mode.
if client._api_client._mode not in ("replay", "auto"):
time.sleep(300)
job = client.tunings.get(name=job.name)
assert job.has_ended
assert job.has_succeeded
continuous_job = client.tunings.tune(
base_model=job.tuned_model.model,
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl",
),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="continuous tuning job",
epoch_count=1,
)
)
while not continuous_job.has_ended:
# Skipping the sleep for when in replay mode.
if client._api_client._mode not in ("replay", "auto"):
time.sleep(300)
continuous_job = client.tunings.get(name=continuous_job.name)
assert continuous_job.has_ended
assert continuous_job.has_succeeded
@@ -0,0 +1,67 @@
# 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.
#
"""Tests for tunings.get()."""
from ... import types as genai_types
from .. import pytest_helper
test_table: list[pytest_helper.TestTableItem] = [
pytest_helper.TestTableItem(
name="test_vertexai",
parameters=genai_types._GetTuningJobParameters(
name="projects/801452371447/locations/us-central1/tuningJobs/4303478340632707072"
),
exception_if_mldev="Not Found",
),
pytest_helper.TestTableItem(
name="test_vertexai_with_pretuned_model",
parameters=genai_types._GetTuningJobParameters(
name="projects/801452371447/locations/us-central1/tuningJobs/8520221517529743360",
),
exception_if_mldev="Not Found",
),
pytest_helper.TestTableItem(
name="test_mldev",
parameters=genai_types._GetTuningJobParameters(
name="tunedModels/generate-num-1896"
),
exception_if_vertex="Not Found",
),
]
pytestmark = pytest_helper.setup(
file=__file__,
globals_for_file=globals(),
test_method="tunings.get",
test_table=test_table,
)
pytest_plugins = ("pytest_asyncio",)
def test_helper_properties(client):
if client._api_client.vertexai:
job = client.tunings.get(
name="projects/801452371447/locations/us-central1/tuningJobs/4303478340632707072",
)
else:
job = client.tunings.get(
name="tunedModels/testdatasetexamples-model-j0fpgpaksvri"
)
assert job.has_ended
assert job.has_succeeded
@@ -0,0 +1,87 @@
# 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.
#
"""Tests for tunings.list()."""
import pytest
from ... import types as genai_types
from .. import pytest_helper
test_table: list[pytest_helper.TestTableItem] = [
pytest_helper.TestTableItem(
name='test_default',
parameters=genai_types._ListTuningJobsParameters(),
exception_if_mldev=(
'only supported in Gemini Enterprise Agent Platform'
),
),
pytest_helper.TestTableItem(
name='test_with_config',
parameters=genai_types._ListTuningJobsParameters(
config=genai_types.ListTuningJobsConfig(page_size=2)
),
exception_if_mldev=(
'only supported in Gemini Enterprise Agent Platform'
),
),
]
pytestmark = pytest_helper.setup(
file=__file__,
globals_for_file=globals(),
test_method='tunings.list',
test_table=test_table,
)
pytest_plugins = ('pytest_asyncio',)
def test_pager(client):
if not client._api_client.vertexai:
return
tuning_jobs = client.tunings.list(config={'page_size': 2})
assert 'content-type' in tuning_jobs.sdk_http_response.headers
assert tuning_jobs.name == 'tuning_jobs'
assert tuning_jobs.page_size == 2
assert len(tuning_jobs) <= 2
# Iterate through all the pages. Then next_page() should raise an exception.
for _ in tuning_jobs:
pass
with pytest.raises(IndexError, match='No more pages to fetch.'):
tuning_jobs.next_page()
@pytest.mark.asyncio
async def test_async_pager(client):
if not client._api_client.vertexai:
return
tuning_jobs = await client.aio.tunings.list(config={'page_size': 2})
assert 'Content-Type' in tuning_jobs.sdk_http_response.headers
assert tuning_jobs.name == 'tuning_jobs'
assert tuning_jobs.page_size == 2
assert len(tuning_jobs) <= 2
# Iterate through all the pages. Then next_page() should raise an exception.
async for _ in tuning_jobs:
pass
with pytest.raises(IndexError, match='No more pages to fetch.'):
await tuning_jobs.next_page()
@@ -0,0 +1,561 @@
# 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.
#
"""Tests for tunings.tune()."""
from ... import types as genai_types
from .. import pytest_helper
import pytest
VERTEX_HTTP_OPTIONS = {
'api_version': 'v1beta1',
'base_url': 'https://us-central1-autopush-aiplatform.sandbox.googleapis.com/',
}
evaluation_config=genai_types.EvaluationConfig(
metrics=[
genai_types.Metric(name="bleu", prompt_template="test prompt template")
],
output_config=genai_types.OutputConfig(
gcs_destination=genai_types.GcsDestination(
output_uri_prefix="gs://sararob_test/"
)
),
)
test_table: list[pytest_helper.TestTableItem] = [
pytest_helper.TestTableItem(
name="test_dataset_gcs_uri",
parameters=genai_types.CreateTuningJobParameters(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_pretuned_model",
parameters=genai_types.CreateTuningJobParameters(
base_model="projects/801452371447/locations/us-central1/models/9030969596621881344",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl",
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_pretuned_model_with_checkpoint_id",
parameters=genai_types.CreateTuningJobParameters(
base_model="projects/801452371447/locations/us-central1/models/9030969596621881344",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl",
),
config=genai_types.CreateTuningJobConfig(
pre_tuned_model_checkpoint_id="3",
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_simple_dpo",
parameters=genai_types.CreateTuningJobParameters(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="Model display name",
epoch_count=1,
method="PREFERENCE_TUNING",
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_dpo_with_beta",
parameters=genai_types.CreateTuningJobParameters(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="Model display name",
epoch_count=1,
method=genai_types.TuningMethod.PREFERENCE_TUNING,
beta=0.5,
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_non_pretuned_model_with_checkpoint_id",
parameters=genai_types.CreateTuningJobParameters(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
),
config=genai_types.CreateTuningJobConfig(
pre_tuned_model_checkpoint_id="3",
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_dataset_gcs_uri_all_parameters",
parameters=genai_types.CreateTuningJobParameters(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="Model display name",
epoch_count=1,
learning_rate_multiplier=1.0,
adapter_size="ADAPTER_SIZE_ONE",
validation_dataset=genai_types.TuningValidationDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_validation_data.jsonl",
),
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_dataset_vertex_dataset_resource",
parameters=genai_types.CreateTuningJobParameters(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(
vertex_dataset_resource="projects/801452371447/locations/us-central1/datasets/5779918772206829568",
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_dataset_dataset_resource_all_parameters",
parameters=genai_types.CreateTuningJobParameters(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(
vertex_dataset_resource="projects/801452371447/locations/us-central1/datasets/5779918772206829568",
),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="Model display name",
epoch_count=1,
learning_rate_multiplier=1.0,
adapter_size="ADAPTER_SIZE_ONE",
validation_dataset=genai_types.TuningValidationDataset(
vertex_dataset_resource="projects/801452371447/locations/us-central1/datasets/1168232753779441664",
),
labels={"testlabelkey": "testlabelvalue"},
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_distillation",
parameters=genai_types.CreateTuningJobParameters(
base_model="meta/llama3_1@llama-3.1-8b-instruct",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://nathreya-oss-tuning-sdk-test/distillation-openai-opposites.jsonl",
),
config=genai_types.CreateTuningJobConfig(
method="DISTILLATION",
base_teacher_model="deepseek-ai/deepseek-v3.1-maas",
epoch_count=20,
validation_dataset=genai_types.TuningValidationDataset(
gcs_uri="gs://nathreya-oss-tuning-sdk-test/distillation-val-openai-opposites.jsonl",
),
output_uri="gs://nathreya-oss-tuning-sdk-test/ayushagra-distillation-test-folder",
http_options=VERTEX_HTTP_OPTIONS,
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_oss_sft",
parameters=genai_types.CreateTuningJobParameters(
base_model="meta/llama3_1@llama-3.1-8b-instruct",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://nathreya-oss-tuning-sdk-test/distillation-openai-opposites.jsonl",
),
config=genai_types.CreateTuningJobConfig(
epoch_count=20,
validation_dataset=genai_types.TuningValidationDataset(
gcs_uri="gs://nathreya-oss-tuning-sdk-test/distillation-val-openai-opposites.jsonl",
),
custom_base_model="gs://nathreya-oss-tuning-sdk-test/ayushagra-distillation-test-folder/postprocess/node-0/checkpoints/final",
output_uri="gs://nathreya-oss-tuning-sdk-test/ayushagra-distillation-test",
http_options=VERTEX_HTTP_OPTIONS,
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_oss_sft_hyperparams",
parameters=genai_types.CreateTuningJobParameters(
base_model="meta/llama3_1@llama-3.1-8b-instruct",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://nathreya-oss-tuning-sdk-test/distillation-openai-opposites.jsonl",
),
config=genai_types.CreateTuningJobConfig(
epoch_count=20,
validation_dataset=genai_types.TuningValidationDataset(
gcs_uri="gs://nathreya-oss-tuning-sdk-test/distillation-val-openai-opposites.jsonl",
),
learning_rate=2.5e-4,
tuning_mode="TUNING_MODE_FULL",
custom_base_model="gs://nathreya-oss-tuning-sdk-test/ayushagra-distillation-test-folder/postprocess/node-0/checkpoints/final",
output_uri="gs://nathreya-oss-tuning-sdk-test/ayushagra-distillation-test",
http_options=VERTEX_HTTP_OPTIONS,
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_oss_distillation",
parameters=genai_types.CreateTuningJobParameters(
base_model="meta/llama3_1@llama-3.1-8b-instruct",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://nathreya-oss-tuning-sdk-test/distillation-openai-opposites.jsonl",
),
config=genai_types.CreateTuningJobConfig(
method="DISTILLATION",
base_teacher_model="deepseek-ai/deepseek-v3.1-maas",
epoch_count=20,
validation_dataset=genai_types.TuningValidationDataset(
gcs_uri="gs://nathreya-oss-tuning-sdk-test/distillation-val-openai-opposites.jsonl",
),
custom_base_model="gs://nathreya-oss-tuning-sdk-test/ayushagra-distillation-test-folder/postprocess/node-0/checkpoints/final",
output_uri="gs://nathreya-oss-tuning-sdk-test/ayushagra-distillation-test",
http_options=VERTEX_HTTP_OPTIONS,
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_oss_distillation_hyperparams",
parameters=genai_types.CreateTuningJobParameters(
base_model="qwen/qwen3@qwen3-4b",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://nathreya-oss-tuning-sdk-test/distillation-openai-opposites.jsonl",
),
config=genai_types.CreateTuningJobConfig(
method="DISTILLATION",
base_teacher_model="deepseek-ai/deepseek-r1-0528-maas",
learning_rate=1e-4,
batch_size=4,
output_uri="gs://nathreya-oss-tuning-sdk-test/ayushagra-distillation-test",
tuning_mode="TUNING_MODE_FULL",
http_options=VERTEX_HTTP_OPTIONS,
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_encryption_spec",
parameters=genai_types.CreateTuningJobParameters(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
),
config=genai_types.CreateTuningJobConfig(
encryption_spec=genai_types.EncryptionSpec(
kms_key_name="projects/vertex-sdk-dev/locations/us-central1/keyRings/ayush-test-key/cryptoKeys/ayush-key",
),
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_reinforcement",
parameters=genai_types.CreateTuningJobParameters(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="Model display name",
epoch_count=1,
method="REINFORCEMENT_TUNING",
adapter_size="ADAPTER_SIZE_ONE",
learning_rate_multiplier=1.0,
batch_size=4,
samples_per_prompt=4,
evaluate_interval=100,
checkpoint_interval=100,
max_output_tokens=2048,
reward_config=genai_types.SingleReinforcementTuningRewardConfig(
autorater_scorer=genai_types.ReinforcementTuningAutoraterScorer(
autorater_config=genai_types.AutoraterConfig(
autorater_model="test-model"
)
)
),
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
pytest_helper.TestTableItem(
name="test_tune_reinforcement_composite",
parameters=genai_types.CreateTuningJobParameters(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="Model display name",
epoch_count=1,
method="REINFORCEMENT_TUNING",
adapter_size="ADAPTER_SIZE_ONE",
learning_rate_multiplier=1.0,
composite_reward_config=genai_types.CompositeReinforcementTuningRewardConfig(
weighted_reward_configs=[
genai_types.CompositeReinforcementTuningRewardConfigWeightedRewardConfig(
weight=1.0,
reward_config=genai_types.SingleReinforcementTuningRewardConfig(
autorater_scorer=genai_types.ReinforcementTuningAutoraterScorer(
autorater_config=genai_types.AutoraterConfig(
autorater_model="test-model"
)
)
)
)
]
),
),
),
exception_if_mldev="parameter is only supported in Gemini Enterprise Agent Platform mode",
),
]
pytestmark = pytest_helper.setup(
file=__file__,
globals_for_file=globals(),
test_method="tunings.tune",
test_table=test_table,
)
pytest_plugins = ("pytest_asyncio",)
@pytest.mark.skipif(
"config.getoption('--private')",
reason="Skipping in pre-public tests"
)
def test_eval_config(client):
"""Tests tuning with eval config."""
if client._api_client.vertexai:
tuning_job = client.tunings.tune(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl"),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="tuning job with eval config",
epoch_count=1,
learning_rate_multiplier=1.0,
adapter_size="ADAPTER_SIZE_ONE",
validation_dataset=genai_types.TuningValidationDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_validation_data.jsonl"
),
evaluation_config=evaluation_config,
),
)
assert tuning_job.state == genai_types.JobState.JOB_STATE_PENDING
assert tuning_job.evaluation_config == evaluation_config
@pytest.mark.skipif(
"config.getoption('--private')",
reason="Skipping in pre-public tests"
)
def test_eval_config_with_metrics(client):
"""Tests tuning with eval config metrics."""
if client._api_client.vertexai:
evaluation_config=genai_types.EvaluationConfig(
metrics=[
genai_types.Metric(
name="prompt-relevance",
prompt_template="How well does the response address the prompt?: PROMPT: {request}\n RESPONSE: {response}\n",
return_raw_output=True,
judge_model_system_instruction="You are a cat. Make all evaluations from this perspective.",
),
genai_types.Metric(name='bleu'),
genai_types.Metric(name='rouge_1'),
],
output_config=genai_types.OutputConfig(
gcs_destination=genai_types.GcsDestination(
output_uri_prefix="gs://sararob_test/"
)
),
autorater_config=genai_types.AutoraterConfig(
sampling_count=1,
autorater_model="test-model",
),
)
tuning_job = client.tunings.tune(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl"),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="tuning job with eval config",
epoch_count=1,
learning_rate_multiplier=1.0,
adapter_size="ADAPTER_SIZE_ONE",
validation_dataset=genai_types.TuningValidationDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_validation_data.jsonl"
),
evaluation_config=evaluation_config,
),
)
assert tuning_job.state == genai_types.JobState.JOB_STATE_PENDING
@pytest.mark.skipif(
"config.getoption('--private')",
reason="Skipping in pre-public tests"
)
def test_eval_config_with_unified_metrics(client):
"""Tests tuning with eval config metrics."""
if client._api_client.vertexai:
evaluation_config=genai_types.EvaluationConfig(
metrics=[
genai_types.UnifiedMetric(
pointwise_metric_spec=genai_types.PointwiseMetricSpec(
metric_prompt_template=(
"How well does the response address the prompt?: "
"PROMPT: {request}\n RESPONSE: {response}\n"
),
system_instruction=(
"You are a cat. Make all evaluations from this perspective."
),
custom_output_format_config=genai_types.CustomOutputFormatConfig(
return_raw_output=True
),
)
),
genai_types.UnifiedMetric(
bleu_spec=genai_types.BleuSpec(use_effective_order=True)
),
genai_types.UnifiedMetric(
rouge_spec=genai_types.RougeSpec(rouge_type="rouge1")
),
],
output_config=genai_types.OutputConfig(
gcs_destination=genai_types.GcsDestination(
output_uri_prefix="gs://sararob_test/"
)
),
autorater_config=genai_types.AutoraterConfig(
sampling_count=1,
autorater_model="test-model",
),
inference_generation_config=genai_types.GenerationConfig(
temperature=0.5,
max_output_tokens=1024,
),
)
tuning_job = client.tunings.tune(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl"),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="tuning job with eval config",
epoch_count=1,
learning_rate_multiplier=1.0,
adapter_size="ADAPTER_SIZE_ONE",
validation_dataset=genai_types.TuningValidationDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_validation_data.jsonl"
),
evaluation_config=evaluation_config,
),
)
assert tuning_job.state == genai_types.JobState.JOB_STATE_PENDING
@pytest.mark.skipif(
"config.getoption('--private')",
reason="Skipping in pre-public tests"
)
def test_eval_config_with_metrics_dict(client):
"""Tests tuning with eval config metrics."""
if client._api_client.vertexai:
evaluation_config=genai_types.EvaluationConfig(
metrics = [
{
"name": "prompt-relevance",
"prompt_template": "How well does the response address the prompt?: PROMPT: {request}\n RESPONSE: {response}\n",
"return_raw_output": True,
"judge_model_system_instruction": "You are a cat. Make all evaluations from this perspective.",
},
{"name": "bleu"},
{"name": "rouge_1"},
{
"bleu_spec": {
"use_effective_order": True
}
},
],
output_config=genai_types.OutputConfig(
gcs_destination=genai_types.GcsDestination(
output_uri_prefix="gs://sararob_test/"
)
),
autorater_config=genai_types.AutoraterConfig(
sampling_count=1,
autorater_model="test-model",
),
)
tuning_job = client.tunings.tune(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl"),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="tuning job with eval config",
epoch_count=1,
learning_rate_multiplier=1.0,
adapter_size="ADAPTER_SIZE_ONE",
validation_dataset=genai_types.TuningValidationDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_validation_data.jsonl"
),
evaluation_config=evaluation_config,
),
)
assert tuning_job.state == genai_types.JobState.JOB_STATE_PENDING
@pytest.mark.skipif(
"config.getoption('--private')",
reason="Skipping in pre-public tests"
)
@pytest.mark.asyncio
async def test_eval_config_async(client):
"""Tests tuning with eval config."""
if client._api_client.vertexai:
tuning_job = await client.aio.tunings.tune(
base_model="gemini-2.5-flash",
training_dataset=genai_types.TuningDataset(gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl"),
config=genai_types.CreateTuningJobConfig(
tuned_model_display_name="tuning job with eval config",
epoch_count=1,
learning_rate_multiplier=1.0,
adapter_size="ADAPTER_SIZE_ONE",
validation_dataset=genai_types.TuningValidationDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_validation_data.jsonl"
),
evaluation_config=evaluation_config,
),
)
assert tuning_job.state == genai_types.JobState.JOB_STATE_PENDING
@@ -0,0 +1,187 @@
# 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.
#
"""Tests for tunings.validate_reward()."""
from ... import types as genai_types
from .. import pytest_helper
test_table: list[pytest_helper.TestTableItem] = [
pytest_helper.TestTableItem(
name="test_validate_reward_single_autorater",
parameters=genai_types._ValidateRewardParameters(
parent="projects/801452371447/locations/us-central1",
sample_response=genai_types.Content(
parts=[genai_types.Part(text="The answer is 42.")]
),
example=genai_types.ReinforcementTuningExample(
contents=[
genai_types.Content(
parts=[
genai_types.Part(text="What is the answer to life?")
]
)
],
),
single_reward_config=genai_types.SingleReinforcementTuningRewardConfig(
autorater_scorer=genai_types.ReinforcementTuningAutoraterScorer(
autorater_config=genai_types.AutoraterConfig(
autorater_model="test-model"
)
)
),
),
exception_if_mldev=(
"only supported in Gemini Enterprise Agent Platform mode"
),
),
pytest_helper.TestTableItem(
name="test_validate_reward_code_execution",
parameters=genai_types._ValidateRewardParameters(
parent="projects/801452371447/locations/us-central1",
sample_response=genai_types.Content(
parts=[genai_types.Part(text="print('hello')")]
),
example=genai_types.ReinforcementTuningExample(
contents=[
genai_types.Content(
parts=[
genai_types.Part(
text="Write a hello world program."
)
]
)
],
references={"reference": "hello"},
),
single_reward_config=genai_types.SingleReinforcementTuningRewardConfig(
reward_name="codeExecReward",
parse_response_config=genai_types.ReinforcementTuningParseResponseConfig(
parse_type="IDENTITY",
),
code_execution_reward_scorer=genai_types.ReinforcementTuningCodeExecutionRewardScorer(
python_code_snippet=(
"reward = 1.0 if response == references['reference']"
" else 0.0"
),
),
),
),
exception_if_mldev=(
"only supported in Gemini Enterprise Agent Platform mode"
),
),
pytest_helper.TestTableItem(
name="test_validate_reward_string_match",
parameters=genai_types._ValidateRewardParameters(
parent="projects/801452371447/locations/us-central1",
sample_response=genai_types.Content(
parts=[genai_types.Part(text="42")]
),
example=genai_types.ReinforcementTuningExample(
contents=[
genai_types.Content(
parts=[genai_types.Part(text="What is 6 times 7?")]
)
],
references={"answer": "42"},
system_instruction=genai_types.Content(
parts=[genai_types.Part(text="You are a math tutor.")]
),
),
single_reward_config=genai_types.SingleReinforcementTuningRewardConfig(
reward_name="stringMatchReward",
parse_response_config=genai_types.ReinforcementTuningParseResponseConfig(
parse_type="REGEX_EXTRACT",
regex_extract_expression=r"(\d+)",
),
string_match_reward_scorer=genai_types.ReinforcementTuningStringMatchRewardScorer(
correct_answer_reward=1.0,
wrong_answer_reward=-1.0,
string_match_expression=genai_types.ReinforcementTuningStringMatchRewardScorerStringMatchExpression(
match_operation="EXACT_MATCH",
expression="{{references.answer}}",
),
),
),
),
exception_if_mldev=(
"only supported in Gemini Enterprise Agent Platform mode"
),
),
pytest_helper.TestTableItem(
name="test_validate_reward_composite",
parameters=genai_types._ValidateRewardParameters(
parent="projects/801452371447/locations/us-central1",
sample_response=genai_types.Content(
parts=[genai_types.Part(text="The answer is 42.")]
),
example=genai_types.ReinforcementTuningExample(
contents=[
genai_types.Content(
parts=[
genai_types.Part(text="What is the answer to life?")
]
)
],
),
composite_reward_config=genai_types.CompositeReinforcementTuningRewardConfig(
weighted_reward_configs=[
genai_types.CompositeReinforcementTuningRewardConfigWeightedRewardConfig(
weight=0.7,
reward_config=genai_types.SingleReinforcementTuningRewardConfig(
reward_name="autoraterReward",
autorater_scorer=genai_types.ReinforcementTuningAutoraterScorer(
autorater_config=genai_types.AutoraterConfig(
autorater_model="test-model"
),
autorater_prompt=(
"Rate the response: {{response}}"
),
exact_match_scorer=genai_types.ReinforcementTuningAutoraterScorerExactMatchScorer(
correct_answer_reward=1.0,
wrong_answer_reward=0.0,
expression="good",
),
),
),
),
genai_types.CompositeReinforcementTuningRewardConfigWeightedRewardConfig(
weight=0.3,
reward_config=genai_types.SingleReinforcementTuningRewardConfig(
reward_name="codeReward",
code_execution_reward_scorer=genai_types.ReinforcementTuningCodeExecutionRewardScorer(
python_code_snippet="reward = 1.0",
),
),
),
]
),
),
exception_if_mldev=(
"only supported in Gemini Enterprise Agent Platform mode"
),
),
]
pytestmark = pytest_helper.setup(
file=__file__,
globals_for_file=globals(),
test_method="tunings.validate_reward",
test_table=test_table,
)
pytest_plugins = ("pytest_asyncio",)