11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
+ import asyncio
14
15
import logging
15
16
import re
16
- import time
17
17
from typing import Any
18
18
from typing import Optional
19
19
@@ -69,7 +69,8 @@ async def create_session(
69
69
if state :
70
70
session_json_dict ['session_state' ] = state
71
71
72
- api_response = self .api_client .request (
72
+ api_client = _get_api_client (self .project , self .location )
73
+ api_response = await api_client .async_request (
73
74
http_method = 'POST' ,
74
75
path = f'reasoningEngines/{ reasoning_engine_id } /sessions' ,
75
76
request_dict = session_json_dict ,
@@ -81,7 +82,7 @@ async def create_session(
81
82
82
83
max_retry_attempt = 5
83
84
while max_retry_attempt >= 0 :
84
- lro_response = self . api_client .request (
85
+ lro_response = await api_client .async_request (
85
86
http_method = 'GET' ,
86
87
path = f'operations/{ operation_id } ' ,
87
88
request_dict = {},
@@ -90,11 +91,11 @@ async def create_session(
90
91
if lro_response .get ('done' , None ):
91
92
break
92
93
93
- time .sleep (1 )
94
+ await asyncio .sleep (1 )
94
95
max_retry_attempt -= 1
95
96
96
97
# Get session resource
97
- get_session_api_response = self . api_client .request (
98
+ get_session_api_response = await api_client .async_request (
98
99
http_method = 'GET' ,
99
100
path = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } ' ,
100
101
request_dict = {},
@@ -124,7 +125,8 @@ async def get_session(
124
125
reasoning_engine_id = _parse_reasoning_engine_id (app_name )
125
126
126
127
# Get session resource
127
- get_session_api_response = self .api_client .request (
128
+ api_client = _get_api_client (self .project , self .location )
129
+ get_session_api_response = await api_client .async_request (
128
130
http_method = 'GET' ,
129
131
path = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } ' ,
130
132
request_dict = {},
@@ -142,7 +144,7 @@ async def get_session(
142
144
last_update_time = update_timestamp ,
143
145
)
144
146
145
- list_events_api_response = self . api_client .request (
147
+ list_events_api_response = await api_client .async_request (
146
148
http_method = 'GET' ,
147
149
path = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } /events' ,
148
150
request_dict = {},
@@ -181,7 +183,8 @@ async def list_sessions(
181
183
) -> ListSessionsResponse :
182
184
reasoning_engine_id = _parse_reasoning_engine_id (app_name )
183
185
184
- api_response = self .api_client .request (
186
+ api_client = _get_api_client (self .project , self .location )
187
+ api_response = await api_client .async_request (
185
188
http_method = 'GET' ,
186
189
path = f'reasoningEngines/{ reasoning_engine_id } /sessions?filter=user_id={ user_id } ' ,
187
190
request_dict = {},
@@ -207,7 +210,8 @@ async def delete_session(
207
210
self , * , app_name : str , user_id : str , session_id : str
208
211
) -> None :
209
212
reasoning_engine_id = _parse_reasoning_engine_id (app_name )
210
- self .api_client .request (
213
+ api_client = _get_api_client (self .project , self .location )
214
+ await api_client .async_request (
211
215
http_method = 'DELETE' ,
212
216
path = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } ' ,
213
217
request_dict = {},
@@ -219,15 +223,25 @@ async def append_event(self, session: Session, event: Event) -> Event:
219
223
await super ().append_event (session = session , event = event )
220
224
221
225
reasoning_engine_id = _parse_reasoning_engine_id (session .app_name )
222
- self .api_client .request (
226
+ api_client = _get_api_client (self .project , self .location )
227
+ await api_client .async_request (
223
228
http_method = 'POST' ,
224
229
path = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session .id } :appendEvent' ,
225
230
request_dict = _convert_event_to_json (event ),
226
231
)
227
-
228
232
return event
229
233
230
234
235
+ def _get_api_client (project : str , location : str ):
236
+ """Instantiates an API client for the given project and location.
237
+
238
+ It needs to be instantiated inside each request so that the event loop
239
+ management.
240
+ """
241
+ client = genai .Client (vertexai = True , project = project , location = location )
242
+ return client ._api_client
243
+
244
+
231
245
def _convert_event_to_json (event : Event ):
232
246
metadata_json = {
233
247
'partial' : event .partial ,
0 commit comments