mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
chore: Support allow_origins in cloud_run deployment
Also reorganize the fast_api_common_options. This resolves https://github.com/google/adk-python/issues/1444. PiperOrigin-RevId: 773890111
This commit is contained in:
committed by
Copybara-Service
parent
8677d5c8dc
commit
2fd8feb65d
@@ -55,7 +55,7 @@ COPY "agents/{app_name}/" "/app/agents/{app_name}/"
|
|||||||
|
|
||||||
EXPOSE {port}
|
EXPOSE {port}
|
||||||
|
|
||||||
CMD adk {command} --port={port} {host_option} {service_option} {trace_to_cloud_option} "/app/agents"
|
CMD adk {command} --port={port} {host_option} {service_option} {trace_to_cloud_option} {allow_origins_option} "/app/agents"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_AGENT_ENGINE_APP_TEMPLATE = """
|
_AGENT_ENGINE_APP_TEMPLATE = """
|
||||||
@@ -121,8 +121,10 @@ def to_cloud_run(
|
|||||||
port: int,
|
port: int,
|
||||||
trace_to_cloud: bool,
|
trace_to_cloud: bool,
|
||||||
with_ui: bool,
|
with_ui: bool,
|
||||||
|
log_level: str,
|
||||||
verbosity: str,
|
verbosity: str,
|
||||||
adk_version: str,
|
adk_version: str,
|
||||||
|
allow_origins: Optional[list[str]] = None,
|
||||||
session_service_uri: Optional[str] = None,
|
session_service_uri: Optional[str] = None,
|
||||||
artifact_service_uri: Optional[str] = None,
|
artifact_service_uri: Optional[str] = None,
|
||||||
memory_service_uri: Optional[str] = None,
|
memory_service_uri: Optional[str] = None,
|
||||||
@@ -150,6 +152,7 @@ def to_cloud_run(
|
|||||||
app_name: The name of the app, by default, it's basename of `agent_folder`.
|
app_name: The name of the app, by default, it's basename of `agent_folder`.
|
||||||
temp_folder: The temp folder for the generated Cloud Run source files.
|
temp_folder: The temp folder for the generated Cloud Run source files.
|
||||||
port: The port of the ADK api server.
|
port: The port of the ADK api server.
|
||||||
|
allow_origins: The list of allowed origins for the ADK api server.
|
||||||
trace_to_cloud: Whether to enable Cloud Trace.
|
trace_to_cloud: Whether to enable Cloud Trace.
|
||||||
with_ui: Whether to deploy with UI.
|
with_ui: Whether to deploy with UI.
|
||||||
verbosity: The verbosity level of the CLI.
|
verbosity: The verbosity level of the CLI.
|
||||||
@@ -183,6 +186,9 @@ def to_cloud_run(
|
|||||||
# create Dockerfile
|
# create Dockerfile
|
||||||
click.echo('Creating Dockerfile...')
|
click.echo('Creating Dockerfile...')
|
||||||
host_option = '--host=0.0.0.0' if adk_version > '0.5.0' else ''
|
host_option = '--host=0.0.0.0' if adk_version > '0.5.0' else ''
|
||||||
|
allow_origins_option = (
|
||||||
|
f'--allow_origins={",".join(allow_origins)}' if allow_origins else ''
|
||||||
|
)
|
||||||
dockerfile_content = _DOCKERFILE_TEMPLATE.format(
|
dockerfile_content = _DOCKERFILE_TEMPLATE.format(
|
||||||
gcp_project_id=project,
|
gcp_project_id=project,
|
||||||
gcp_region=region,
|
gcp_region=region,
|
||||||
@@ -197,6 +203,7 @@ def to_cloud_run(
|
|||||||
memory_service_uri,
|
memory_service_uri,
|
||||||
),
|
),
|
||||||
trace_to_cloud_option='--trace_to_cloud' if trace_to_cloud else '',
|
trace_to_cloud_option='--trace_to_cloud' if trace_to_cloud else '',
|
||||||
|
allow_origins_option=allow_origins_option,
|
||||||
adk_version=adk_version,
|
adk_version=adk_version,
|
||||||
host_option=host_option,
|
host_option=host_option,
|
||||||
)
|
)
|
||||||
@@ -226,7 +233,7 @@ def to_cloud_run(
|
|||||||
'--port',
|
'--port',
|
||||||
str(port),
|
str(port),
|
||||||
'--verbosity',
|
'--verbosity',
|
||||||
verbosity,
|
log_level.lower() if log_level else verbosity,
|
||||||
'--labels',
|
'--labels',
|
||||||
'created-by=adk',
|
'created-by=adk',
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -39,6 +39,11 @@ from .fast_api import get_fast_api_app
|
|||||||
from .utils import envs
|
from .utils import envs
|
||||||
from .utils import logs
|
from .utils import logs
|
||||||
|
|
||||||
|
LOG_LEVELS = click.Choice(
|
||||||
|
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
|
||||||
|
case_sensitive=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class HelpfulCommand(click.Command):
|
class HelpfulCommand(click.Command):
|
||||||
"""Command that shows full help on error instead of just the error message.
|
"""Command that shows full help on error instead of just the error message.
|
||||||
@@ -498,13 +503,6 @@ def fast_api_common_options():
|
|||||||
"""Decorator to add common fast api options to click commands."""
|
"""Decorator to add common fast api options to click commands."""
|
||||||
|
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
@click.option(
|
|
||||||
"--host",
|
|
||||||
type=str,
|
|
||||||
help="Optional. The binding host of the server",
|
|
||||||
default="127.0.0.1",
|
|
||||||
show_default=True,
|
|
||||||
)
|
|
||||||
@click.option(
|
@click.option(
|
||||||
"--port",
|
"--port",
|
||||||
type=int,
|
type=int,
|
||||||
@@ -518,10 +516,7 @@ def fast_api_common_options():
|
|||||||
)
|
)
|
||||||
@click.option(
|
@click.option(
|
||||||
"--log_level",
|
"--log_level",
|
||||||
type=click.Choice(
|
type=LOG_LEVELS,
|
||||||
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
|
|
||||||
case_sensitive=False,
|
|
||||||
),
|
|
||||||
default="INFO",
|
default="INFO",
|
||||||
help="Optional. Set the logging level",
|
help="Optional. Set the logging level",
|
||||||
)
|
)
|
||||||
@@ -535,7 +530,10 @@ def fast_api_common_options():
|
|||||||
@click.option(
|
@click.option(
|
||||||
"--reload/--no-reload",
|
"--reload/--no-reload",
|
||||||
default=True,
|
default=True,
|
||||||
help="Optional. Whether to enable auto reload for server.",
|
help=(
|
||||||
|
"Optional. Whether to enable auto reload for server. Not supported"
|
||||||
|
" for Cloud Run."
|
||||||
|
),
|
||||||
)
|
)
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
@@ -547,6 +545,13 @@ def fast_api_common_options():
|
|||||||
|
|
||||||
|
|
||||||
@main.command("web")
|
@main.command("web")
|
||||||
|
@click.option(
|
||||||
|
"--host",
|
||||||
|
type=str,
|
||||||
|
help="Optional. The binding host of the server",
|
||||||
|
default="127.0.0.1",
|
||||||
|
show_default=True,
|
||||||
|
)
|
||||||
@fast_api_common_options()
|
@fast_api_common_options()
|
||||||
@adk_services_options()
|
@adk_services_options()
|
||||||
@deprecated_adk_services_options()
|
@deprecated_adk_services_options()
|
||||||
@@ -578,7 +583,7 @@ def cli_web(
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
adk web --session_service_uri=[uri] --port=[port] path/to/agents_dir
|
adk web --port=[port] path/to/agents_dir
|
||||||
"""
|
"""
|
||||||
logs.setup_adk_logger(getattr(logging, log_level.upper()))
|
logs.setup_adk_logger(getattr(logging, log_level.upper()))
|
||||||
|
|
||||||
@@ -628,6 +633,16 @@ def cli_web(
|
|||||||
|
|
||||||
|
|
||||||
@main.command("api_server")
|
@main.command("api_server")
|
||||||
|
@click.option(
|
||||||
|
"--host",
|
||||||
|
type=str,
|
||||||
|
help="Optional. The binding host of the server",
|
||||||
|
default="127.0.0.1",
|
||||||
|
show_default=True,
|
||||||
|
)
|
||||||
|
@fast_api_common_options()
|
||||||
|
@adk_services_options()
|
||||||
|
@deprecated_adk_services_options()
|
||||||
# The directory of agents, where each sub-directory is a single agent.
|
# The directory of agents, where each sub-directory is a single agent.
|
||||||
# By default, it is the current working directory
|
# By default, it is the current working directory
|
||||||
@click.argument(
|
@click.argument(
|
||||||
@@ -637,9 +652,6 @@ def cli_web(
|
|||||||
),
|
),
|
||||||
default=os.getcwd(),
|
default=os.getcwd(),
|
||||||
)
|
)
|
||||||
@fast_api_common_options()
|
|
||||||
@adk_services_options()
|
|
||||||
@deprecated_adk_services_options()
|
|
||||||
def cli_api_server(
|
def cli_api_server(
|
||||||
agents_dir: str,
|
agents_dir: str,
|
||||||
log_level: str = "INFO",
|
log_level: str = "INFO",
|
||||||
@@ -661,7 +673,7 @@ def cli_api_server(
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
adk api_server --session_service_uri=[uri] --port=[port] path/to/agents_dir
|
adk api_server --port=[port] path/to/agents_dir
|
||||||
"""
|
"""
|
||||||
logs.setup_adk_logger(getattr(logging, log_level.upper()))
|
logs.setup_adk_logger(getattr(logging, log_level.upper()))
|
||||||
|
|
||||||
@@ -720,19 +732,7 @@ def cli_api_server(
|
|||||||
" of the AGENT source code)."
|
" of the AGENT source code)."
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@click.option(
|
@fast_api_common_options()
|
||||||
"--port",
|
|
||||||
type=int,
|
|
||||||
default=8000,
|
|
||||||
help="Optional. The port of the ADK API server (default: 8000).",
|
|
||||||
)
|
|
||||||
@click.option(
|
|
||||||
"--trace_to_cloud",
|
|
||||||
is_flag=True,
|
|
||||||
show_default=True,
|
|
||||||
default=False,
|
|
||||||
help="Optional. Whether to enable Cloud Trace for cloud run.",
|
|
||||||
)
|
|
||||||
@click.option(
|
@click.option(
|
||||||
"--with_ui",
|
"--with_ui",
|
||||||
is_flag=True,
|
is_flag=True,
|
||||||
@@ -743,6 +743,11 @@ def cli_api_server(
|
|||||||
" only)"
|
" only)"
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@click.option(
|
||||||
|
"--verbosity",
|
||||||
|
type=LOG_LEVELS,
|
||||||
|
help="Deprecated. Use --log_level instead.",
|
||||||
|
)
|
||||||
@click.option(
|
@click.option(
|
||||||
"--temp_folder",
|
"--temp_folder",
|
||||||
type=str,
|
type=str,
|
||||||
@@ -756,20 +761,6 @@ def cli_api_server(
|
|||||||
" (default: a timestamped folder in the system temp directory)."
|
" (default: a timestamped folder in the system temp directory)."
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@click.option(
|
|
||||||
"--verbosity",
|
|
||||||
type=click.Choice(
|
|
||||||
["debug", "info", "warning", "error", "critical"], case_sensitive=False
|
|
||||||
),
|
|
||||||
default="WARNING",
|
|
||||||
help="Optional. Override the default verbosity level.",
|
|
||||||
)
|
|
||||||
@click.argument(
|
|
||||||
"agent",
|
|
||||||
type=click.Path(
|
|
||||||
exists=True, dir_okay=True, file_okay=False, resolve_path=True
|
|
||||||
),
|
|
||||||
)
|
|
||||||
@click.option(
|
@click.option(
|
||||||
"--adk_version",
|
"--adk_version",
|
||||||
type=str,
|
type=str,
|
||||||
@@ -782,6 +773,12 @@ def cli_api_server(
|
|||||||
)
|
)
|
||||||
@adk_services_options()
|
@adk_services_options()
|
||||||
@deprecated_adk_services_options()
|
@deprecated_adk_services_options()
|
||||||
|
@click.argument(
|
||||||
|
"agent",
|
||||||
|
type=click.Path(
|
||||||
|
exists=True, dir_okay=True, file_okay=False, resolve_path=True
|
||||||
|
),
|
||||||
|
)
|
||||||
def cli_deploy_cloud_run(
|
def cli_deploy_cloud_run(
|
||||||
agent: str,
|
agent: str,
|
||||||
project: Optional[str],
|
project: Optional[str],
|
||||||
@@ -792,8 +789,11 @@ def cli_deploy_cloud_run(
|
|||||||
port: int,
|
port: int,
|
||||||
trace_to_cloud: bool,
|
trace_to_cloud: bool,
|
||||||
with_ui: bool,
|
with_ui: bool,
|
||||||
verbosity: str,
|
|
||||||
adk_version: str,
|
adk_version: str,
|
||||||
|
log_level: Optional[str] = None,
|
||||||
|
verbosity: str = "WARNING",
|
||||||
|
reload: bool = True,
|
||||||
|
allow_origins: Optional[list[str]] = None,
|
||||||
session_service_uri: Optional[str] = None,
|
session_service_uri: Optional[str] = None,
|
||||||
artifact_service_uri: Optional[str] = None,
|
artifact_service_uri: Optional[str] = None,
|
||||||
memory_service_uri: Optional[str] = None,
|
memory_service_uri: Optional[str] = None,
|
||||||
@@ -808,6 +808,7 @@ def cli_deploy_cloud_run(
|
|||||||
|
|
||||||
adk deploy cloud_run --project=[project] --region=[region] path/to/my_agent
|
adk deploy cloud_run --project=[project] --region=[region] path/to/my_agent
|
||||||
"""
|
"""
|
||||||
|
log_level = log_level or verbosity
|
||||||
session_service_uri = session_service_uri or session_db_url
|
session_service_uri = session_service_uri or session_db_url
|
||||||
artifact_service_uri = artifact_service_uri or artifact_storage_uri
|
artifact_service_uri = artifact_service_uri or artifact_storage_uri
|
||||||
try:
|
try:
|
||||||
@@ -820,7 +821,9 @@ def cli_deploy_cloud_run(
|
|||||||
temp_folder=temp_folder,
|
temp_folder=temp_folder,
|
||||||
port=port,
|
port=port,
|
||||||
trace_to_cloud=trace_to_cloud,
|
trace_to_cloud=trace_to_cloud,
|
||||||
|
allow_origins=allow_origins,
|
||||||
with_ui=with_ui,
|
with_ui=with_ui,
|
||||||
|
log_level=log_level,
|
||||||
verbosity=verbosity,
|
verbosity=verbosity,
|
||||||
adk_version=adk_version,
|
adk_version=adk_version,
|
||||||
session_service_uri=session_service_uri,
|
session_service_uri=session_service_uri,
|
||||||
|
|||||||
@@ -162,6 +162,7 @@ def test_to_cloud_run_happy_path(
|
|||||||
trace_to_cloud=True,
|
trace_to_cloud=True,
|
||||||
with_ui=True,
|
with_ui=True,
|
||||||
verbosity="info",
|
verbosity="info",
|
||||||
|
log_level="info",
|
||||||
session_service_uri="sqlite://",
|
session_service_uri="sqlite://",
|
||||||
artifact_service_uri="gs://bucket",
|
artifact_service_uri="gs://bucket",
|
||||||
memory_service_uri="rag://",
|
memory_service_uri="rag://",
|
||||||
@@ -206,6 +207,7 @@ def test_to_cloud_run_cleans_temp_dir(
|
|||||||
trace_to_cloud=False,
|
trace_to_cloud=False,
|
||||||
with_ui=False,
|
with_ui=False,
|
||||||
verbosity="info",
|
verbosity="info",
|
||||||
|
log_level="info",
|
||||||
adk_version="1.0.0",
|
adk_version="1.0.0",
|
||||||
session_service_uri=None,
|
session_service_uri=None,
|
||||||
artifact_service_uri=None,
|
artifact_service_uri=None,
|
||||||
|
|||||||
Reference in New Issue
Block a user