Vertex AI Express Mode: Using Vertex AI Sessions and Memory for Free¶
如果你有兴趣使用 VertexAiSessionService
或 VertexAiMemoryBankService
,但没有 Google Cloud 项目,你可以注册 Vertex AI Express Mode,免费获取访问权限并试用这些服务!你可以使用符合条件的 gmail 账户在这里注册。有关 Vertex AI Express Mode 的更多详细信息,请参阅概述页面。
注册后,获取一个 API 密钥,你就可以开始在本地 ADK 智能体中使用 Vertex AI 会话和记忆服务了!
Info
Vertex AI Express Mode 在免费层级有一定的限制。免费的 Express Mode 项目只有 90 天有效期,只有特定服务可以使用且配额有限。例如,智能体引擎数量限制为 10 个,部署到智能体引擎仅限付费层级。要移除配额限制并使用 Vertex AI 的所有服务,请为你的 Express Mode 项目添加计费账户。
创建智能体引擎¶
Session
对象是 AgentEngine
的子对象。当使用 Vertex AI Express Mode 时,我们可以创建一个空的 AgentEngine
父对象来管理我们所有的 Session
和 Memory
对象。
首先,确保你的环境变量设置正确。例如,在 Python 中:
```env title="weather_agent/.env"
GOOGLE_GENAI_USE_VERTEXAI=TRUE
GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
```
Next, we can create our Agent Engine instance. You can use the Gen AI SDK.
-
导入 Gen AI SDK。
-
将 Vertex AI 设置为 True,然后使用 POST 请求创建智能体引擎
-
用你的用例替换
你的智能体引擎显示名称
和你的智能体引擎描述
。 -
从响应中获取智能体引擎名称和 ID
使用 VertexAiSessionService
管理会话¶
VertexAiSessionService 与 Vertex AI Express Mode API 密钥兼容。我们可以 在不需要任何项目或位置的情况下初始化会话对象。
```py
# 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=REASONING_ENGINE_APP_ID, user_id= ...)
```
Info
For Free Express Mode Projects, VertexAiSessionService
has the following quota:
- 100 Session Entities
- 10,000 Event Entities
使用 VertexAiMemoryBankService
管理记忆¶
VertexAiMemoryBankService 与 Vertex AI Express Mode API 密钥兼容。我们可以 在不需要任何项目或位置的情况下初始化记忆对象。
```py
# 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 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:
- 200 Memory Entities
代码示例:使用 Vertex AI Express Mode 的带会话和记忆的天气智能体¶
在这个示例中,我们创建了一个天气智能体,它使用 VertexAiSessionService
和 VertexAiMemoryBankService
进行上下文管理,让我们的智能体能够回忆用户偏好和对话!
Weather Agent with Session and Memory using Vertex AI Express Mode