Skip to content

Vertex AI Express Mode: Using Vertex AI Sessions and Memory

Vertex AI 快速模式:使用 Vertex AI 会话和记忆

Supported in ADKPython v0.1.0Java v0.1.0

If you are interested in using either 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!

如果您有兴趣使用 VertexAiSessionServiceVertexAiMemoryBankService,但没有 Google Cloud 项目,则可以注册 Vertex AI 快速模式并免费获得访问权限试用这些服务!您可以使用合格的gmail 帐户在此处注册。有关 Vertex AI 快速模式的更多详细信息,请参阅概述页面。 注册后,获取API 密钥,您就可以开始使用 Vertex AI 会话和记忆服务以及本地 ADK 智能体!

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, number of Agent Engines is restricted to 10 and deployment to Agent Engine is reserved for the paid tier only. To remove quota restrictions and use all of Vertex AI's services, add a billing account to your Express Mode project.

Vertex AI 快速模式在免费层级中具有某些限制。免费快速模式项目仅有效期为 90 天,并且只有选定的服务可用且配额有限。例如,Agent Engine 的数量限制为 10 个,而部署到 Agent Engine 仅限于付费层级。要删除配额限制并使用 Vertex AI 的所有服务,请将计费帐户添加到您的快速模式项目中。

Create an Agent Engine

创建 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:

Session 对象是 AgentEngine 的子对象。当使用 Vertex AI 快速模式时,我们可以创建一个空的 AgentEngine 父对象来管理所有 SessionMemory 对象。 首先,确保正确设置了环境变量。例如,在 Python 中:

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 Vertex AI SDK.

接下来,我们可以创建 Agent Engine 实例。您可以使用 Vertex AI SDK。

  1. Import Vertex AI SDK.

    导入 Vertex AI SDK。

    import vertexai
    from vertexai import agent_engines
    
  2. Initialize Vertex AI Client with your API key and create an agent engine instance.

    使用 API 密钥初始化 Vertex AI 客户端并创建 agent engine 实例。

    # Create Agent Engine with Gen AI SDK
    # 使用 Gen AI SDK 创建 Agent Engine
    client = vertexai.Client(
      api_key="YOUR_API_KEY",
    )
    
    agent_engine = client.agent_engines.create(
      config={
        "display_name": "Demo Agent Engine",
        "description": "Agent Engine for Session and Memory",
      })
    
  3. Replace YOUR_AGENT_ENGINE_DISPLAY_NAME and YOUR_AGENT_ENGINE_DESCRIPTION with your use case.

    YOUR_AGENT_ENGINE_DISPLAY_NAMEYOUR_AGENT_ENGINE_DESCRIPTION 替换为您的用例。

  4. Get Agent Engine name and ID from the response to use with Memories and Sessions.

    从响应中获取 Agent Engine 名称和 ID,以便与 Memories 和 Sessions 一起使用。

    APP_ID = agent_engine.api_resource.name.split('/')[-1]
    

Managing Sessions with a VertexAiSessionService

使用 VertexAiSessionService 管理会话

VertexAiSessionService is compatible with Vertex AI Express mode API Keys. We can instead initialize the session object without any project or location.

VertexAiSessionService 与 Vertex AI 快速模式 API 密钥兼容。我们可以不使用任何项目或位置来初始化会话对象。

# 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
# 需要: pip install google-adk[vertexai]
# 加上环境变量设置:
# 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 to Reasoning Engine ID or name
# 与此服务一起使用的 app_name 应该是 Reasoning Engine ID 或名称
APP_ID = "your-reasoning-engine-id"

# Project and location are not required when initializing with Vertex Express Mode
# 使用 Vertex 快速模式初始化时不需要项目和位置
session_service = VertexAiSessionService(agent_engine_id=APP_ID)
# Use REASONING_ENGINE_APP_ID when calling service methods, e.g.:
# 调用服务方法时使用 REASONING_ENGINE_APP_ID,例如:
# session = await session_service.create_session(app_name=APP_ID, user_id= ...)

Info

对于免费快速模式项目,VertexAiSessionService 具有以下配额:

For Free Express Mode Projects, VertexAiSessionService has following quota:

  • 10 Create, delete, or update Vertex AI Agent Engine sessions per minute 每分钟创建、删除或更新 10 个 Vertex AI Agent Engine 会话
  • 30 Append event to Vertex AI Agent Engine sessions per minute 每分钟向 Vertex AI Agent Engine 会话附加 30 个事件

Managing Memories with a VertexAiMemoryBankService

使用 VertexAiMemoryBankService 管理记忆

VertexAiMemoryBankService is compatible with Vertex AI Express mode API Keys. We can instead initialize the memory object without any project or location.

VertexAiMemoryBankService 与 Vertex AI 快速模式 API 密钥兼容。我们可以不使用任何项目或位置来初始化记忆对象。

# 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
# 需要: pip install google-adk[vertexai]
# 加上环境变量设置:
# 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 to Reasoning Engine ID or name
# 与此服务一起使用的 app_name 应该是 Reasoning Engine ID 或名称
APP_ID = "your-reasoning-engine-id"

# Project and location are not required when initializing with Vertex Express Mode
# 使用 Vertex 快速模式初始化时不需要项目和位置
memory_service = VertexAiMemoryBankService(agent_engine_id=APP_ID)
# Generate a memory from that session so that Agent can remember relevant details about the user
# 从该会话生成记忆,以便 Agent 可以记住有关用户的相关详细信息
# memory = await memory_service.add_session_to_memory(session)

Info

对于免费快速模式项目,VertexAiMemoryBankService 具有以下配额:

For Free Express Mode Projects, VertexAiMemoryBankService has following quota:

  • 10 Create, delete, or update Vertex AI Agent Engine memory resources per minute 每分钟创建、删除或更新 10 个 Vertex AI Agent Engine 记忆资源
  • 10 Get, list, or retrieve from Vertex AI Agent Engine Memory Bank per minute 每分钟从 Vertex AI Agent Engine 记忆库获取、列出或检索 10 个记忆

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

代码示例:使用 Vertex AI 快速模式的天气智能体(包含会话和记忆)

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!

在此示例中,我们创建了一个天气智能体,该智能体利用 VertexAiSessionServiceVertexAiMemoryBankService 进行上下文管理,使我们的智能体能够回忆用户偏好和对话!

Weather Agent with Session and Memory using Vertex AI Express Mode

使用 Vertex AI 快速模式的天气智能体(包含会话和记忆)