
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 a database and enable pgVector extension
/ 20
Populate database with the sample dataset
/ 20
Create the service account retrieval-identity
/ 20
Deploy the Retrieval Service
/ 20
Create a Client Id
/ 20
One of the best tools for improving the quality of responses from large language models (LLMs) is retrieval augmented generation (RAG). RAG is the pattern of retrieving some non-public data and using that data to augment your prompt sent to the LLM. RAG allows the LLM to generate more accurate responses based on the data included in the prompt.
You'll use AlloyDB, Google Cloud's scalable and performant PostgreSQL-compatible database, to store and search by a special kind of vector data called vector embeddings. Vector embeddings can be retrieved using a semantic search, which allows retrieval of the available data that is the best match for a user's natural language query. The retrieved data is then passed to the LLM in the prompt.
You'll also use Vertex AI, Google Cloud's fully-managed, unified AI development platform for building and using generative AI. Your application uses Gemini Pro, a multimodal foundation model that supports adding image, audio, video, and PDF files in text or chat prompts and supports long-context understanding.
In this lab, you'll learn:
This Qwiklabs hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that 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 pop-up opens for you to select your payment method. On the left is a panel populated with the temporary credentials that you must use for this lab.
Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Choose an account page.
On the Choose an account page, click Use Another Account. The Sign in page opens.
Paste the username that you copied from the Connection Details panel. Then copy and paste the password.
After a few moments, the Cloud console opens in this tab.
Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
In this task, you install a PostgreSQL client and connect it to the AlloyDB instance.
A virtual machine (VM) has been created. This VM hosts the application. You also create the PostgreSQL client on this VM.
To connect to the VM, run the following command:
If asked to authorize, click Authorize.
For each question asked by the gcloud compute ssh
command, click Enter or Return to specify the default input.
After a short wait, you are logged into the VM.
To install the PostgreSQL client, in the VM session, run the following commands:
An AlloyDB instance has already been created.
To create required shell variables, run the following command:
To connect to the AlloyDB instance using psql, run the following command:
psql connects to the AlloyDB database and displays a postgres=>
prompt. You are now connected to the database.
To exit the psql session, run the following command:
In this task, you use the PostgreSQL client to create the AlloyDB database and enable vector embeddings.
To create a new database, in the VM session, run the following command:
psql responds with CREATE DATABASE
.
To enable the database to support semantic searches, the entities should be represented by vector embeddings.
To enable vector embeddings in this database, run the following command:
psql responds with CREATE EXTENSION
.
Click Check my progress to verify the objective.
In this task, you install Python in the VM. Python is used to populate the database.
To install Python and Git, in the VM, run the following commands:
When the installation is complete, you are left in the virtual Python environment, with a (.venv)
prompt.
If the VM SSH session ever times out or the tab is closed, you can SSH into the VM again and use the command source ~/.venv/bin/activate
to restart the virtual Python environment.
To confirm the python version, run the following command:
Your response should look similar to this:
In this task, you populate the vector database in AlloyDB with sample data. This data is used for the sample chat application.
The example application and data are stored in a GitHub repository named genai-databases-retrieval-app.
To clone the repo, in the VM, run the following command:
To see the data model, run the following command:
The Python data models are shown here. The model includes airports, flights, amenities within the terminals, policies, and tickets.
To see an example of the airport data, run the following commands:
These commands show the CSV header that specifies the column names for the airport dataset followed by the row for the San Francisco International airport (SFO). The data in the airport model can be retrieved based on the International Air Transport Association (IATA) code, or by country, city, and airport name. You can use keyword search to find rows in this table, so there are no vector embeddings for this data.
To see an example of the flight data, run the following commands:
These commands show the CSV header that specifies the column names for the flights dataset followed by the first 10 rows of flights to or from SFO. The data in the flights model can be retrieved based on the airline and flight number, or by the departure and arrival airport codes.
To see an example of the amenities data, run the following command:
This command shows the CSV header that specifies the column names for the amenities dataset followed by the first amenity.
You'll notice that the first amenity has several simple values, including name, description, location, terminal, category, and business hours. The next value is content
, which incorporates the name, description, and location. The last value is embedding
, the vector embedding for the row.
The embedding is an array of 768 numbers which is used when performing a semantic search. These embeddings are calculated using an AI model provided by Vertex AI. When a user provides a query, a vector embedding can be created from the query, and data with vector embeddings that are close to the search's embedding can be retrieved.
The policy data also uses vector embeddings in a similar fashion.
To create a database configuration file, run the following commands:
The config file config.yml
is created with the instance IP address, username, password, and database updated. Your configuration file should now resemble this:
To populate the database with the sample dataset, run the following commands:
The first command adds all required packages to the Python virtual environment and the second command populates the database with the data.
In this task, you create a service account for the retrieval service.
The retrieval service is responsible for extracting relevant information from the database. It extracts the necessary information from the database based on the request from an AI application. This service account is used as the identity of that Cloud Run service.
The SSH user does not have permission for the project instance to provide the service account with the correct role. You create the service account using a new Cloud Shell tab.
In Cloud Shell, to open a new Cloud Shell tab, click Open a new tab (+).
To create a service account and grant it the necessary privileges, in the new tab, run the following commands:
This service account is granted the role roles/aiplatform.user
, which allows the service to call Vertex AI.
To close the new tab, run the following command:
In this task, you deploy the retrieval service to Cloud Run.
To deploy the retrieval service, in the VM SSH Cloud Shell tab, run the following commands:
Wait a few minutes until the deployment completes.
To verify the service, run the following command:
If you see the "Hello World" message, the service is up and serving requests.
In this task, you register the OAuth consent screen that is presented to users who are logging in.
When you use OAuth 2.0 for authorization, Google displays a consent screen to capture the user's consent to share data with the application.
In the Google Cloud console, select the Navigation menu (), and then select APIs & Services > OAuth consent screen.
Click Get Started.
For App name, enter Cymbal Air
.
Click User support email, then click the student email, and then click Next.
For Audience, select Internal, and then click Next.
Users with access to the project should be able to log in to the app.
On the left panel of the lab instructions, copy the Username.
For Contact information, paste the copied username.
Click Next.
Click Checkbox to agree the User Data Policy, then click Continue, and then click Create.
The consent screen is now set up.
In this task, you create a client ID for the application.
The application requires a client ID to use Google's OAuth service. You configure the allowed origins that can make this request, and a redirect URI where the web app is redirected after the user has consented to log in.
In the Google Cloud console, select the Navigation menu (), and then select APIs & Services > Credentials.
Click + Create Credentials, and then click OAuth client ID.
A client ID is used to identify a single app to Google's OAuth servers.
For Application type, select Web application.
For Name, enter Cymbal Air
.
You can generate the JavaScript origin and redirect URI using Cloud Shell.
In Cloud Shell, to open a new Cloud Shell tab, click Open a new tab (+).
To get the origin and redirect URI, in the new tab, run the following commands:
For Authorized JavaScript origins, click + Add URI.
Copy the origin URI that was created by the echo command, and then, for URIs 1, paste in the URI.
For Authorized redirect URIs, click + Add URI.
Copy the redirect URI that was created by the echo command, and then, for URIs 1, paste in the URI.
Click Create.
The client ID and client secret are created. For this test application, you only use the client ID.
To create the environment variable, in the VM SSH Cloud Shell tab, paste the following command without clicking Enter:
Click Copy client ID ().
The client ID is copied to the clipboard.
In the VM SSH Cloud Shell tab, paste the client ID, and then click Enter.
The export should look similar to this:
In this task, you run a sample chat application that uses the retrieval service.
To install the Python requirements for the chat application, in the VM SSH Cloud Shell tab, run the following commands:
Before starting the application, you need to set up some environment variables. The basic functionality of the application, including querying flights and returning airport amenities, requires an environment variable named BASE_URL
to contain the base URL of the retrieval service.
To specify the base URL of the retrieval service, run the following commands:
The base URL is used by the local application to access the retrieval service.
To run the application, run the following command:
Your response should look similar to this:
The application is now running.
You have several ways to connect to the application running on the VM. For example, you can open port 8081 on the VM using firewall rules in the VPC, or create a load balancer with a public IP. Here you use a SSH tunnel to the VM, translating the Cloud Shell port 8080 to the VM port 8081.
In Cloud Shell, to open a new Cloud Shell tab, click Open a new tab (+).
To create an SSH tunnel to the VM port, in the new tab, run the following command:
The gcloud command connects port 8080 in Cloud Shell with port 8081 on the VM. You can ignore the error "Cannot assign requested address."
To run the application in the web browser, click Web Preview, and then select Preview on port 8080.
A new tab is opened in the browser, and the application is running. The Cymbal Air application prompts "Welcome to Cymbal Air! How may I assist you?"
Enter the following query:
The application responds with the next flight from SFO to Dallas/Fort Worth.
Enter the following query:
The application understands the context, and responds with restaurants near the departure gate in SFO.
In this task, you log into the application to book the flight.
Click Sign in.
A pop-up window opens.
In the pop-up window, select the student.
The student account is logged in.
If you are asked to confirm that you want to sign in as the student, click Confirm.
Enter the following query:
The application presents the flight that was being discussed.
Click Looks good to me. Book it.
The flight is booked.
Enter the following query:
The flight you just booked is shown.
The chat app can help answer user questions like:
The application uses the latest Google foundation models to generate responses and augment them with information about flights and amenities from the operational AlloyDB database. You can read more about this demo application on the GitHub page of the project.
You've successfully built a chat application that leverages large language models (LLMs) and retrieval augmented generation (RAG) to create engaging and informative conversations.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2024 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