1. 概览
在当今快节奏的零售环境中,提供卓越的客户服务并实现个性化的购物体验至关重要。我们将带您踏上技术之旅,了解如何创建知识驱动型聊天应用,该应用旨在回答客户问题、引导产品发现和定制搜索结果。这种创新解决方案结合了 AlloyDB 的数据存储功能、用于上下文理解的内部分析引擎、用于相关性验证的 Gemini(大语言模型)以及用于快速启动智能对话助理的 Google Agent Builder。
挑战: 现代零售客户希望获得即时答案和符合其独特偏好的产品推荐。传统的搜索方法往往无法提供这种程度的个性化服务。
解决方案: 我们的知识驱动型聊天应用直接应对这一挑战。它利用从零售数据中提取的丰富知识库来了解客户意图、智能响应并提供高度相关的结果。
构建内容
在此实验(第 2 部分)中,您将:
- 构建 Vertex AI Agent Builder 应用构建智能体
- 将 AlloyDB 工具与代理集成
要求
2. 架构
数据流:我们来详细了解一下数据在系统中的流动方式:
提取:
零售数据(库存、产品说明、客户互动)会持续加载到 AlloyDB 中。
分析引擎:
我们将使用 AlloyDB 作为分析引擎来执行以下操作:
- 上下文提取:引擎分析 AlloyDB 中存储的数据,以了解产品、类别、客户行为等之间的关系(如果适用)。
- 嵌入创建:为用户的查询和 AlloyDB 中存储的信息生成嵌入(文本的数学表示形式)。
- 向量搜索:引擎执行相似性搜索,将查询嵌入与产品说明、评价和其他相关数据的嵌入进行比较。这会识别出 25 个最相关的“最近邻”。
Gemini 验证:
这些潜在的响应会发送给 Gemini 进行评估。Gemini 会确定这些响应是否真正相关且可以安全地与用户分享。
响应生成:
经过验证的响应会构建为 JSON 数组,整个引擎会打包到无服务器 Cloud Run 函数中,该函数从 Agent Builder 调用。
上述步骤已在实验的第 1 部分 中介绍。
我们讨论了创建知识驱动型分析引擎的技术细节,该引擎为我们的智能购物助理提供支持。现在,我们来探索如何利用 Agent Builder 的魔力在对话界面中将此引擎变为现实。在开始第 2 部分之前,请确保您已准备好端点网址。接下来,我们将在此实验中介绍以下步骤:
对话互动:
Agent Builder 以自然语言格式向用户呈现响应,从而促进来回对话。
3. 准备工作
创建项目
- 在 Google Cloud 控制台 的项目选择器页面上,选择或创建一个 Google Cloud 项目。
- 确保您的云项目已启用结算功能。了解如何 检查项目是否已启用结算功能 。
- 您将使用 Cloud Shell,这是一个在 Google Cloud 中运行的命令行环境,它预加载了 bq。点击 Google Cloud 控制台顶部的“激活 Cloud Shell”。

- 连接到 Cloud Shell 后,您可以使用以下命令检查自己是否已通过身份验证,以及项目是否已设置为您的项目 ID:
gcloud auth list
- 在 Cloud Shell 中运行以下命令,以确认 gcloud 命令了解您的项目。
gcloud config list project
- 如果项目未设置,请使用以下命令进行设置:
gcloud config set project <YOUR_PROJECT_ID>
- 启用所需的 API。除了使用 gcloud 命令之外,您还可以通过控制台搜索每个产品或使用此 链接来启用 API。
如果缺少任何 API,您始终可以在实现过程中启用它。
如需了解 gcloud 命令和用法,请参阅 文档。
重要提示: 此外,请确保您已完成实验的 第 1 部分,以便完成此部分。
4. 创建代理
Agent Builder 简介
Agent Builder 是一款功能强大的低代码工具,可帮助我们快速高效地创建对话代理。它简化了设计对话流程、集成知识库和连接到外部 API 的过程。在本实验中,我们将使用 Agent Builder 与我们在第 1 部分中构建的 Cloud Functions 函数端点无缝连接,使我们的购物助理能够访问我们的零售知识库并智能地响应客户查询。
构建代理
我们来开始创建这个新代理,以便回答用户有关服装产品的问题。
- 首先,登录 Agent Builder 平台。如果系统提示您激活 API,请点击“继续并激活 API”。
- 点击“创建应用”,然后为您的代理指定一个描述性名称(例如“零售购物助理”)。
- 点击应用类型“代理”。

