将 Gemini Enterprise 智能体与 Google Workspace 集成

1. 准备工作

7zN3mxxU73oY8Gi.png

什么是 Gemini Enterprise?

Gemini Enterprise 是一个先进的智能体平台,它将 Google AI 的卓越能力带给每一位员工,融入每一个工作流。它使团队能够在同一安全环境中发现、创建、共享和运行 AI 智能体。

  • 使用高级模型:用户可以立即使用 Google 最强大的多模态 AI(包括 Gemini),以应对复杂的业务挑战。
  • 利用专业智能体:该套件包含 Google 提供的即用型智能体,可用于研究、编码和笔记,能立即创造价值。
  • 赋能每位员工:无代码和专业代码选项让所有部门的员工都能构建和管理自己的自定义智能体,实现工作流自动化。
  • 数据智能体:智能体可以安全地连接到公司内部数据和第三方应用,确保其回答符合语境。
  • 集中治理:管理员可以直观呈现和审核所有智能体活动,确保组织符合严格的安全和合规标准。
  • 通过生态系统进行扩展:该平台与广泛的合作伙伴应用和服务提供商网络集成,可将自动化扩展到不同的系统。

127f2ed7d484722c.png

Google Workspace 是什么?

Google Workspace 是一套基于云的高效办公和协作解决方案,专为个人、学校和企业打造:

  • 沟通:专业邮箱服务 (Gmail)、视频会议 (Meet) 和团队消息传递 (Chat)。
  • 内容创作:用于撰写文档(Google 文档)、构建电子表格(Google 表格)和设计演示文稿(Google 幻灯片)的工具。
  • 组织:共享日历(日历)和数字笔记(Keep)。
  • 存储空间:集中式云端空间,用于安全地保存和共享文件(云端硬盘)。
  • 管理:用于管理受管理的用户和安全设置的管理控制功能(Workspace 管理控制台)。

有哪些自定义集成?

Google Workspace 和 Gemini Enterprise 共同构建了一个强大的反馈环:Workspace 提供实时数据和协作情境,而 Gemini Enterprise 则提供实现智能工作流自动化所需的模型、智能体推理和编排能力。

  • 智能连接:Google 管理的数据存储区、API 和 MCP 服务器(Google 管理和自定义)使智能体能够安全、无缝地访问 Workspace 数据,并代表用户执行操作。
  • 自定义代理:团队可以使用无代码设计器或专业代码框架,构建基于管理员管理的 Workspace 数据和操作的专用代理。
  • 原生集成:Workspace 插件通过专用界面组件或后台进程,弥合了 AI 系统与 Chat 和 Gmail 等应用之间的差距。这样,智能体就能在用户需要时提供即时、情境感知的帮助。

通过将 Google Workspace 强大的生产力生态系统与 Gemini Enterprise 先进的智能体功能相结合,组织可以利用基于数据的自定义 AI 智能体,直接在团队日常使用的工具中实现复杂工作流的自动化,从而实现运营转型。

前提条件

如果您想在自己的环境中执行所有步骤,则需要:

构建内容

在此 Codelab 中,我们将构建三个与 Google Workspace 紧密集成的 Gemini Enterprise AI 智能体解决方案。他们将演示可用于与数据、操作和界面进行交互的架构模式。

无代码自定义智能体

此智能体可让用户使用自然语言搜索数据并对 Workspace 执行操作。它依赖于以下元素:

  • 模型:Gemini。
  • 数据和操作:适用于 Google Workspace(日历、Gmail、云端硬盘、NotebookLM)的 Gemini Enterprise 数据存储区、Google 搜索。
  • 智能体构建工具:Gemini Enterprise Agent Designer。
  • 代理主机:Gemini Enterprise。
  • 界面:Gemini Enterprise Web 应用。

90e42539e5959634.png60e62437ce29a818.png

专业代码自定义智能体

此智能体允许用户使用自定义工具和规则,以自然语言搜索数据并对 Workspace 执行操作。它依赖于以下元素:

  • 模型:Gemini。
  • 数据和操作:适用于 Google Workspace(日历、Gmail、云端硬盘、NotebookLM)的 Gemini Enterprise 数据存储区、Google 搜索、Google 管理的 Agent Search 的 Model Context Protocol (MCP) 服务器、用于发送 Google Chat 聊天消息的自定义工具函数(通过 Google Chat API)。
  • 智能体构建工具:智能体开发套件 (ADK)。
  • 智能体主机:Agent Runtime。
  • 界面:Gemini Enterprise Web 应用。

1647ebff031c42e7.pnga8087d2351e77fb4.png

默认智能体作为 Google Workspace 插件

此代理可让用户在 Workspace 应用界面中,使用自然语言搜索 Workspace 数据。它依赖于以下元素:

  • 模型:Gemini。
  • 数据:Gemini Enterprise 数据存储区,用于 Google Workspace(日历、Gmail、云端硬盘、NotebookLM)、Google 搜索。
  • 代理主机:Gemini Enterprise。
  • 界面:适用于 Chat 和 Gmail 的 Google Workspace 插件(可轻松扩展到 Google 日历、云端硬盘、文档、表格和幻灯片)。
  • Google Workspace 插件:Apps 脚本、Gemini Enterprise API、上下文(用户元数据、所选 Gmail 邮件)。

c8c63fb3f324fecf.pngd33b8cb50ee251b7.png

学习内容

  • Gemini Enterprise 与 Google Workspace 之间的集成点,可实现数据访问和操作执行。
  • 在 Gemini Enterprise 中托管的自定义智能体,可采用无代码和专业代码两种方式构建。
  • 用户可以通过 Gemini Enterprise Web 应用和 Google Workspace 应用访问智能体。

轻松访问此 Codelab

82JGvYaTp8dhuvv.png

2. 进行设置

查看概念

Gemini Enterprise 应用

Gemini Enterprise 应用可为最终用户提供搜索结果、操作和智能体。在 API 的上下文中,术语“应用”与“引擎”可以互换使用。应用必须连接到数据存储区,才能使用其中的数据来提供搜索结果、答案或操作。

Gemini Enterprise Web 应用

