使用生成式 AI 在电子商务应用中获取个性化推荐

目标

在本教程中,您将学习如何:

  • 在 Spanner 数据库中使用 Google 提供的 Vertex AI 生成式 AI 模型。
  • 使用生成式 AI 在示例电子商务应用中提供个性化商品推荐。

费用

本教程使用 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 数据库中注册此模型,请execute以下 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:项目 ID
  • LOCATION:您使用 Vertex AI 的区域

架构发现和验证不适用于生成式 AI 模型。因此,您必须提供与模型架构匹配的 INPUTOUTPUT 子句。您可以在 Vertex AI 模型 API 参考文档页面中找到 text-bison 模型的完整架构。

只要数据库和端点位于同一项目中,Spanner 就应该自动授予适当的权限。否则,请查看 CREATE MODEL 参考页面的模型端点访问权限控制部分。

如需验证模型是否已正确注册,请使用 ML.PREDICT 函数进行查询。模型需要一个名为 promptSTRING 列。您可以使用 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 的应用。