- 为您的代理指定一个描述性名称,例如“零售购物助理”,并将区域设置为 us-central1

- 输入代理的详细信息:
- 将代理名称更改为“零售购物代理”。
- 添加以下“目标”:
You are a shopping assistant agent! Your job is to help the customer search for their ideal apparels, allow them to add items to their cart, remove items from their cart, and review items in their cart. Once a user is done searching, open the search results in a user friendly html page.

- 此时保存,暂时将说明留空。
- 然后,在导航菜单中点击“工具”,再点击“创建”。

输入工具名称: 零售购物工具
输入工具说明:
This tool refers to the dataset in the backend as the context information for product inventory. It takes as input the user's search text summarized by the agent and matches with the most appropriate list of items and returns as an array of items.
输入架构 - YAML 格式的 OpenAPI:
在此部分中,我们将使用后端端点为代理提供支持。复制以下 OpenAPI 规范,并将网址占位符 (用尖括号括起来)替换为您的 Cloud Functions 端点:
openapi: 3.0.0
info:
title: AlloyDB Product Matcher
description: A Cloud Function to query AlloyDB for product matches based on user search text.
version: 1.0.0
servers:
- url: <<https://us-central1-YOUR_PROJECT_ID.cloudfunctions.net/alloy-gem>>
paths:
/:
post:
summary: Find matching products based on search text.
operationId: apparelSearch
requestBody:
description: JSON object containing the search text.
required: true
content:
application/json:
schema:
type: object
properties:
search:
type: string
description: The user's search query for product matching.
responses:
'200':
description: Successful response with a JSON array of matching products.
content:
application/json:
schema:
type: array
items:
type: object
properties:
id:
type: string
description: Product ID.
category:
type: string
description: Product category.
sub_category:
type: string
description: Product sub-category.
uri:
type: string
description: Product URI or URL.
description:
type: string
description: Product description.
literature:
type: object
description: JSON object containing match information from the ML model.
properties:
MATCH:
type: string
description: Whether the product matches the search query (YES/NO).
PERCENTAGE:
type: string
description: Percentage of match.
DIFFERENCE:
type: string
description: Description of differences between the search and product.
'500':
description: Internal server error.
将其他配置保留为默认值,然后点击“保存”。
- 此时返回代理,因为我们要将“工具”配置添加到代理的“说明”中。将以下内容添加到说明占位符中(请注意,缩进对于定义流程非常重要):
- Greet the user and answer their question to the best of your knowledge.
- Summarize the user's request and ask them to confirm that you understood correctly.
- Check if the request has details like gender, color, material, style and other key apparel details already.
- If not, seek clarifying details.
- If the search request is very broad, then ask the user to narrow down the request with specific details that you believe could be personal preferences.
- Once all the necessary details are met, summarize your final understanding of the request with the user.
- Use ${TOOL: Retail Shopping Tool} to help the user with their task.
- If the request has unrelated input text, gracefully convey that you don't have information on that topic.
- Do not give product availability information outside the source that is provided to you in ${TOOL: Retail Shopping Tool}.
- Do not assist with any information unless you are certain that you know the answer and it is grounded in the source of truth.
- Thank the user for their business and say goodbye.
确保在“可用工具”部分中选择了“零售购物工具”工具,然后再次保存代理。
5. 测试代理
在右侧窗格中,您应该会看到“预览代理”部分,您可以在其中测试代理。
如以下屏幕截图所示,我以用户的身份问好,并开始聊天,请求提供带有蓝色图案的白衬衫:

这是 JSON 响应:

