Generate text embeddings by using an open model and the AI.GENERATE_EMBEDDING function
Preview
This product or feature is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of theService Specific Terms. Pre-GA products and features are available "as is" and might have limited support. For more information, see thelaunch stage descriptions.
Note: To give feedback or request support for this feature, contactbqml-feedback@google.com.This tutorial shows you how to create aremote modelthat's based on theopen-source text embedding modelQwen3-Embedding-0.6B,and then how to use that model with theAI.GENERATE_EMBEDDING functionto embed movie reviews from thebigquery-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.getIamPolicyandresourcemanager.projects.setIamPolicy - Deploy and undeploy a Vertex AI model:
aiplatform.endpoints.deployaiplatform.endpoints.undeploy
- Create a model and run inference:
bigquery.jobs.createbigquery.models.createbigquery.models.getDatabigquery.models.updateDatabigquery.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.
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
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
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.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.createpermission.Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
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.enablepermission.Learn how to grant roles.
Create a dataset
Create a BigQuery dataset to store your ML model.
Console
In the Google Cloud console, go to theBigQuery page.
In theExplorer pane, click your project name.
ClickView actions > Create dataset
On theCreate dataset page, do the following:
ForDataset ID, enter
bqml_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.
Create a dataset named
bqml_tutorialwith 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
--datasetflag, the command uses the-dshortcut.If you omit-dand--dataset, the command defaults to creating adataset.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:
In the Google Cloud console, go to theBigQuery page.
In the query editor, run the following statement:
CREATEORREPLACEMODEL`bqml_tutorial.qwen3_embedding_model`REMOTEWITHCONNECTIONDEFAULTOPTIONS(HUGGING_FACE_MODEL_ID='Qwen/Qwen3-Embedding-0.6B');
The query takes up to 20 minutes to complete, after which theqwen3_embedding_model model appears in thebqml_tutorial dataset in theExplorer pane. Because the query uses aCREATE MODEL statement to create amodel, there are no query results.
Perform text embedding
Perform text embedding onIMDB movie reviews byusing the remote model and theAI.GENERATE_EMBEDDING function:
In the Google Cloud console, go to theBigQuery page.
In the query editor, enter the following statement to perform text embedding on five movie reviews:
SELECT*FROMAI.GENERATE_EMBEDDING(MODEL`bqml_tutorial.qwen3_embedding_model`,(SELECTreviewAScontent,*FROM`bigquery-public-data.imdb.reviews`LIMIT5));
The results include the following columns:
embedding: an array of double to represent the generated embeddings.status: the API response status for the correspondingrow. If the operation was successful, this value is empty.content: the input text from which to extract embeddings.- All of the columns from the
bigquery-public-data.imdb.reviewstable.
Undeploy model
If you choose not todelete your project as recommended, you mustundeploy the Qwen3 embedding 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.qwen3_embedding_model`SETOPTIONS(deploy_model=false);
For more information, seeAutomatic or immediate open model undeployment.
Clean up
What's next
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.