目標
在本教學課程中,您將瞭解以下內容:
- 在 Spanner 資料庫中,使用 Google 提供的 Gemini Enterprise Agent Platform 生成式 AI 模型。
- 在範例電子商務應用程式中,運用生成式 AI 提供個人化的產品推薦服務。
費用
本教學課程使用 Cloud de Confiance by S3NS的計費元件,包括:
- Spanner
- Gemini Enterprise Agent Platform
建立電子商務網站結構定義
在本教學課程中,我們會使用下列結構定義和資料:
CREATE TABLE Products (
id INT64,
name STRING(MAX),
description STRING(MAX),
category_id INT64,
) PRIMARY KEY(id);
CREATE TABLE Categories (
id INT64,
name STRING(MAX)
) PRIMARY KEY(id);
CREATE TABLE Users (
id INT64,
age INT64,
likes STRING(MAX)
) PRIMARY KEY(id);
INSERT INTO Categories (id, name) VALUES
(1, "Toys"),
(2, "Tools");
INSERT INTO Products (id, name, description, category_id) VALUES
(1, "Plush Bear", "Really fluffy. Safe for infants.", 1),
(2, "Bike", "Bike for teenagers.", 1),
(3, "Drill", "Cordless.", 2);
INSERT INTO Users (id, age, likes) VALUES
(1, 30, "DIY"),
(2, 14, "Toys");
在 Spanner 結構定義中註冊生成式 AI 模型
在本教學課程中,您將使用 Agent Platform gemini-3.8-flash 模型,為顧客提供個人化產品推薦。如要在 Spanner 資料庫中註冊這個模型,請執行下列 DDL 陳述式:
CREATE MODEL GeminiFlash
INPUT (prompt STRING(MAX))
OUTPUT (content STRING(MAX))
REMOTE
OPTIONS (
endpoint = '//aiplatform.googleapis.com/projects/PROJECT/locations/global/publishers/google/models/gemini-3.8-flash',
default_batch_size = 1
);
將 PROJECT 替換為 Cloud de Confiance 專案 ID。
這個範例使用全域端點 (locations/global)。如果您有資料落地需求,可以將 global 替換為支援的多區域端點 (例如 us 或 eu)。這個模型不支援單一區域端點 (例如 us-central1)。
結構定義探索和驗證功能不適用於生成式 AI 模型。因此,您必須提供符合模型結構定義的 INPUT 和 OUTPUT 子句。如要進一步瞭解 gemini-3.8-flash 模型,請參閱 Agent Platform 的 Gemini 說明文件。
如果資料庫和端點位於同一個專案,Spanner 會自動授予必要權限。如要瞭解跨專案端點,請參閱「設定 Spanner Agent Platform 整合至 Agent Platform 端點的存取權」。
如要確認模型是否已正確註冊,請使用 ML.PREDICT 函式查詢。模型預期會收到名為 prompt 的單一 STRING 資料欄。您可以使用 Spanner 子查詢產生 prompt 欄。gemini-3.8-flash 模型會使用部分輸出詞元進行內部推論 (思考詞元)。請將 maxOutputTokens 設為足夠高的值 (例如 1024),以免思考詞元在生成文字輸出前就用盡詞元限制。模型不支援批次處理,因此請在模型選項中設定 default_batch_size = 1,一次傳送一個提示。
SELECT content
FROM ML.PREDICT(
MODEL GeminiFlash,
(SELECT "Is 13 prime?" AS prompt),
STRUCT(1024 AS maxOutputTokens)
);
+--------------------+
| content |
+--------------------+
| "Yes, 13 is prime" |
+--------------------+
使用 GeminiFlash 模型回答客戶問題
生成式 AI 文字模型可以解決各種問題。舉例來說,電子商務網站的使用者可能會瀏覽適合嬰兒的產品。只要執行單一查詢,就能將問題傳送至 GeminiFlash 模型。從資料庫擷取產品詳細資料,為問題提供相關背景資訊。
注意:部分模型答案經過編輯,力求簡潔。
SELECT product_id, product_name, content
FROM ML.PREDICT(
MODEL GeminiFlash,
(SELECT
product.id as product_id,
product.name as product_name,
CONCAT("Is this product safe for infants?", "\n",
"Product Name: ", product.name, "\n",
"Category Name: ", category.name, "\n",
"Product Description:", product.description) AS prompt
FROM
Products AS product JOIN Categories AS category
ON product.category_id = category.id),
STRUCT(1024 AS maxOutputTokens)
);
-- The model correctly recommends a Plush Bear as safe for infants.
-- Other products are not safe and the model provides justification why.
+------------+-----------------+--------------------------------------------------------------------------------------------------+
| product_id | product_name | content |
+------------+-----------------+--------------------------------------------------------------------------------------------------+
| 1 | "Plush Bear" | "Yes, this product is infant safe. [...] " |
| | | "The product description says that the product is safe for infants. [...]" |
+------------+-----------------+--------------------------------------------------------------------------------------------------+
| 2 | "Bike" | "No, this product is not infant safe. [...] " |
| | | "It is not safe for infants because it is too big and heavy for them to use. [...]" |
+------------+-----------------+--------------------------------------------------------------------------------------------------+
| 3 | "Drill" | "No, this product is not infant safe. [...]" |
| | | " If an infant were to grab the drill, they could pull it on themselves and cause injury. [...]" |
+------------+-----------------+--------------------------------------------------------------------------------------------------+
如要直接在參數中填入顧客問題,可以將問題文字替換為查詢參數,例如 @UserQuestion。為顧客提供 AI 輔助的線上購物體驗。
為顧客提供個人化產品推薦
除了產品詳細資料,您也可以將客戶相關資訊新增至 prompt。這樣一來,模型就能考量使用者偏好,提供完全個人化的產品推薦。
SELECT product_id, product_name, content
FROM ML.PREDICT(
MODEL GeminiFlash,
(SELECT
product.id as product_id,
product.name as product_name,
CONCAT(
"Answer with YES or NO only: Is this a good fit for me?",
"\nMy age:", CAST(user.age AS STRING), "\n",
"I like:", user.likes, "\n",
"Product name: ", product.name, "\n",
"Category name: ", category.name, "\n",
"Product description:", product.description) AS prompt,
FROM
Products AS product
JOIN Categories AS category ON product.category_id = category.id
JOIN Users AS user ON user.id = 1),
STRUCT(1024 AS maxOutputTokens)
);
-- The model correctly guessed that the user might be interested in a Drill
-- as they are interested in DIY.
+------------+-----------------+-------------+
| product_id | product_name | content |
+------------+-----------------+-------------+
| 1 | "Plush Bear" | "NO" |
+------------+-----------------+-------------+
| 2 | "Bike" | "NO" |
+------------+-----------------+-------------+
| 3 | "Drill" | "YES" |
+------------+-----------------+-------------+
使用者可以為青少年建立個人資料,查看不同的推薦清單,為孩子挑選禮物:
SELECT product_id, product_name, content
FROM ML.PREDICT(
MODEL GeminiFlash,
(SELECT
product.id as product_id,
product.name as product_name,
CONCAT(
"Answer with YES or NO only: Is this a good fit for me?",
"\nMy age:", CAST(user.age AS STRING),
"\nI like:", user.likes,
"\nProduct Name: ", product.name,
"\nCategory Name: ", category.name,
"\nProduct Description:", product.description) AS prompt,
FROM
Products AS product
JOIN Categories AS category ON product.category_id = category.id
JOIN Users AS user ON user.id = 2),
STRUCT(1024 AS maxOutputTokens)
);
-- The model correctly guesses that a teenager is interested in a Bike,
-- but not a plush bear for infants or a drill.
+------------+-----------------+---------+
| product_id | product_name | content |
+------------+-----------------+---------+
| 1 | "Plush Bear" | "NO" |
+------------+-----------------+---------+
| 2 | "Bike" | "YES" |
+------------+-----------------+---------+
| 3 | "Drill" | "NO" |
+------------+-----------------+---------+
您可以在提示中加入購買記錄或其他相關詳細資料,為顧客提供更貼近需求的體驗。
Spanner Agent Platform 整合功能可協助您組合包含即時資料的複雜提示,並用於建構由 AI 技術支援的應用程式。