Gemini Enterprise Web 应用与 Gemini Enterprise 应用相关联。它是一个集中式 AI 大本营,员工可以在其中使用单个对话界面搜索孤立的公司数据、运行专用 AI 智能体来处理复杂的工作流,以及生成具有企业级隐私保护的专业级内容。

初始化和访问资源

在本部分中,您可以使用自己偏好的网络浏览器访问和配置以下资源。

Gemini Enterprise 应用

在新标签页中打开 Google Cloud 控制台,然后按照以下步骤操作:

  1. 选择您的项目。
  2. 在 Google Cloud 搜索字段中,搜索并选择 Gemini Enterprise,然后点击 + 创建应用。如果您没有 Gemini Enterprise 许可,系统会提示您激活 30 天免费试用许可。

  1. 将应用名称设置为 codelab。
  2. 系统会根据名称生成 ID,并将其显示在字段下方,请复制该 ID。
  3. 将多区域设置为 global (Global)。
  4. 点击创建。

8712ada39377205e.png

  1. 应用创建完毕后,系统会自动将您重定向到 Gemini Enterprise > 概览。
  2. 在获取完整访问权限下,点击设置身份。
  3. 在新界面中,选择使用 Google Identity,然后点击确认员工身份。

3209c156eff4ba43.png

  1. 系统会保存配置,并自动将您重定向到 Gemini Enterprise > 概览。
  2. 前往配置。
  3. 在功能管理标签页中,开启启用代理设计工具,然后点击保存。

f0cd9da419b41cb6.png

Gemini Enterprise Web 应用

在新的标签页中,从 Cloud 控制台打开 Gemini Enterprise,然后按照以下步骤操作:

  1. 点击名为 codelab 的应用。
  2. 复制显示的网址,因为我们将在后续步骤中使用该网址前往 Gemini Enterprise Web 应用。

b46ee6176744565d.png

3. 无代码自定义智能体

此智能体可让用户使用自然语言搜索数据并对 Workspace 执行操作。它依赖于以下元素:

  • 模型:Gemini。
  • 数据和操作:适用于 Google Workspace(日历、Gmail、云端硬盘、NotebookLM)的 Gemini Enterprise 数据存储区、Google 搜索。
  • 智能体构建工具:Gemini Enterprise Agent Designer。
  • 代理主机:Gemini Enterprise。
  • 界面:Gemini Enterprise Web 应用。

查看概念

Gemini

Gemini 是 Google 推出的一款多模态 LLM。它能帮助人们释放潜力,从而加强想象力、增加好奇心并提高工作效率。

Gemini Enterprise 数据存储区

Gemini Enterprise 数据存储区是一种实体,其中包含从第一方数据源(例如 Google Workspace)或第三方应用(例如 Jira 或 Salesforce)提取的数据。包含第三方应用数据的数据存储区也称为数据连接器。

Gemini Enterprise Agent Designer

Gemini Enterprise Agent Designer 是一个交互式无代码/低代码平台,用于在 Gemini Enterprise 中创建、管理和启动单步和多步智能体。

查看解决方案架构

e77aafb772502aaf.png

启用 API

Gemini Enterprise Workspace 数据存储区需要启用以下 API:

  1. 在 Google Cloud 控制台中,启用 Calendar API、Gmail API 和 People API:

573322606b715a69.png

  1. 依次点击菜单 ☰ > API 和服务 > 已启用的 API 和服务,然后确认 Google 日历 API、Gmail API 和 People API 均在列表中。

Gemini Enterprise Workspace 日历和 Gmail 操作需要配置权限请求页面:

  1. 在 Google Cloud 控制台中,依次点击菜单 ☰ > Google Auth 平台 > 品牌信息。

  1. 点击开始使用。
  2. 在应用信息下,将应用名称设置为 Codelab。
  3. 在用户支持邮箱中,选择一个支持邮箱,供用户在对其同意情况有疑问时与您联系。
  4. 点击下一步。
  5. 在受众群体下,选择内部。
  6. 点击下一步。
  7. 在联系信息下,输入一个邮箱,以便您接收有关项目变更的通知。
  8. 点击下一步。
  9. 在完成下,查看 Google API 服务用户数据政策,如果您同意,请选择我同意 Google API 服务:用户数据政策。
  10. 依次点击继续和创建。

578c2b38219b2f7b.png

  1. 系统会保存配置,并自动将您重定向到 Google Auth Platform > 概览。
  2. 前往数据访问权限。
  3. 点击添加或移除范围。
  4. 复制以下范围,并将其粘贴到手动添加范围字段中。
https://www.googleapis.com/auth/calendar.readonly
https://www.googleapis.com/auth/calendar.events
https://www.googleapis.com/auth/calendar.calendars
https://www.googleapis.com/auth/gmail.send
https://www.googleapis.com/auth/gmail.readonly
  1. 依次点击添加到表格、更新和保存。

874b1dda14e8f379.png

如需了解详情,请参阅完整的配置 OAuth 同意指南。

创建 OAuth 客户端凭证

为 Gemini Enterprise 创建一个新的 OAuth 客户端,以便对用户进行身份验证:

  1. 在 Google Cloud 控制台中,依次点击菜单 ☰ > Google Auth Platform > 客户端。

  1. 点击 + 创建客户端。
  2. 在应用类型部分,选择 Web 应用。
  3. 将名称设置为 codelab。
  4. 跳过已获授权的 JavaScript 来源。
  5. 在已获授权的重定向 URI 部分中,点击添加 URI,然后输入 https://vertexaisearch.cloud.google.com/oauth-redirect。
  6. 点击创建。
  7. 系统会显示一个对话框,其中包含您新创建的 OAuth 客户端 ID 和密钥。请将此信息保存在安全的地方。

a46e5ebfb851aea5.png

创建数据存储区

您需要创建数据存储区,以便将 Gemini Enterprise 与 Workspace 数据关联。

