Skip to content

用于 ADK 的 Google Cloud Agent Platform Express Mode

Supported in ADKPython v0.1.0Java v0.1.0Preview

Google Cloud Agent Platform 快速模式提供了一个无成本的访问层级,用于原型开发和测试,让你无需创建完整的 Google Cloud 项目即可使用 Agent Platform 服务。该服务包括对许多强大的 Agent Platform 服务的访问,包括:

你可以使用 Google 账号注册快速模式账号,并获取用于 ADK 的 API 密钥。通过 Google Cloud 控制台获取 API 密钥。更多信息请参阅Agent Platform 快速模式

预览版本

Agent Platform 快速模式功能目前处于预览阶段。更多信息请参阅发布阶段说明

Agent Platform 快速模式限制

Agent Platform 快速模式项目仅在 90 天内有效,且只有部分服务可用并有配额限制。例如,Agent Runtime 实例数量限制为 10 个,且部署到 Agent Runtime 需要付费访问权限。如需解除配额限制并使用 Agent Platform 的所有服务,请为你的快速模式项目添加计费账号。

配置 Agent Runtime 容器

使用 Agent Platform 快速模式时,需要创建一个 AgentEngine 对象来让 Agent Platform 管理智能体组件,例如 SessionMemory 对象。通过这种方式,Session 对象将作为 AgentEngine 对象的子对象来处理。在运行智能体之前,请确保环境变量已正确设置,如下所示:

agent/.env
GOOGLE_GENAI_USE_ENTERPRISE=TRUE
GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE

接下来,使用 Agent Platform SDK 创建 Agent Runtime 实例。

  1. 导入 Agent Platform SDK。

    import vertexai
    from vertexai import agent_engines
    
  2. 使用你的 API 密钥初始化 Agent Platform 客户端并创建 Agent Engine 实例。

    # 使用 Gen AI SDK 创建 Agent Runtime
    client = vertexai.Client(
      api_key="YOUR_API_KEY",
    )
    
    agent_engine = client.agent_engines.create(
      config={
        "display_name": "Demo Agent Runtime",
        "description": "Agent Runtime for Session and Memory",
      })
    
  3. 从响应中获取 Agent Runtime 名称和 ID,以便用于记忆和会话。

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

使用 VertexAiSessionService 管理会话

VertexAiSessionService 兼容 Agent Platform 快速模式 API 密钥。你可以在不指定项目或位置的情况下初始化会话对象。

# 需要安装: pip install google-adk[vertexai]
# 加上以下环境变量设置:
# GOOGLE_GENAI_USE_ENTERPRISE=TRUE
# GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
from google.adk.sessions import VertexAiSessionService

# 使用此服务时,app_name 应为 Reasoning Engine 的 ID 或名称
APP_ID = "your-reasoning-engine-id"

# 使用 Agent Platform 快速模式初始化时不需要项目和位置信息
session_service = VertexAiSessionService(agent_engine_id=APP_ID)
# 调用服务方法时使用 REASONING_ENGINE_APP_ID,例如:
# session = await session_service.create_session(app_name=APP_ID, user_id= ...)

会话服务配额

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

  • 每分钟 10 次创建、删除或更新 Agent Runtime 会话的操作
  • 每分钟 30 次向 Agent Runtime 会话追加事件的操作

使用 VertexAiMemoryBankService 管理记忆

VertexAiMemoryBankService 兼容 Agent Platform 快速模式 API 密钥。你可以在不指定项目或位置的情况下初始化记忆对象。

# 需要安装: pip install google-adk[vertexai]
# 加上以下环境变量设置:
# GOOGLE_GENAI_USE_ENTERPRISE=TRUE
# GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
from google.adk.memory import VertexAiMemoryBankService

# 使用此服务时,app_name 应为 Reasoning Engine 的 ID 或名称
APP_ID = "your-reasoning-engine-id"

# 使用快速模式初始化时不需要项目和位置信息
memory_service = VertexAiMemoryBankService(agent_engine_id=APP_ID)
# 从该会话生成记忆,以便智能体记住用户的相关信息
# memory = await memory_service.add_session_to_memory(session)

记忆服务配额

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

  • 每分钟 10 次创建、删除或更新 Agent Runtime 记忆资源的操作
  • 每分钟 10 次获取、列出或检索 Agent Runtime Memory Bank 的操作

代码示例:具有会话和记忆功能的天气智能体

此代码示例展示了一个天气智能体,它同时使用 VertexAiSessionServiceVertexAiMemoryBankService 进行上下文管理,使你的智能体能够回忆用户偏好和对话历史。