1. 總覽
在現今快速變化的零售環境中,提供卓越的客戶服務並打造個人化購物體驗至關重要。我們將帶您踏上技術之旅,瞭解如何建立知識驅動的聊天應用程式,回答顧客問題、引導產品探索,以及提供量身打造的搜尋結果。這項創新解決方案結合了 AlloyDB 的資料儲存功能、用於瞭解情境的內部分析引擎、用於驗證相關性的 Gemini (大型語言模型),以及用於快速啟動智慧對話式助理的 Google 代理程式建構工具。
挑戰:現代零售業顧客期望獲得即時回覆,以及符合個人偏好的產品推薦。傳統的搜尋方法往往無法提供這種程度的個人化服務。
解決方案:我們的知識導向即時通訊應用程式可直接解決這項挑戰。這項功能會運用從零售資料衍生的豐富知識庫,瞭解顧客意圖、智慧回覆,並提供極為相關的結果。
建構項目
在本實驗室 (第 2 部分) 中,您將:
- 建構 Vertex AI Agent Builder 代理程式
- 將 AlloyDB 工具與代理程式整合
需求條件
2. 架構
資料流程:讓我們進一步瞭解資料在系統中的流動方式:
擷取:
零售資料 (包括庫存、產品說明、顧客互動) 會持續載入 AlloyDB。
Analytics Engine:
我們將使用 AlloyDB 做為分析引擎,執行下列作業:
- 擷取背景資訊:引擎會分析 AlloyDB 中儲存的資料,瞭解產品、類別、消費者行為等之間的關係 (如適用)。
- 建立嵌入:系統會為使用者查詢和 AlloyDB 中儲存的資訊產生嵌入 (文字的數學表示)。
- 向量搜尋:引擎會執行相似度搜尋,比較查詢嵌入項目與產品說明、評論和其他相關資料的嵌入項目。這會找出 25 個最相關的「最鄰近項目」。
Gemini 驗證:
這些可能的回答會送交 Gemini 評估。Gemini 會判斷這些資訊是否確實相關,以及是否適合分享給使用者。
生成回覆:
經過驗證的回應會建構為 JSON 陣列,整個引擎則會封裝到無伺服器 Cloud Run 函式中,並從 Agent Builder 叫用。
上述步驟已在實驗室第 1 部分 中說明。
我們討論了如何建立知識驅動的數據分析引擎,為智慧購物助理提供支援。現在,讓我們瞭解如何運用 Agent Builder 的神奇力量,在對話介面中實現這個引擎。請務必先準備好端點網址,再開始第 2 部分。本實驗室將涵蓋下一個步驟:
對話式互動:
Agent Builder 會以自然語言格式向使用者呈現回覆,方便進行來回對話。
3. 事前準備
建立專案
- 在 Google Cloud 控制台的專案選取器頁面中,選取或建立 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」。
- 按一下「建立應用程式」,然後為代理程式命名 (例如「零售購物助理」)。
- 按一下「代理商」應用程式類型。

- 為代理程式提供描述性名稱,例如「零售購物助理」,並將區域設為 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.

- 此時請儲存,並暫時將操作說明留空。
- 接著點選導覽選單中的「工具」,然後點選「建立」。

輸入工具名稱:零售購物工具
輸入工具說明:
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.
其他設定保留預設值,然後點選「儲存」。
- 此時請返回 Agent,因為我們要將「工具」設定新增至 Agent 的「指示」。在指令預留位置中新增下列內容 (請注意,縮排在定義流程時非常重要):
- 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 Function 處理 AlloyDB 相似度搜尋時傳回的原始 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!
少量樣本提示
現在,假設您想在對話式代理中以特定圖像格式 (包括圖片和連結) 查看結果,我們會使用對話範例 (也稱為少量樣本提示) 達成這個目標。
也就是說,我們想在代理程式建構工具中新增幾個範例,確保結果格式一致。
在「測試代理程式」一節的其中一個先前螢幕截圖中,我們看到代理程式的回覆是「I see. 你正在尋找印有藍色圖案的白色上衣...」。前往該回覆,或從服務專員的右側窗格建立新的測試對話:
- 在「Enter User Input」(輸入使用者輸入內容) 部分中輸入下列內容:

你會看到類似以下的回覆:

您也會看到 API 端點傳回的 JSON 陣列。
- 現在,請按一下預覽區邊緣的服務專員名稱 (如下圖以紅色勾號標示),聊天預覽區的所有分頁都會醒目顯示。現在請按一下預覽區右上角的「儲存範例」。介面外觀如下:

- 將對話儲存為「附上圖片的回覆」或類似的顯示名稱,然後按一下「建立」。
- 現在請前往上圖中以黑色勾號標示的回覆分頁,並將「I see. 你想找黃色涼鞋,「Is that correct?」替換為下列內容:
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 的整合功能,輕鬆將代理程式部署至各種管道。您可以將其嵌入網站、與熱門訊息平台整合,甚至建立專屬的行動應用程式。我們也可以直接在網頁用戶端應用程式中使用 Agent Builder API,這部分已在這篇網誌中說明。
8. 清理
如要避免系統向您的 Google Cloud 帳戶收取本文章所用資源的費用,請按照下列步驟操作:
- 前往 Google Cloud 控制台的「管理」
- 資源頁面。
- 在專案清單中選取要刪除的專案,然後點按「刪除」。
- 在對話方塊中輸入專案 ID,然後按一下「Shut down」(關機) 即可刪除專案。
9. 恭喜
恭喜!我們將自建的強大分析引擎與 Agent Builder 的直覺式介面整合,打造出智慧零售購物助理,提供個人化體驗、準確回答問題,最終提升顧客滿意度和銷售量。結合 AlloyDB、Vertex AI 和 Vector Search 的功能,我們在提供情境和向量搜尋功能方面取得重大進展,讓這類搜尋變得更有效率、真正以意義為導向,且具備代理功能!