在新标签页中,从 Cloud 控制台打开 Gemini Enterprise,然后按照以下步骤操作:

  1. 点击名为 codelab 的应用。
  2. 在导航菜单中,点击关联的数据存储区。
  3. 点击 + 新建数据存储区。
  4. 在来源中,搜索 Google 日历,然后点击选择。
  5. 在操作部分,输入之前步骤中保存的客户端 ID 和客户端密钥,然后点击验证身份,并按照步骤对 OAuth 客户端进行身份验证和授权。
  6. 启用创建日历活动和更新日历活动操作。
  7. 点击继续。

a1d76e70edec0cf.png

  1. 在配置部分中,将数据连接器名称设置为 calendar。
  2. 点击创建。
  3. 系统会自动将您重定向到关联的数据存储区,您可以在其中看到新添加的数据存储区。

创建 Google Gmail 数据存储区:

  1. 点击 + 新建数据存储区。
  2. 在来源中,搜索 Google Gmail,然后点击选择。
  3. 在操作部分,输入之前步骤中保存的客户端 ID 和客户端密钥,然后点击验证 Auth。
  4. 启用发送电子邮件操作。
  5. 点击继续。
  6. 在配置部分中,将数据连接器名称设置为 gmail。
  7. 点击创建。
  8. 系统会自动将您重定向到关联的数据存储区,您可以在其中看到新添加的数据存储区。

创建 Google 云端硬盘数据存储区:

  1. 点击 + 新建数据存储区。
  2. 在来源中,搜索 Google 云端硬盘,然后点击选择。
  3. 在数据部分,选择全部,然后点击继续。
  4. 在配置部分中,将数据连接器名称设置为 drive。
  5. 点击创建。
  6. 系统会自动将您重定向到关联的数据存储区,您可以在其中看到新添加的数据存储区。

创建 NotebookLM 数据存储区:

  1. 点击 + 新建数据存储区。
  2. 在来源中,搜索 NotebookLM,然后点击选择。
  3. 在配置部分中,将数据连接器名称设置为 notebooklm。
  4. 点击创建。
  5. 系统会自动将您重定向到关联的数据存储区,您可以在其中看到新添加的数据存储区。

几分钟后,所有已连接的数据存储区(NotebookLM 除外)的状态都将变为有效。如果您看到任何错误,可以点击相应数据源,查看错误详情。

ceba9eb2480a2696.png

测试数据存储区

通过运行一些测试查询,验证数据存储区是否正确提取数据。

打开我们之前复制的 Gemini Enterprise Web 应用网址:

  1. 依次点击菜单 ☰ > 发起新对话。
  2. 在“新聊天消息”字段的页脚中,点击连接器图标,然后启用所有连接器。
  3. 现在,您可以尝试使用与连接器相关的提示。例如,在对话中输入 Do I have any meetings today?,然后按 enter。
  4. 接下来,尝试输入 How many emails did I receive today?,然后按 enter。
  5. 最后,输入 Give me the title of the last Drive file I created,然后按 enter。

90e42539e5959634.png

创建自定义智能体

在 Gemini Enterprise Web 应用中,使用 Agent Designer 创建新的智能体:

  1. 依次点击“菜单”图标 ☰ > + 新建代理。
  2. 在对话中,输入 An agent that always sends pirate-themed emails but use normal English otherwise,然后按 enter。

2803c1dedd20433e.png

  1. 智能体设计器会根据提示草拟智能体,并在编辑器中打开。
  2. 点击创建

试用自定义智能体

测试新智能体,看看它如何回答问题和执行操作。

  1. 在 Gemini Enterprise Web 应用中,与新创建的智能体对话:
  2. 依次点击菜单 ☰ > 代理。
  3. 在您的代理下选择相应代理。
  4. 在新的聊天消息字段的页脚中,点击连接器图标,然后点击邮件对应的启用操作,并按照说明向代理授权
  5. 在对话中,输入 Send an email to someone@example.com saying I'll see them at Cloud Next, generate some subject and body yourself,然后按 enter。您可以将示例邮箱替换为您的邮箱。
  6. 点击 ✔️ 发送邮件。

60e62437ce29a818.pngd4fb65d14fdf27da.png

4. 专业代码自定义智能体

此智能体允许用户使用自定义工具和规则,以自然语言搜索数据并对 Workspace 执行操作。它依赖于以下元素:

  • 模型:Gemini。
  • 数据和操作:适用于 Google Workspace(日历、Gmail、云端硬盘、NotebookLM)的 Gemini Enterprise 数据存储区、Google 搜索、Google 管理的 Agent Search 的 Model Context Protocol (MCP) 服务器、用于发送 Google Chat 聊天消息的自定义工具函数(通过 Google Chat API)。
  • 智能体构建工具:智能体开发套件 (ADK)。
  • 智能体主机:Agent Runtime。
  • 界面:Gemini Enterprise Web 应用。

它将使用自带功能集成到 Gemini Enterprise 中,因此我们需要完成部署、注册和配置步骤。

查看概念

Gemini Enterprise Agent Platform

Gemini Enterprise Agent Platform(以前称为 Vertex AI)是一个开放而全面的平台,可帮助企业快速构建、扩缩、治理和优化依托企业自身数据的企业级智能体。

4670fcf7a826af4d.png

智能体开发套件 (ADK)

智能体开发套件 (ADK) 是一套专门的工具和框架,旨在通过提供用于推理、内存管理和工具集成的预构建模块,简化自主 AI 智能体的创建。

Model Context Protocol (MCP)

Model Context Protocol (MCP) 是一种开放标准,旨在通过通用的“即插即用”接口,实现 AI 应用与各种数据源或工具之间的无缝、安全集成。

函数工具

函数工具是一种预定义的可执行例程,AI 模型可以触发该例程来执行特定操作或从外部系统检索实时数据,从而将其功能扩展到简单的文本生成之外。

查看解决方案架构

43df337e0f3d64e8.png

查看源代码

agent.py

