Generate text by using a Gemma open model and the AI.GENERATE_TEXT function

This tutorial shows you how to create aremote modelthat's based on theGemma model,and then how to use that model with theAI.GENERATE_TEXT functionto extract keywords and perform sentiment analysis on movie reviews fromthebigquery-public-data.imdb.reviews public table.

Required permissions

To run this tutorial, you need the following Identity and Access Management (IAM)roles:

  • Create and use BigQuery datasets, connections, and models:BigQuery Admin (roles/bigquery.admin).
  • Grant permissions to the connection's service account: Project IAM Admin(roles/resourcemanager.projectIamAdmin).
  • Deploy and undeploy models in Vertex AI: Vertex AI Administrator(roles/aiplatform.admin).

These predefined roles contain the permissions required to perform the tasks inthis document. To see the exact permissions that are required, expand theRequired permissions section:

Required permissions

  • Create a dataset:bigquery.datasets.create
  • Create, delegate, and use a connection:bigquery.connections.*
  • Set the default connection:bigquery.config.*
  • Set service account permissions:resourcemanager.projects.getIamPolicy andresourcemanager.projects.setIamPolicy
  • Deploy and undeploy a Vertex AI model:
    • aiplatform.endpoints.deploy
    • aiplatform.endpoints.undeploy
  • Create a model and run inference:
    • bigquery.jobs.create
    • bigquery.models.create
    • bigquery.models.getData
    • bigquery.models.updateData
    • bigquery.models.updateMetadata

You might also be able to get these permissions withcustom roles or otherpredefined roles.

Costs

In this document, you use the following billable components of Google Cloud:

  • BigQuery ML: You incur costs for the data that you process in BigQuery.
  • Vertex AI: You incur costs for calls to the Vertex AI model that's represented by the remote model.

To generate a cost estimate based on your projected usage, use thepricing calculator.

New Google Cloud users might be eligible for afree trial.

For more information about BigQuery pricing, seeBigQuery pricing inthe BigQuery documentation.

Open models that you deploy to Vertex AI are charged permachine-hour. This means billing starts as soon as the endpoint is fully setup, and continues until you un-deploy it.For more information about Vertex AI pricing, see theVertex AI pricingpage.

Before you begin

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. Enable the BigQuery, BigQuery Connection, and Vertex AI APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enable permission.Learn how to grant roles.

    Enable the APIs

Create a dataset

Create a BigQuery dataset to store your ML model.

Console

  1. In the Google Cloud console, go to theBigQuery page.

    Go to the BigQuery page

  2. In theExplorer pane, click your project name.

  3. ClickView actions > Create dataset

  4. On theCreate dataset page, do the following:

    • ForDataset ID, enterbqml_tutorial.

    • ForLocation type, selectMulti-region, and then selectUS (multiple regions in United States).

    • Leave the remaining default settings as they are, and clickCreate dataset.

bq

To create a new dataset, use thebq mk commandwith the--location flag. For a full list of possible parameters, see thebq mk --dataset commandreference.

  1. Create a dataset namedbqml_tutorial with the data location set toUSand a description ofBigQuery ML tutorial dataset:

    bq --location=US mk -d \ --description "BigQuery ML tutorial dataset." \ bqml_tutorial

    Instead of using the--dataset flag, the command uses the-d shortcut.If you omit-d and--dataset, the command defaults to creating adataset.

  2. Confirm that the dataset was created:

    bqls

API

Call thedatasets.insertmethod with a defineddataset resource.

{"datasetReference":{"datasetId":"bqml_tutorial"}}

BigQuery DataFrames

Before trying this sample, follow the BigQuery DataFrames setup instructions in theBigQuery quickstart using BigQuery DataFrames. For more information, see theBigQuery DataFrames reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up ADC for a local development environment.

importgoogle.cloud.bigquerybqclient=google.cloud.bigquery.Client()bqclient.create_dataset("bqml_tutorial",exists_ok=True)

Create the remote model

Create a remote model that represents a hosted Vertex AImodel:

  1. In the Google Cloud console, go to theBigQuery page.

    Go to BigQuery

  2. In the query editor, run the following statement:

CREATEORREPLACEMODEL`bqml_tutorial.gemma_model`REMOTEWITHCONNECTIONDEFAULTOPTIONS(MODEL_GARDEN_MODEL_NAME='publishers/google/models/gemma3@gemma-3-270m-it',MACHINE_TYPE='g2-standard-12');

The query takes up to 20 minutes to complete, after which thegemma_modelmodel appears in thebqml_tutorial dataset in theExplorer pane. Becausethe query uses aCREATE MODEL statement to create a model, there are no queryresults.

Perform keyword extraction

