目标
在本教程中,您将学习如何:
费用
本教程使用 Google Cloud的以下计费组件:
- Spanner
- Vertex AI
如需详细了解 Spanner 费用,请参阅 Spanner 价格页面。
如需详细了解 Vertex AI 费用,请参阅 Vertex AI 价格页面。
创建电子商务网站架构
在本教程中,我们使用以下架构和数据:
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 模型
在本教程中,我们将使用 Vertex AI text-bison 模型为最终客户提供个性化商品推荐。如需在 Spanner 数据库中注册此模型,请执行以下 DDL 语句:
CREATE MODEL TextBison
INPUT (prompt STRING(MAX))
OUTPUT (content STRING(MAX))
REMOTE
OPTIONS (
endpoint = '//aiplatform.googleapis.com/projects/PROJECT/locations/LOCATION/publishers/google/models/text-bison'
);
替换以下内容:
PROJECT
:项目 IDLOCATION
:您在其中使用 Vertex AI 的区域
架构发现和验证不适用于生成式 AI 模型。因此,您必须提供与模型架构匹配的 INPUT
和 OUTPUT
子句。您可以在 Vertex AI Model API 参考文档页面上找到 text-bison
模型的完整架构。
只要数据库和端点位于同一项目中,Spanner 应自动授予适当的权限。否则,请参阅 CREATE MODEL
参考页面的模型端点访问权限控制部分。
如需验证模型是否已正确注册,请使用 ML.PREDICT 函数对其进行查询。模型需要一个名为 prompt
的 STRING
列。您可以使用 Spanner 子查询生成 prompt
列。TextBison
模型需要您指定 maxOutputTokens
模型参数。其他参数为可选参数。Vertex AI text-bison
模型不支持批处理,因此您必须使用 @{remote_udf_max_rows_per_rpc=1}
参数将批次大小设置为 1。
SELECT content
FROM ML.PREDICT(
MODEL TextBison,
(SELECT "Is 13 prime?" AS prompt),
STRUCT(256 AS maxOutputTokens, 0.2 AS temperature, 40 as topK, 0.95 AS topP)
) @{remote_udf_max_rows_per_rpc=1};
+--------------------+
| content |
+--------------------+
| "Yes, 13 is prime" |
+--------------------+
使用 TextBison
模型回答客户问题
生成式 AI 文本模型可以解决各种问题。例如,某个用户在电子商务网站上浏览婴幼儿安全的商品。只需一个查询,我们就可以将用户的问题传递给 TextBison
模型。我们只需从数据库中提取商品详情,为问题提供相关背景信息即可。
注意:为简洁起见,部分模型回答已经过编辑。
SELECT product_id, product_name, content
FROM ML.PREDICT(
MODEL TextBison,
(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(100 AS maxOutputTokens)
) @{remote_udf_max_rows_per_rpc=1};
-- 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 TextBison,
(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?",
"My age:", CAST(user.age AS STRING), "\n",
"I like:", user.likes, "\n",
"Product name: ", product.name, "\n",
"Category mame: ", 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(256 AS maxOutputTokens)
) @{remote_udf_max_rows_per_rpc=1};
-- 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 TextBison,
(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's 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(40 AS maxOutputTokens)
) @{remote_udf_max_rows_per_rpc=1};
-- The model correctly guesses that a teenager is interested in a Bike,
-- but not a plush bear for infants or spicy peppers.
+------------+-----------------+---------+
| product_id | product_name | content |
+------------+-----------------+---------+
| 1 | "Plush Bear" | "NO" |
+------------+-----------------+---------+
| 2 | "Bike" | "YES" |
+------------+-----------------+---------+
| 3 | "Spicy peppers" | "NO" |
+------------+-----------------+---------+
您可以向提示中添加购买记录或其他相关详细信息,以便为客户提供更加个性化的体验。
Spanner Vertex AI 集成可帮助您组合包含实时数据的复杂提示,并使用这些提示构建支持 AI 的应用。