使用生成式 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 数据库中注册此模型, 执行以下 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 子句, 与模型架构相符。您可以找到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 赋能的应用。