1. 概览
在当今快节奏的零售环境中,提供卓越的客户服务并打造个性化的购物体验至关重要。我们将带您踏上技术之旅,创建一个知识驱动型聊天应用,该应用旨在回答客户问题、引导客户发现产品,并量身定制搜索结果。这款创新解决方案结合了 AlloyDB 强大的数据存储功能、用于上下文理解的内部分析引擎、用于相关性验证的 Gemini(大型语言模型),以及 Google 的 Agent Builder,可快速启动智能对话式助理。
面临的挑战:现代零售客户希望获得符合其独特偏好的即时答案和产品推荐。传统的搜索方法通常无法提供这种程度的个性化体验。
解决方案:我们的知识驱动型聊天应用可直接应对这一挑战。它利用从您的零售数据中提取的丰富知识库,了解客户意图、智能地做出响应,并提供高度相关的结果。
构建内容
在本实验(第 2 部分)中,您将:
- 构建 Vertex AI Agent Builder 代理
- 将 AlloyDB 工具与代理集成
要求
2. 架构
数据流:让我们来详细了解一下数据是如何在系统中移动的:
提取:
零售数据(目录、商品说明、客户互动)会持续加载到 AlloyDB 中。
分析引擎:
我们将使用 AlloyDB 作为分析引擎来执行以下操作:
- 情境提取:该引擎会分析存储在 AlloyDB 中的数据,以了解产品、类别、客户行为等之间的关系(如适用)。
- 嵌入创建:为用户的查询和 AlloyDB 中存储的信息生成嵌入(文本的数学表示法)。
- 向量搜索:引擎会执行相似度搜索,将查询嵌入与商品说明、评价和其他相关数据的嵌入进行比较。这将确定 25 个最相关的“最近邻”。
Gemini 验证:
这些可能的回答会发送给 Gemini 进行评估。Gemini 会确定这些信息是否真正相关且安全,是否可以与用户分享。
回答生成:
经过验证的响应会被构建为 JSON 数组,整个引擎会打包到从 Agent Builder 调用的无服务器 Cloud Run 函数中。
上述步骤已在本实验第 1 部分中介绍过。
我们讨论了创建知识驱动型分析引擎(为我们的购物助理提供支持)的技术细节。现在,我们来探索如何利用 Agent Builder 的强大功能,在对话界面中实现此引擎。在开始第 2 部分之前,请确保您已准备好端点网址。接下来我们将在本实验中介绍下一个步骤:
对话式互动:
Agent Builder 以自然语言格式向用户呈现响应,便于来回对话。
3. 准备工作
创建项目
- 在 Google Cloud Console 的项目选择器页面上,选择或创建一个 Google Cloud 项目。
- 确保您的 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,您随时可以在实现过程中启用它。
如需了解 gcloud 命令和用法,请参阅文档。
重要提示:此外,您还需要确保已完成实验的第 1 部分,才能完成此操作。
4. 创建代理
隆重推出 Agent Builder
Agent Builder 是一款功能强大的低代码工具,使我们能够快速高效地创建对话式代理。它简化了设计对话流程、集成知识库和连接到外部 API 的流程。在本例中,我们将使用 Agent Builder 无缝连接到我们在第 1 部分中构建的 Cloud Functions 函数端点,使购物助理能够访问我们的零售知识库,并智能地响应客户查询。
构建代理
我们先创建一个新的客服人员,用于回答用户关于服装产品的问题。
- 首先登录 Agent Builder 平台。如果系统提示您激活该 API,请点击“继续并激活该 API”。
- 点击“创建应用”,然后为您的客服人员指定一个描述性名称(例如“Retail Shopping Assistant”)。
- 点击应用类型“代理”。
- ,了解所有最新动态。为代理指定一个描述性名称,例如“Retail Shopping Assistant”并将区域设为 us-central1
- 输入客服人员的详细信息:
- 将代理名称更改为“Retail Shopping Agent”。
- 添加以下“目标”:
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.
- 此时,请保存该文件,暂时将说明留空。
- 然后,点击导航菜单中的“工具”,再点击“创建”。
输入工具名称:Retail Shopping Tool
输入工具说明:
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.
确保“Retail Shopping Tool”选择“可用工具”部分,然后重新保存该代理。
5. 测试代理
在右侧窗格中,您应该会看到“预览代理”部分,您可以在此部分测试代理。
如下面的屏幕截图所示,我作为一名用户向您问好,并开始聊天,请求购买带蓝印花的白色衬衫:
以下是 JSON 响应:
这是来自处理 AlloyDB 相似度搜索的 Cloud Functions 函数的原始 JSON 结果。大功告成!客服代理现在一切就绪了。
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!
少样本问题
现在,假设您希望以某种图形格式(包括图片和链接)在对话式 AI 助理中查看结果。我们将使用对话示例(也称为少样本提示)来实现这一点。
这意味着,我们需要向代理构建器添加一些示例,以使结果格式保持一致。
在之前的“测试客服人员”部分的某个屏幕截图中,我们看到客服人员的回答是“我明白。您要找一件印有蓝色图案的白色衬衫……”。转到该响应,或从代理的右侧窗格中创建新的测试对话:
- 在“输入用户输入”框中输入以下内容部分:
您会看到如下所示的响应:
您还会看到 API 端点返回的 JSON 数组。
- 现在,点击预览部分边缘的“Agent Name”(客服人员名称,如下图所示,用红色对勾突出显示),聊天预览部分中的所有标签页都会突出显示。现在点击“保存示例”图标。应用栏如下所示:
- 使用显示名称“回复(含图片)”保存对话或类似名称,然后点击“创建”。
- 现在,前往上图中用黑色对勾标记的“回答”标签页,然后替换“我明白。您要找黄色凉鞋。对吗?”替换为:
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 的功能,我们在使内容感知搜索和矢量搜索变得简单易用、高效、真正以意义为导向且具有代理功能方面取得了长足进步!