以下代码会向 Gemini Enterprise 进行身份验证,初始化 Agent Search MCP 和 Chat API 工具,并定义智能体的行为。

  1. 身份验证:它使用辅助函数 _get_access_token_from_context 检索 Gemini Enterprise 注入的身份验证令牌 (CLIENT_AUTH_NAME)。此令牌对于安全调用 Agent Search MCP 和 Google Chat 工具等下游服务至关重要。
  2. 工具设置:初始化 vertexai_mcp(一个连接到 Agent Search Model Context Protocol [MCP] 服务器的工具集)和 send_direct_message 工具。这样,智能体便能够搜索已连接的数据存储区并发送 Google Chat 聊天消息。
  3. 智能体定义:使用 gemini-2.5-flash 模型定义 root_agent。这些指令会告知智能体优先使用搜索工具进行信息检索,并使用 send_direct_message 工具执行操作,从而有效地将智能体接地到企业数据。
...
MODEL = "gemini-2.5-flash"

# Gemini Enterprise authentication injects a bearer token into the ToolContext state.
# The key pattern is "CLIENT_AUTH_NAME_<random_digits>".
# We dynamically parse this token to authenticate our MCP and API calls.
CLIENT_AUTH_NAME = "enterprise-ai"

VERTEXAI_SEARCH_TIMEOUT = 15.0

def get_project_id():
    """Fetches the consumer project ID from the environment natively."""
    _, project = google.auth.default()
    if project:
        return project
    raise Exception(f"Failed to resolve GCP Project ID from environment.")

def find_serving_config_path():
    """Dynamically finds the default serving config in the engine."""
    project_id = get_project_id()
    engines = discoveryengine_v1.EngineServiceClient().list_engines(
        parent=f"projects/{project_id}/locations/global/collections/default_collection"
    )
    for engine in engines:
        # engine.name natively contains the numeric Project Number
        return f"{engine.name}/servingConfigs/default_serving_config"
    raise Exception(f"No Discovery Engines found in project {project_id}")

def _get_access_token_from_context(tool_context: ToolContext) -> str:
    """Helper method to dynamically parse the intercepted bearer token from the context state."""
    escaped_name = re.escape(CLIENT_AUTH_NAME)
    pattern = re.compile(fr"^{escaped_name}_\d+$")
    # Handle ADK varying state object types (Raw Dict vs ADK State)
    state_dict = tool_context.state.to_dict() if hasattr(tool_context.state, 'to_dict') else tool_context.state
    matching_keys = [k for k in state_dict.keys() if pattern.match(k)]
    if matching_keys:
        return state_dict.get(matching_keys[0])
    raise Exception(f"No bearer token found in ToolContext state matching pattern {pattern.pattern}")

def auth_header_provider(tool_context: ToolContext) -> dict[str, str]:
    token = _get_access_token_from_context(tool_context)
    return {"Authorization": f"Bearer {token}"}

def send_direct_message(email: str, message: str, tool_context: ToolContext) -> dict:
    """Sends a Google Chat Direct Message (DM) to a specific user by email address."""
    chat_client = chat_v1.ChatServiceClient(
        credentials=Credentials(token=_get_access_token_from_context(tool_context))
    )

    # 1. Setup the DM space or find existing one
    person = chat_v1.User(
        name=f"users/{email}",
        type_=chat_v1.User.Type.HUMAN
    )
    membership = chat_v1.Membership(member=person)
    space_req = chat_v1.Space(space_type=chat_v1.Space.SpaceType.DIRECT_MESSAGE)
    setup_request = chat_v1.SetUpSpaceRequest(
        space=space_req,
        memberships=[membership]
    )
    space_response = chat_client.set_up_space(request=setup_request)
    space_name = space_response.name
    
    # 2. Send the message
    msg = chat_v1.Message(text=message)
    message_request = chat_v1.CreateMessageRequest(
        parent=space_name,
        message=msg
    )
    message_response = chat_client.create_message(request=message_request)
    
    return {"status": "success", "message_id": message_response.name, "space": space_name}

agentsearch_mcp = McpToolset(
    connection_params=StreamableHTTPConnectionParams(
        url="https://discoveryengine.googleapis.com/mcp",
        timeout=VERTEXAI_SEARCH_TIMEOUT,
        sse_read_timeout=VERTEXAI_SEARCH_TIMEOUT
    ),
    tool_filter=['search'],
    # The auth_header_provider dynamically injects the bearer token from the ToolContext
    # into the MCP call for authentication.
    header_provider=auth_header_provider
)

# Answer nicely the following user queries:
#  - Please find my meetings for today, I need their titles and links
#  - What is the latest Drive file I created?
#  - What is the latest Gmail message I received?
#  - Please send the following message to someone@example.com: Hello, this is a test message.

root_agent = LlmAgent(
    model=MODEL,
    name='enterprise_ai',
    instruction=f"""
        You are a helpful assistant that always uses the MCP search tool to answer the user's message, unless the user asks you to send a message to someone.
        If the user asks you to send a message to someone, use the send_direct_message tool to send the message.
        You MUST unconditionally use the MCP search tool to find answer, even if you believe you already know the answer or believe the MCP search tool does not contain the data.
        The MCP search tool accesses the user's data through datastores including Google Drive, Google Calendar, and Gmail.
        Only use the MCP search tool with servingConfig and query parameters, do not use any other parameters.
        Always use the servingConfig {find_serving_config_path()} while using the MCP search tool.
    """,
    tools=[agentsearch_mcp, FunctionTool(send_direct_message)]
)

启用 API

该解决方案需要启用其他 API:

  1. 在 Google Cloud 控制台中,启用 Agent Platform API、Cloud Resource Manager API 和 Google Chat API:

4f02a36b050bab00.png

  1. 依次点击菜单 ☰ > API 和服务 > 已启用的 API 和服务,然后确认 Vertex AI API、Cloud Resource Manager API 和 Google Chat API 均在列表中。

此解决方案需要额外的访问权限:

  1. 在 Google Cloud 控制台中,依次点击菜单 ☰ > Google Auth 平台 > 数据访问。

  1. 点击添加或移除范围。
  2. 复制以下范围,并将其粘贴到手动添加范围字段中。
  3. 依次点击添加到表格、更新和保存。
https://www.googleapis.com/auth/cloud-platform
https://www.googleapis.com/auth/chat.messages.create
https://www.googleapis.com/auth/chat.spaces.create
  1. 依次点击添加到表格、更新和保存。

56fbba733139acfe.png

更新 OAuth 客户端凭证

