1. Introduction
This codelab provides a guide to deploying AlloyDB with AI operators and utilizing them for tasks such as semantic search, joins, and result ranking.
Prerequisites
- A basic understanding of Google Cloud, console
- Basic skills in command line interface and Cloud Shell
What you'll learn
- How to deploy AlloyDB for Postgres
- How to enable AlloyDB AI operators
- How to use different AlloyDB AI operators
- How to use AlloyDB AI operators in a RAG workflow
What you'll need
- A Google Cloud Account and Google Cloud Project
- A web browser such as Chrome supporting Google Cloud console and Cloud Shell
2. Setup and Requirements
Self-paced environment setup
- Sign-in to the Google Cloud Console and create a new project or reuse an existing one. If you don't already have a Gmail or Google Workspace account, you must create one.
- The Project name is the display name for this project's participants. It is a character string not used by Google APIs. You can always update it.
- The Project ID is unique across all Google Cloud projects and is immutable (cannot be changed after it has been set). The Cloud Console auto-generates a unique string; usually you don't care what it is. In most codelabs, you'll need to reference your Project ID (typically identified as
PROJECT_ID
). If you don't like the generated ID, you might generate another random one. Alternatively, you can try your own, and see if it's available. It can't be changed after this step and remains for the duration of the project. - For your information, there is a third value, a Project Number, which some APIs use. Learn more about all three of these values in the documentation.
- Next, you'll need to enable billing in the Cloud Console to use Cloud resources/APIs. Running through this codelab won't cost much, if anything at all. To shut down resources to avoid incurring billing beyond this tutorial, you can delete the resources you created or delete the project. New Google Cloud users are eligible for the $300 USD Free Trial program.
Start Cloud Shell
While Google Cloud can be operated remotely from your laptop, in this codelab you will be using Google Cloud Shell, a command line environment running in the Cloud.
From the Google Cloud Console, click the Cloud Shell icon on the top right toolbar:
It should only take a few moments to provision and connect to the environment. When it is finished, you should see something like this:
This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on Google Cloud, greatly enhancing network performance and authentication. All of your work in this codelab can be done within a browser. You do not need to install anything.
3. Before you begin
Enable API
Inside Cloud Shell, make sure that your project ID is setup:
gcloud config set project [YOUR-PROJECT-ID]
Set environment variable PROJECT_ID:
PROJECT_ID=$(gcloud config get-value project)
Enable all necessary services:
gcloud services enable alloydb.googleapis.com \
compute.googleapis.com \
cloudresourcemanager.googleapis.com \
servicenetworking.googleapis.com \
aiplatform.googleapis.com \
discoveryengine.googleapis.com \
secretmanager.googleapis.com
Expected output
student@cloudshell:~ (test-project-001-402417)$ gcloud config set project test-project-001-402417 Updated property [core/project]. student@cloudshell:~ (test-project-001-402417)$ PROJECT_ID=$(gcloud config get-value project) Your active configuration is: [cloudshell-14650] student@cloudshell:~ (test-project-001-402417)$ student@cloudshell:~ (test-project-001-402417)$ gcloud services enable alloydb.googleapis.com \ compute.googleapis.com \ cloudresourcemanager.googleapis.com \ servicenetworking.googleapis.com \ aiplatform.googleapis.com Operation "operations/acat.p2-4470404856-1f44ebd8-894e-4356-bea7-b84165a57442" finished successfully.
4. Deploy AlloyDB
Create AlloyDB cluster and primary instance. The following procedure describes how to create an AlloyDB cluster and instance using Google Cloud SDK. If you prefer the console approach you can follow the documentation here.
Before creating an AlloyDB cluster we need an available private IP range in our VPC to be used by the future AlloyDB instance. If we don't have it then we need to create it, assign it to be used by internal Google services and after that we will be able to create the cluster and instance.
Create private IP range
We need to configure Private Service Access configuration in our VPC for AlloyDB. The assumption here is that we have the "default" VPC network in the project and it is going to be used for all actions.
Create the private IP range:
gcloud compute addresses create psa-range \
--global \
--purpose=VPC_PEERING \
--prefix-length=24 \
--description="VPC private service access" \
--network=default
Create private connection using the allocated IP range:
gcloud services vpc-peerings connect \
--service=servicenetworking.googleapis.com \
--ranges=psa-range \
--network=default
Expected console output:
student@cloudshell:~ (test-project-402417)$ gcloud compute addresses create psa-range \ --global \ --purpose=VPC_PEERING \ --prefix-length=24 \ --description="VPC private service access" \ --network=default Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/addresses/psa-range]. student@cloudshell:~ (test-project-402417)$ gcloud services vpc-peerings connect \ --service=servicenetworking.googleapis.com \ --ranges=psa-range \ --network=default Operation "operations/pssn.p24-4470404856-595e209f-19b7-4669-8a71-cbd45de8ba66" finished successfully. student@cloudshell:~ (test-project-402417)$
Create AlloyDB Cluster
In this section we are creating an AlloyDB cluster in the us-central1 region.
Define password for the postgres user. You can define your own password or use a random function to generate one
export PGPASSWORD=`openssl rand -hex 12`
Expected console output:
student@cloudshell:~ (test-project-402417)$ export PGPASSWORD=`openssl rand -hex 12`
Note the PostgreSQL password for future use:
echo $PGPASSWORD
Expected console output:
student@cloudshell:~ (test-project-402417)$ echo $PGPASSWORD bbefbfde7601985b0dee5723
Create a Free Trial Cluster
If you haven't been using AlloyDB before you can create a free trial cluster:
Define region and AlloyDB cluster name. We are going to use us-central1 region and alloydb-aip-01 as a cluster name:
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
Run command to create the cluster:
gcloud alloydb clusters create $ADBCLUSTER \
--password=$PGPASSWORD \
--network=default \
--region=$REGION \
--subscription-type=TRIAL
Expected console output:
export REGION=us-central1 export ADBCLUSTER=alloydb-aip-01 gcloud alloydb clusters create $ADBCLUSTER \ --password=$PGPASSWORD \ --network=default \ --region=$REGION \ --subscription-type=TRIAL Operation ID: operation-1697655441138-6080235852277-9e7f04f5-2012fce4 Creating cluster...done.
Create an AlloyDB primary instance for our cluster in the same cloud shell session. If you are disconnected you will need to define the region and cluster name environment variables again.
gcloud alloydb instances create $ADBCLUSTER-pr \
--instance-type=PRIMARY \
--cpu-count=8 \
--region=$REGION \
--cluster=$ADBCLUSTER
Expected console output:
student@cloudshell:~ (test-project-402417)$ gcloud alloydb instances create $ADBCLUSTER-pr \ --instance-type=PRIMARY \ --cpu-count=8 \ --region=$REGION \ --availability-type ZONAL \ --cluster=$ADBCLUSTER Operation ID: operation-1697659203545-6080315c6e8ee-391805db-25852721 Creating instance...done.
Create AlloyDB Standard Cluster
If it is not your first AlloyDB cluster in the project proceed with creation of a standard cluster.
Define region and AlloyDB cluster name. We are going to use us-central1 region and alloydb-aip-01 as a cluster name:
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
Run command to create the cluster:
gcloud alloydb clusters create $ADBCLUSTER \
--password=$PGPASSWORD \
--network=default \
--region=$REGION
Expected console output:
export REGION=us-central1 export ADBCLUSTER=alloydb-aip-01 gcloud alloydb clusters create $ADBCLUSTER \ --password=$PGPASSWORD \ --network=default \ --region=$REGION Operation ID: operation-1697655441138-6080235852277-9e7f04f5-2012fce4 Creating cluster...done.
Create an AlloyDB primary instance for our cluster in the same cloud shell session. If you are disconnected you will need to define the region and cluster name environment variables again.
gcloud alloydb instances create $ADBCLUSTER-pr \
--instance-type=PRIMARY \
--cpu-count=2 \
--region=$REGION \
--cluster=$ADBCLUSTER
Expected console output:
student@cloudshell:~ (test-project-402417)$ gcloud alloydb instances create $ADBCLUSTER-pr \ --instance-type=PRIMARY \ --cpu-count=2 \ --region=$REGION \ --availability-type ZONAL \ --cluster=$ADBCLUSTER Operation ID: operation-1697659203545-6080315c6e8ee-391805db-25852721 Creating instance...done.
5. Prepare Database
We need to create a database, enable Vertex AI integration, create database objects and import the data.
Grant Necessary Permissions to AlloyDB
Add Vertex AI permissions to the AlloyDB service agent.
Open another Cloud Shell tab using the sign "+" at the top.
In the new cloud shell tab execute:
PROJECT_ID=$(gcloud config get-value project)
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:service-$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")@gcp-sa-alloydb.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"
Expected console output:
student@cloudshell:~ (test-project-001-402417)$ PROJECT_ID=$(gcloud config get-value project) Your active configuration is: [cloudshell-11039] student@cloudshell:~ (test-project-001-402417)$ gcloud projects add-iam-policy-binding $PROJECT_ID \ --member="serviceAccount:service-$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")@gcp-sa-alloydb.iam.gserviceaccount.com" \ --role="roles/aiplatform.user" Updated IAM policy for project [test-project-001-402417]. bindings: - members: - serviceAccount:[email protected] role: roles/aiplatform.user - members: ... etag: BwYIEbe_Z3U= version: 1
Close the tab by either execution command "exit" in the tab:
exit
Connect to AlloyDB Studio
In the following chapters all the SQL commands requiring connection to the database can be alternatively executed in the AlloyDB Studio. To run the command you need to open the web console interface for your AlloyDB cluster by clicking on the primary instance.
Then click on AlloyDB Studio on the left:
Choose the postgres database, user postgres and provide the password noted when we created the cluster. Then click on the "Authenticate" button.
It will open the AlloyDB Studio interface. To run the commands in the database you click on the "Editor 1" tab on the right.
It opens interface where you can run SQL commands
Create Database
Create database quickstart.
In the AlloyDB Studio Editor execute the following command.
Create database:
CREATE DATABASE quickstart_db
Expected output:
Statement executed successfully
Connect to quickstart_db
Reconnect to the studio using the button to switch user/database.
Pick up from the dropdown list the new quickstart_db database and use the same user and password as before.
It will open a new connection where you can work with objects from the quickstart_db database.
Verify google_ml extension
Check the google_ml extension version to make sure it is 1.4.4 or higher to be able to use the AI query engine.
In the AlloyDB Studio while connected to the quickstart_db execute:
SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';
Expected output:
1.4.4
If the version is lower than the required then update the extension.
In the AlloyDB Studio while connected to the quickstart_db execute:
CALL google_ml.upgrade_to_preview_version();
Expected output:
Statement executed successfully
After successful execution, verify the version again.
In the AlloyDB Studio while connected to the quickstart_db execute:
SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';
Expected output:
1.4.4
6. Sample Data
Now we need to create objects in the database and load data. We are going to use a fictional movie dataset with a few rows.
Copy the following statements to the AlloyDB Studio Editor and push the "Run" button.
-- Drop tables if they exist to prevent errors on re-running the script
DROP TABLE IF EXISTS movie_reviews;
DROP TABLE IF EXISTS movies;
-- Create the 'movies' table
-- This table stores information about each movie.
CREATE TABLE movies (
id BIGINT PRIMARY KEY, -- Unique identifier for the movie
title TEXT NOT NULL, -- Title of the movie
description TEXT, -- A brief description or synopsis of the movie
genres TEXT, -- Comma-separated list of genres (e.g., "Action, Adventure, Sci-Fi")
actors TEXT -- Comma-separated list of main actors
);
-- Create the 'movie_reviews' table
-- This table stores reviews for the movies.
CREATE TABLE movie_reviews (
review_id BIGINT PRIMARY KEY, -- Unique identifier for the review
movie_id BIGINT NOT NULL, -- Foreign key referencing the movie being reviewed
reviewer_name TEXT, -- Name of the person who wrote the review
rating INT CHECK (rating >= 1 AND rating <= 5), -- Rating from 1 to 5 stars
review_text TEXT, -- The content of the review
review_date DATE DEFAULT CURRENT_DATE, -- Date when the review was submitted
FOREIGN KEY (movie_id) REFERENCES movies(id) ON DELETE CASCADE -- Ensures referential integrity; if a movie is deleted, its reviews are also deleted.
);
-- Insert sample data into the 'movies' table (20 rows)
INSERT INTO movies (id, title, description, genres, actors) VALUES
(1, 'Inception', 'A thief who steals information by entering people''s dreams.', 'Sci-Fi, Thriller, Action', 'Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page'),
(2, 'The Matrix', 'A computer hacker learns about the true nature of his reality.', 'Sci-Fi, Action', 'Keanu Reeves, Laurence Fishburne, Carrie-Anne Moss'),
(3, 'Interstellar', 'A team of explorers journey through a cosmic passage beyond our world in an attempt to ensure humanity''s survival.', 'Sci-Fi, Drama, Adventure', 'Matthew McConaughey, Anne Hathaway, Jessica Chastain'), -- Updated description
(4, 'The Dark Knight', 'When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.', 'Action, Crime, Drama', 'Christian Bale, Heath Ledger, Aaron Eckhart'),
(5, 'Pulp Fiction', 'The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.', 'Crime, Drama', 'John Travolta, Uma Thurman, Samuel L. Jackson'),
(6, 'Forrest Gump', 'The presidencies of Kennedy and Johnson, the Vietnam War, the Watergate scandal and other historical events unfold from the perspective of an Alabama man with an IQ of 75.', 'Drama, Romance', 'Tom Hanks, Robin Wright, Gary Sinise'),
(7, 'The Shawshank Redemption', 'Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.', 'Drama', 'Tim Robbins, Morgan Freeman, Bob Gunton'),
(8, 'Gladiator', 'A former Roman General sets out to exact vengeance against the corrupt emperor who murdered his family and sent him into slavery.', 'Action, Adventure, Drama', 'Russell Crowe, Joaquin Phoenix, Connie Nielsen'),
(9, 'Fight Club', 'An insomniac office worker looking for a way to change his life crosses paths with a devil-may-care soap maker and they form an underground fight club that evolves into something much, much more.', 'Drama', 'Brad Pitt, Edward Norton, Meat Loaf'),
(10, 'The Lord of the Rings: The Return of the King', 'Gandalf and Aragorn lead the World of Men against Sauron''s army to draw his gaze from Frodo and Sam as they approach Mount Doom with the One Ring.', 'Action, Adventure, Drama', 'Elijah Wood, Viggo Mortensen, Ian McKellen'),
(11, 'Spirited Away', 'During her family''s move to the suburbs, a sullen 10-year-old girl wanders into a world ruled by gods, witches, and spirits, and where humans are changed into beasts.', 'Animation, Adventure, Family', 'Daveigh Chase, Suzanne Pleshette, Miyu Irino'),
(12, 'Parasite', 'Greed and class discrimination threaten the newly formed symbiotic relationship between the wealthy Park family and the destitute Kim clan.', 'Comedy, Drama, Thriller', 'Song Kang-ho, Lee Sun-kyun, Cho Yeo-jeong'),
(13, 'The Godfather', 'The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.', 'Crime, Drama', 'Marlon Brando, Al Pacino, James Caan'),
(14, 'Avengers: Endgame', 'After the devastating events of Avengers: Infinity War, the universe is in ruins. With the help of remaining allies, the Avengers assemble once more in order to reverse Thanos'' actions and restore balance to the universe.', 'Action, Adventure, Drama', 'Robert Downey Jr., Chris Evans, Mark Ruffalo'),
(15, 'Joker', 'In Gotham City, mentally troubled comedian Arthur Fleck is disregarded and mistreated by society. He then embarks on a downward spiral of revolution and bloody crime.', 'Crime, Drama, Thriller', 'Joaquin Phoenix, Robert De Niro, Zazie Beetz'),
(16, 'Mad Max: Fury Road', 'In a post-apocalyptic wasteland, a woman rebels against a tyrannical ruler in search for her homeland with the help of a group of female prisoners, a psychotic worshiper, and a drifter named Max.', 'Action, Adventure, Sci-Fi', 'Tom Hardy, Charlize Theron, Nicholas Hoult'),
(17, 'Coco', 'Aspiring musician Miguel, confronted with his family''s ancestral ban on music, enters the Land of the Dead to find his great-great-grandfather, a legendary singer.', 'Animation, Adventure, Family', 'Anthony Gonzalez, Gael García Bernal, Benjamin Bratt'),
(18, 'Whiplash', 'A promising young drummer enrolls at a cut-throat music conservatory where his dreams of greatness are mentored by an instructor who will stop at nothing to realize a student''s potential.', 'Drama, Music', 'Miles Teller, J.K. Simmons, Paul Reiser'),
(19, 'The Grand Budapest Hotel', 'The adventures of Gustave H, a legendary concierge at a famous hotel from the fictional Republic of Zubrowka between the first and second World Wars, and Zero Moustafa, the lobby boy who becomes his most trusted friend.', 'Adventure, Comedy, Drama', 'Ralph Fiennes, F. Murray Abraham, Mathieu Amalric'),
(20, 'Blade Runner 2049', 'Young Blade Runner K''s discovery of a long-buried secret leads him to track down former Blade Runner Rick Deckard, who''s been missing for thirty years.', 'Action, Drama, Mystery', 'Ryan Gosling, Harrison Ford, Ana de Armas');
-- Insert sample data into the 'movie_reviews' table (30 rows)
-- Reviews are linked to movies via movie_id. Includes a mix of positive and negative reviews.
-- Movie title is prepended to the review text.
INSERT INTO movie_reviews (review_id, movie_id, reviewer_name, rating, review_text) VALUES
(1, 1, 'Alice Wonderland', 5, 'Inception: Absolutely mind-bending! A masterpiece of sci-fi.'),
(2, 1, 'Bob The Critic', 2, 'Inception: Too confusing and pretentious. Didn''t enjoy it.'),
(3, 2, 'Charlie Reviewer', 5, 'The Matrix: Revolutionary visuals and a compelling story.'),
(4, 3, 'Diana Prince', 5, 'Interstellar: Visually stunning and emotionally powerful. A must-see.'),
(5, 3, 'Edward Nigma', 4, 'Interstellar: Long, but worth it for the spectacle and ideas.'),
(6, 4, 'Fiona Glenanne', 5, 'The Dark Knight: Heath Ledger''s Joker is iconic. Dark and thrilling.'),
(7, 5, 'George Costanza', 5, 'Pulp Fiction: Quirky, violent, and endlessly quotable.'),
(8, 5, 'Hannah Montana', 1, 'Pulp Fiction: Way too violent and the timeline was confusing. Hated it.'),
(9, 6, 'Ian Malcolm', 4, 'Forrest Gump: A heartwarming story with a great performance by Hanks.'),
(10, 7, 'Jane Doe', 5, 'The Shawshank Redemption: An uplifting story of hope and friendship. Perfect.'),
(11, 7, 'John Smith', 5, 'The Shawshank Redemption: Morgan Freeman is amazing. Truly a classic.'),
(12, 8, 'Kyle Broflovski', 2, 'Gladiator: Generic plot and boring action scenes. Overrated.'),
(13, 9, 'Laura Palmer', 5, 'Fight Club: Provocative and thought-provoking. Norton and Pitt are fantastic.'),
(14, 10, 'Michael Scott', 5, 'The Lord of the Rings: The Return of the King: A fitting and epic conclusion to a legendary trilogy.'),
(15, 11, 'Nancy Drew', 5, 'Spirited Away: Beautiful animation and a magical story for all ages.'),
(16, 12, 'Oscar Martinez', 5, 'Parasite: A brilliant satire with unexpected twists. Loved it!'),
(17, 12, 'Pam Beesly', 4, 'Parasite: Very intense, but incredibly well-directed and acted.'),
(18, 13, 'Quentin Coldwater', 5, 'The Godfather: A cinematic masterpiece. Brando is unforgettable.'),
(19, 14, 'Rachel Green', 3, 'Avengers: Endgame: It was okay, but felt bloated and had too many characters.'),
(20, 14, 'Steve Rogers', 5, 'Avengers: Endgame: The culmination of a decade of storytelling. Perfect ending.'),
(21, 15, 'Tony Stark', 4, 'Joker: A dark and disturbing character study. Phoenix is mesmerizing.'),
(22, 16, 'Uma Thurman', 5, 'Mad Max: Fury Road: Non-stop action and incredible practical effects. What a ride!'),
(23, 17, 'Victor Frankenstein', 5, 'Coco: A heartwarming and visually stunning celebration of family and culture.'),
(24, 18, 'Walter White', 5, 'Whiplash: Intense and gripping. J.K. Simmons is terrifyingly good.'),
(25, 19, 'Xena Warrior', 2, 'The Grand Budapest Hotel: Too quirky for its own good. Style over substance.'),
(26, 20, 'Ygritte Snow', 5, 'Blade Runner 2049: A worthy sequel that expands on the original in meaningful ways. Visually breathtaking.'),
(27, 1, 'Zack Morris', 4, 'Inception: Kept me on the edge of my seat. Very clever.'),
(28, 4, 'Buffy Summers', 5, 'The Dark Knight: The best superhero movie ever made. Ledger is a legend.'),
(29, 8, 'Clark Kent', 3, 'Gladiator: Decent action, but the story felt predictable and dragged a bit.'),
(30, 15, 'Diana Troy', 3, 'Joker: Hard to watch at times, but a powerful performance. Felt it was a bit one-note though.');
If you have your own sample data and your CSV files compatible with the Cloud SQL import tool available from the Cloud console you can use it instead of the presented approach.
7. Use IF operator
Let's try first the classic search using standard PostgreSQL approaches.
If we try to search a movie about space adventures we can try the following query
SELECT title,description AS movies_about_space
FROM movies
WHERE description like '%space%' OR title like '%space%';
It didn't return any results. But I am sure we have at least one movie which would fall to that category. We can try to use a full text search approach.
SELECT title,description
FROM movies
WHERE to_tsvector('english', description) @@ to_tsquery('english', 'space');
And we might not get any results at all. So we need to know some key words or phrases to be able to utilize "classic" techniques of the PostgreSQL search.
Now we can try to use AI driven semantic filtering with google_ml.if function. It will be using AI behind the scenes to perform semantic filtering based on our natural language request.
SELECT title,description AS movies_about_space
FROM movies
WHERE
google_ml.if(
prompt => 'Here are descriptions of movies, can you return the ones about space adventures: '||description);
We are getting the "Interstellar" movie based on the semantic meaning of the request even if it doesn't have the term "space" neither in the title nor in the description. As you've noticed we also didn't build anything in advance and relied exclusively on automated built-in functions.
8. Use JOIN with IF operator
What if we want to join two tables using AI driven semantic filtering? For example we can try to match users reviews to the movies based on the user reviews if the movie was mentioned in the review.
Run in a new AlloyDB Studio editor tab:
SELECT title, rating, movie_reviews
FROM movies
JOIN
movie_reviews ON
google_ml.if(
prompt => 'Does the following reviews talk about a movie mentioned? The review: ' || review_text||' and the movie title is: '||title)
AND
title='Interstellar';
And we get two reviews matching our request based on the name of the movie mentioned in the title. And we can even more simplify the request:
SELECT title, rating, movie_reviews
FROM movies
JOIN
movie_reviews ON
google_ml.if(
prompt => 'Do we have the movie in the review?: ' || review_text||' and the movie title is: '||title)
AND
title='Interstellar';
9. Score Results Based on Contents
The table movie_reviews has ratings for the movies but if we want to implement our own rating we can use the google_ml.rank function for that. We can score our reviews based on conditions defined by a natural language and get for example top 5 reviews for the movies and show the original rating for comparison.
SELECT rating,review_text AS top_five
FROM movie_reviews
ORDER BY google_ml.rank('Score of 7 to 10 if the review says the movie was really good, 3 to 6 if the review says it''s alright is and 1 to 2 if the review says it was not worth of time. Review: ' || review_text) DESC
LIMIT 5;
And if we want to show the new rating next to the original rating we can add it to the list of columns.
SELECT rating,
google_ml.rank('Score of 7 to 10 if the review says the movie was really good, 3 to 6 if the review says it''s alright is and 1 to 2 if the review says it was not worth of time. Review: ' || review_text) AS ml_rank,
review_text AS top_five
FROM movie_reviews
ORDER BY ml_rank DESC
LIMIT 5;
And here are the top 5 reviews.
rating | ml_rank | top_five
--------+---------+-----------------------------------------------------------------------
5 | 9 | The Dark Knight: Heath Ledger's Joker is iconic. Dark and thrilling.
5 | 9 | The Matrix: Revolutionary visuals and a compelling story.
5 | 9 | Interstellar: Visually stunning and emotionally powerful. A must-see.
5 | 9 | Inception: Absolutely mind-bending! A masterpiece of sci-fi.
5 | 9 | Pulp Fiction: Quirky, violent, and endlessly quotable.
(5 rows)
5 rows in set (0.13 sec)
The chosen rating was 9 out of 10 for the top reviews.
10. Improve Semantic Search Using Ranking
We can combine our semantic search with ranking to get more precise results.
Register Reranking Model
Open another Cloud Shell tab using the sign "+" at the top.
In the new cloud shell tab execute:
export PROJECT_ID=$(gcloud config get-value project)
gcloud iam service-accounts create aip-reranking
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:aip-reranking@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/discoveryengine.viewer"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:aip-reranking@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"
gcloud iam service-accounts add-iam-policy-binding aip-reranking@$PROJECT_ID.iam.gserviceaccount.com \
--member="user:$(gcloud auth list --filter=status:ACTIVE --format="value(account)")" --role="roles/iam.serviceAccountTokenCreator"
gcloud iam service-accounts add-iam-policy-binding aip-reranking@$PROJECT_ID.iam.gserviceaccount.com \
--member="user:$(gcloud auth list --filter=status:ACTIVE --format="value(account)")" --role="roles/iam.serviceAccountUser"
Generate an access token and add it as a secret with name "rerank-secret":
echo -n $(gcloud auth print-access-token \
--impersonate-service-account="aip-reranking@$PROJECT_ID.iam.gserviceaccount.com" \
--lifetime=3600) | gcloud secrets create rerank-secret \
--replication-policy="automatic" \
--data-file=-
gcloud secrets add-iam-policy-binding 'rerank-secret' \
--member="serviceAccount:service-$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")@gcp-sa-alloydb.iam.gserviceaccount.com" \
--role="roles/secretmanager.secretAccessor"
In the example the secret will be valid for an hour and after that it should be updated by creating a new version. For example:
echo -n $(gcloud auth print-access-token \
--impersonate-service-account="aip-reranking@$PROJECT_ID.iam.gserviceaccount.com" \
--lifetime=3600) | gcloud secrets versions add rerank-secret \
--data-file=-
Now we need to switch to AlloyDB Studio and create a secret to access our reranking model.
In the AlloyDB Studio console execute after changing $PROJECT_ID by your project:
CALL
google_ml.create_sm_secret(
secret_id => 'rerank-secret',
secret_path => 'projects/PROJECT_ID/secrets/rerank-secret/versions/latest');
And register the model in the same Studio session after changing PROJECT_ID by your project:
CALL
google_ml.create_model(
model_id => 'semantic-ranker-512-002',
model_type => 'reranking',
model_provider => 'custom',
model_request_url =>
'https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/rankingConfigs/default_ranking_config:rank',
model_qualified_name => 'semantic-ranker-512@002',
model_auth_type => 'secret_manager',
model_auth_id => 'rerank-secret',
model_in_transform_fn => 'google_ml.vertexai_reranking_input_transform',
model_out_transform_fn => 'google_ml.vertexai_reranking_output_transform');
Use Reranking Model
Now we can use our rerunking model in the queries improving semantic search results making it more specific and choosing the best options.
Let's find action movies and then rank them using "computers and future" as conditions.
WITH
action_movies AS (
SELECT
title,
description,
ROW_NUMBER() OVER (ORDER BY title, description) AS ref_number
FROM
movies
WHERE
google_ml.if(
prompt => 'The following movies are action movies. The movie title: ' || title || ' and the description is: ' || description
)
),
ranked_documents_array AS (
SELECT
ARRAY_AGG(description ORDER BY ref_number) AS docs
FROM
action_movies
),
reranked_results AS (
SELECT
r.index,
r.score
FROM
ranked_documents_array,
ai.rank(
model_id => 'semantic-ranker-512-002',
search_string => 'Computers and future',
documents => ranked_documents_array.docs
) AS r
)
SELECT
am.title,
left(am.description,80) as description,
rr.score
FROM
action_movies am
JOIN
reranked_results rr ON am.ref_number = rr.index
ORDER BY
rr.score DESC;
The results will list action movies hopefully putting movies about future and computers to the top.
title | description | score
-----------------------------------------------+----------------------------------------------------------------------------------+--------
The Matrix | A computer hacker learns about the true nature of his reality. | 0.0145
Mad Max: Fury Road | In a post-apocalyptic wasteland, a woman rebels against a tyrannical ruler in se | 0.002
Avengers: Endgame | After the devastating events of Avengers: Infinity War, the universe is in ruins | 0.0018
Blade Runner 2049 | Young Blade Runner K's discovery of a long-buried secret leads him to track down | 0.0004
The Lord of the Rings: The Return of the King | Gandalf and Aragorn lead the World of Men against Sauron's army to draw his gaze | 1e-05
The Dark Knight | When the menace known as the Joker wreaks havoc and chaos on the people of Gotha | 1e-05
Inception | A thief who steals information by entering people's dreams. | 1e-05
Gladiator | A former Roman General sets out to exact vengeance against the corrupt emperor w | 1e-05
(8 rows)
Try it with different conditions and see how the ranking impacts the output order.
11. Clean up environment
Destroy the AlloyDB instances and cluster when you are done with the lab
Delete AlloyDB cluster and all instances
The cluster is destroyed with option force which also deletes all the instances belonging to the cluster.
In the cloud shell define the project and environment variables if you've been disconnected and all the previous settings are lost:
gcloud config set project <your project id>
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
export PROJECT_ID=$(gcloud config get-value project)
Delete the cluster:
gcloud alloydb clusters delete $ADBCLUSTER --region=$REGION --force
Expected console output:
student@cloudshell:~ (test-project-001-402417)$ gcloud alloydb clusters delete $ADBCLUSTER --region=$REGION --force All of the cluster data will be lost when the cluster is deleted. Do you want to continue (Y/n)? Y Operation ID: operation-1697820178429-6082890a0b570-4a72f7e4-4c5df36f Deleting cluster...done.
Delete AlloyDB Backups
Delete all AlloyDB backups for the cluster:
for i in $(gcloud alloydb backups list --filter="CLUSTER_NAME: projects/$PROJECT_ID/locations/$REGION/clusters/$ADBCLUSTER" --format="value(name)" --sort-by=~createTime) ; do gcloud alloydb backups delete $(basename $i) --region $REGION --quiet; done
Expected console output:
student@cloudshell:~ (test-project-001-402417)$ for i in $(gcloud alloydb backups list --filter="CLUSTER_NAME: projects/$PROJECT_ID/locations/$REGION/clusters/$ADBCLUSTER" --format="value(name)" --sort-by=~createTime) ; do gcloud alloydb backups delete $(basename $i) --region $REGION --quiet; done Operation ID: operation-1697826266108-60829fb7b5258-7f99dc0b-99f3c35f Deleting backup...done.
12. Congratulations
Congratulations for completing the codelab.
What we've covered
- How to deploy AlloyDB for Postgres
- How to enable AlloyDB AI operators
- How to use different AlloyDB AI operators
- How to use AlloyDB AI operators in a RAG workflow
13. Survey
Output: