Skip to content

Commit 8400336

Browse files
feat: Update express mode code sample (#494)
* Update quickstart.md * Create express-mode.md * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update mkdocs.yml * Update mkdocs.yml * Update session.md * Update mkdocs.yml * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update quickstart.md * Update express-mode.md * Add files via upload * Update express-mode.md * Update express-mode.md * Update express-mode.md * Delete examples/python/notebooks/express-mode-weather-agent.ipynb * Add files via upload * Delete examples/python/notebooks/express-mode-weather-agent.ipynb * Add files via upload * Update express-mode-weather-agent.ipynb * Update quickstart.md * Update express-mode.md * Update express-mode.md * Update mkdocs.yml * Delete examples/python/notebooks/express-mode-weather-agent.ipynb * Add files via upload * Update express-mode.md * Update express-mode.md * Update express-mode.md * Update mkdocs.yml * Update express-mode.md --------- Co-authored-by: Lavi Nigam <[email protected]>
1 parent 3dccaba commit 8400336

File tree

3 files changed

+51
-48
lines changed

3 files changed

+51
-48
lines changed

docs/sessions/express-mode.md

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Vertex Express Mode: Using Sessions and Memory for Free
1+
# Vertex AI Express Mode: Using Vertex AI Sessions and Memory for Free
22

33
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
44
for free and try out these services! You can sign up with an eligible ***gmail*** account [here](https://console.cloud.google.com/expressmode). For more details about Vertex AI Express mode, see the [overview page](https://cloud.google.com/vertex-ai/generative-ai/docs/start/express-mode/overview).
@@ -13,12 +13,12 @@ Once you sign up, get an [API key](https://cloud.google.com/vertex-ai/generative
1313
`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.
1414
First, ensure that your enviornment variables are set correctly. For example, in Python:
1515

16-
```env title="multi_tool_agent/.env"
17-
GOOGLE_GENAI_USE_VERTEXAI=TRUE
18-
GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
19-
```
16+
```env title="weather_agent/.env"
17+
GOOGLE_GENAI_USE_VERTEXAI=TRUE
18+
GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
19+
```
2020

21-
Next, we can create our Agent Engine instance. You can use the Gen AI sdk.
21+
Next, we can create our Agent Engine instance. You can use the Gen AI SDK.
2222

2323
=== "GenAI SDK"
2424
1. Import Gen AI SDK.
@@ -54,24 +54,25 @@ Next, we can create our Agent Engine instance. You can use the Gen AI sdk.
5454
[VertexAiSessionService](session.md###sessionservice-implementations) is compatible with Vertex AI Express mode API Keys. We can
5555
instead initialize the session object without any project or location.
5656

57-
```py
58-
# Requires: pip install google-adk[vertexai]
59-
# Plus environment variable setup:
60-
# GOOGLE_GENAI_USE_VERTEXAI=TRUE
61-
# GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
62-
from google.adk.sessions import VertexAiSessionService
63-
64-
# The app_name used with this service should be the Reasoning Engine ID or name
65-
APP_ID = "your-reasoning-engine-id"
66-
67-
# Project and location are not required when initializing with Vertex Express Mode
68-
session_service = VertexAiSessionService(agent_engine_id=APP_ID)
69-
# Use REASONING_ENGINE_APP_ID when calling service methods, e.g.:
70-
# session = await session_service.create_session(app_name=REASONING_ENGINE_APP_ID, user_id= ...)
71-
```
57+
```py
58+
# Requires: pip install google-adk[vertexai]
59+
# Plus environment variable setup:
60+
# GOOGLE_GENAI_USE_VERTEXAI=TRUE
61+
# GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
62+
from google.adk.sessions import VertexAiSessionService
63+
64+
# The app_name used with this service should be the Reasoning Engine ID or name
65+
APP_ID = "your-reasoning-engine-id"
66+
67+
# Project and location are not required when initializing with Vertex Express Mode
68+
session_service = VertexAiSessionService(agent_engine_id=APP_ID)
69+
# Use REASONING_ENGINE_APP_ID when calling service methods, e.g.:
70+
# session = await session_service.create_session(app_name=REASONING_ENGINE_APP_ID, user_id= ...)
71+
```
7272
!!! info Session Service Quotas
7373

7474
For Free Express Mode Projects, `VertexAiSessionService` has the following quota:
75+
7576
- 100 Session Entities
7677
- 10,000 Event Entities
7778

@@ -80,28 +81,29 @@ instead initialize the session object without any project or location.
8081
[VertexAiMemoryBankService](memory.md###memoryservice-implementations) is compatible with Vertex AI Express mode API Keys. We can
8182
instead initialize the memory object without any project or location.
8283

83-
```py
84-
# Requires: pip install google-adk[vertexai]
85-
# Plus environment variable setup:
86-
# GOOGLE_GENAI_USE_VERTEXAI=TRUE
87-
# GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
88-
from google.adk.sessions import VertexAiMemoryBankService
89-
90-
# The app_name used with this service should be the Reasoning Engine ID or name
91-
APP_ID = "your-reasoning-engine-id"
92-
93-
# Project and location are not required when initializing with Vertex Express Mode
94-
session_service = VertexAiMemoryBankService(agent_engine_id=APP_ID)
95-
# Generate a memory from that session so the Agent can remember relevant details about the user
96-
# memory = await memory_service.add_session_to_memory(session)
97-
```
84+
```py
85+
# Requires: pip install google-adk[vertexai]
86+
# Plus environment variable setup:
87+
# GOOGLE_GENAI_USE_VERTEXAI=TRUE
88+
# GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
89+
from google.adk.sessions import VertexAiMemoryBankService
90+
91+
# The app_name used with this service should be the Reasoning Engine ID or name
92+
APP_ID = "your-reasoning-engine-id"
93+
94+
# Project and location are not required when initializing with Vertex Express Mode
95+
session_service = VertexAiMemoryBankService(agent_engine_id=APP_ID)
96+
# Generate a memory from that session so the Agent can remember relevant details about the user
97+
# memory = await memory_service.add_session_to_memory(session)
98+
```
9899
!!! info Memory Service Quotas
99100

100101
For Free Express Mode Projects, `VertexAiMemoryBankService` has the following quota:
102+
101103
- 200 Memory Entities
102104

103105
## Code Sample: Weather Agent with Session and Memory using Vertex AI Express Mode
104106

105107
In this sample, we create a weather agent that utilizes both `VertexAiSessionService` and `VertexAiMemoryBankService` for context maangement, allowing our agent to recall user prefereneces and conversations!
106108

107-
**[Weather Agent with Session and Memory using Vertex Express Mode](https://github.com/google/adk-docs/blob/main/examples/python/notebooks/express-mode-weather-agent.ipynb)**
109+
**[Weather Agent with Session and Memory using Vertex AI Express Mode](https://github.com/google/adk-docs/blob/main/examples/python/notebooks/express-mode-weather-agent.ipynb)**

examples/python/notebooks/express-mode-weather-agent.ipynb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"outputs": [],
9999
"source": [
100100
"# Gemini API Key (Get from Vertex Express Mode)\n",
101-
"easygcp_api_key = \"INSERT_USER_ID\" #@param {type:\"string\"}\n",
101+
"easygcp_api_key = \"INSERT_API_KEY\" #@param {type:\"string\"}\n",
102102
"os.environ[\"GOOGLE_API_KEY\"] = easygcp_api_key\n",
103103
"# Set vertex to true\n",
104104
"os.environ[\"GOOGLE_GENAI_USE_VERTEXAI\"] = \"True\"\n",
@@ -128,7 +128,7 @@
128128
"# --- Define Model Constants for easier use ---\n",
129129
"\n",
130130
"# Use an allowlisted model for EasyGCP, we will use gemini 2.0\n",
131-
"MODEL_GEMINI_2_5_PRO = \"gemini-2.5-pro\"\n",
131+
"MODEL_GEMINI_2_0_FLASH = \"gemini-2.0-flash-001\"\n",
132132
"\n",
133133
"print(\"\\nEnvironment configured.\")"
134134
]
@@ -222,7 +222,7 @@
222222
"We configure it with several key parameters:\n",
223223
"\n",
224224
"* `name`: A unique identifier for this agent (e.g., \"weather\\_agent\\_v1\"). \n",
225-
"* `model`: Specifies which LLM to use (e.g., `MODEL_GEMINI_2_5_PRO`). We'll start with a specific Gemini model. \n",
225+
"* `model`: Specifies which LLM to use (e.g., `MODEL_GEMINI_2_0_FLASH`). We'll start with a specific Gemini model. \n",
226226
"* `description`: A concise summary of the agent's overall purpose. This becomes crucial later when other agents need to decide whether to delegate tasks to *this* agent. \n",
227227
"* `instruction`: Detailed guidance for the LLM on how to behave, its persona, its goals, and specifically *how and when* to utilize its assigned `tools`. \n",
228228
"* `tools`: A list containing the actual Python tool functions the agent is allowed to use (e.g., `[get_weather]`).\n",
@@ -244,17 +244,18 @@
244244
"\n",
245245
"weather_agent = Agent(\n",
246246
" name=\"weather_agent_v1\",\n",
247-
" model=MODEL_GEMINI_2_5_PRO,\n",
247+
" model=MODEL_GEMINI_2_0_FLASH,\n",
248248
" description=\"Provides weather information for specific cities.\",\n",
249249
" instruction=\"You are a helpful weather assistant. \"\n",
250250
" \"When the user asks for the weather in a specific city, \"\n",
251251
" \"use the 'get_weather' tool to find the information. \"\n",
252252
" \"If the tool returns an error, inform the user politely. \"\n",
253-
" \"If the tool is successful, present the weather report clearly.\",\n",
253+
" \"If the tool is successful, present the weather report clearly.\"\n",
254+
" \"You can help the user find what city has better weather based on their preferences using the preload memory tool.\",\n",
254255
" tools=[get_weather, adk.tools.preload_memory_tool.PreloadMemoryTool()], # Pass the function directly\n",
255256
")\n",
256257
"\n",
257-
"print(f\"Agent '{weather_agent.name}' created using model '{MODEL_GEMINI_2_5_PRO}'.\")"
258+
"print(f\"Agent '{weather_agent.name}' created using model '{MODEL_GEMINI_2_0_FLASH}'.\")"
258259
]
259260
},
260261
{
@@ -534,8 +535,8 @@
534535
"# @title Create a Memory Based on the Previous Session\n",
535536
"\n",
536537
"# We can generate a memory given the previous session id\n",
537-
"memory_service.add_session_to_memory(session)\n",
538-
"response"
538+
"session = await session_service.get_session(app_name=APP_ID, session_id=SESSION_ID, user_id = USER_ID)\n",
539+
"await memory_service.add_session_to_memory(session=session)"
539540
],
540541
"metadata": {
541542
"id": "T1ZZua59gP4Q"
@@ -554,7 +555,7 @@
554555
"\n",
555556
"print(f\"New Session created: App='{APP_NAME}', User='{USER_ID}', Session='{SESSION_ID}'\")\n",
556557
"\n",
557-
"await call_agent_async(\"What weather do I prefer?\",\n",
558+
"await call_agent_async(\"What city has the best weather for me based on my preferences?\",\n",
558559
" runner=runner,\n",
559560
" user_id=USER_ID,\n",
560561
" session_id=SESSION_ID)"
@@ -581,4 +582,4 @@
581582
},
582583
"nbformat": 4,
583584
"nbformat_minor": 0
584-
}
585+
}

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ nav:
151151
- Session: sessions/session.md
152152
- State: sessions/state.md
153153
- Memory: sessions/memory.md
154-
- Try Vertex AI for free: sessions/express-mode.md
154+
- Vertex AI Express Mode: sessions/express-mode.md
155155
- Callbacks:
156156
- callbacks/index.md
157157
- Types of callbacks: callbacks/types-of-callbacks.md

0 commit comments

Comments
 (0)