Skip to content

Commit db489f8

Browse files
speedstorm1copybara-github
authored andcommitted
feat: allow users to configure max wait time for prompt management queries
PiperOrigin-RevId: 944229591
1 parent f5f750c commit db489f8

2 files changed

Lines changed: 130 additions & 29 deletions

File tree

agentplatform/_genai/prompts.py

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ def _restore_version(
11741174
request_dict = _common.convert_to_dict(request_dict)
11751175
request_dict = _common.encode_unserializable_types(request_dict)
11761176

1177-
response = self._api_client.request("get", path, request_dict, http_options)
1177+
response = self._api_client.request("post", path, request_dict, http_options)
11781178

11791179
response_dict = {} if not response.body else json.loads(response.body)
11801180

@@ -1565,6 +1565,9 @@ def create(
15651565
dataset_resource_name = self._wait_for_operation(
15661566
operation=create_prompt_dataset_operation,
15671567
timeout=config.timeout if config else 90,
1568+
max_wait_time=(
1569+
config.max_wait_time if config and config.max_wait_time else 60
1570+
),
15681571
)
15691572
dataset_id = dataset_resource_name.split("/")[-1]
15701573

@@ -1637,6 +1640,9 @@ def create_version(
16371640
dataset_resource_name = self._wait_for_operation(
16381641
operation=create_prompt_dataset_operation,
16391642
timeout=config.timeout if config else 90,
1643+
max_wait_time=(
1644+
config.max_wait_time if config and config.max_wait_time else 60
1645+
),
16401646
)
16411647
dataset_id = dataset_resource_name.split("/")[-1]
16421648

@@ -1661,6 +1667,9 @@ def create_version(
16611667
dataset_version_resource_name = self._wait_for_operation(
16621668
operation=create_dataset_version_operation,
16631669
timeout=config.timeout if config else 90,
1670+
max_wait_time=(
1671+
config.max_wait_time if config and config.max_wait_time else 60
1672+
),
16641673
)
16651674

16661675
# Step 4: Get the dataset version resource and return it with the prompt
@@ -1679,12 +1688,14 @@ def _wait_for_operation(
16791688
self,
16801689
operation: types.DatasetOperation,
16811690
timeout: int,
1691+
max_wait_time: int = 60,
16821692
) -> str:
16831693
"""Waits for a dataset operation to complete.
16841694
16851695
Args:
16861696
operation: The dataset operation to wait for.
16871697
timeout: The maximum time to wait for the operation to complete.
1698+
max_wait_time: The maximum interval between polling requests in seconds.
16881699
16891700
Returns:
16901701
The name of the Dataset resource from the operation result.
@@ -1706,7 +1717,6 @@ def _wait_for_operation(
17061717
start_time = time.time()
17071718
sleep_duration = 5
17081719
wait_multiplier = 2
1709-
max_wait_time = 60
17101720
previous_time = time.time()
17111721

17121722
while not done:
@@ -1715,11 +1725,6 @@ def _wait_for_operation(
17151725
"Create prompt operation did not complete within the"
17161726
f" specified timeout of {timeout} seconds."
17171727
)
1718-
current_time = time.time()
1719-
if current_time - previous_time >= sleep_duration:
1720-
sleep_duration = min(sleep_duration * wait_multiplier, max_wait_time)
1721-
previous_time = current_time
1722-
time.sleep(sleep_duration)
17231728
prompt_dataset_operation = self._get_dataset_operation(
17241729
dataset_id=dataset_id,
17251730
operation_id=operation_id,
@@ -1729,6 +1734,14 @@ def _wait_for_operation(
17291734
if hasattr(prompt_dataset_operation, "done")
17301735
else False
17311736
)
1737+
if not done:
1738+
current_time = time.time()
1739+
if current_time - previous_time >= sleep_duration:
1740+
sleep_duration = min(
1741+
sleep_duration * wait_multiplier, max_wait_time
1742+
)
1743+
previous_time = current_time
1744+
time.sleep(sleep_duration)
17321745
if (
17331746
not prompt_dataset_operation
17341747
or prompt_dataset_operation.response is None
@@ -1923,6 +1936,7 @@ def _wait_for_project_operation(
19231936
self,
19241937
operation: genai_types.ProjectOperation,
19251938
timeout: int,
1939+
max_wait_time: int = 60,
19261940
) -> None:
19271941
"""Waits for a dataset deletion operation to complete.
19281942
@@ -1931,6 +1945,7 @@ def _wait_for_project_operation(
19311945
Args:
19321946
operation: The project operation to wait for.
19331947
timeout: The maximum time to wait for the operation to complete.
1948+
max_wait_time: The maximum interval between polling requests in seconds.
19341949
Raises:
19351950
TimeoutError: If the operation does not complete within the timeout.
19361951
ValueError: If the operation fails.
@@ -1940,19 +1955,13 @@ def _wait_for_project_operation(
19401955
start_time = time.time()
19411956
sleep_duration = 5
19421957
wait_multiplier = 2
1943-
max_wait_time = 60
19441958
previous_time = time.time()
19451959
while not done:
19461960
if (time.time() - start_time) > timeout:
19471961
raise TimeoutError(
19481962
f"Delete operation did not complete within the"
19491963
f" specified timeout of {timeout} seconds."
19501964
)
1951-
current_time = time.time()
1952-
if current_time - previous_time >= sleep_duration:
1953-
sleep_duration = min(sleep_duration * wait_multiplier, max_wait_time)
1954-
previous_time = current_time
1955-
time.sleep(sleep_duration)
19561965
operations_module = operations.Operations(api_client_=self._api_client)
19571966

19581967
if operation.name is None:
@@ -1961,6 +1970,14 @@ def _wait_for_project_operation(
19611970
operation_id=operation.name.split("/")[-1],
19621971
)
19631972
done = (operation.done or False) if hasattr(operation, "done") else False
1973+
if not done:
1974+
current_time = time.time()
1975+
if current_time - previous_time >= sleep_duration:
1976+
sleep_duration = min(
1977+
sleep_duration * wait_multiplier, max_wait_time
1978+
)
1979+
previous_time = current_time
1980+
time.sleep(sleep_duration)
19641981
if hasattr(operation, "error") and operation.error is not None:
19651982
raise ValueError(f"Error in delete operation: {operation.error}")
19661983

@@ -1985,7 +2002,11 @@ def delete(
19852002
config=config,
19862003
)
19872004
self._wait_for_project_operation(
1988-
operation=delete_prompt_operation, timeout=config.timeout if config else 90
2005+
operation=delete_prompt_operation,
2006+
timeout=config.timeout if config and config.timeout else 90,
2007+
max_wait_time=(
2008+
config.max_wait_time if config and config.max_wait_time else 60
2009+
),
19892010
)
19902011
logger.info(f"Deleted prompt with id: {prompt_id}")
19912012

@@ -2013,7 +2034,11 @@ def delete_version(
20132034
)
20142035

20152036
self._wait_for_project_operation(
2016-
operation=delete_version_operation, timeout=config.timeout if config else 90
2037+
operation=delete_version_operation,
2038+
timeout=config.timeout if config and config.timeout else 90,
2039+
max_wait_time=(
2040+
config.max_wait_time if config and config.max_wait_time else 60
2041+
),
20172042
)
20182043
logger.info(
20192044
f"Deleted prompt version {version_id} from prompt with id: {prompt_id}"
@@ -2043,7 +2068,10 @@ def restore_version(
20432068
)
20442069
self._wait_for_project_operation(
20452070
operation=restore_prompt_operation,
2046-
timeout=90,
2071+
timeout=config.timeout if config and config.timeout else 90,
2072+
max_wait_time=(
2073+
config.max_wait_time if config and config.max_wait_time else 60
2074+
),
20472075
)
20482076
dataset_version_resource = self._get_dataset_version_resource(
20492077
dataset_id=prompt_id,
@@ -2369,7 +2397,7 @@ def update(
23692397

23702398
# Step 1: Update the dataset resource for the prompt and wait for the operation to complete.
23712399
updated_dataset_resource = self._update_dataset_resource(
2372-
name=f"projects/{self._api_client.project}/locations/{self._api_client.location}",
2400+
name=f"projects/{self._api_client.project}/locations/{self._api_client.location}/datasets/{prompt_id}",
23732401
dataset_id=prompt_id,
23742402
display_name=(
23752403
config.prompt_display_name
@@ -2401,6 +2429,9 @@ def update(
24012429
dataset_version_resource_name = self._wait_for_operation(
24022430
operation=create_dataset_version_operation,
24032431
timeout=config.timeout if config else 90,
2432+
max_wait_time=(
2433+
config.max_wait_time if config and config.max_wait_time else 60
2434+
),
24042435
)
24052436
dataset_version_id = dataset_version_resource_name.split("/")[-1]
24062437

@@ -3159,7 +3190,7 @@ async def _restore_version(
31593190
request_dict = _common.encode_unserializable_types(request_dict)
31603191

31613192
response = await self._api_client.async_request(
3162-
"get", path, request_dict, http_options
3193+
"post", path, request_dict, http_options
31633194
)
31643195

31653196
response_dict = {} if not response.body else json.loads(response.body)
@@ -3559,6 +3590,9 @@ async def create(
35593590
dataset_resource_name = await self._wait_for_operation(
35603591
operation=create_prompt_dataset_operation,
35613592
timeout=config.timeout if config else 90,
3593+
max_wait_time=(
3594+
config.max_wait_time if config and config.max_wait_time else 60
3595+
),
35623596
)
35633597
dataset_id = dataset_resource_name.split("/")[-1]
35643598

@@ -3630,6 +3664,9 @@ async def create_version(
36303664
dataset_resource_name = await self._wait_for_operation(
36313665
operation=create_prompt_dataset_operation,
36323666
timeout=config.timeout if config else 90,
3667+
max_wait_time=(
3668+
config.max_wait_time if config and config.max_wait_time else 60
3669+
),
36333670
)
36343671
dataset_id = dataset_resource_name.split("/")[-1]
36353672

@@ -3654,6 +3691,9 @@ async def create_version(
36543691
dataset_version_resource_name = await self._wait_for_operation(
36553692
operation=create_dataset_version_operation,
36563693
timeout=config.timeout if config else 90,
3694+
max_wait_time=(
3695+
config.max_wait_time if config and config.max_wait_time else 60
3696+
),
36573697
)
36583698

36593699
# Step 4: Get the dataset version resource and return it with the prompt
@@ -3709,7 +3749,7 @@ async def update(
37093749

37103750
# Step 1: Update the dataset resource for the prompt and wait for the operation to complete.
37113751
updated_dataset_resource = await self._update_dataset_resource(
3712-
name=f"projects/{self._api_client.project}/locations/{self._api_client.location}",
3752+
name=f"projects/{self._api_client.project}/locations/{self._api_client.location}/datasets/{prompt_id}",
37133753
dataset_id=prompt_id,
37143754
display_name=(
37153755
config.prompt_display_name
@@ -3741,6 +3781,9 @@ async def update(
37413781
dataset_version_resource_name = await self._wait_for_operation(
37423782
operation=create_dataset_version_operation,
37433783
timeout=config.timeout if config else 90,
3784+
max_wait_time=(
3785+
config.max_wait_time if config and config.max_wait_time else 60
3786+
),
37443787
)
37453788
dataset_version_id = dataset_version_resource_name.split("/")[-1]
37463789

@@ -3760,12 +3803,14 @@ async def _wait_for_operation(
37603803
self,
37613804
operation: types.DatasetOperation,
37623805
timeout: int,
3806+
max_wait_time: int = 60,
37633807
) -> str:
37643808
"""Waits for a dataset operation to complete.
37653809
37663810
Args:
37673811
operation: The dataset operation to wait for.
37683812
timeout: The maximum time to wait for the operation to complete.
3813+
max_wait_time: The maximum interval between polling requests in seconds.
37693814
37703815
Returns:
37713816
The name of the Dataset resource from the operation result.
@@ -3787,7 +3832,6 @@ async def _wait_for_operation(
37873832
start_time = time.time()
37883833
sleep_duration = 5
37893834
wait_multiplier = 2
3790-
max_wait_time = 60
37913835
previous_time = time.time()
37923836

37933837
while not done:
@@ -3885,6 +3929,7 @@ async def _wait_for_project_operation(
38853929
self,
38863930
operation: genai_types.ProjectOperation,
38873931
timeout: int,
3932+
max_wait_time: int = 60,
38883933
) -> None:
38893934
"""Waits for a dataset deletion operation to complete.
38903935
@@ -3893,6 +3938,7 @@ async def _wait_for_project_operation(
38933938
Args:
38943939
operation: The project operation to wait for.
38953940
timeout: The maximum time to wait for the operation to complete.
3941+
max_wait_time: The maximum interval between polling requests in seconds.
38963942
Raises:
38973943
TimeoutError: If the operation does not complete within the timeout.
38983944
ValueError: If the operation fails.
@@ -3902,19 +3948,13 @@ async def _wait_for_project_operation(
39023948
start_time = time.time()
39033949
sleep_duration = 5
39043950
wait_multiplier = 2
3905-
max_wait_time = 60
39063951
previous_time = time.time()
39073952
while not done:
39083953
if (time.time() - start_time) > timeout:
39093954
raise TimeoutError(
39103955
f"Delete operation did not complete within the"
39113956
f" specified timeout of {timeout} seconds."
39123957
)
3913-
current_time = time.time()
3914-
if current_time - previous_time >= sleep_duration:
3915-
sleep_duration = min(sleep_duration * wait_multiplier, max_wait_time)
3916-
previous_time = current_time
3917-
await asyncio.sleep(sleep_duration)
39183958
operations_module = operations.AsyncOperations(api_client_=self._api_client)
39193959

39203960
if operation.name is None:
@@ -3923,6 +3963,14 @@ async def _wait_for_project_operation(
39233963
operation_id=operation.name.split("/")[-1],
39243964
)
39253965
done = (operation.done or False) if hasattr(operation, "done") else False
3966+
if not done:
3967+
current_time = time.time()
3968+
if current_time - previous_time >= sleep_duration:
3969+
sleep_duration = min(
3970+
sleep_duration * wait_multiplier, max_wait_time
3971+
)
3972+
previous_time = current_time
3973+
await asyncio.sleep(sleep_duration)
39263974
if hasattr(operation, "error") and operation.error is not None:
39273975
raise ValueError(f"Error in delete operation: {operation.error}")
39283976

@@ -3947,7 +3995,11 @@ async def delete(
39473995
config=config,
39483996
)
39493997
await self._wait_for_project_operation(
3950-
operation=delete_prompt_operation, timeout=config.timeout if config else 90
3998+
operation=delete_prompt_operation,
3999+
timeout=config.timeout if config and config.timeout else 90,
4000+
max_wait_time=(
4001+
config.max_wait_time if config and config.max_wait_time else 60
4002+
),
39514003
)
39524004
logger.info(f"Deleted prompt with id: {prompt_id}")
39534005

@@ -3975,7 +4027,11 @@ async def delete_version(
39754027
)
39764028

39774029
await self._wait_for_project_operation(
3978-
operation=delete_version_operation, timeout=config.timeout if config else 90
4030+
operation=delete_version_operation,
4031+
timeout=config.timeout if config and config.timeout else 90,
4032+
max_wait_time=(
4033+
config.max_wait_time if config and config.max_wait_time else 60
4034+
),
39794035
)
39804036
logger.info(
39814037
f"Deleted prompt version {version_id} from prompt with id: {prompt_id}"
@@ -4114,7 +4170,10 @@ async def restore_version(
41144170
)
41154171
await self._wait_for_project_operation(
41164172
operation=restore_prompt_operation,
4117-
timeout=90,
4173+
timeout=config.timeout if config and config.timeout else 90,
4174+
max_wait_time=(
4175+
config.max_wait_time if config and config.max_wait_time else 60
4176+
),
41184177
)
41194178
dataset_version_resource = await self._get_dataset_version_resource(
41204179
dataset_id=prompt_id,

0 commit comments

Comments
 (0)