Perform keyword extraction onIMDB movie reviews byusing the remote model and theAI.GENERATE_TEXT function:

  1. In the Google Cloud console, go to theBigQuery page.

    Go to BigQuery

  2. In the query editor, enter the following statement to perform keywordextraction on 10 movie reviews:

    -- This function takes your instruction and wraps it with chat template for-- better output quality.-- This is usually the recommended way when using Gemma instruction-tuned models.CREATETEMPFUNCTIONFormatPromptWithChatTemplate(user_instructionSTRING)AS(CONCAT('<start_of_turn>user\n',user_instruction,'<end_of_turn>\n<start_of_turn>model\n'));SELECT*FROMAI.GENERATE_TEXT(MODEL`bqml_tutorial.gemma_model`,(SELECTFormatPromptWithChatTemplate('Extract the key words from the movie review below: '||review)ASprompt,*FROM`bigquery-public-data.imdb.reviews`LIMIT10),STRUCT(0.2AStemperature,100ASmax_output_tokens));

    For more information about using chat templates with Gemma, seeGemma formatting and system instructions.

    The output is similar to the following, with non-generated columns omittedfor clarity:

    +----------------------------------------------+-------------------------+-----------------------------+-----+| result                                       | status                  | prompt                      | ... |+----------------------------------------------+-------------------------------------------------------+-----+| Here are some key words from the             |                         | <start_of_turn>user         |     || movie review:* **Romance:**                 |                         | Extract the key words from  |     || "romantic tryst," "elope"* **Comedy:**      |                         | the movie review below:     |     || "Contrived Comedy"* **Burglary:**           |                         | Linda Arvidson (as Jennie)  |     || "burglar," "rob," "booty"* **Chase:**       |                         | and Harry Solter (as Frank) |     || "chases," "escape"* **Director:** "D.W.     |                         | are enjoying a romantic     |     || Griffith"* **Actors:** "Linda Arvidson,"... |                         | tryst, when in walks her... |     |+----------------------------------------------+-------------------------+-----------------------------+-----+| Here are some key words from the             |                         | <start_of_turn>user         |     || movie review:* **Elderbush Gilch:** The     |                         | Extract the key words from  |     || name of the movie being reviewed.*          |                         | the movie review below:     |     || **Disappointment:** The reviewer's           |                         | This is the second addition |     || overall feeling about the film.*            |                         | to Frank Baum's personally  |     || **Dim-witted:** Describes the story          |                         | produced trilogy of Oz      |     || line negatively.* **Moronic, sadistic,...   |                         | films. It's essentially ... |     |+----------------------------------------------+-------------------------+-----------------------------+-----+

    The results include the following columns:

    • result: the generated text.
    • status: the API response status for the correspondingrow. If the operation was successful, this value is empty.
    • prompt: the prompt that is used for the sentiment analysis.
    • All of the columns from thebigquery-public-data.imdb.reviews table.

Perform sentiment analysis

Perform sentiment analysis onIMDB movie reviews byusing the remote model and theAI.GENERATE_TEXT function:

  1. In the Google Cloud console, go to theBigQuery page.

    Go to BigQuery

  2. In the query editor, run the following statement to perform sentimentanalysis on 10 movie reviews:

    -- This function takes your instruction and wraps it with chat template for-- better output quality.-- This is usually the recommended way when using Gemma instruction-tuned models.CREATETEMPFUNCTIONFormatPromptWithChatTemplate(user_instructionSTRING)AS(CONCAT('<start_of_turn>user\n',user_instruction,'<end_of_turn>\n<start_of_turn>model\n'));SELECT*FROMAI.GENERATE_TEXT(MODEL`bqml_tutorial.gemma_model`,(SELECTFormatPromptWithChatTemplate('Analyze the sentiment of the following movie review and classify it as either POSITIVE or NEGATIVE. \nMovie Review: '||review)ASprompt,*FROM`bigquery-public-data.imdb.reviews`LIMIT10),STRUCT(0.2AStemperature,128ASmax_output_tokens));

    For more information about using chat templates with Gemma, seeGemma formatting and system instructions.

    The output is similar to the following, with non-generated columns omittedfor clarity:

    +-----------------------------+-------------------------+-----------------------------+-----+| result                      | status                  | prompt                      | ... |+-----------------------------+-------------------------------------------------------+-----+| **NEGATIVE**                |                         | <start_of_turn>user         |     ||                             |                         | Analyze the sentiment of    |     ||                             |                         | movie review and classify   |     ||                             |                         | it as either POSITIVE or    |     ||                             |                         | NEGATIVE. Movie Review:     |     ||                             |                         | Although Charlie Chaplin    |     ||                             |                         | made some great short       |     ||                             |                         | comedies in the late...     |     |+-----------------------------+-------------------------+-----------------------------+-----+| **NEGATIVE**                |                         | <start_of_turn>user         |     ||                             |                         | Analyze the sentiment of    |     ||                             |                         | movie review and classify   |     ||                             |                         | it as either POSITIVE or    |     ||                             |                         | NEGATIVE. Movie Review:     |     ||                             |                         | Opulent sets and sumptuous  |     ||                             |                         | costumes well photographed  |     ||                             |                         | by Theodor Sparkuhl, and... |     |+-----------------------------+-------------------------+-----------------------------+-----+

    The results include the same columns documented forPerform keyword extraction.

Undeploy model

If you choose not todelete your project as recommended, you mustundeploy the Gemma model in Vertex AI to avoidcontinued billing for it. BigQuery automatically undeploys themodel after a specified period of idleness (6.5 hours by default).Alternatively, you can immediately undeploy the model by using theALTER MODEL statement,as shown in the following example:

ALTERMODEL`bqml_tutorial.gemma_model`SETOPTIONS(deploy_model=false);

For more information, seeAutomatic or immediate open model undeployment.

Clean up

    Caution: Deleting a project has the following effects:
    • Everything in the project is deleted. If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
    • Custom project IDs are lost. When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as anappspot.com URL, delete selected resources inside the project instead of deleting the whole project.

    If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects can help you avoid exceeding project quota limits.

  1. In the Google Cloud console, go to theManage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then clickDelete.
  3. In the dialog, type the project ID, and then clickShut down to delete the project.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-16 UTC.