Agent Platform Memory Bank 可讓代理管理跨工作階段的長期記憶。搭配 Agent Development Kit (ADK) 使用時,代理會自動安排對 Memory Bank 的呼叫,根據使用者互動儲存及擷取記憶。
本文說明如何建立 ADK 代理、設定使用 Memory Bank,以及與代理互動來生成及存取記憶。
如要瞭解如何在不使用 ADK 的情況下直接呼叫 API,請參閱「Memory Bank API 快速入門導覽課程」。
使用 ADK 記憶服務和 Memory Bank 管理記憶
VertexAiMemoryBankService 是 ADK 的 Memory Bank 包裝函式,由 ADK 的 BaseMemoryService 定義。您可以定義回呼和工具,與記憶服務互動,讀取及寫入記憶。
VertexAiMemoryBankService 介面包含:
memory_service.add_session_to_memory會使用所提供adk.Session中的所有事件做為來源內容,向記憶庫觸發GenerateMemories要求。您可以在回呼中使用callback_context.add_session_to_memory,安排對這個方法的呼叫。from google.adk.agents.callback_context import CallbackContext async def add_session_to_memory_callback(callback_context: CallbackContext): await callback_context.add_session_to_memory() return Nonememory_service.add_events_to_memory,這會使用事件子集,觸發傳送至 Memory Bank 的GenerateMemories要求。您可以在回呼中使用callback_context.add_events_to_memory,對這個方法進行呼叫。from google.adk.agents.callback_context import CallbackContext async def add_events_to_memory_callback(callback_context: CallbackContext): await callback_context.add_events_to_memory(events=callback_context.session.events[-5:-1]) return Nonememory_service.search_memory會觸發RetrieveMemories要求,向 Memory Bank 擷取目前user_id和app_name的相關記憶。您可以使用內建記憶體工具 (LoadMemoryTool或PreloadMemoryTool),或叫用tool_context.search_memory的自訂工具,來安排對這個方法的呼叫。
事前準備
如要完成本教學課程示範的步驟,請先按照「設定記憶庫」頁面中「開始使用」一節的步驟操作。
設定環境變數
如要使用 ADK,請設定環境變數:
import os
os.environ["GOOGLE_GENAI_USE_ENTERPRISE"] = "TRUE"
os.environ["GOOGLE_CLOUD_PROJECT"] = "PROJECT_ID"
os.environ["GOOGLE_CLOUD_LOCATION"] = "LOCATION"
更改下列內容:
- PROJECT_ID:專案 ID。
- LOCATION:所在區域。請參閱 Memory Bank 支援的區域。
建立 ADK 代理
如要建立具備記憶體的代理,請設定工具和回呼,用於協調對記憶體服務的呼叫。
定義記憶生成回呼
如要自動調度記憶生成呼叫,請建立會觸發記憶生成的回呼函式。您可以傳送部分事件 (使用 callback_context.add_events_to_memory) 或工作階段中的所有事件 (使用 callback_context.add_session_to_memory),在背景處理:
from google.adk.agents.callback_context import CallbackContext
async def generate_memories_callback(callback_context: CallbackContext):
# Option 1 (Recommended): Send events to Memory Bank for memory generation,
# which is ideal for incremental processing of events.
await callback_context.add_events_to_memory(
events=callback_context.session.events[-5:-1])
# Option 2: Send the full session to Memory Bank for memory generation.
# It's recommended to only call this at the end of a session to minimize
# how many times a single event is re-processed.
await callback_context.add_session_to_memory()
return None
定義記憶體檢索工具
開發 ADK 代理時,請加入記憶工具,控管代理擷取記憶的時間,以及記憶如何納入提示。
如果使用 PreloadMemoryTool,代理會在每個回合開始時擷取記憶,並將擷取的記憶納入系統指令,這有助於建立使用者的基準情境。如果使用 LoadMemoryTool,模型會在判斷需要記憶才能回答使用者查詢時,呼叫這項工具。
from google import adk
from google.adk.tools.load_memory_tool import LoadMemoryTool
from google.adk.tools.preload_memory_tool import PreloadMemoryTool
memory_retrieval_tools = [
# Option 1: Retrieve memories at the start of every turn.
PreloadMemoryTool(),
# Option 2: Retrieve memories via tool calls. The model will only call this tool
# when it decides that memories are necessary to respond to the user query.
LoadMemoryTool()
]
agent = adk.Agent(
model="gemini-3.5-flash",
name='stateful_agent',
instruction="""You are a Vehicle Voice Agent, designed to assist users with information and in-vehicle actions.
1. **Direct Action:** If a user requests a specific vehicle function (e.g., "turn on the AC"), execute it immediately using the corresponding tool. You don't have the outcome of the actual tool execution, so provide a hypothetical tool execution outcome.
2. **Information Retrieval:** Respond concisely to general information requests with your own knowledge (e.g., restaurant recommendation).
3. **Clarity:** When necessary, try to seek clarification to better understand the user's needs and preference before taking an action.
4. **Brevity:** Limit responses to under 30 words.
""",
tools=memory_retrieval_tools,
after_agent_callback=generate_memories_callback
)
或者,您也可以建立自訂工具來擷取記憶,這在您想指示代理何時擷取記憶時非常實用:
from google import adk
from google.adk.tools import ToolContext, FunctionTool
async def search_memories(query: str, tool_context: ToolContext):
"""Query this tool when you need to fetch information about user preferences."""
return await tool_context.search_memory(query)
agent = adk.Agent(
model="gemini-3.5-flash",
name='stateful_agent',
instruction="""...""",
tools=[FunctionTool(func=search_memories)],
after_agent_callback=generate_memories_callback
)
定義 ADK Memory Bank 記憶體服務和 Memory Bank 執行個體
建立啟用記憶功能的代理後,您必須將其連結至記憶服務。設定 ADK 記憶體服務的程序,取決於 ADK 代理程式執行的位置。執行階段會自動調度管理代理、工具和回呼的執行作業。
建立 Memory Bank 執行個體
您必須先建立記憶庫執行個體。如果使用 Agent Runtime 部署代理,則可略過這個步驟。如要進一步瞭解如何自訂記憶庫行為,請參閱「設定記憶庫」頁面的「設定記憶庫執行個體」一節。
import agentplatform
client = agentplatform.Client(
project="PROJECT_ID",
location="LOCATION"
)
# If you don't have a Memory Bank instance already, create a
# Memory Bank instance using the default configuration.
memory_bank = client.memory_banks.create()
# Optionally, print out the resource name. You will need the
# resource name if you want to interact with your Memory Bank instance later on.
print(memory_bank.name)
memory_bank_id = memory_bank.name.split("/")[-1]
更改下列內容:
- PROJECT_ID:專案 ID。
- LOCATION:所在區域。請參閱 Memory Bank 支援的地區。
建立 ADK 執行階段
將 Memory Bank 執行個體 ID 傳遞至執行階段或部署指令碼,讓代理使用 Memory Bank 做為 ADK 記憶服務。
本機執行器
adk.Runner 通常用於本機環境,例如 Colab。在這種情況下,您需要直接建立記憶體服務和執行工具。
import asyncio
from google.adk.memory import VertexAiMemoryBankService
from google.adk.sessions import VertexAiSessionService
from google.genai import types
memory_service = VertexAiMemoryBankService(
project="PROJECT_ID",
location="LOCATION",
agent_engine_id="MEMORY_BANK_ID",
)
# You can use any ADK session service. This example uses Sessions.
session_service = VertexAiSessionService(
project="PROJECT_ID",
location="LOCATION",
agent_engine_id="SESSIONS_ID",
)
runner = adk.Runner(
agent=agent,
app_name="APP_NAME",
session_service=session_service,
memory_service=memory_service
)
async def call_agent(query, session, user_id):
content = types.Content(role='user', parts=[types.Part(text=query)])
events = runner.run_async(
user_id=user_id, session_id=session, new_message=content)
async for event in events:
if event.is_final_response():
final_response = event.content.parts[0].text
print("Agent Response: ", final_response)
更改下列內容:
- PROJECT_ID:專案 ID。
- LOCATION:所在區域。請參閱 Memory Bank 支援的地區。
- APP_NAME:ADK 應用程式名稱。應用程式名稱會包含在生成的記憶
scope字典中,因此記憶會在使用者和應用程式之間隔離。 - MEMORY_BANK_ID:Memory Bank 執行個體 ID。例如,
projects/my-project/locations/us-central1/reasoningEngines/456中的456。 - SESSIONS_ID:Agent Platform 工作階段執行個體 ID。例如,
projects/my-project/locations/us-central1/reasoningEngines/789中的789。
Gemini Enterprise Agent Platform 的 Agent Runtime
Agent Runtime ADK 範本 (AdkApp) 可用於本機,也可將 ADK 代理部署至 Agent Runtime。部署至 Agent Platform 時,Memory Bank ADK 範本會使用 VertexAiMemoryBankService 做為預設記憶體服務。因此,您可以建立 Memory Bank 執行個體,並在單一步驟中部署至執行階段。
如要進一步瞭解如何設定記憶庫執行個體,包括如何自訂記憶庫行為,請參閱「設定記憶庫」。
使用下列程式碼,將具備記憶功能的 ADK 代理部署至 Agent Runtime:
import asyncio
import agentplatform
from agentplatform.frameworks import AdkApp
client = agentplatform.Client(
project="PROJECT_ID",
location="LOCATION"
)
adk_app = AdkApp(agent=agent)
# Create a new resource with your agent deployed to Agent Runtime.
# The Agent Runtime instance will also include an empty Memory Bank instance.
runtime_instance = client.runtimes.create(
agent=adk_app,
config={
"staging_bucket": "STAGING_BUCKET",
"requirements": ["google-cloud-agentplatform[runtimes,adk]"]
}
)
# Alternatively, update an existing resource to deploy your agent to Agent Platform.
# Your agent will have access to the Runtime instance's existing memories.
runtime_instance = client.runtimes.update(
name=agent_engine.api_resource.name,
agent=adk_app,
config={
"staging_bucket": "STAGING_BUCKET",
"requirements": ["google-cloud-agentplatform[runtimes,adk]"]
}
)
async def call_agent(query, session_id, user_id):
async for event in agent_engine.async_stream_query(
user_id=user_id,
session_id=session_id,
message=query,
):
print(event)
更改下列內容:
- PROJECT_ID:專案 ID。
- LOCATION:所在區域。請參閱 Memory Bank 支援的區域。
- STAGING_BUCKET:用於暫存 Agent Runtime 的 Cloud Storage bucket。
在本機執行時,ADK 範本會使用 InMemoryMemoryService 做為預設記憶體服務。不過,您可以覆寫預設記憶體服務,改用 VertexAiMemoryBankService:
def memory_bank_service_builder():
return VertexAiMemoryBankService(
project="PROJECT_ID",
location="LOCATION",
agent_engine_id="MEMORY_BANK_ID"
)
adk_app = AdkApp(
agent=adk_agent,
# Override the default memory service.
memory_service_builder=memory_bank_service_builder
)
async def call_agent(query, session_id, user_id):
# adk_app is a local agent. If you want to deploy it to Agent Runtime,
# use `client.runtimes.create(...)` or `client.runtimes.update(...)`
# and call the returned Agent Runtime instance instead.
async for event in adk_app.async_stream_query(
user_id=user_id,
session_id=session_id,
message=query,
):
print(event)
更改下列內容:
- PROJECT_ID:專案 ID。
- LOCATION:所在區域。請參閱 Memory Bank 支援的區域。
- MEMORY_BANK_ID:Memory Bank 要使用的 Memory Bank 執行個體 ID。例如,
projects/my-project/locations/us-central1/reasoningEngines/456中的456。
Cloud Run
如要將代理部署至 Cloud Run,請參閱 ADK 說明文件,瞭解如何定義要部署至 Cloud Run 的代理。
adk deploy cloud_run \
...
--memory_service_uri=agentengine://AGENT_ENGINE_ID
Google Kubernetes Engine (GKE)
如要將代理部署至 GKE,請參閱 ADK 說明文件中的操作說明,瞭解如何定義要部署至 GKE 的代理。
adk deploy gke \
...
--memory_service_uri=agentengine://AGENT_ENGINE_ID
ADK Web
ADK 網頁介面可讓您直接在瀏覽器中測試代理。
export GOOGLE_CLOUD_PROJECT="PROJECT_ID"
export GOOGLE_CLOUD_LOCATION="LOCATION"
adk web --memory_service_uri=agentengine://MEMORY_BANK_ID
更改下列內容:
- PROJECT_ID:專案 ID。
- LOCATION:所在區域。請參閱 Memory Bank 支援的區域。
- MEMORY_BANK_ID:Memory Bank 執行個體 ID。例如,
projects/my-project/locations/us-central1/reasoningEngines/456中的456。
與代理互動
定義代理並設定記憶庫後,即可與代理互動。如果在初始化代理時,您提供了觸發記憶生成的回呼,則每次叫用代理時,都會觸發記憶生成。
記憶會儲存在 {"user_id": USER_ID, "app_name":
APP_NAME} 範圍,對應到執行代理的使用者 ID 和應用程式名稱。
與代理互動的方法取決於其執行環境:
本機執行器
# Use `asyncio.run(session_service.create(...))` if you're running this
# code as a standard Python script.
session = await session_service.create_session(
app_name="APP_NAME",
user_id="USER_ID"
)
# Use `asyncio.run(call_agent(...))` if you're running this code as a
# standard Python script.
await call_agent(
"Can you fix the temperature?",
session.id,
"USER_ID"
)
更改下列內容:
- APP_NAME:執行器的應用程式名稱。
- USER_ID:使用者 ID。這個工作階段產生的記憶體會以這個不透明 ID 為索引鍵。系統會將生成的記憶範圍儲存為
{"user_id": "USER_ID"}。
Agent Runtime
使用 ADK 範本時,您可以呼叫 Agent Runtime,與記憶體和工作階段互動。
# Use `asyncio.run(agent_engine.async_create_session(...))` if you're
# running this code as a standard Python script.
session = await agent_engine.async_create_session(user_id="USER_ID")
# Use `asyncio.run(call_agent(...))` if you're running this code as a
# standard Python script.
await call_agent(
"Can you fix the temperature?",
session.get("id"),
"USER_ID"
)
更改下列內容:
- USER_ID:使用者 ID。系統會根據這個不透明 ID,為這個工作階段產生的記憶建立索引。生成的記憶範圍會儲存為
{"user_id": "USER_ID"}。
Cloud Run
請參閱 ADK Cloud Run 部署說明文件的「測試代理」一節。
GKE
請參閱 ADK GKE 部署說明文件的「測試代理」一節。
ADK Web
如要使用 ADK Web,請前往本機伺服器 http://localhost:8000。
根據預設,ADK Web 會將使用者 ID 設為 user。如要覆寫預設使用者 ID,請在查詢參數中加入 userId,例如 http://localhost:8000?userId=YOUR_USER_ID。
詳情請參閱 ADK 說明文件中的 ADK Web 頁面。
互動樣本
第一堂課
如果使用 PreloadMemoryTool,代理會在每輪對話開始時嘗試擷取記憶,存取使用者先前與代理溝通時提供的偏好設定。代理與使用者最初互動時,沒有可擷取的記憶。因此,代理不知道任何使用者偏好設定,例如偏好的溫度,如以下範例所示:
第一回合:
使用者:「可以調整溫度嗎?」
(工具呼叫):ADK 嘗試擷取記憶,但沒有可用的記憶。
模型:「你偏好哪種溫度?」
(回呼):ADK 觸發記憶生成。未擷取任何記憶。
第二輪:
使用者:我覺得 71 度很舒適。
(工具呼叫):ADK 嘗試擷取記憶,但沒有可用的記憶。
模型:好的,我已將溫度調到 21 度。
(回呼):ADK 觸發記憶生成。系統會建立「我喜歡 21.5 度的溫度」這項記憶。
第二場
下次使用相同應用程式名稱和使用者 ID 的工作階段時,就能使用擷取的記憶。如果使用者提供的資訊與現有記憶相似或矛盾,新資訊會與現有記憶合併。
第一輪
使用者:調整溫度。真不舒服!
(工具呼叫):ADK 嘗試擷取記憶。系統擷取「我喜歡 21.5 度的溫度」這項記憶。
模型:好的,我已將溫度調到 21 度。
(回呼):ADK 觸發記憶生成。使用者未分享任何有意義的內容,因此系統不會擷取任何記憶。
第二輪
使用者:其實我希望早上暖和一點。
(工具呼叫):ADK 嘗試擷取記憶。系統擷取了「我喜歡 21.5 度的溫度」這項記憶。
Model:好的,我已調高溫度。
(回呼):ADK 觸發記憶生成。現有的記憶「我喜歡 21 度的溫度」會更新為「我通常喜歡 21 度的溫度,但早上喜歡暖一點」。
搭配使用多區域 Memory Bank 與區域執行階段
使用內建 Memory Bank 的執行階段時,代理和 Memory Bank 預設會部署在相同區域。不過,您可以將兩者分離,使用多區域記憶庫 (例如 us) 搭配區域執行階段 (例如 us-central1)。這樣一來,您就能在不同區域部署項目中,維護中央記憶庫。
如要使用多區域 Memory Bank,必須覆寫預設 ADK 記憶體服務建構工具,指向多區域位置和對應的 Memory Bank ID。
import agentplatform
from google.adk.memory import VertexAiMemoryBankService
from agentplatform.frameworks import AdkApp
# Create the Memory Bank instance in a multi-region location (for example, 'us')
client_mb = agentplatform.Client(project="PROJECT_ID", location="us")
memory_bank = client_mb.memory_banks.create()
memory_bank_id = memory_bank.name.split("/")[-1]
# Point your memory service to the 'us' location, 'us' Memory Bank
def memory_bank_service_builder():
return VertexAiMemoryBankService(
project="PROJECT_ID",
location="us",
agent_engine_id=memory_bank_id
)
# Create the AdkApp with the overridden builder
adk_app = AdkApp(
agent=agent,
memory_service_builder=memory_bank_service_builder
)
# Deploy the runtime to a specific region (for example, 'us-central1')
client_runtime = agentplatform.Client(project="PROJECT_ID", location="us-central1")
runtime_instance = client_runtime.runtimes.create(
agent=adk_app,
config={
"staging_bucket": "STAGING_BUCKET",
"requirements": ["google-cloud-agentplatform[runtimes,adk]"]
}
)
更改下列內容:
- PROJECT_ID:專案 ID。
- STAGING_BUCKET:用於暫存 Agent Runtime 的 Cloud Storage bucket。
清除所用資源
如要清除這項專案中使用的所有資源,您可以刪除 Google Cloud快速入門導覽課程使用的專案。
或者,您也可以按照下列步驟,刪除在本教學課程中建立的個別資源:
使用下列程式碼範例刪除 Agent Runtime 執行個體,這也會刪除該執行階段的所有工作階段或記憶。
client_runtime.runtimes.delete(name=runtime_instance.api_resource.name, force=True) client_mb.memory_banks.delete(name=memory_bank.name, force=True)刪除在本機建立的所有檔案。