File size: 11,191 Bytes
3d142aa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import datetime
import importlib
import inspect
import json
import logging
import warnings
from typing import Any
import click
import google.auth
import vertexai
from vertexai._genai import _agent_engines_utils
from vertexai._genai.types import AgentEngine, AgentEngineConfig
# Suppress google-cloud-storage version compatibility warning
warnings.filterwarnings(
"ignore", category=FutureWarning, module="google.cloud.aiplatform"
)
def generate_class_methods_from_agent(agent_instance: Any) -> list[dict[str, Any]]:
"""Generate method specifications with schemas from agent's register_operations().
See: https://docs.cloud.google.com/agent-builder/agent-engine/use/custom#supported-operations
"""
registered_operations = _agent_engines_utils._get_registered_operations(
agent=agent_instance
)
class_methods_spec = _agent_engines_utils._generate_class_methods_spec_or_raise(
agent=agent_instance,
operations=registered_operations,
)
class_methods_list = [
_agent_engines_utils._to_dict(method_spec) for method_spec in class_methods_spec
]
return class_methods_list
def parse_key_value_pairs(kv_string: str | None) -> dict[str, str]:
"""Parse key-value pairs from a comma-separated KEY=VALUE string."""
result = {}
if kv_string:
for pair in kv_string.split(","):
if "=" in pair:
key, value = pair.split("=", 1)
result[key.strip()] = value.strip()
else:
logging.warning(f"Skipping malformed key-value pair: {pair}")
return result
def write_deployment_metadata(
remote_agent: Any,
metadata_file: str = "deployment_metadata.json",
) -> None:
"""Write deployment metadata to file."""
metadata = {
"remote_agent_engine_id": remote_agent.api_resource.name,
"deployment_target": "agent_engine",
"is_a2a": False,
"deployment_timestamp": datetime.datetime.now().isoformat(),
}
with open(metadata_file, "w") as f:
json.dump(metadata, f, indent=2)
logging.info(f"Agent Engine ID written to {metadata_file}")
def print_deployment_success(
remote_agent: Any,
location: str,
project: str,
) -> None:
"""Print deployment success message with console URL."""
# Extract agent engine ID and project number for console URL
resource_name_parts = remote_agent.api_resource.name.split("/")
agent_engine_id = resource_name_parts[-1]
project_number = resource_name_parts[1]
print("\nβ
Deployment successful!")
service_account = remote_agent.api_resource.spec.service_account
if service_account:
print(f"Service Account: {service_account}")
else:
default_sa = (
f"service-{project_number}@gcp-sa-aiplatform-re.iam.gserviceaccount.com"
)
print(f"Service Account: {default_sa}")
playground_url = f"https://console.cloud.google.com/vertex-ai/agents/locations/{location}/agent-engines/{agent_engine_id}/playground?project={project}"
print(f"\nπ Open Console Playground: {playground_url}\n")
@click.command()
@click.option(
"--project",
default=None,
help="GCP project ID (defaults to application default credentials)",
)
@click.option(
"--location",
default="asia-southeast1",
help="GCP region (defaults to asia-southeast1)",
)
@click.option(
"--display-name",
default="adk-rag-agent",
help="Display name for the agent engine",
)
@click.option(
"--description",
default="",
help="Description of the agent",
)
@click.option(
"--source-packages",
multiple=True,
default=["./rag_agent"],
help="Source packages to deploy. Can be specified multiple times (e.g., --source-packages=./app --source-packages=./lib)",
)
@click.option(
"--entrypoint-module",
default="rag_agent.agent_engine_app",
help="Python module path for the agent entrypoint (required)",
)
@click.option(
"--entrypoint-object",
default="agent_engine",
help="Name of the agent instance at module level (required)",
)
@click.option(
"--requirements-file",
default="rag_agent/app_utils/.requirements.txt",
help="Path to requirements.txt file",
)
@click.option(
"--set-env-vars",
default=None,
help="Comma-separated list of environment variables in KEY=VALUE format",
)
@click.option(
"--labels",
default=None,
help="Comma-separated list of labels in KEY=VALUE format",
)
@click.option(
"--service-account",
default=None,
help="Service account email to use for the agent engine",
)
@click.option(
"--min-instances",
type=int,
default=1,
help="Minimum number of instances (default: 1)",
)
@click.option(
"--max-instances",
type=int,
default=10,
help="Maximum number of instances (default: 10)",
)
@click.option(
"--cpu",
default="4",
help="CPU limit (default: 4)",
)
@click.option(
"--memory",
default="8Gi",
help="Memory limit (default: 8Gi)",
)
@click.option(
"--container-concurrency",
type=int,
default=9,
help="Container concurrency (default: 9)",
)
@click.option(
"--num-workers",
type=int,
default=1,
help="Number of worker processes (default: 1)",
)
def deploy_agent_engine_app(
project: str | None,
location: str,
display_name: str,
description: str,
source_packages: tuple[str, ...],
entrypoint_module: str,
entrypoint_object: str,
requirements_file: str,
set_env_vars: str | None,
labels: str | None,
service_account: str | None,
min_instances: int,
max_instances: int,
cpu: str,
memory: str,
container_concurrency: int,
num_workers: int,
) -> AgentEngine:
"""Deploy the agent engine app to Vertex AI."""
logging.basicConfig(level=logging.INFO)
logging.getLogger("httpx").setLevel(logging.WARNING)
# Parse environment variables and labels if provided
env_vars = parse_key_value_pairs(set_env_vars)
labels_dict = parse_key_value_pairs(labels)
# Set GOOGLE_CLOUD_REGION to match deployment location
env_vars["GOOGLE_CLOUD_REGION"] = location
# Add NUM_WORKERS from CLI argument (can be overridden via --set-env-vars)
if "NUM_WORKERS" not in env_vars:
env_vars["NUM_WORKERS"] = str(num_workers)
# Enable telemetry by default for Agent Engine
if "GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY" not in env_vars:
env_vars["GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY"] = "true"
if "OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT" not in env_vars:
env_vars["OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"] = "true"
if not project:
_, project = google.auth.default()
print("""
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β π€ DEPLOYING AGENT TO VERTEX AI AGENT ENGINE π€ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
""")
# Log deployment parameters
click.echo("\nπ Deployment Parameters:")
click.echo(f" Project: {project}")
click.echo(f" Location: {location}")
click.echo(f" Display Name: {display_name}")
click.echo(f" Min Instances: {min_instances}")
click.echo(f" Max Instances: {max_instances}")
click.echo(f" CPU: {cpu}")
click.echo(f" Memory: {memory}")
click.echo(f" Container Concurrency: {container_concurrency}")
if service_account:
click.echo(f" Service Account: {service_account}")
if env_vars:
click.echo("\nπ Environment Variables:")
for key, value in sorted(env_vars.items()):
click.echo(f" {key}: {value}")
source_packages_list = list(source_packages)
# Initialize vertexai client
client = vertexai.Client(
project=project,
location=location,
)
vertexai.init(project=project, location=location)
# Add agent garden labels if configured
# Dynamically import the agent instance to generate class_methods
logging.info(f"Importing {entrypoint_module}.{entrypoint_object}")
module = importlib.import_module(entrypoint_module)
agent_instance = getattr(module, entrypoint_object)
# If the agent_instance is a coroutine, await it to get the actual instance
if inspect.iscoroutine(agent_instance):
logging.info(f"Detected coroutine, awaiting {entrypoint_object}...")
agent_instance = asyncio.run(agent_instance)
# Generate class methods spec from register_operations
class_methods_list = generate_class_methods_from_agent(agent_instance)
config = AgentEngineConfig(
display_name=display_name,
description=description,
source_packages=source_packages_list,
entrypoint_module=entrypoint_module,
entrypoint_object=entrypoint_object,
class_methods=class_methods_list,
env_vars=env_vars,
service_account=service_account,
requirements_file=requirements_file,
labels=labels_dict,
min_instances=min_instances,
max_instances=max_instances,
resource_limits={"cpu": cpu, "memory": memory},
container_concurrency=container_concurrency,
agent_framework="google-adk",
)
# Check if an agent with this name already exists
existing_agents = list(client.agent_engines.list())
matching_agents = [
agent
for agent in existing_agents
if agent.api_resource.display_name == display_name
]
# Deploy the agent (create or update)
if matching_agents:
click.echo(f"\nπ Updating existing agent: {display_name}")
else:
click.echo(f"\nπ Creating new agent: {display_name}")
click.echo("π Deploying to Vertex AI Agent Engine (this can take 3-5 minutes)...")
if matching_agents:
remote_agent = client.agent_engines.update(
name=matching_agents[0].api_resource.name, config=config
)
else:
remote_agent = client.agent_engines.create(config=config)
write_deployment_metadata(remote_agent)
print_deployment_success(remote_agent, location, project)
return remote_agent
if __name__ == "__main__":
deploy_agent_engine_app()
|