생성형 AI를 사용하여 전자상거래 애플리케이션의 맞춤 추천 받기

목표

이 튜토리얼에서는 다음을 수행하는 방법을 알아봅니다.

  • Google에서 제공하는 Vertex AI 생성형 AI 모델을 Spanner 데이터베이스에서 사용합니다.
  • 생성형 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 절을 제공해야 합니다. Vertex AI 모델 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 기반 애플리케이션을 빌드하는 데 도움이 됩니다.