该解决方案需要一个额外的已获授权的重定向 URI:

  1. 在 Google Cloud 控制台中,依次点击菜单 ☰ > Google Auth Platform > 客户端。

  1. 点击客户名称 codelab。
  2. 在已获授权的重定向 URI 部分中,点击添加 URI,然后输入 https://vertexaisearch.cloud.google.com/static/oauth/oauth.html。
  3. 点击保存。

deed597aa54fec91.png

启用 Agent Search MCP

  1. 在终端中执行以下命令:
gcloud beta services mcp enable discoveryengine.googleapis.com \
     --project=$(gcloud config get-value project)

配置 Chat 应用

为 Google Chat 应用配置基本信息。

  1. 在 Google Cloud 控制台中,在 Google Cloud 搜索字段中搜索 Google Chat API,点击 Google Chat API,点击管理,然后点击配置。

  1. 将应用名称和说明设置为 Gemini Enterprise。
  2. 将头像网址设置为 https://developers.google.com/workspace/add-ons/images/quickstart-app-avatar.png。
  3. 取消选中启用互动功能,然后在显示的模态对话框中点击停用。
  4. 选择将错误记录到 Logging。
  5. 点击保存。

90cb612e51bce4e6.png

在 Agent Runtime 中部署智能体

将智能体部署到 Agent Runtime 基础设施。

  1. 下载此 GitHub 代码库。

  1. 在终端中,打开 solutions/enterprise-ai-agent 目录,然后执行以下命令:
# 1. Create and activate a new virtual environment
python3 -m venv .venv
source .venv/bin/activate

# 2. Install poetry and project dependencies
pip install poetry
poetry install

# 3. Deploy the agent
adk deploy agent_engine \
  --project=$(gcloud config get-value project) \
  --region=us-central1 \
  --display_name="Enterprise AI" \
  enterprise_ai

eafd2f9c4fbf305.png

  1. 当您在日志中看到 Deploying to agent runtime…(正在部署到 Agent Runtime…)这一行时,请打开一个新终端并执行以下命令,向 Vertex AI Reasoning Engine Service Agent 添加所需权限:
# 1. Get the current Project ID
PROJECT_ID=$(gcloud config get-value project)

# 2. Extract the Project Number for that ID
PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')

# 3. Construct the Service Account name
SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-aiplatform-re.iam.gserviceaccount.com"

# 4. Apply the IAM policy binding
gcloud projects add-iam-policy-binding $PROJECT_ID \
     --member="serviceAccount:$SERVICE_ACCOUNT" \
     --role="roles/discoveryengine.viewer"
  1. 等待 adk deploy 命令执行完毕,然后从命令输出中复制新部署的智能体的资源名称(以绿色显示)。

d098fe1347d6581b.png

在 Gemini Enterprise 中注册智能体

在 Gemini Enterprise 中注册已部署的智能体,以便用户能够访问。

在新的标签页中,从 Cloud 控制台打开 Gemini Enterprise,然后按照以下步骤操作:

  1. 点击名为 codelab 的应用。
  2. 在导航菜单中,点击代理。
  3. 点击 + 添加智能体。
  4. 对于通过 Agent Runtime 构建的自定义智能体,点击添加。系统会显示授权部分。
  5. 点击添加授权。
  6. 将授权名称设置为 enterprise-ai。系统会根据名称生成 ID,并将其显示在字段下方,请复制该 ID。
  7. 将客户端 ID 设置为与在前面步骤中创建和更新的 OAuth 客户端相同的值。
  8. 将客户端密钥设置为与前面步骤中创建和更新的 OAuth 客户端相同的值。
  9. 将令牌 URI 设置为 https://oauth2.googleapis.com/token。
  10. 将 <CLIENT_ID> 替换为在前面步骤中创建和更新的 OAuth 客户端 ID 后,将授权 URI 设置为以下值。
https://accounts.google.com/o/oauth2/v2/auth?client_id=<CLIENT_ID>&redirect_uri=https%3A%2F%2Fvertexaisearch.cloud.google.com%2Fstatic%2Foauth%2Foauth.html&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar.calendars%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar.events%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.send%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fchat.messages.create%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fchat.spaces.create&include_granted_scopes=true&response_type=code&access_type=offline&prompt=consent
  1. 点击完成,然后点击下一步。系统会显示配置部分。
  2. 将代理名称和代理说明设置为 Enterprise AI。
  3. 将 Agent Runtime 推理引擎设置为在前面步骤中复制的推理引擎资源名称。其格式如下:
projects/<PROJECT_ID>/locations/<LOCATION>/reasoningEngines/<REASONING_ENGINE_ID>
  1. 点击创建。新添加的代理现在会列在代理下。

试用代理

通过与自定义智能体对话来验证流程。

  1. 在 Gemini Enterprise Web 应用中,与新注册的智能体对话:
  2. 依次点击菜单 ☰ > 代理。
  3. 在来自贵组织下选择代理。
  4. 在对话中,输入 Please find my meetings for today, I need their titles and links,然后按 enter。
  5. 点击授权,然后按照授权流程操作。

ed61cf654cbcd76c.png

  1. 代理会回答一系列日历活动(具体取决于用户的账号)。
  2. 在对话中,输入 Please send a Chat message to someone@example.com with the following text: Hello!,然后按 enter。
  3. 智能体将回答一条确认消息。

1647ebff031c42e7.pnga8087d2351e77fb4.png

5. 默认代理作为 Google Workspace 插件

借助此代理,用户可以在 Workspace 应用界面中,使用自然语言搜索 Workspace 数据。它依赖于以下元素:

  • 模型:Gemini。
  • 数据:Gemini Enterprise 数据存储区,用于 Google Workspace(日历、Gmail、云端硬盘、NotebookLM)、Google 搜索。
  • 代理主机:Gemini Enterprise。
  • 界面:适用于 Chat 和 Gmail 的 Google Workspace 插件(可轻松扩展到 Google 日历、云端硬盘、文档、表格和幻灯片)。
  • Google Workspace 插件:Apps 脚本、Gemini Enterprise API、上下文(用户元数据、所选 Gmail 邮件)。

Google Workspace 插件将使用 StreamAssist API 连接到 Gemini Enterprise。

