-
Make sure you have a Google Cloud project and billing is enabled.
-
Set your
PROJECT_ID
environment variable:export PROJECT_ID=<YOUR_PROJECT_ID>
-
Install the gcloud CLI.
-
Set gcloud project:
gcloud config set project $PROJECT_ID
-
Enable APIs:
gcloud services enable alloydb.googleapis.com \ compute.googleapis.com \ cloudresourcemanager.googleapis.com \ servicenetworking.googleapis.com \ vpcaccess.googleapis.com \ aiplatform.googleapis.com
-
Install python and set up a python virtual environment.
-
Make sure you have python version 3.11+ installed.
python -V
-
Download and install postgres-client cli (
psql
).
This section assumes you've already set up a non-cloud database like AlloyDB Omni.
New to non-cloud Postgres? We recommend using AlloyDB for a fully managed and scalable experience on Google Cloud. Follow our guide on setting up AlloyDB Cluster on GCP to get started.
-
Get AlloyDB IP address:
export ALLOYDB_IP=$(gcloud alloydb instances describe $INSTANCE \ --cluster=$CLUSTER \ --region=$REGION \ --format 'value(ipAddress)')
-
Note the AlloyDB IP address for later use:
echo $ALLOYDB_IP
AlloyDB supports network connectivity through private, internal IP addresses only. For this section, we will create a Google Cloud Engine VM in the same VPC as the AlloyDB cluster. We can use this VM to connect to our AlloyDB cluster using Private IP.
-
Set environment variables:
export ZONE=us-central1-a export PROJECT_NUM=$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)") export VM_INSTANCE=postgres-proxy-vm
-
Create a Compute Engine VM:
gcloud compute instances create $VM_INSTANCE \ --project=$PROJECT_ID \ --zone=$ZONE \ --machine-type=e2-medium \ --network-interface=network-tier=PREMIUM,stack-type=IPV4_ONLY,subnet=default \ --maintenance-policy=MIGRATE \ --provisioning-model=STANDARD \ --service-account=$PROJECT_NUM[email protected] \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --create-disk=auto-delete=yes,boot=yes,device-name=$VM_INSTANCE,image-family=ubuntu-2004-lts,image-project=ubuntu-os-cloud,mode=rw,size=10,type=projects/$PROJECT_ID/zones/$ZONE/diskTypes/pd-balanced \ --no-shielded-secure-boot \ --shielded-vtpm \ --shielded-integrity-monitoring \ --labels=goog-ec-src=vm_add-gcloud \ --reservation-affinity=any
-
Create an SSH tunnel through your GCE VM using port forwarding. This will listen to
127.0.0.1:5432
and forward through the GCE VM to your AlloyDB instance:gcloud compute ssh --project=$PROJECT_ID --zone=$ZONE $VM_INSTANCE \ -- -NL 5432:$ALLOYDB_IP:5432
You will need to allow this command to run while you are connecting to AlloyDB. You may wish to open a new terminal to connect with.
-
Verify you can connect to your instance with the
psql
tool. Enter password for AlloyDB ($DB_PASS
environment variable set above) when prompted:psql -h 127.0.0.1 -U postgres
Update config.yml
with your database information. Keep using 127.0.0.1
as the datastore host IP address for port forwarding.
host: 0.0.0.0
datastore:
# Example for postgres.py provider
kind: "postgres"
host: 127.0.0.1
port: 5432
# Update this with the database name
database: "assistantdemo"
# Update with database user, the default is `postgres`
user: "postgres"
# Update with database user password
password: "my-postgres-pass"
-
While connected using
psql
, create a database and switch to it:CREATE DATABASE assistantdemo; \c assistantdemo
-
Install
pgvector
extension in the database:CREATE EXTENSION vector;
-
Exit from
psql
:exit
-
Change into the retrieval service directory:
cd genai-databases-retrieval-app/retrieval_service
-
Install requirements:
pip install -r requirements.txt
-
Make a copy of
example-config.yml
and name itconfig.yml
.cp example-config.yml config.yml
-
Populate data into database:
python run_database_init.py
Clean up after completing the demo.
-
Set environment variables:
export VM_INSTANCE=postgres-proxy-vm export CLUSTER=my-postgres-cluster export REGION=us-central1 export RANGE_NAME=my-allocated-range-default
-
Delete Compute Engine VM:
gcloud compute instances delete $VM_INSTANCE
-
Delete AlloyDB cluster that contains instances:
gcloud alloydb clusters delete $CLUSTER \ --force \ --region=$REGION \ --project=$PROJECT_ID
-
Delete an allocated IP address range:
gcloud compute addresses delete $RANGE_NAME \ --global
This section is for developers that want to develop and run the app locally.
Set environment variables:
export DB_USER=""
export DB_PASS=""
export DB_NAME=""
export DB_HOST=""
Run retrieval service unit tests:
gcloud builds submit --config retrieval_service/postgres.tests.cloudbuild.yaml \
--substitutions _DATABASE_HOST=$DB_HOST,_DATABASE_NAME=$DB_NAME,_DATABASE_USER=$DB_USER
Where $DB_HOST
,$DB_NAME
,$DB_USER
are environment variables with your database values.