Vertex AI Express Mode: Using Vertex AI Sessions and Memory¶
If you are interested in using either the VertexAiSessionService or VertexAiMemoryBankService but you don't have a Google Cloud Project, you can sign up for Vertex AI Express Mode and get access
for without cost and try out these services! You can sign up with an eligible gmail account here. For more details about Vertex AI Express mode, see the overview page.
Once you sign up, get an API key and you can get started using your local ADK agent with Vertex AI Session and Memory services!
Info
Vertex AI Express Mode has certain limitations in the free tier. Free Express mode projects are only valid for 90 days and only select services are available to be used with limited quota. For example, the number of Agent Engines is restricted to 10 and deployment to Agent Engine is reserved for the paid tier only. To remove the quota restrictions and use all of Vertex AI's services, add a billing account to your Express Mode project.
Create an Agent Engine¶
Session objects are children of an AgentEngine. When using Vertex AI Express Mode, we can create an empty AgentEngine parent to manage all of our Session and Memory objects.
First, ensure that your environment variables are set correctly. For example, in Python:
Next, we can create our Agent Engine instance. You can use the Vertex AI SDK.
-
Import Vertex AI SDK.
-
Initialize the Vertex AI Client with your API key and create an agent engine instance.
-
Replace
YOUR_AGENT_ENGINE_DISPLAY_NAMEandYOUR_AGENT_ENGINE_DESCRIPTIONwith your use case. -
Get the Agent Engine name and ID from the response to use with Memories and Sessions.
Managing Sessions with a VertexAiSessionService¶
VertexAiSessionService is compatible with Vertex AI Express mode API Keys. We can
instead initialize the session object without any project or location.
# Requires: pip install google-adk[vertexai]
# Plus environment variable setup:
# GOOGLE_GENAI_USE_VERTEXAI=TRUE
# GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
from google.adk.sessions import VertexAiSessionService
# The app_name used with this service should be the Reasoning Engine ID or name
APP_ID = "your-reasoning-engine-id"
# Project and location are not required when initializing with Vertex Express Mode
session_service = VertexAiSessionService(agent_engine_id=APP_ID)
# Use REASONING_ENGINE_APP_ID when calling service methods, e.g.:
# session = await session_service.create_session(app_name=APP_ID, user_id= ...)
Info
For Free Express Mode Projects, VertexAiSessionService has the following quota:
- 10 Create, delete, or update Vertex AI Agent Engine sessions per minute
- 30 Append event to Vertex AI Agent Engine sessions per minute
Managing Memories with a VertexAiMemoryBankService¶
VertexAiMemoryBankService is compatible with Vertex AI Express mode API Keys. We can
instead initialize the memory object without any project or location.
# Requires: pip install google-adk[vertexai]
# Plus environment variable setup:
# GOOGLE_GENAI_USE_VERTEXAI=TRUE
# GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
from google.adk.memory import VertexAiMemoryBankService
# The app_name used with this service should be the Reasoning Engine ID or name
APP_ID = "your-reasoning-engine-id"
# Project and location are not required when initializing with Vertex Express Mode
memory_service = VertexAiMemoryBankService(agent_engine_id=APP_ID)
# Generate a memory from that session so the Agent can remember relevant details about the user
# memory = await memory_service.add_session_to_memory(session)
Info
For Free Express Mode Projects, VertexAiMemoryBankService has the following quota:
- 10 Create, delete, or update Vertex AI Agent Engine memory resources per minute
- 10 Get, list, or retrieve from Vertex AI Agent Engine Memory Bank per minute
Code Sample: Weather Agent with Session and Memory using Vertex AI Express Mode¶
In this sample, we create a weather agent that utilizes both VertexAiSessionService and VertexAiMemoryBankService for context management, allowing our agent to recall user preferences and conversations!
Weather Agent with Session and Memory using Vertex AI Express Mode