查看概念

Google Workspace 插件

Google Workspace 插件是一种自定义应用,可扩展一个或多个 Google Workspace 应用(Gmail、Chat、日历、文档、云端硬盘、Meet、表格和幻灯片)。

Apps 脚本

Apps 脚本是一个基于云的 JavaScript 平台,由 Google 云端硬盘提供支持,可让您与 Google 各项产品集成并自动执行任务。

Google Workspace 卡片框架

Google Workspace 中的卡片框架使开发者能够创建丰富的互动式界面。它支持构建井然有序且富有视觉吸引力的卡片,其中可以包含文本、图片、按钮和其他 widget。这些卡片可提供结构化信息,并支持直接在 Workspace 应用中执行快速操作,从而提升用户体验。

查看解决方案架构

1798c39f7aaed8fc.png

查看源代码

appsscript.json

以下配置定义了插件的触发器和权限。

  1. 定义插件:它会告知 Google Workspace,此项目是 Chat 和 Gmail 的插件。
  2. 情境触发器:对于 Gmail,它会设置一个 contextualTrigger,每当用户打开电子邮件时,该触发器就会触发 onAddonEvent。这样,插件就可以“看到”电子邮件内容。
  3. 权限:列出插件运行所需的 oauthScopes,例如读取当前邮件、执行脚本以及连接到外部服务(如 Gemini Enterprise API)的权限。
...
"addOns": {
    "common": {
      "name": "Enterprise AI",
      "logoUrl": "https://developers.google.com/workspace/add-ons/images/quickstart-app-avatar.png"
    },
    "chat": {},
    "gmail": {
      "contextualTriggers": [
        {
          "unconditional": {},
          "onTriggerFunction": "onAddonEvent"
        }
      ]
    }
  },
  "oauthScopes": [
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/discoveryengine.assist.readwrite",
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/gmail.addons.current.message.readonly"
  ]
...

Chat.gs

以下代码用于处理传入的 Google Chat 消息。

  1. 接收消息:onMessage 函数是消息互动的入口点。
  2. 管理上下文:它会将 space.name(Chat 聊天室的 ID)保存到用户的属性中。这样可以确保当代理准备好回复时,它能准确知道应该将消息发布到哪个对话中。
  3. 委托给代理:它会调用 requestAgent,将用户的消息传递给处理 API 通信的核心逻辑。
...
// Service that handles Google Chat operations.

// Handle incoming Google Chat message events, actions will be taken via Google Chat API calls
function onMessage(event) {
  if (isInDebugMode()) {
    console.log(`MESSAGE event received (Chat): ${JSON.stringify(event)}`);
  }
  // Extract data from the event.
  const chatEvent = event.chat;
  setChatConfig(chatEvent.messagePayload.space.name);

  // Request AI agent to answer the message
  requestAgent(chatEvent.messagePayload.message);
  // Respond with an empty response to the Google Chat platform to acknowledge execution
  return null; 
}

// --- Utility functions ---

// The Chat direct message (DM) space associated with the user
const SPACE_NAME_PROPERTY = "DM_SPACE_NAME"

// Sets the Chat DM space name for subsequent operations.
function setChatConfig(spaceName) {
  const userProperties = PropertiesService.getUserProperties();
  userProperties.setProperty(SPACE_NAME_PROPERTY, spaceName);
  console.log(`Space is set to ${spaceName}`);
}

// Retrieved the Chat DM space name to sent messages to.
function getConfiguredChat() {
  const userProperties = PropertiesService.getUserProperties();
  return userProperties.getProperty(SPACE_NAME_PROPERTY);
}

// Finds the Chat DM space name between the Chat app and the given user.
function findChatAppDm(userName) {
  return Chat.Spaces.findDirectMessage(
    { 'name': userName },
    {'Authorization': `Bearer ${getAddonCredentials().getAccessToken()}`}
  ).name;
}

// Creates a Chat message in the configured space.
function createMessage(message) {
  const spaceName = getConfiguredChat();
  console.log(`Creating message in space ${spaceName}...`);
  return Chat.Spaces.Messages.create(
    message,
    spaceName,
    {},
    {'Authorization': `Bearer ${getAddonCredentials().getAccessToken()}`}
  ).name;
}

Sidebar.gs

以下代码会构建 Gmail 边栏并捕获电子邮件上下文。

  1. 构建界面:createSidebarCard 使用 Workspace Card Service 构建可视化界面。它会创建一个简单的布局,其中包含文本输入区域和“发送消息”按钮。
  2. 捕获邮件上下文:在 handleSendMessage 中,代码会检查用户当前是否正在查看邮件 (event.gmail.messageId)。如果是,它会安全地提取邮件的主题和正文,并将其附加到用户的提示中。
  3. 显示结果:代理回复后,代码会更新边栏卡片以显示回复。
...
// Service that handles Gmail operations.

// Triggered when the user opens the Gmail Add-on or selects an email.
function onAddonEvent(event) {
  // If this was triggered by a button click, handle it
  if (event.parameters && event.parameters.action === 'send') {
    return handleSendMessage(event);
  }

  // Otherwise, just render the default initial sidebar
  return createSidebarCard();
}

// Creates the standard Gmail sidebar card consisting of a text input and send button.
// Optionally includes an answer section if a response was generated.
function createSidebarCard(optionalAnswerSection) {
  const card = CardService.newCardBuilder();
  const actionSection = CardService.newCardSection();

  // Create text input for the user's message
  const messageInput = CardService.newTextInput()
    .setFieldName("message")
    .setTitle("Message")
    .setMultiline(true);

  // Create action for sending the message
  const sendAction = CardService.newAction()
    .setFunctionName('onAddonEvent')
    .setParameters({ 'action': 'send' });

  const sendButton = CardService.newTextButton()
    .setText("Send message")
    .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
    .setOnClickAction(sendAction);

  actionSection.addWidget(messageInput);
  actionSection.addWidget(CardService.newButtonSet().addButton(sendButton));

  card.addSection(actionSection);

  // Attach the response at the bottom if we have one
  if (optionalAnswerSection) {
    card.addSection(optionalAnswerSection);
  }

  return card.build();
}

