@@ -242,10 +242,10 @@ def to_agent_engine(
242
242
agent_folder : str ,
243
243
temp_folder : str ,
244
244
adk_app : str ,
245
- project : str ,
246
- region : str ,
247
245
staging_bucket : str ,
248
246
trace_to_cloud : bool ,
247
+ project : Optional [str ] = None ,
248
+ region : Optional [str ] = None ,
249
249
display_name : Optional [str ] = None ,
250
250
description : Optional [str ] = None ,
251
251
requirements_file : Optional [str ] = None ,
@@ -287,7 +287,9 @@ def to_agent_engine(
287
287
If not specified, the `requirements.txt` file in the `agent_folder` will
288
288
be used.
289
289
env_file (str): The filepath to the `.env` file for environment variables.
290
- If not specified, the `.env` file in the `agent_folder` will be used.
290
+ If not specified, the `.env` file in the `agent_folder` will be used. The
291
+ values of `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` will be
292
+ overridden by `project` and `region` if they are specified.
291
293
"""
292
294
# remove temp_folder if it exists
293
295
if os .path .exists (temp_folder ):
@@ -306,13 +308,7 @@ def to_agent_engine(
306
308
from vertexai import agent_engines
307
309
308
310
sys .path .append (temp_folder )
309
-
310
- vertexai .init (
311
- project = _resolve_project (project ),
312
- location = region ,
313
- staging_bucket = staging_bucket ,
314
- )
315
- click .echo ('Vertex AI initialized.' )
311
+ project = _resolve_project (project )
316
312
317
313
click .echo ('Resolving files and dependencies...' )
318
314
if not requirements_file :
@@ -333,6 +329,37 @@ def to_agent_engine(
333
329
334
330
click .echo (f'Reading environment variables from { env_file } ' )
335
331
env_vars = dotenv_values (env_file )
332
+ if 'GOOGLE_CLOUD_PROJECT' in env_vars :
333
+ env_project = env_vars .pop ('GOOGLE_CLOUD_PROJECT' )
334
+ if env_project :
335
+ if project :
336
+ click .secho (
337
+ 'Ignoring GOOGLE_CLOUD_PROJECT in .env as `--project` was'
338
+ ' explicitly passed and takes precedence' ,
339
+ fg = 'yellow' ,
340
+ )
341
+ else :
342
+ project = env_project
343
+ click .echo (f'{ project = } set by GOOGLE_CLOUD_PROJECT in { env_file } ' )
344
+ if 'GOOGLE_CLOUD_LOCATION' in env_vars :
345
+ env_region = env_vars .pop ('GOOGLE_CLOUD_LOCATION' )
346
+ if env_region :
347
+ if region :
348
+ click .secho (
349
+ 'Ignoring GOOGLE_CLOUD_LOCATION in .env as `--region` was'
350
+ ' explicitly passed and takes precedence' ,
351
+ fg = 'yellow' ,
352
+ )
353
+ else :
354
+ region = env_region
355
+ click .echo (f'{ region = } set by GOOGLE_CLOUD_LOCATION in { env_file } ' )
356
+
357
+ vertexai .init (
358
+ project = project ,
359
+ location = region ,
360
+ staging_bucket = staging_bucket ,
361
+ )
362
+ click .echo ('Vertex AI initialized.' )
336
363
337
364
adk_app_file = f'{ adk_app } .py'
338
365
with open (
0 commit comments