> [!WARNING]
> **PostgreSQL interface note:** Spanner Agent Platform integration is not supported in PostgreSQL-dialect databases.

## Objective

In this tutorial, you learn how to:

- Use Google provided Gemini Enterprise Agent Platform [Generative AI](https://docs.cloud.google.com/gemini-enterprise-agent-platform/overview.md.txt) models in a Spanner database.
- Use [Generative AI](https://docs.cloud.google.com/gemini-enterprise-agent-platform/overview.md.txt) to provide personalized product recommendations in a sample ecommerce application.

## Costs

This tutorial uses billable components of Google Cloud, including:

- Spanner
- Gemini Enterprise Agent Platform

For more information about Spanner costs, see the
[Spanner pricing](https://docs.cloud.google.com/spanner/pricing) page.

For more information about Agent Platform costs, see the
[Agent Platform pricing](https://docs.cloud.google.com/products/gemini-enterprise-agent-platform/pricing) page.

## Create the ecommerce website schema

For this tutorial, we use the following schema and data:

```sql
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");
```

## Register a Generative AI model in a Spanner schema

In this tutorial, you use the Agent Platform
`gemini-3.8-flash` model to provide personalized product recommendations to
customers. To register this model in a Spanner database,
[execute](https://docs.cloud.google.com/spanner/docs/schema-updates.md.txt) the following DDL [statement](https://docs.cloud.google.com/spanner/docs/reference/standard-sql/data-definition-language.md.txt#create_model):

```sql
CREATE MODEL GeminiFlash
INPUT (prompt STRING(MAX))
OUTPUT (content STRING(MAX))
REMOTE
OPTIONS (
  endpoint = '//aiplatform.googleapis.com/projects/PROJECT/locations/global/publishers/google/models/gemini-3.8-flash',
  default_batch_size = 1
);
```

Replace `PROJECT` with your Google Cloud project ID.

This example uses the global endpoint (`locations/global`). If you have
data-residency requirements, you can replace `global` with a supported
multi-region endpoint (such as `us` or `eu`). Single-region endpoints (such as
`us-central1`) aren't supported for this model.

Schema discovery and validation isn't available for Generative AI models.
Therefore, you must provide `INPUT` and `OUTPUT` clauses that match the model's
schema. For more information about the `gemini-3.8-flash` model, see the
Agent Platform
[Gemini documentation](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/guides/gemini-3-8-flash.md.txt).

Spanner grants the necessary permissions automatically when the
database and the endpoint are in the same project. For cross-project endpoints,
see
[Configure access for Spanner Agent Platform integration to Agent Platform endpoints](https://docs.cloud.google.com/spanner/docs/ml-tutorial.md.txt#setup).

To verify that the model was registered correctly, query it with the
[ML.PREDICT](https://docs.cloud.google.com/spanner/docs/reference/standard-sql/ml-functions.md.txt#mlpredict) function. The model expects a single `STRING` column
named `prompt`. You can use a [Spanner subquery](https://docs.cloud.google.com/spanner/docs/reference/standard-sql/subqueries.md.txt)
to generate the `prompt` column. The `gemini-3.8-flash` model uses some output
tokens for internal reasoning (*thinking tokens* ). Set `maxOutputTokens` high
enough, such as `1024`, so that thinking tokens don't exhaust the token limit
before generating text output. The model doesn't support batching, so set
`default_batch_size = 1` in the model options to send prompts one at a time.

```sql
SELECT content
FROM ML.PREDICT(
  MODEL GeminiFlash,
  (SELECT "Is 13 prime?" AS prompt),
  STRUCT(1024 AS maxOutputTokens)
);

+---+
| content            |
+---+
| "Yes, 13 is prime" |
+---+
```

## Use the `GeminiFlash` model to answer customer questions

Generative AI text models can solve a wide array of problems.
For example, a user on an ecommerce website might browse for
products that are safe for infants. With a single query, you can
pass their question to the `GeminiFlash` model. Provide relevant context for
the question by fetching product details from the database.

NOTE: Some model answers were edited for brevity.

```sql
SELECT product_id, product_name, content
FROM ML.PREDICT(
  MODEL GeminiFlash,
  (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(1024 AS maxOutputTokens)
);

-- 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. [...]" |
+---+---+---+
```

You can replace the question literal with a query parameter, such as
`@UserQuestion`, if you want to directly populate the parameter with a
customer question. This gives the customer an AI-powered online
shopping experience.

## Provide personalized product recommendations to customers

In addition to product details, you can also add information about the
customer to the `prompt`. This lets the model take user preferences into
consideration so that it can provide fully personalized product recommendations.

```sql
SELECT product_id, product_name, content
FROM ML.PREDICT(
  MODEL GeminiFlash,
  (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 age:", CAST(user.age AS STRING), "\n",
        "I like:", user.likes,  "\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
       JOIN Users AS user ON user.id = 1),
  STRUCT(1024 AS maxOutputTokens)
);

-- 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"       |
+---+---+---+
```

To look for a gift for their child, the user can create a profile for their
teenager and see a different list of recommendations:

```sql
SELECT product_id, product_name, content
FROM ML.PREDICT(
  MODEL GeminiFlash,
  (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 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(1024 AS maxOutputTokens)
);

-- The model correctly guesses that a teenager is interested in a Bike,
-- but not a plush bear for infants or a drill.
+---+---+---+
| product_id | product_name    | content |
+---+---+---+
|          1 | "Plush Bear"    | "NO"    |
+---+---+---+
|          2 | "Bike"          | "YES"   |
+---+---+---+
|          3 | "Drill"         | "NO"    |
+---+---+---+
```

You can add purchase history or other relevant details to the prompt to give
the customer a more customized experience.

Spanner Agent Platform integration helps you assemble complex prompts containing live data
and use them to build AI-enabled applications.