59
59
"""
60
60
61
61
_AGENT_ENGINE_APP_TEMPLATE = """
62
- from agent import root_agent
62
+ from {app_name}. agent import root_agent
63
63
from vertexai.preview.reasoning_engines import AdkApp
64
64
65
65
adk_app = AdkApp(
@@ -254,6 +254,7 @@ def to_agent_engine(
254
254
adk_app : str ,
255
255
staging_bucket : str ,
256
256
trace_to_cloud : bool ,
257
+ absolutize_imports : bool = True ,
257
258
project : Optional [str ] = None ,
258
259
region : Optional [str ] = None ,
259
260
display_name : Optional [str ] = None ,
@@ -293,6 +294,8 @@ def to_agent_engine(
293
294
region (str): Google Cloud region.
294
295
staging_bucket (str): The GCS bucket for staging the deployment artifacts.
295
296
trace_to_cloud (bool): Whether to enable Cloud Trace.
297
+ absolutize_imports (bool): Whether to absolutize imports. If True, all relative
298
+ imports will be converted to absolute import statements. Default is True.
296
299
requirements_file (str): The filepath to the `requirements.txt` file to use.
297
300
If not specified, the `requirements.txt` file in the `agent_folder` will
298
301
be used.
@@ -301,14 +304,16 @@ def to_agent_engine(
301
304
values of `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` will be
302
305
overridden by `project` and `region` if they are specified.
303
306
"""
304
- # remove temp_folder if it exists
305
- if os .path .exists (temp_folder ):
307
+ app_name = os .path .basename (agent_folder )
308
+ agent_src_path = os .path .join (temp_folder , app_name )
309
+ # remove agent_src_path if it exists
310
+ if os .path .exists (agent_src_path ):
306
311
click .echo ('Removing existing files' )
307
- shutil .rmtree (temp_folder )
312
+ shutil .rmtree (agent_src_path )
308
313
309
314
try :
310
315
click .echo ('Copying agent source code...' )
311
- shutil .copytree (agent_folder , temp_folder )
316
+ shutil .copytree (agent_folder , agent_src_path )
312
317
click .echo ('Copying agent source code complete.' )
313
318
314
319
click .echo ('Initializing Vertex AI...' )
@@ -317,13 +322,13 @@ def to_agent_engine(
317
322
import vertexai
318
323
from vertexai import agent_engines
319
324
320
- sys .path .append (temp_folder )
325
+ sys .path .append (temp_folder ) # To register the adk_app operations
321
326
project = _resolve_project (project )
322
327
323
328
click .echo ('Resolving files and dependencies...' )
324
329
if not requirements_file :
325
330
# Attempt to read requirements from requirements.txt in the dir (if any).
326
- requirements_txt_path = os .path .join (temp_folder , 'requirements.txt' )
331
+ requirements_txt_path = os .path .join (agent_src_path , 'requirements.txt' )
327
332
if not os .path .exists (requirements_txt_path ):
328
333
click .echo (f'Creating { requirements_txt_path } ...' )
329
334
with open (requirements_txt_path , 'w' , encoding = 'utf-8' ) as f :
@@ -333,7 +338,7 @@ def to_agent_engine(
333
338
env_vars = None
334
339
if not env_file :
335
340
# Attempt to read the env variables from .env in the dir (if any).
336
- env_file = os .path .join (temp_folder , '.env' )
341
+ env_file = os .path .join (agent_src_path , '.env' )
337
342
if os .path .exists (env_file ):
338
343
from dotenv import dotenv_values
339
344
@@ -371,21 +376,35 @@ def to_agent_engine(
371
376
)
372
377
click .echo ('Vertex AI initialized.' )
373
378
374
- adk_app_file = f'{ adk_app } .py'
375
- with open (
376
- os .path .join (temp_folder , adk_app_file ), 'w' , encoding = 'utf-8'
377
- ) as f :
379
+ adk_app_file = os .path .join (temp_folder , f'{ adk_app } .py' )
380
+ with open (adk_app_file , 'w' , encoding = 'utf-8' ) as f :
378
381
f .write (
379
382
_AGENT_ENGINE_APP_TEMPLATE .format (
380
- trace_to_cloud_option = trace_to_cloud
383
+ app_name = app_name ,
384
+ trace_to_cloud_option = trace_to_cloud ,
381
385
)
382
386
)
383
- click .echo (f'Created { os . path . join ( temp_folder , adk_app_file ) } ' )
387
+ click .echo (f'Created { adk_app_file } ' )
384
388
click .echo ('Files and dependencies resolved' )
389
+ if absolutize_imports :
390
+ for root , _ , files in os .walk (agent_src_path ):
391
+ for file in files :
392
+ if file .endswith ('.py' ):
393
+ absolutize_imports_path = os .path .join (root , file )
394
+ try :
395
+ click .echo (
396
+ f'Running `absolufy-imports { absolutize_imports_path } `'
397
+ )
398
+ subprocess .run (
399
+ ['absolufy-imports' , absolutize_imports_path ],
400
+ cwd = temp_folder ,
401
+ )
402
+ except Exception as e :
403
+ click .echo (f'The following exception was raised: { e } ' )
385
404
386
405
click .echo ('Deploying to agent engine...' )
387
406
agent_engine = agent_engines .ModuleAgent (
388
- module_name = adk_app ,
407
+ module_name = 'agent_engine_app' ,
389
408
agent_name = 'adk_app' ,
390
409
register_operations = {
391
410
'' : [
0 commit comments