目标
在本教程中,您将学习如何:
费用
本教程使用 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
子句,
与模型架构相符。您可以找到text-bison
的完整架构
Vertex AI 上的
模型 API 参考文档页面。
只要数据库和端点在同一个项目中,
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 的应用。