Skip to content

Commit 5878176

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: Add support for Vertex Express Mode API key in AdkApp
PiperOrigin-RevId: 825638989
1 parent c81f912 commit 5878176

File tree

1 file changed

+51
-17
lines changed
  • vertexai/agent_engines/templates

1 file changed

+51
-17
lines changed

vertexai/agent_engines/templates/adk.py

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ def __init__(
557557
"artifact_service_builder": artifact_service_builder,
558558
"memory_service_builder": memory_service_builder,
559559
"instrumentor_builder": instrumentor_builder,
560+
"express_mode_api_key": initializer.global_config.api_key,
560561
}
561562

562563
async def _init_session(
@@ -700,9 +701,18 @@ def set_up(self):
700701

701702
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "1"
702703
project = self._tmpl_attrs.get("project")
703-
os.environ["GOOGLE_CLOUD_PROJECT"] = project
704+
if project:
705+
os.environ["GOOGLE_CLOUD_PROJECT"] = project
704706
location = self._tmpl_attrs.get("location")
705-
os.environ["GOOGLE_CLOUD_LOCATION"] = location
707+
if location:
708+
os.environ["GOOGLE_CLOUD_LOCATION"] = location
709+
express_mode_api_key = self._tmpl_attrs.get("express_mode_api_key")
710+
if express_mode_api_key and not project:
711+
os.environ["GOOGLE_API_KEY"] = express_mode_api_key
712+
# Clear location if project is not set and express mode api key is
713+
# provided.
714+
os.environ.pop("GOOGLE_CLOUD_LOCATION", None)
715+
location = None
706716

707717
# Disable content capture in custom ADK spans unless user enabled
708718
# tracing explicitly with the old flag
@@ -749,21 +759,37 @@ def set_up(self):
749759
VertexAiSessionService,
750760
)
751761

752-
self._tmpl_attrs["session_service"] = VertexAiSessionService(
753-
project=project,
754-
location=location,
755-
agent_engine_id=os.environ.get("GOOGLE_CLOUD_AGENT_ENGINE_ID"),
756-
)
762+
if is_version_sufficient("1.18.0"):
763+
self._tmpl_attrs["session_service"] = VertexAiSessionService(
764+
project=project,
765+
location=location,
766+
agent_engine_id=os.environ.get("GOOGLE_CLOUD_AGENT_ENGINE_ID"),
767+
express_mode_api_key=express_mode_api_key,
768+
)
769+
else:
770+
self._tmpl_attrs["session_service"] = VertexAiSessionService(
771+
project=project,
772+
location=location,
773+
agent_engine_id=os.environ.get("GOOGLE_CLOUD_AGENT_ENGINE_ID"),
774+
)
757775
except (ImportError, AttributeError):
758776
from google.adk.sessions.vertex_ai_session_service_g3 import (
759777
VertexAiSessionService,
760778
)
761779

762-
self._tmpl_attrs["session_service"] = VertexAiSessionService(
763-
project=project,
764-
location=location,
765-
agent_engine_id=os.environ.get("GOOGLE_CLOUD_AGENT_ENGINE_ID"),
766-
)
780+
if is_version_sufficient("1.18.0"):
781+
self._tmpl_attrs["session_service"] = VertexAiSessionService(
782+
project=project,
783+
location=location,
784+
agent_engine_id=os.environ.get("GOOGLE_CLOUD_AGENT_ENGINE_ID"),
785+
express_mode_api_key=express_mode_api_key,
786+
)
787+
else:
788+
self._tmpl_attrs["session_service"] = VertexAiSessionService(
789+
project=project,
790+
location=location,
791+
agent_engine_id=os.environ.get("GOOGLE_CLOUD_AGENT_ENGINE_ID"),
792+
)
767793

768794
else:
769795
self._tmpl_attrs["session_service"] = InMemorySessionService()
@@ -779,11 +805,19 @@ def set_up(self):
779805
VertexAiMemoryBankService,
780806
)
781807

782-
self._tmpl_attrs["memory_service"] = VertexAiMemoryBankService(
783-
project=project,
784-
location=location,
785-
agent_engine_id=os.environ.get("GOOGLE_CLOUD_AGENT_ENGINE_ID"),
786-
)
808+
if is_version_sufficient("1.18.0"):
809+
self._tmpl_attrs["memory_service"] = VertexAiMemoryBankService(
810+
project=project,
811+
location=location,
812+
agent_engine_id=os.environ.get("GOOGLE_CLOUD_AGENT_ENGINE_ID"),
813+
express_mode_api_key=express_mode_api_key,
814+
)
815+
else:
816+
self._tmpl_attrs["memory_service"] = VertexAiMemoryBankService(
817+
project=project,
818+
location=location,
819+
agent_engine_id=os.environ.get("GOOGLE_CLOUD_AGENT_ENGINE_ID"),
820+
)
787821
except (ImportError, AttributeError):
788822
# TODO(ysian): Handle this via _g3 import for google3.
789823
pass

0 commit comments

Comments
 (0)