|
35 | 35 |
|
36 | 36 | import click |
37 | 37 | from click.core import ParameterSource |
38 | | -from fastapi import FastAPI |
39 | | -import uvicorn |
40 | 38 |
|
41 | 39 | from .. import version |
42 | | -from ..agents.run_config import StreamingMode |
43 | | -from ..evaluation.constants import MISSING_EVAL_DEPENDENCIES_MESSAGE |
44 | 40 | from ..features import FeatureName |
45 | 41 | from ..features import override_feature_enabled |
46 | 42 | from ..utils._telemetry_config import read_telemetry_consent |
47 | 43 | from ..utils._telemetry_config import write_telemetry_consent |
48 | 44 | from ._telemetry._metrics_collector import MetricsCollector |
49 | | -from .cli import run_cli |
50 | 45 | from .utils import envs |
51 | 46 | from .utils import logs |
52 | 47 |
|
53 | 48 | if TYPE_CHECKING: |
| 49 | + from fastapi import FastAPI |
| 50 | + |
54 | 51 | from ..agents.llm_agent import LlmAgent |
| 52 | + from ..agents.run_config import StreamingMode |
| 53 | + |
55 | 54 |
|
56 | 55 | LOG_LEVELS = click.Choice( |
57 | 56 | ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], |
58 | 57 | case_sensitive=False, |
59 | 58 | ) |
60 | 59 |
|
| 60 | +_STREAMING_MODE_CHOICES = ("None", "sse", "bidi") |
| 61 | + |
| 62 | + |
| 63 | +def _missing_eval_dependencies_message() -> str: |
| 64 | + # Imported lazily so loading the CLI does not pull in the evaluation stack. |
| 65 | + from ..evaluation.constants import MISSING_EVAL_DEPENDENCIES_MESSAGE |
| 66 | + |
| 67 | + return MISSING_EVAL_DEPENDENCIES_MESSAGE |
| 68 | + |
| 69 | + |
| 70 | +def _parse_streaming_mode( |
| 71 | + _ctx: click.Context, |
| 72 | + param: click.Parameter, |
| 73 | + value: str | None, |
| 74 | +) -> StreamingMode | None: |
| 75 | + """Converts a validated CLI value without importing the runtime for help.""" |
| 76 | + if value is None: |
| 77 | + return None |
| 78 | + |
| 79 | + from ..agents.run_config import StreamingMode |
| 80 | + |
| 81 | + mode = next( |
| 82 | + (m for m in StreamingMode if str(m.value).lower() == value.lower()), None |
| 83 | + ) |
| 84 | + if mode is None: |
| 85 | + raise click.BadParameter(f"unknown streaming mode {value!r}", param=param) |
| 86 | + return mode |
| 87 | + |
61 | 88 |
|
62 | 89 | def _logging_options(): |
63 | 90 | """Decorator to add logging options to click commands.""" |
@@ -426,13 +453,8 @@ def conformance(): |
426 | 453 | ) |
427 | 454 | @click.argument( |
428 | 455 | "streaming-mode", |
429 | | - type=click.Choice( |
430 | | - [str(m.value) for m in StreamingMode], case_sensitive=False |
431 | | - ), |
432 | | - callback=lambda ctx, param, value: next( |
433 | | - (m for m in StreamingMode if str(m.value).lower() == value.lower()), |
434 | | - value, |
435 | | - ), |
| 456 | + type=click.Choice(_STREAMING_MODE_CHOICES, case_sensitive=False), |
| 457 | + callback=_parse_streaming_mode, |
436 | 458 | ) |
437 | 459 | @click.pass_context |
438 | 460 | def cli_conformance_record( |
@@ -516,15 +538,8 @@ def cli_conformance_record( |
516 | 538 | ) |
517 | 539 | @click.option( |
518 | 540 | "--streaming-mode", |
519 | | - type=click.Choice( |
520 | | - [str(m.value) for m in StreamingMode], case_sensitive=False |
521 | | - ), |
522 | | - callback=lambda ctx, param, value: next( |
523 | | - (m for m in StreamingMode if str(m.value).lower() == value.lower()), |
524 | | - value, |
525 | | - ) |
526 | | - if value is not None |
527 | | - else None, |
| 541 | + type=click.Choice(_STREAMING_MODE_CHOICES, case_sensitive=False), |
| 542 | + callback=_parse_streaming_mode, |
528 | 543 | required=False, |
529 | 544 | default=None, |
530 | 545 | ) |
@@ -940,6 +955,8 @@ def cli_run( |
940 | 955 | sys.exit(exit_code) |
941 | 956 | else: |
942 | 957 | # Legacy interactive mode |
| 958 | + from .cli import run_cli |
| 959 | + |
943 | 960 | asyncio.run( |
944 | 961 | run_cli( |
945 | 962 | agent_parent_dir=agent_parent_folder, |
@@ -1182,7 +1199,7 @@ def cli_eval( |
1182 | 1199 | from .cli_eval import parse_and_get_evals_to_run |
1183 | 1200 | from .cli_eval import pretty_print_eval_result |
1184 | 1201 | except ModuleNotFoundError as mnf: |
1185 | | - raise click.ClickException(MISSING_EVAL_DEPENDENCIES_MESSAGE) from mnf |
| 1202 | + raise click.ClickException(_missing_eval_dependencies_message()) from mnf |
1186 | 1203 |
|
1187 | 1204 | eval_config = get_evaluation_criteria_or_default(config_file_path) |
1188 | 1205 | print(f"Using evaluation criteria: {eval_config}") |
@@ -1305,7 +1322,7 @@ def cli_eval( |
1305 | 1322 | ) |
1306 | 1323 | ) |
1307 | 1324 | except ModuleNotFoundError as mnf: |
1308 | | - raise click.ClickException(MISSING_EVAL_DEPENDENCIES_MESSAGE) from mnf |
| 1325 | + raise click.ClickException(_missing_eval_dependencies_message()) from mnf |
1309 | 1326 |
|
1310 | 1327 | click.echo( |
1311 | 1328 | "*********************************************************************" |
@@ -1413,7 +1430,7 @@ def cli_optimize( |
1413 | 1430 | from .cli_eval import get_root_agent |
1414 | 1431 |
|
1415 | 1432 | except ModuleNotFoundError as mnf: |
1416 | | - raise click.ClickException(MISSING_EVAL_DEPENDENCIES_MESSAGE) from mnf |
| 1433 | + raise click.ClickException(_missing_eval_dependencies_message()) from mnf |
1417 | 1434 |
|
1418 | 1435 | with open(sampler_config_file_path, "r", encoding="utf-8") as f: |
1419 | 1436 | content = f.read() |
@@ -1551,7 +1568,7 @@ def cli_add_eval_case( |
1551 | 1568 | from .cli_eval import get_eval_sets_manager |
1552 | 1569 |
|
1553 | 1570 | except ModuleNotFoundError as mnf: |
1554 | | - raise click.ClickException(MISSING_EVAL_DEPENDENCIES_MESSAGE) from mnf |
| 1571 | + raise click.ClickException(_missing_eval_dependencies_message()) from mnf |
1555 | 1572 |
|
1556 | 1573 | app_name = os.path.basename(agent_module_file_path) |
1557 | 1574 | agents_dir = os.path.dirname(agent_module_file_path) |
@@ -1649,7 +1666,7 @@ def cli_generate_eval_cases( |
1649 | 1666 | from .utils.state import create_empty_state |
1650 | 1667 |
|
1651 | 1668 | except ModuleNotFoundError as mnf: |
1652 | | - raise click.ClickException(MISSING_EVAL_DEPENDENCIES_MESSAGE) from mnf |
| 1669 | + raise click.ClickException(_missing_eval_dependencies_message()) from mnf |
1653 | 1670 |
|
1654 | 1671 | app_name = os.path.basename(agent_module_file_path) |
1655 | 1672 | agents_dir = os.path.dirname(agent_module_file_path) |
@@ -1994,6 +2011,8 @@ async def _lifespan(app: FastAPI): |
1994 | 2011 | fg="green", |
1995 | 2012 | ) |
1996 | 2013 |
|
| 2014 | + import uvicorn |
| 2015 | + |
1997 | 2016 | from .fast_api import get_fast_api_app |
1998 | 2017 |
|
1999 | 2018 | app = get_fast_api_app( |
@@ -2123,6 +2142,8 @@ def cli_api_server( |
2123 | 2142 |
|
2124 | 2143 | logs.setup_adk_logger(getattr(logging, log_level.upper())) |
2125 | 2144 |
|
| 2145 | + import uvicorn |
| 2146 | + |
2126 | 2147 | from .fast_api import get_fast_api_app |
2127 | 2148 |
|
2128 | 2149 | config = uvicorn.Config( |
|
0 commit comments