// Handles clicks from the Send message button.
function handleSendMessage(event) {
  const commonEventObject = event.commonEventObject || {};
  const formInputs = commonEventObject.formInputs || {};
  const messageInput = formInputs.message;

  let userMessage = "";
  if (messageInput && messageInput.stringInputs && messageInput.stringInputs.value.length > 0) {
    userMessage = messageInput.stringInputs.value[0];
  }

  if (!userMessage || userMessage.trim().length === 0) {
    return CardService.newActionResponseBuilder()
      .setNotification(CardService.newNotification().setText("Please enter a message."))
      .build();
  }

  let finalQueryText = `USER MESSAGE TO ANSWER: ${userMessage}`;

  // If we have an email selected in Gmail, append its content as context
  if (event.gmail && event.gmail.messageId) {
    try {
      GmailApp.setCurrentMessageAccessToken(event.gmail.accessToken);
      const message = GmailApp.getMessageById(event.gmail.messageId);

      const subject = message.getSubject();
      const bodyText = message.getPlainBody() || message.getBody();

      finalQueryText += `\n\nEMAIL THE USER HAS OPENED ON SCREEN:\nSubject: ${subject}\nBody:\n---\n${bodyText}\n---`;
    } catch (e) {
      console.error("Could not fetch Gmail context: " + e);
      // Invalidate the token explicitly so the next prompt requests the missing scopes
      ScriptApp.invalidateAuth();

      CardService.newAuthorizationException()
        .setResourceDisplayName("Enterprise AI")
        .setAuthorizationUrl(ScriptApp.getAuthorizationUrl())
        .throwException();
    }
  }

  try {
    const responseText = queryAgent({ text: finalQueryText, forceNewSession: true });

    // We leverage the 'showdown' library to parse the LLM's Markdown output into HTML
    // We also substitute markdown listings with arrows and adjust newlines for clearer rendering in the sidebar
    let displayedText = substituteListingsFromMarkdown(responseText);
    displayedText = new showdown.Converter().makeHtml(displayedText).replace(/\n/g, '\n\n');

    const textParagraph = CardService.newTextParagraph();
    textParagraph.setText(displayedText);

    const answerSection = CardService.newCardSection()
      .addWidget(textParagraph);

    const updatedCard = createSidebarCard(answerSection);

    return CardService.newActionResponseBuilder()
      .setNavigation(CardService.newNavigation().updateCard(updatedCard))
      .build();

  } catch (err) {
    return CardService.newActionResponseBuilder()
      .setNotification(CardService.newNotification().setText("Error fetching response: " + err.message))
      .build();
  }
}
...

AgentHandler.gs

以下代码会编排对 Gemini Enterprise 的 API 调用。

  1. 编排 API 调用:queryAgent 是插件与 Gemini Enterprise 之间的桥梁。它会构建一个请求,其中包含用户的查询、时区和活动会话 ID。
  2. 动态发现:它会调用 getAgentDataStores 来找出可用的数据连接器。
  3. 流式传输回答:由于智能体回答可能需要一些时间,因此它会将 streamAssist API 与服务器发送的事件 (SSE) 搭配使用。代码会分块收集回答,并重构完整回答。
...
// Service that handles Gemini Enterprise AI Agent operations.

// Submits a query to the AI agent and returns the response string synchronously
function queryAgent(input) {
  const isNewSession = input.forceNewSession || !PropertiesService.getUserProperties().getProperty(AGENT_SESSION_NAME);
  const sessionName = input.forceNewSession ? createAgentSession() : getOrCreateAgentSession();

  let systemPrompt = "SYSTEM PROMPT START Do not respond with tables but use bullet points instead.";
  if (input.forceNewSession) {
    systemPrompt += " Do not ask the user follow-up questions or converse with them as history is not kept in this interface.";
  }
  systemPrompt += " SYSTEM PROMPT END\n\n";

  const queryText = isNewSession ? systemPrompt + input.text : input.text;

  const requestPayload = {
    "session": sessionName,
    "userMetadata": { "timeZone": Session.getScriptTimeZone() },
    "query": { "text": queryText },
    "toolsSpec": { "vertexAiSearchSpec": { "dataStoreSpecs": getAgentDataStores().map(ds => { dataStore: ds }) } },
    "agentsSpec": { "agentSpecs": [{ "agentId": getAgentId() }] }
  };

  const responseContentText = UrlFetchApp.fetch(
    `https://${getLocation()}-discoveryengine.googleapis.com/v1alpha/${getReasoningEngine()}/assistants/default_assistant:streamAssist?alt=sse`,
    {
      method: 'post',
      headers: { 'Authorization': `Bearer ${ScriptApp.getOAuthToken()}` },
      contentType: 'application/json',
      payload: JSON.stringify(requestPayload),
      muteHttpExceptions: true
    }
  ).getContentText();

  if (isInDebugMode()) {
    console.log(`Response: ${responseContentText}`);
  }

  const events = responseContentText.split('\n').map(s => s.replace(/^data:\s*/, '')).filter(s => s.trim().length > 0);
  console.log(`Received ${events.length} agent events.`);

  let answerText = "";
  for (const eventJson of events) {
    if (isInDebugMode()) {
      console.log("Event: " + eventJson);
    }
    const event = JSON.parse(eventJson);

    // Ignore internal events
    if (!event.answer) {
      console.log(`Ignored: internal event`);
      continue;
    }

    // Handle text replies
    const replies = event.answer.replies || [];
    for (const reply of replies) {
      const content = reply.groundedContent.content;
      if (content) {
        if (isInDebugMode()) {
          console.log(`Processing content: ${JSON.stringify(content)}`);
        }
        if (content.thought) {
          console.log(`Ignored: thought event`);
          continue;
        }
        answerText += content.text;
      }
    }

    if (event.answer.state === "SUCCEEDED") {
      console.log(`Answer text: ${answerText}`);
      return answerText;
    } else if (event.answer.state !== "IN_PROGRESS") {
      throw new Error("Something went wrong, check the Apps Script logs for more info.");
    }
  }
  return answerText;
}

