Skip to content

Commit a38d3c8

Browse files
sararobcopybara-github
authored andcommitted
chore: GenAI SDK client - Add async replay test for Prompt Optimizer
PiperOrigin-RevId: 783037244
1 parent 6cc1758 commit a38d3c8

2 files changed

Lines changed: 80 additions & 24 deletions

File tree

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

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,25 @@
1818

1919
from tests.unit.vertexai.genai.replays import pytest_helper
2020
from vertexai._genai import types
21+
import pytest
2122

2223

23-
# If you re-record this test, you will need to update the replay file to
24-
# include the placeholder values for config path and service account
25-
def test_optimize(client):
26-
"""Tests the optimize request parameters method."""
27-
24+
def _raise_for_unset_env_vars() -> None:
2825
if not os.environ.get("VAPO_CONFIG_PATH"):
2926
raise ValueError("VAPO_CONFIG_PATH environment variable is not set.")
3027
if not os.environ.get("VAPO_SERVICE_ACCOUNT_PROJECT_NUMBER"):
3128
raise ValueError(
3229
"VAPO_SERVICE_ACCOUNT_PROJECT_NUMBER " "environment variable is not set."
3330
)
3431

32+
33+
# If you re-record this test, you will need to update the replay file to
34+
# include the placeholder values for config path and service account
35+
def test_optimize(client):
36+
"""Tests the optimize request parameters method."""
37+
38+
_raise_for_unset_env_vars()
39+
3540
config = types.PromptOptimizerVAPOConfig(
3641
config_path=os.environ.get("VAPO_CONFIG_PATH"),
3742
wait_for_completion=True,
@@ -53,3 +58,46 @@ def test_optimize(client):
5358
globals_for_file=globals(),
5459
test_method="prompt_optimizer.optimize",
5560
)
61+
62+
63+
pytest_plugins = ("pytest_asyncio",)
64+
65+
66+
@pytest.mark.asyncio
67+
async def test_optimize_async(client):
68+
_raise_for_unset_env_vars()
69+
70+
config = types.PromptOptimizerVAPOConfig(
71+
config_path=os.environ.get("VAPO_CONFIG_PATH"),
72+
service_account_project_number=os.environ.get(
73+
"VAPO_SERVICE_ACCOUNT_PROJECT_NUMBER"
74+
),
75+
optimizer_job_display_name="optimizer_job_test",
76+
)
77+
job = await client.aio.prompt_optimizer.optimize(
78+
method="vapo",
79+
config=config,
80+
)
81+
assert isinstance(job, types.CustomJob)
82+
assert job.state == types.JobState.JOB_STATE_PENDING
83+
84+
85+
@pytest.mark.asyncio
86+
async def test_optimize_async_with_config_wait_for_completion(client, caplog):
87+
_raise_for_unset_env_vars()
88+
89+
config = types.PromptOptimizerVAPOConfig(
90+
config_path=os.environ.get("VAPO_CONFIG_PATH"),
91+
service_account_project_number=os.environ.get(
92+
"VAPO_SERVICE_ACCOUNT_PROJECT_NUMBER"
93+
),
94+
optimizer_job_display_name="optimizer_job_test",
95+
wait_for_completion=True,
96+
)
97+
job = await client.aio.prompt_optimizer.optimize(
98+
method="vapo",
99+
config=config,
100+
)
101+
assert isinstance(job, types.CustomJob)
102+
assert job.state == types.JobState.JOB_STATE_PENDING
103+
assert "Ignoring wait_for_completion=True" in caplog.text

vertexai/_genai/prompt_optimizer.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -825,39 +825,47 @@ async def _get_custom_job(
825825
self._api_client._verify_response(return_value)
826826
return return_value
827827

828+
# Todo: b/428953357 - Add example in the README.
828829
async def optimize(
829830
self,
830831
method: str,
831832
config: types.PromptOptimizerVAPOConfigOrDict,
832833
) -> types.CustomJob:
833834
"""Call async Vertex AI Prompt Optimizer (VAPO).
834835
835-
# Todo: b/428953357 - Add example in the README.
836-
Example usage:
837-
client = vertexai.Client(project=PROJECT_NAME, location='us-central1')
838-
vapo_config = vertexai.types.PromptOptimizerVAPOConfig(
839-
config_path="gs://you-bucket-name/your-config.json",
840-
service_account=service_account,
841-
wait_for_completion=True
842-
)
843-
job = await client.aio.prompt_optimizer.optimize(
844-
method="vapo", config=vapo_config)
845-
846-
Args:
847-
method: The method for optimizing multiple prompts (currently only
848-
vapo is supported).
849-
config: PromptOptimizerVAPOConfig instance containing the
850-
configuration for prompt optimization.
851-
852-
Returns:
853-
The custom job that was created.
836+
Note: The `wait_for_completion` parameter in the config will be
837+
ignored when using the AsyncClient, as it is not supported.
838+
839+
Example usage:
840+
client = vertexai.Client(project=PROJECT_NAME, location='us-central1')
841+
vapo_config = vertexai.types.PromptOptimizerVAPOConfig(
842+
config_path="gs://you-bucket-name/your-config.json",
843+
service_account=service_account,
844+
)
845+
job = await client.aio.prompt_optimizer.optimize(
846+
method="vapo", config=vapo_config)
847+
848+
Args:
849+
method: The method for optimizing multiple prompts (currently only
850+
vapo is supported).
851+
config: PromptOptimizerVAPOConfig instance containing the
852+
configuration for prompt optimization.
853+
854+
Returns:
855+
The custom job that was created.
854856
"""
855857
if method != "vapo":
856858
raise ValueError("Only vapo methods is currently supported.")
857859

858860
if isinstance(config, dict):
859861
config = types.PromptOptimizerVAPOConfig(**config)
860862

863+
if config.wait_for_completion:
864+
logger.info(
865+
"Ignoring wait_for_completion=True since the AsyncClient does"
866+
" not support it."
867+
)
868+
861869
if config.optimizer_job_display_name:
862870
display_name = config.optimizer_job_display_name
863871
else:

0 commit comments

Comments
 (0)