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
180 changes: 161 additions & 19 deletions agentplatform/_genai/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,9 @@ def _optimize(
self._api_client._verify_response(return_value)
return return_value

_DEFAULT_TIMEOUT = 90
_DEFAULT_MAX_WAIT_TIME = 60

def create(
self,
*,
Expand Down Expand Up @@ -1564,7 +1567,16 @@ def create(
)
dataset_resource_name = self._wait_for_operation(
operation=create_prompt_dataset_operation,
timeout=config.timeout if config else 90,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
dataset_id = dataset_resource_name.split("/")[-1]

Expand Down Expand Up @@ -1636,7 +1648,16 @@ def create_version(
)
dataset_resource_name = self._wait_for_operation(
operation=create_prompt_dataset_operation,
timeout=config.timeout if config else 90,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
dataset_id = dataset_resource_name.split("/")[-1]

Expand All @@ -1660,7 +1681,16 @@ def create_version(
)
dataset_version_resource_name = self._wait_for_operation(
operation=create_dataset_version_operation,
timeout=config.timeout if config else 90,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)

# Step 4: Get the dataset version resource and return it with the prompt
Expand All @@ -1679,12 +1709,14 @@ def _wait_for_operation(
self,
operation: types.DatasetOperation,
timeout: int,
max_wait_time: int = _DEFAULT_MAX_WAIT_TIME,
) -> str:
"""Waits for a dataset operation to complete.

Args:
operation: The dataset operation to wait for.
timeout: The maximum time to wait for the operation to complete.
max_wait_time: The maximum interval between polling requests in seconds.

Returns:
The name of the Dataset resource from the operation result.
Expand All @@ -1706,7 +1738,6 @@ def _wait_for_operation(
start_time = time.time()
sleep_duration = 5
wait_multiplier = 2
max_wait_time = 60
previous_time = time.time()

while not done:
Expand Down Expand Up @@ -1923,6 +1954,7 @@ def _wait_for_project_operation(
self,
operation: genai_types.ProjectOperation,
timeout: int,
max_wait_time: int = _DEFAULT_MAX_WAIT_TIME,
) -> None:
"""Waits for a dataset deletion operation to complete.

Expand All @@ -1931,6 +1963,7 @@ def _wait_for_project_operation(
Args:
operation: The project operation to wait for.
timeout: The maximum time to wait for the operation to complete.
max_wait_time: The maximum interval between polling requests in seconds.
Raises:
TimeoutError: If the operation does not complete within the timeout.
ValueError: If the operation fails.
Expand All @@ -1940,7 +1973,6 @@ def _wait_for_project_operation(
start_time = time.time()
sleep_duration = 5
wait_multiplier = 2
max_wait_time = 60
previous_time = time.time()
while not done:
if (time.time() - start_time) > timeout:
Expand Down Expand Up @@ -1985,7 +2017,17 @@ def delete(
config=config,
)
self._wait_for_project_operation(
operation=delete_prompt_operation, timeout=config.timeout if config else 90
operation=delete_prompt_operation,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
logger.info(f"Deleted prompt with id: {prompt_id}")

Expand Down Expand Up @@ -2013,7 +2055,17 @@ def delete_version(
)

self._wait_for_project_operation(
operation=delete_version_operation, timeout=config.timeout if config else 90
operation=delete_version_operation,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
logger.info(
f"Deleted prompt version {version_id} from prompt with id: {prompt_id}"
Expand All @@ -2040,10 +2092,20 @@ def restore_version(
restore_prompt_operation = self._restore_version(
dataset_id=prompt_id,
version_id=version_id,
config=config,
)
self._wait_for_project_operation(
operation=restore_prompt_operation,
timeout=90,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
dataset_version_resource = self._get_dataset_version_resource(
dataset_id=prompt_id,
Expand All @@ -2070,7 +2132,7 @@ def _wait_for_completion(self, job_name: str) -> types.CustomJob:

log_wait = 5
wait_multiplier = 2
max_wait_time = 60
max_wait_time = self._DEFAULT_MAX_WAIT_TIME
previous_time = time.time()

job = self._get_custom_job(name=job_name)
Expand Down Expand Up @@ -2400,7 +2462,16 @@ def update(
)
dataset_version_resource_name = self._wait_for_operation(
operation=create_dataset_version_operation,
timeout=config.timeout if config else 90,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
dataset_version_id = dataset_version_resource_name.split("/")[-1]

Expand Down Expand Up @@ -3499,6 +3570,9 @@ async def _optimize(
self._api_client._verify_response(return_value)
return return_value

_DEFAULT_TIMEOUT = 90
_DEFAULT_MAX_WAIT_TIME = 60

async def create(
self,
*,
Expand Down Expand Up @@ -3558,7 +3632,16 @@ async def create(
)
dataset_resource_name = await self._wait_for_operation(
operation=create_prompt_dataset_operation,
timeout=config.timeout if config else 90,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
dataset_id = dataset_resource_name.split("/")[-1]

Expand Down Expand Up @@ -3629,7 +3712,16 @@ async def create_version(
)
dataset_resource_name = await self._wait_for_operation(
operation=create_prompt_dataset_operation,
timeout=config.timeout if config else 90,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
dataset_id = dataset_resource_name.split("/")[-1]

Expand All @@ -3653,7 +3745,16 @@ async def create_version(
)
dataset_version_resource_name = await self._wait_for_operation(
operation=create_dataset_version_operation,
timeout=config.timeout if config else 90,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)

# Step 4: Get the dataset version resource and return it with the prompt
Expand Down Expand Up @@ -3740,7 +3841,16 @@ async def update(
)
dataset_version_resource_name = await self._wait_for_operation(
operation=create_dataset_version_operation,
timeout=config.timeout if config else 90,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
dataset_version_id = dataset_version_resource_name.split("/")[-1]

Expand All @@ -3760,12 +3870,14 @@ async def _wait_for_operation(
self,
operation: types.DatasetOperation,
timeout: int,
max_wait_time: int = _DEFAULT_MAX_WAIT_TIME,
) -> str:
"""Waits for a dataset operation to complete.

Args:
operation: The dataset operation to wait for.
timeout: The maximum time to wait for the operation to complete.
max_wait_time: The maximum interval between polling requests in seconds.

Returns:
The name of the Dataset resource from the operation result.
Expand All @@ -3787,7 +3899,6 @@ async def _wait_for_operation(
start_time = time.time()
sleep_duration = 5
wait_multiplier = 2
max_wait_time = 60
previous_time = time.time()

while not done:
Expand Down Expand Up @@ -3885,6 +3996,7 @@ async def _wait_for_project_operation(
self,
operation: genai_types.ProjectOperation,
timeout: int,
max_wait_time: int = _DEFAULT_MAX_WAIT_TIME,
) -> None:
"""Waits for a dataset deletion operation to complete.

Expand All @@ -3893,6 +4005,7 @@ async def _wait_for_project_operation(
Args:
operation: The project operation to wait for.
timeout: The maximum time to wait for the operation to complete.
max_wait_time: The maximum interval between polling requests in seconds.
Raises:
TimeoutError: If the operation does not complete within the timeout.
ValueError: If the operation fails.
Expand All @@ -3902,7 +4015,6 @@ async def _wait_for_project_operation(
start_time = time.time()
sleep_duration = 5
wait_multiplier = 2
max_wait_time = 60
previous_time = time.time()
while not done:
if (time.time() - start_time) > timeout:
Expand Down Expand Up @@ -3947,7 +4059,17 @@ async def delete(
config=config,
)
await self._wait_for_project_operation(
operation=delete_prompt_operation, timeout=config.timeout if config else 90
operation=delete_prompt_operation,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
logger.info(f"Deleted prompt with id: {prompt_id}")

Expand Down Expand Up @@ -3975,7 +4097,17 @@ async def delete_version(
)

await self._wait_for_project_operation(
operation=delete_version_operation, timeout=config.timeout if config else 90
operation=delete_version_operation,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
logger.info(
f"Deleted prompt version {version_id} from prompt with id: {prompt_id}"
Expand Down Expand Up @@ -4111,10 +4243,20 @@ async def restore_version(
restore_prompt_operation = await self._restore_version(
dataset_id=prompt_id,
version_id=version_id,
config=config,
)
await self._wait_for_project_operation(
operation=restore_prompt_operation,
timeout=90,
timeout=(
config.timeout
if config and config.timeout is not None
else self._DEFAULT_TIMEOUT
),
max_wait_time=(
config.max_wait_time
if config and config.max_wait_time is not None
else self._DEFAULT_MAX_WAIT_TIME
),
)
dataset_version_resource = await self._get_dataset_version_resource(
dataset_id=prompt_id,
Expand Down
Loading
Loading