Skip to content

Commit b3b7003

Browse files
wuliang229copybara-github
authored andcommitted
feat: support deploying config agent to Agent Engine in CLI
Both pure config and config with code are successfully deployed. PiperOrigin-RevId: 795645102
1 parent 22f34e9 commit b3b7003

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

src/google/adk/cli/cli_deploy.py

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,19 @@
6060
"""
6161

6262
_AGENT_ENGINE_APP_TEMPLATE = """
63-
from {app_name}.agent import root_agent
6463
from vertexai.preview.reasoning_engines import AdkApp
6564
65+
if {is_config_agent}:
66+
from google.adk.agents import config_agent_utils
67+
try:
68+
# This path is for local loading.
69+
root_agent = config_agent_utils.from_config("{agent_folder}/root_agent.yaml")
70+
except FileNotFoundError:
71+
# This path is used to support the file structure in Agent Engine.
72+
root_agent = config_agent_utils.from_config("./{temp_folder}/{app_name}/root_agent.yaml")
73+
else:
74+
from {app_name}.agent import root_agent
75+
6676
adk_app = AdkApp(
6777
agent=root_agent,
6878
enable_tracing={trace_to_cloud_option},
@@ -293,21 +303,26 @@ def to_agent_engine(
293303
code.
294304
temp_folder (str): The temp folder for the generated Agent Engine source
295305
files. It will be replaced with the generated files if it already exists.
296-
project (str): Google Cloud project id.
297-
region (str): Google Cloud region.
306+
adk_app (str): The name of the file (without .py) containing the AdkApp
307+
instance.
298308
staging_bucket (str): The GCS bucket for staging the deployment artifacts.
299309
trace_to_cloud (bool): Whether to enable Cloud Trace.
300-
agent_engine_id (str): The ID of the Agent Engine instance to update. If not
301-
specified, a new Agent Engine instance will be created.
302-
absolutize_imports (bool): Whether to absolutize imports. If True, all relative
303-
imports will be converted to absolute import statements. Default is True.
304-
requirements_file (str): The filepath to the `requirements.txt` file to use.
305-
If not specified, the `requirements.txt` file in the `agent_folder` will
306-
be used.
307-
env_file (str): The filepath to the `.env` file for environment variables.
308-
If not specified, the `.env` file in the `agent_folder` will be used. The
309-
values of `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` will be
310-
overridden by `project` and `region` if they are specified.
310+
agent_engine_id (str): Optional. The ID of the Agent Engine instance to
311+
update. If not specified, a new Agent Engine instance will be created.
312+
absolutize_imports (bool): Optional. Default is True. Whether to absolutize
313+
imports. If True, all relative imports will be converted to absolute
314+
import statements.
315+
project (str): Optional. Google Cloud project id.
316+
region (str): Optional. Google Cloud region.
317+
display_name (str): Optional. The display name of the Agent Engine.
318+
description (str): Optional. The description of the Agent Engine.
319+
requirements_file (str): Optional. The filepath to the `requirements.txt`
320+
file to use. If not specified, the `requirements.txt` file in the
321+
`agent_folder` will be used.
322+
env_file (str): Optional. The filepath to the `.env` file for environment
323+
variables. If not specified, the `.env` file in the `agent_folder` will be
324+
used. The values of `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION`
325+
will be overridden by `project` and `region` if they are specified.
311326
"""
312327
app_name = os.path.basename(agent_folder)
313328
agent_src_path = os.path.join(temp_folder, app_name)
@@ -388,12 +403,21 @@ def to_agent_engine(
388403
)
389404
click.echo('Vertex AI initialized.')
390405

406+
is_config_agent = False
407+
config_root_agent_file = os.path.join(agent_src_path, 'root_agent.yaml')
408+
if os.path.exists(config_root_agent_file):
409+
click.echo('Config agent detected.')
410+
is_config_agent = True
411+
391412
adk_app_file = os.path.join(temp_folder, f'{adk_app}.py')
392413
with open(adk_app_file, 'w', encoding='utf-8') as f:
393414
f.write(
394415
_AGENT_ENGINE_APP_TEMPLATE.format(
395416
app_name=app_name,
396417
trace_to_cloud_option=trace_to_cloud,
418+
is_config_agent=is_config_agent,
419+
temp_folder=temp_folder,
420+
agent_folder=agent_folder,
397421
)
398422
)
399423
click.echo(f'Created {adk_app_file}')
@@ -449,8 +473,8 @@ def to_agent_engine(
449473
if not agent_engine_id:
450474
agent_engines.create(**agent_config)
451475
else:
452-
name = f'projects/{project}/locations/{region}/reasoningEngines/{agent_engine_id}'
453-
agent_engines.update(resource_name=name, **agent_config)
476+
resource_name = f'projects/{project}/locations/{region}/reasoningEngines/{agent_engine_id}'
477+
agent_engines.update(resource_name=resource_name, **agent_config)
454478
finally:
455479
click.echo(f'Cleaning up the temp folder: {temp_folder}')
456480
shutil.rmtree(temp_folder)
@@ -485,7 +509,10 @@ def to_gke(
485509
cluster_name: The name of the GKE cluster.
486510
service_name: The service name in GKE.
487511
app_name: The name of the app, by default, it's basename of `agent_folder`.
488-
temp_folder: The local directory to use as a temporary workspace for preparing deployment artifacts. The tool populates this folder with a copy of the agent's source code and auto-generates necessary files like a Dockerfile and deployment.yaml.
512+
temp_folder: The local directory to use as a temporary workspace for
513+
preparing deployment artifacts. The tool populates this folder with a copy
514+
of the agent's source code and auto-generates necessary files like a
515+
Dockerfile and deployment.yaml.
489516
port: The port of the ADK api server.
490517
trace_to_cloud: Whether to enable Cloud Trace.
491518
with_ui: Whether to deploy with UI.

0 commit comments

Comments
 (0)