|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import subprocess |
| 17 | +from typing import Optional |
| 18 | +from typing import Tuple |
| 19 | + |
| 20 | +import click |
| 21 | + |
| 22 | +_INIT_PY_TEMPLATE = """\ |
| 23 | +from . import agent |
| 24 | +""" |
| 25 | + |
| 26 | +_AGENT_PY_TEMPLATE = """\ |
| 27 | +from google.adk.agents import Agent |
| 28 | +
|
| 29 | +root_agent = Agent( |
| 30 | + model='{model_name}', |
| 31 | + name='root_agent', |
| 32 | + description='A helpful assistant for user questions.', |
| 33 | + instruction='Answer user questions to the best of your knowledge', |
| 34 | +) |
| 35 | +""" |
| 36 | + |
| 37 | + |
| 38 | +_GOOGLE_API_MSG = """ |
| 39 | +Don't have API Key? Create one in AI Studio: https://aistudio.google.com/apikey |
| 40 | +""" |
| 41 | + |
| 42 | +_GOOGLE_CLOUD_SETUP_MSG = """ |
| 43 | +You need an existing Google Cloud account and project, check out this link for details: |
| 44 | +https://google.github.io/adk-docs/get-started/quickstart/#gemini---google-cloud-vertex-ai |
| 45 | +""" |
| 46 | + |
| 47 | +_OTHER_MODEL_MSG = """ |
| 48 | +Please see below guide to configure other models: |
| 49 | +https://google.github.io/adk-docs/agents/models |
| 50 | +""" |
| 51 | + |
| 52 | +_SUCCESS_MSG = """ |
| 53 | +Agent created in {agent_folder}: |
| 54 | +- .env |
| 55 | +- __init__.py |
| 56 | +- agent.py |
| 57 | +""" |
| 58 | + |
| 59 | + |
| 60 | +def _get_gcp_project_from_gcloud() -> str: |
| 61 | + """Uses gcloud to get default project.""" |
| 62 | + try: |
| 63 | + result = subprocess.run( |
| 64 | + ["gcloud", "config", "get-value", "project"], |
| 65 | + capture_output=True, |
| 66 | + text=True, |
| 67 | + check=True, |
| 68 | + ) |
| 69 | + return result.stdout.strip() |
| 70 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 71 | + return "" |
| 72 | + |
| 73 | + |
| 74 | +def _get_gcp_region_from_gcloud() -> str: |
| 75 | + """Uses gcloud to get default region.""" |
| 76 | + try: |
| 77 | + result = subprocess.run( |
| 78 | + ["gcloud", "config", "get-value", "compute/region"], |
| 79 | + capture_output=True, |
| 80 | + text=True, |
| 81 | + check=True, |
| 82 | + ) |
| 83 | + return result.stdout.strip() |
| 84 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 85 | + return "" |
| 86 | + |
| 87 | + |
| 88 | +def _prompt_str( |
| 89 | + prompt_prefix: str, |
| 90 | + *, |
| 91 | + prior_msg: Optional[str] = None, |
| 92 | + default_value: Optional[str] = None, |
| 93 | +) -> str: |
| 94 | + if prior_msg: |
| 95 | + click.secho(prior_msg, fg="green") |
| 96 | + while True: |
| 97 | + value: str = click.prompt( |
| 98 | + prompt_prefix, default=default_value or None, type=str |
| 99 | + ) |
| 100 | + if value and value.strip(): |
| 101 | + return value.strip() |
| 102 | + |
| 103 | + |
| 104 | +def _prompt_for_google_cloud( |
| 105 | + google_cloud_project: Optional[str], |
| 106 | +) -> str: |
| 107 | + """Prompts user for Google Cloud project ID.""" |
| 108 | + google_cloud_project = ( |
| 109 | + google_cloud_project |
| 110 | + or os.environ.get("GOOGLE_CLOUD_PROJECT", None) |
| 111 | + or _get_gcp_project_from_gcloud() |
| 112 | + ) |
| 113 | + |
| 114 | + google_cloud_project = _prompt_str( |
| 115 | + "Enter Google Cloud project ID", default_value=google_cloud_project |
| 116 | + ) |
| 117 | + |
| 118 | + return google_cloud_project |
| 119 | + |
| 120 | + |
| 121 | +def _prompt_for_google_cloud_region( |
| 122 | + google_cloud_region: Optional[str], |
| 123 | +) -> str: |
| 124 | + """Prompts user for Google Cloud region.""" |
| 125 | + google_cloud_region = ( |
| 126 | + google_cloud_region |
| 127 | + or os.environ.get("GOOGLE_CLOUD_LOCATION", None) |
| 128 | + or _get_gcp_region_from_gcloud() |
| 129 | + ) |
| 130 | + |
| 131 | + google_cloud_region = _prompt_str( |
| 132 | + "Enter Google Cloud region", |
| 133 | + default_value=google_cloud_region or "us-central1", |
| 134 | + ) |
| 135 | + return google_cloud_region |
| 136 | + |
| 137 | + |
| 138 | +def _prompt_for_google_api_key( |
| 139 | + google_api_key: Optional[str], |
| 140 | +) -> str: |
| 141 | + """Prompts user for Google API key.""" |
| 142 | + google_api_key = google_api_key or os.environ.get("GOOGLE_API_KEY", None) |
| 143 | + |
| 144 | + google_api_key = _prompt_str( |
| 145 | + "Enter Google API key", |
| 146 | + prior_msg=_GOOGLE_API_MSG, |
| 147 | + default_value=google_api_key, |
| 148 | + ) |
| 149 | + return google_api_key |
| 150 | + |
| 151 | + |
| 152 | +def _generate_files( |
| 153 | + agent_folder: str, |
| 154 | + *, |
| 155 | + google_api_key: Optional[str] = None, |
| 156 | + google_cloud_project: Optional[str] = None, |
| 157 | + google_cloud_region: Optional[str] = None, |
| 158 | + model: Optional[str] = None, |
| 159 | +): |
| 160 | + """Generates a folder name for the agent.""" |
| 161 | + os.makedirs(agent_folder, exist_ok=True) |
| 162 | + |
| 163 | + dotenv_file_path = os.path.join(agent_folder, ".env") |
| 164 | + init_file_path = os.path.join(agent_folder, "__init__.py") |
| 165 | + agent_file_path = os.path.join(agent_folder, "agent.py") |
| 166 | + |
| 167 | + with open(dotenv_file_path, "w", encoding="utf-8") as f: |
| 168 | + lines = [] |
| 169 | + if google_api_key: |
| 170 | + lines.append("GOOGLE_GENAI_USE_VERTEXAI=0") |
| 171 | + elif google_cloud_project and google_cloud_region: |
| 172 | + lines.append("GOOGLE_GENAI_USE_VERTEXAI=1") |
| 173 | + if google_api_key: |
| 174 | + lines.append(f"GOOGLE_API_KEY={google_api_key}") |
| 175 | + if google_cloud_project: |
| 176 | + lines.append(f"GOOGLE_CLOUD_PROJECT={google_cloud_project}") |
| 177 | + if google_cloud_region: |
| 178 | + lines.append(f"GOOGLE_CLOUD_LOCATION={google_cloud_region}") |
| 179 | + f.write("\n".join(lines)) |
| 180 | + |
| 181 | + with open(init_file_path, "w", encoding="utf-8") as f: |
| 182 | + f.write(_INIT_PY_TEMPLATE) |
| 183 | + |
| 184 | + with open(agent_file_path, "w", encoding="utf-8") as f: |
| 185 | + f.write(_AGENT_PY_TEMPLATE.format(model_name=model)) |
| 186 | + |
| 187 | + click.secho( |
| 188 | + _SUCCESS_MSG.format(agent_folder=agent_folder), |
| 189 | + fg="green", |
| 190 | + ) |
| 191 | + |
| 192 | + |
| 193 | +def _prompt_for_model() -> str: |
| 194 | + model_choice = click.prompt( |
| 195 | + """\ |
| 196 | +Choose a model for the root agent: |
| 197 | +1. gemini-2.0-flash-001 |
| 198 | +2. Other models (fill later) |
| 199 | +Choose model""", |
| 200 | + type=click.Choice(["1", "2"]), |
| 201 | + ) |
| 202 | + if model_choice == "1": |
| 203 | + return "gemini-2.0-flash-001" |
| 204 | + else: |
| 205 | + click.secho(_OTHER_MODEL_MSG, fg="green") |
| 206 | + return "<FILL_IN_MODEL>" |
| 207 | + |
| 208 | + |
| 209 | +def _prompt_to_choose_backend( |
| 210 | + google_api_key: Optional[str], |
| 211 | + google_cloud_project: Optional[str], |
| 212 | + google_cloud_region: Optional[str], |
| 213 | +) -> Tuple[Optional[str], Optional[str], Optional[str]]: |
| 214 | + """Prompts user to choose backend. |
| 215 | +
|
| 216 | + Returns: |
| 217 | + A tuple of (google_api_key, google_cloud_project, google_cloud_region). |
| 218 | + """ |
| 219 | + backend_choice = click.prompt( |
| 220 | + "1. Google AI\n2. Vertex AI\nChoose a backend", |
| 221 | + type=click.Choice(["1", "2"]), |
| 222 | + ) |
| 223 | + if backend_choice == "1": |
| 224 | + google_api_key = _prompt_for_google_api_key(google_api_key) |
| 225 | + elif backend_choice == "2": |
| 226 | + click.secho(_GOOGLE_CLOUD_SETUP_MSG, fg="green") |
| 227 | + google_cloud_project = _prompt_for_google_cloud(google_cloud_project) |
| 228 | + google_cloud_region = _prompt_for_google_cloud_region(google_cloud_region) |
| 229 | + return google_api_key, google_cloud_project, google_cloud_region |
| 230 | + |
| 231 | + |
| 232 | +def run_cmd( |
| 233 | + agent_name: str, |
| 234 | + *, |
| 235 | + model: Optional[str], |
| 236 | + google_api_key: Optional[str], |
| 237 | + google_cloud_project: Optional[str], |
| 238 | + google_cloud_region: Optional[str], |
| 239 | +): |
| 240 | + """Runs `adk create` command to create agent template. |
| 241 | +
|
| 242 | + Args: |
| 243 | + agent_name: str, The name of the agent. |
| 244 | + google_api_key: Optional[str], The Google API key for using Google AI as |
| 245 | + backend. |
| 246 | + google_cloud_project: Optional[str], The Google Cloud project for using |
| 247 | + VertexAI as backend. |
| 248 | + google_cloud_region: Optional[str], The Google Cloud region for using |
| 249 | + VertexAI as backend. |
| 250 | + """ |
| 251 | + agent_folder = os.path.join(os.getcwd(), agent_name) |
| 252 | + # check folder doesn't exist or it's empty. Otherwise, throw |
| 253 | + if os.path.exists(agent_folder) and os.listdir(agent_folder): |
| 254 | + # Prompt user whether to override existing files using click |
| 255 | + if not click.confirm( |
| 256 | + f"Non-empty folder already exist: '{agent_folder}'\n" |
| 257 | + "Override existing content?", |
| 258 | + default=False, |
| 259 | + ): |
| 260 | + raise click.Abort() |
| 261 | + |
| 262 | + if not model: |
| 263 | + model = _prompt_for_model() |
| 264 | + |
| 265 | + if not google_api_key and not (google_cloud_project and google_cloud_region): |
| 266 | + if model.startswith("gemini"): |
| 267 | + google_api_key, google_cloud_project, google_cloud_region = ( |
| 268 | + _prompt_to_choose_backend( |
| 269 | + google_api_key, google_cloud_project, google_cloud_region |
| 270 | + ) |
| 271 | + ) |
| 272 | + |
| 273 | + _generate_files( |
| 274 | + agent_folder, |
| 275 | + google_api_key=google_api_key, |
| 276 | + google_cloud_project=google_cloud_project, |
| 277 | + google_cloud_region=google_cloud_region, |
| 278 | + model=model, |
| 279 | + ) |
0 commit comments