这是 Cloud Functions 的原始 JSON 结果,用于处理 AlloyDB 相似性搜索。大功告成!我们现在已设置好代理。
6. 对话流程示例
此时,您可以测试和迭代对话代理的流程:
User: I'm looking for women's boots for winter.
Agent: (OpenAPI call to Cloud Function that works with AlloyDB Similarity Search)
Cloud Function: (Processes query, returns JSON array)
Agent: Based on your preferences, here are some options: [Product 1, Product 2, Product 3]. Would you like to see more details about any of these?
User: No, That is all. Bye!
Agent: Have a good day!
少样本提示
现在,假设您希望以某种图形格式(包括图片和链接)在对话代理中查看结果。我们将使用对话示例(也称为“少样本提示”)来实现此目的。
这意味着,我们希望向 Agent Builder 添加一些示例,以获得一致的结果格式。
在“测试代理”部分中的一个屏幕截图中,我们看到代理的响应为“我知道了。您在寻找带有蓝色图案的白衬衫…”。转到该响应,或在代理的右侧窗格中创建新的测试对话:
- 在“输入用户输入”部分中输入以下内容:

您将看到如下响应:

您还将看到 API 端点返回的 JSON 数组。
- 现在,点击预览部分边距中的代理名称(如下图中用红色对勾标记突出显示),聊天预览部分中的所有标签页都将突出显示。现在,点击预览部分右上角的“保存示例”。它将如下所示:

- 使用显示名称“带有图片的响应”或类似名称保存对话,然后点击“创建”。
- 现在,转到上图中用黑色对勾标记突出显示的响应标签页,并将文本“我知道了。您在寻找黄色凉鞋。是这样吗?”替换为以下内容:
I see you are looking for yellow sandals. Here are a few options for you:
<!DOCTYPE html>
<html>
<body>
<h2>Featured Sandals</h2>
<table style="overflow-x: auto; white-space: nowrap;">
<tr>
<td>
<img src="https://assets.myntassets.com/v1/images/style/properties/d27dbd8e9666b9af2d72fbfde315f76d_images.jpg" alt="Yellow sandals from Estd. 1977" width="300">
</td>
<td>
<img src="https://assets.myntassets.com/v1/images/style/properties/b7a479fe5f56457e930541a789c5df68_images.jpg" alt="Yellow sandals from Gliders" width="300">
</td>
<td>
<img src="https://assets.myntassets.com/v1/images/style/properties/b6c813734b29b89d1055fd000ea50743_images.jpg" alt="Yellow sandals from Rocia" width="300">
</td>
<td>
<img src="https://assets.myntassets.com/v1/images/style/properties/ee0e918c229e76e0e7e61712e9d2ecb3_images.jpg" alt="Yellow flip flops from Numero Uno" width="300">
</td>
<td>
<img src="https://assets.myntassets.com/v1/images/style/properties/5bdd4c9e739205e28ee134ff7849bc60_images.jpg" alt="Yellow flip flops from Numero Uno" width="300">
</td>
</tr>
</table>
</body>
</html>
- 点击顶部的“保存”。
针对您想要的任意数量的示例重复此过程,并使用不同的对话变体和流程。
现在,继续使用用户输入进行测试:

其他一些类型的响应:

大功告成。我们已成功为零售应用创建并测试了对话代理。
7. 部署和集成
对代理感到满意后,您可以使用 Agent Builder 的集成功能轻松将其部署到各种渠道。您可以将其嵌入到网站中,与热门消息传递平台集成,甚至可以创建专用的移动应用。我们还可以在 Web 客户端应用中直接使用 Agent Builder API,我们已在此 博客中对此进行了介绍。
8. 清理
为避免系统因本博文中使用的资源向您的 Google Cloud 账号收取费用,请按照以下步骤操作:
9. 恭喜
恭喜!通过将我们自定义构建的分析引擎的强大功能与 Agent Builder 的直观界面相结合,我们创建了一个智能零售购物助理,该助理可提供个性化体验、准确回答问题,并最终提高客户满意度和销售额。通过结合 AlloyDB、Vertex AI 和 Vector Search 的能力,我们在实现上下文搜索和向量搜索的无障碍性、高效性、真正意义驱动性和智能体方面取得了巨大飞跃!