检查点
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
Build an LLM and RAG-based Chat Application using AlloyDB and LangChain
- GSP1226
- Overview
- Setup and Requirements
- Task 1. Initialize the Environment
- Task 2. Initialize the database
- Task 3. Install Python
- Task 4. Populate Database
- Task 5. Deploy the Retrieval Service to Cloud Run
- Task 6. Deploy the Retrieval Service
- Task 7. Deploy sample application
- (Optional) Task 100 Sign into the application
- Congratulations
GSP1226
Overview
One of the best tools for reducing GenAI hallucinations is to use Retrieval Augmented Generation (RAG). RAG is the concept of retrieving some data or information, then augmenting your prompt used with your Large Language Model (LLM), which allows it to generate more accurate responses based on the data included in the prompt.
You'll also leverage the capabilities of AlloyDB AI, Google Cloud's database for AI-powered applications, and LangChain, a framework for developing applications, to connect the LLM to external data sources. By the end of this lab, you'll have a functional chat application that can intelligently answer questions by retrieving relevant information from your database.
What you'll learn
- How LLMs process language and how RAG enhances their capabilities by retrieving relevant information from a knowledge base.
- Set up and interact with AlloyDB, a scalable and performant PostgreSQL database designed for demanding workloads.
- Explore LangChain's tools and components for building LLM-powered applications, including document loaders, prompt templates, and chains.
- Connect AlloyDB as a vector store with LangChain, allowing the chat application to access and retrieve relevant information for generating responses.
- Develop a user-friendly interface for interacting with the chat application, allowing users to ask questions and receive informative answers.
Setup and Requirements
Before you click the Start Lab button
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 will be made available to you.
This 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:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
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 the Lab Details panel with the following:
- The Open Google Cloud console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
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.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}} You can also find the Username in the Lab Details panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}} You can also find the Password in the Lab Details panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Activate Cloud Shell
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. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
- Click Authorize.
Output:
- (Optional) You can list the project ID with this command:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Configure Cloud Shell
Inside Cloud Shell, make sure that your project ID is setup:
Usually the project ID is shown in parentheses in the command prompt in the cloud shell as it is shown in the picture:
Configure your default region to us-central1
to use the Vertex AI models. Read more about regional restrictions.
Task 1. Initialize the Environment
Install Postgres client
Install the PostgreSQL client software on the deployed VM
- Connect to the VM:
Expected console output:
- Install the software running command inside the VM:
Expected console output:
Connect to the Instance
Connect to the primary instance from the VM using psql.
Continue with the opened SSH session to your VM. If you have been disconnected then connect again using the same command as above.
- Use the previously noted $PGASSWORD and the cluster name to connect to AlloyDB from the Compute Engine VM:
Expected console output:
- Exit from the psql session keeping the SSH connection up:
Expected console output:
Task 2. Initialize the database
You are going to use your client VM as a platform to populate the database with data and host your application. The first step is to create a database and populate it with data.
Create database
Create a database named assistantdemo
.
- In the VM session execute:
Expected console ouput:
- Enable pgVector extension:
Expected console output:
Click Check my progress to verify the objective.
Task 3. Install Python
next, you are going to use prepared Python scripts from GitHub repository, but before doing that, you need to install the required software.
- In the VM execute:
Expected console output:
- In the VM execute:
Expected console output:
Task 4. Populate Database
Clone the GitHub repository with the code for the retrieval service and sample application.
- In the VM execute:
Expected console output:
- Prepare configuration file. In the VM execute:
export PGPASSWORD=
REGION=us-central1
INSTANCE_IP=$(gcloud alloydb instances describe $ADBCLUSTER-pr --cluster=$ADBCLUSTER --region=$REGION --format="value(ipAddress)")
Expected console output:
- Now populate database with the sample dataset. In the VM execute:
The first command is adding all required packages to the Python virtual environment and the second command is populating the database with the data.
Expected console output(redacted):
Task 5. Deploy the Retrieval Service to Cloud Run
Now you can deploy the retrieval service to Cloud Run. The service is responsible for working with the database. It extracts the necessary information from the database based on the request from an AI application.
Create Service Account
Create a service account for the extension service and grant necessary privileges.
- Open another Cloud Shell tab using the "+" at the top.
- In the new Cloud Shell tab execute:
Expected console output:
- Close the tab by either execution command "exit" in the tab:
Task 6. Deploy the Retrieval Service
Continue in the first tab where you are connected to the VM through SSH by deploying the service.
- In the VM SSH session execute:
Expected console output:
Verify The Service
Now we can check if the service runs correctly and the VM has access to the endpoint. We use gcloud utility to get the retrieval service endpoint. Alternatively you can check it in the cloud console and replace in the curl command the "$(gcloud run services list –filter="(retrieval-service)" by the value from there.
- In the VM SSH session execute:
Expected console output:
If you see the "Hello World" message it means your service is up and serving the requests.
Task 7. Deploy sample application
When you have the retrieval service up and running, you can deploy a sample application which is going to use the service. The application can be deployed on the VM or any other service like Cloud Run, Kubernetes or even locally on a laptop.
Next you are going to deploy a sample application on the VM.
Prepare the environment
First, add the necessary modules to Python.
- In the VM SSH session execute:
Expected output (redacted):
Run Assistant application
Before starting the application you need to set up some environment variables. The basic functionality of the application, such as query flights and airport amenities, requires BASE_URL
which points application to the retrieval service. We can get it using the gcloud command .
- In the VM SSH session execute:
Expected output (redacted):
Prepare Client ID
To use more advanced capabilities of the application like booking and changing flights we need to sign-in to the application using our Google account and for that purpose we need to provide CLIENT_ID environment variable using the OAuth client ID from the Prepare Client ID chapter:
To use booking functionality of the application we need to prepare OAuth 2.0 Client ID using Cloud Console. It will be when we sign into the application since booking is using clients credentials to record the booking data in the database.
- In the Cloud Console go to the APIs and Services and click on "OAuth consent screen" and choose "Internal" user.
- Then push "Create" and follow on the next screen.
- You need to fill out required fields such as "App name" and "User support email". Also you can add a domain you want to show on the consent screen and finally the "Developer contact information".
- Then you push the "Save And Continue" button at the bottom of the page and it will lead you to the next page.
You don't need to change anything there unless you want to specify the scopes. Finally you confirm it by pushing the button "Save And Continue" again. That will set up the application consent screen.
- The next step is to create the client ID. On the left panel you click "Credentials" which lead you to the credentials for OAuth2.
- Here you click "Create Credentials" at the top and choose OAuth ClientID. Then it will open another screen.
-
Pick up "Web application" from the dropdown list for application type and put your application URI (and port - optionally) as the "Authorized JavaScript origins". And you need to add to the "Authorized redirect URIs" your application host with "/login/google" at the end to be able to use the authorization popup screen.
-
After pushing the "Create" button you get a popup window with your clients credentials.
We will need the Client ID (and optionally Client secret) later to use with your application.
Expected output (redacted):
Now you can run the application:
Exptected ouptut:
Connect to the Application
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 public IP. Here we are going to use a SSH tunnel to the VM translating the local port 8080 to the VM port 8081.
- Open another Cloud Shell tab using the sign "+" at the top.
- In the new cloud shell tab start the tunnel to your VM by executing the
gcloud
command:
It will show an error "Cannot assign requested address" - please ignore it.
Here is the expected output:
It opens port 8080 in your Cloud Shell which can be used for the Web preview.
- Click on the Web preview button on the right top of your Cloud Shell then select Preview on port 8080.
This opens a new tab in your web browser with the application interface. You should see the Cymbal Air Customer Service Assistant page. In the address bar of the page you'll see the URI to the preview page.
- Remove the part
/?authuser=0&redirectedPreviously=true
at the end.
Use the first part of URI like "https://8080-cs-35704030349-default.cs-us-east1-vpcf.cloudshell.dev/" is to be left in the browser window and to provide as the "Authorized JavaScript origins" and "Authorized redirect URIs" for our credentials created in the "Prepare Client ID" chapter replacing or adding to the originally provided http://localhost:8080 values. The upper value would look like "https://8080-cs-35704030349-default.cs-us-east1-vpcf.cloudshell.dev" and the lower one would be "https://8080-cs-35704030349-default.cs-us-east1-vpcf.cloudshell.dev/login/google"
(Optional) Task 100 Sign into the application
- Click the__Sign in__ button at the top right of the application screen to provide our credentials. That is optional and required only if you want to try booking functionality of the application.
This demo showcases the Cymbal Air customer service assistant. Cymbal Air is a fictional passenger airline. The assistant is an AI chatbot that helps travellers to manage flights and look up information about Cymbal Air's hub at San Francisco International Airport (SFO).
It can help answer users questions like:
- When is the next flight to Denver?
- Are there any luxury shops around gate D50?
- Where can I get coffee near gate A6?
- Where can I buy a gift?
When you are signed in to the application you can try other capabilities like booking flights or check if the seat assigned to you is a window or aisle seat.
The application uses the latest Google foundation models to generate responses and augment it by 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.
Congratulations
You've successfully built a cutting-edge chat application that leverages Large Language Models (LLMs) and Retrieval Augmented Generation (RAG) for engaging and informative conversations. By combining the power of AlloyDB's vector database with LangChain's flexible framework, you've created an intelligent agent capable of accessing and utilizing vast amounts of information to generate contextually relevant responses.
Next steps/Learn more
- Read more information about the GenAI Retrieval Service and the sample application.
Google Cloud training and certification
...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 June 10, 2024
Lab Last Tested June 10, 2024
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.