// Gets the list of data stores configured for the agent to include in the request.
function getAgentDataStores() {
  const responseContentText = UrlFetchApp.fetch(
    `https://${getLocation()}-discoveryengine.googleapis.com/v1/${getReasoningEngine().split('/').slice(0, 6).join('/')}/dataStores`,
    {
      method: 'get',
      // Use the add on service account credentials for data store listing access
      headers: { 'Authorization': `Bearer ${getAddonCredentials().getAccessToken()}` },
      contentType: 'application/json',
      muteHttpExceptions: true
    }
  ).getContentText();
  if (isInDebugMode()) {
    console.log(`Response: ${responseContentText}`);
  }
  const dataStores = JSON.parse(responseContentText).dataStores.map(ds => ds.name);
  if (isInDebugMode()) {
    console.log(`Data stores: ${dataStores}`);
  }
  return dataStores;
}
...

启动服务账号

创建一个专用服务账号,用于授权插件的服务器端操作。

在 Google Cloud 控制台中,按以下步骤操作:

  1. 依次点击菜单 ☰ > IAM 和管理 > 服务账号 > + 创建服务账号。

  1. 将服务账号名称设置为 ge-add-on。

d44d6aae29e2464c.png

  1. 点击创建并继续。
  2. 在权限中添加 Discovery Engine Viewer 角色。

f1374efa4f326ef5.png

  1. 依次点击继续和完成。您将会转到服务账号页面,并看到创建的服务账号。

b9496085f1404c5c.png

  1. 选择新创建的服务账号,然后选择密钥标签页。
  2. 依次点击添加密钥和创建新密钥。
  3. 选择 JSON,然后点击创建。

f4280f5533a08821.png

  1. 对话框随即关闭,新创建的公钥/私钥对将以 JSON 文件的形式自动下载到本地环境。

创建和配置 Apps 脚本项目

创建一个新的 Apps 脚本项目来托管插件代码并配置其连接属性。

  1. 点击以下按钮,打开 Enterprise AI 插件 Apps 脚本项目:

  1. 依次点击概览 > 复制。
  2. 在 Apps 脚本项目中,依次点击项目设置 > 修改脚本属性 > 添加脚本属性,以添加脚本属性。
  3. 将 REASONING_ENGINE_RESOURCE_NAME 设置为 Gemini Enterprise 应用资源名称。其格式如下:
# 1. Replace PROJECT_ID with the Google Cloud project ID.
# 2. Replace GE_APP_ID with the codelab app ID found in Google Cloud console > Gemini Enterprise > Apps.

projects/<PROJECT_ID>/locations/global/collections/default_collection/engines/<GE_APP_ID>
  1. 将 APP_SERVICE_ACCOUNT_KEY 设置为在前面步骤中下载的服务账号文件中的 JSON 密钥。
  2. 点击保存脚本属性

部署到 Gmail 和 Chat

部署插件,以便直接在 Gmail 和 Google Chat 中进行测试。

在 Apps 脚本项目中,按照以下步骤操作:

  1. 依次点击部署 > 测试部署,然后点击安装。现在,您可以在 Gmail 中使用该功能。
  2. 点击主要部署 ID 下的复制。

2ed2df972ad92715.png

在 Google Cloud 控制台中,按以下步骤操作:

  1. 在 Google Cloud 搜索字段中搜索 Google Chat API,点击 Google Chat API,点击管理,然后点击配置。

  1. 选择启用互动功能。
  2. 取消选择加入聊天室和群组对话。
  3. 在连接设置下,选择 Apps 脚本。
  4. 将部署 ID 设置为在前面步骤中复制的主要部署 ID。
  5. 在公开范围下,选择面向您的 Workspace 网域中的特定人员和群组提供此 Chat 应用,然后输入您的邮箱。
  6. 点击保存。

3b7d461c423f7c51.png

试用插件

与您的实时插件互动,验证它能否获取数据并根据上下文回答问题。

在新标签页中打开 Google Chat,然后按以下步骤操作:

  1. 打开与 Chat 应用 Gemini Enterprise 的私信对话。

3da8690d19baf2d0.png

  1. 点击配置,然后完成身份验证流程。
  2. 输入 What are my meetings for today?,然后按 enter。Gemini Enterprise Chat 应用应回复结果。

c8c63fb3f324fecf.png

在新标签页中打开 Gmail,然后按照以下步骤操作:

  1. 给自己发送一封电子邮件,主题设置为 We need to talk,正文设置为 Are you available today between 8 and 9 AM?
  2. 打开新收到的电子邮件。
  3. 打开 Enterprise AI 插件边栏。
  4. 将消息设置为 Am I?
  5. 点击发送讯息。
  6. 答案会显示在按钮后面。

d33b8cb50ee251b7.png

6. 清理

删除 Google Cloud 项目

为避免系统因此 Codelab 中使用的资源向您的 Google Cloud 账号收取费用,我们建议您删除该 Google Cloud 项目。

在 Google Cloud 控制台中,按以下步骤操作:

  1. 依次点击菜单 ☰ > IAM 和管理 > 设置。

  1. 点击关停。
  2. 输入项目 ID。
  3. 点击仍要关停。

3b9492d97f771b2c.png

7. 恭喜

恭喜!您构建了解决方案,让员工能够更紧密地结合使用 Gemini Enterprise 和 Google Workspace,充分发挥其强大功能!

接下来怎么做?

在此 Codelab 中,我们仅展示了最典型的用例,但您或许需要在您的解决方案中考虑很多值得扩展的地方,例如:

  • 使用 AI 赋能的开发者工具,例如 Gemini CLI 和 Antigravity。
  • 与其他智能体框架和工具(例如自定义 MCP、自定义函数调用和生成式界面)集成。
  • 与托管在 Agent Platform 等专用平台中的其他 AI 模型(包括自定义模型)集成。
  • 与托管在 Dialogflow 等专用平台或由第三方通过 Cloud Marketplace 托管的其他代理集成。
  • 在 Cloud Marketplace 上发布智能体,为团队、组织或公众用户赋能。

了解详情

我们为开发者提供了大量资源,例如 YouTube 视频、文档网站、代码示例和教程: