
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Create an embeddings model and configure it to a Vertex AI model endpoint
/ 20
Create a new table and load the products data
/ 40
Generate and store text embeddings for the products data
/ 20
Perform vector search using text embeddings in Spanner
/ 20
Imagine your applications searching your Spanner database and quickly identifying related data, even if the provided search phase is not actually included in the stored text! This is now possible by leveraging the power of Vertex AI text embeddings to conduct vector search within Spanner.
Spanner is a fully managed database service that offers transactional consistency at global scale and automatic, synchronous replication for high availability. In addition, you can leverage artificial intelligence (AI) functionality in Spanner to accomplish tasks such as building Generative AI applications and surfacing data in your Spanner database based on relevance to your specific search terms.
Vector search is a methodology that can be used to quickly find similar items based on their semantic meaning (rather than exact keyword matching) and can be applied to many types of data including audio, images, videos, and text. For text specifically, vector search enables you to find similar text items without needing their contents to match the exact text or phrase used in the search.
In this lab, you learn the fundamentals of configuring vector search in Spanner by first generating and storing text embeddings (vectors containing numerical representations of semantic meaning of text), and then using those text embeddings to perform fast similarity searches. This hands-on lab was adapted from the codelab titled Getting started with Spanner Vector Search and uses a dataset of bicycle products to highlight how vector search can be leveraged in Spanner to find the products most relevant to a search phrase without needing an exact match in the text.
In this lab, you learn how to:
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
A Spanner instance named cymbal-retail and a database named cymbal-bicycles have been provisioned for you in this lab environment.
In this task, you start the overall workflow in Spanner by creating and configuring the embeddings model used to generate the text embeddings, which also auto-initiates the creation of the Spanner service account needed for later tasks. Specifically, you create an embeddings model named EmbeddingsModel and configure it to an endpoint for the Vertex AI text embeddings model named text-embedding-005
.
In the Google Cloud console, click on the Navigation menu () > View All Products. Under Databases, click on Spanner.
On the Instances page, click on the instance named Cymbal Retail Spanner Instance to examine the instance details.
Under Databases, click on the database named cymbal-bicycles.
In the Spanner menu under Database, click Spanner Studio.
On the Spanner Studio page, click New SQL editor tab to open a new query window.
To create and configure the embeddings model to a Vertex AI model endpoint, copy and paste the following query in the query window, and click Run.
Click Check my progress to verify the objective.
When the query has executed successfully, you see a message that says Update completed.
Remain in Spanner Studio and proceed to the next task.
Now that you have created and configured the embeddings model, you need the data for which you want to generate the text embeddings for vector search.
In this task, you create a new table containing descriptive columns for products (such as product names and inventory count) for a fictitious company named Cymbal Bicycles, plus an extra column for the vector embeddings for the product descriptions (to be generated in the next task). Last, you load a sample of the Cymbal Bicycles products data into the table in Spanner.
In the Spanner Studio query editor, click Clear (along the same menu bar as Run) to remove the previous query.
To create a new table named products, copy and paste the following query in the query window, and click Run.
The column named productDescriptionEmbedding is of the type ARRAY<FLOAT32>
, which supports storage for the vector values that you create in the next task.
When the query has executed successfully, you see a message that says Update completed.
Click Clear again (along the same menu bar as Run) to remove the previous query.
To load data into the table, copy and paste the following query in the query window, and click Run.
Note that this query loads a small sample of products, but it includes all of the information needed for each of the 10 products loaded into the table.
When the query has executed successfully, you see a message that says This statement inserted 10 rows and did not return any rows.
Click Check my progress to verify the objective.
With the creation of the embeddings model in Task 1, you also initiated the creation of the Spanner service account used to access the embeddings model via the Vertex AI model endpoint.
In this task, you begin by ensuring that this service account has the role needed to access the Vertex AI endpoint. Then, you run a query to generate the embeddings for the product descriptions and store them in the products table.
In the Google Cloud console, on the Navigation menu (), select IAM & Admin > IAM.
Click Grant access.
For New principals, enter the Spanner service account ID: service-
For Select a role, search for Cloud Spanner API Service Agent using the filter, and select it to populate the Role box.
Click Save.
No change - principal already exists on the policy
, you may proceed to the next section without attempting to grant the role again.
Now that you have ensured the Spanner service account has the appropriate role, you can generate the text embeddings for the product descriptions and add them to the products table.
Return to Spanner Studio by following steps 1-4 in Task 1.
Copy and paste the following query into the query window to update the column named productDescriptionEmbedding with the generated embeddings, and click Run.
Permission denied on the resource
, wait a few minutes for the permissions that you assigned in the previous steps to fully propagate, and then run the query again.
When the query has executed successfully, you see a message that says 10 rows updated by your query.
Click Check my progress to verify the objective.
Remain in Spanner Studio and proceed to the next task.
After generating and storing the text embeddings for the product descriptions, your data is now ready for your first real time vector search!
In this task, you run a query to perform similarity search based on the phrase I'd like to buy a starter bike for my 3 year old child
and quickly return the top 5 most relevant products, despite there not being an exact match for this text in the product data.
In the Spanner Studio query editor, click Clear (along the same menu bar as Run) to remove the previous query.
To perform a vector search using the text embeddings, copy and paste the following query in the query window, and click Run.
This query uses the values in the productDescriptionEmbedding column to find the top 5 database rows that are the most semantically similar to the search phrase I'd like to buy a starter bike for my 3 year old child
.
Note that the embedding for the search phase is generated dynamically in the query. Feel free to explore this query more by replacing I'd like to buy a starter bike for my 3 year old child
in the code above with a new search term of your choice.
The output resembles the following:
productName | productDescription | inventoryCount | distance |
---|---|---|---|
Cymbal Sprout | Let their cycling journey begin with the Cymbal Sprout, the ideal balance bike for beginning riders ages 2-4 years... | 10 | 0.3094387191860244 |
Cymbal Spark Jr. | Light, vibrant, and ready for adventure, the Spark Jr. is the perfect first bike for young riders (ages 5-8)... | 34 | 0.3412342902117166 |
Cymbal Helios Helmet | Safety meets style with the Cymbal children's bike helmet... | 100 | 0.4197863319656684 |
Cymbal Breeze | Cruise in style and embrace effortless pedaling with the Breeze electric bike... | 72 | 0.485231776523978 |
Cymbal Phoenix Lights | See and be seen with the Phoenix bike lights... | 87 | 0.5251486508206732 |
Click Check my progress to verify the objective.
In this lab, you learned how to create an embeddings model in Spanner and configure it to a Vertex AI model endpoint, generate and store text embeddings in a Spanner table, and perform vector search in Spanner using the stored text embeddings.
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated February 10, 2025
Lab Last Tested February 10, 2025
Copyright 2025 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one