-
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
). -
Install the AlloyDB Auth Proxy.
In this step, we will enable Private Services Access so that AlloyDB is able to connect to your VPC. You should only need to do this once per VPC (per project).
-
Set environment variables:
export RANGE_NAME=my-allocated-range-default export DESCRIPTION="peering range for alloydb-service"
-
Create an allocated IP address range:
gcloud compute addresses create $RANGE_NAME \ --global \ --purpose=VPC_PEERING \ --prefix-length=16 \ --description="$DESCRIPTION" \ --network=default
-
Create a private connection:
gcloud services vpc-peerings connect \ --service=servicenetworking.googleapis.com \ --ranges="$RANGE_NAME" \ --network=default
-
Set environment variables. For security reasons, use a different password for
$DB_PASS
and note it for future use:export CLUSTER=my-alloydb-cluster export INSTANCE=my-alloydb-instance export REGION=us-central1 export DB_USER=postgres export DB_PASS=my-alloydb-pass
-
Create an AlloyDB cluster:
gcloud alloydb clusters create $CLUSTER \ --password=$DB_PASS\ --network=default \ --region=$REGION \ --project=$PROJECT_ID
-
Create a primary instance:
gcloud alloydb instances create $INSTANCE \ --instance-type=PRIMARY \ --cpu-count=8 \ --region=$REGION \ --cluster=$CLUSTER \ --project=$PROJECT_ID \ --ssl-mode=ALLOW_UNENCRYPTED_AND_ENCRYPTED \ --database-flags=password.enforce_complexity=on
-
Enable public IP on instance:
gcloud alloydb instances update $INSTANCE \ --cluster=$CLUSTER \ --region=$REGION \ --assign-inbound-public-ip=ASSIGN_IPV4
-
Connect to instance using AlloyDB auth proxy:
./alloydb-auth-proxy --public-ip \ "projects/$PROJECT_ID/locations/$REGION/clusters/$CLUSTER/instances/$INSTANCE"
-
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 -p 5432 -U $DB_USER
-
Change into the retrieval service directory:
cd ./retrieval_service
-
Install requirements:
pip install -r requirements.txt
-
Make a copy of
example-config.yml
and name itconfig.yml
.cp example-config-alloydb.yml config.yml
-
Update
config.yml
with your database information.
host: 0.0.0.0
datastore:
# Example for alloydb.py provider
kind: "alloydb-postgres"
# Update this with your project ID
project: <PROJECT_ID>
region: us-central1
cluster: my-alloydb-cluster
instance: my-alloydb-instance
# 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-alloydb-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;
-
Populate data into database:
python run_database_init.py
Clean up after completing the demo.
-
Set environment variables:
export CLUSTER=my-alloydb-cluster export REGION=us-central1
-
Delete AlloyDB cluster that contains instances:
gcloud alloydb clusters delete $CLUSTER \ --force \ --region=$REGION \ --project=$PROJECT_ID
This section is for developers that want to develop and run the app locally.
Follow the steps here to create two secrets with the following Secret names:
alloy_db_user
alloy_db_pass
Set the Secret values as your database username and password respectively.
export DB_NAME=""
export DB_USER=""
export DB_PASS=""
export DB_PROJECT=""
export DB_REGION=""
export DB_CLUSTER=""
export DB_INSTANCE=""
Run retrieval service unit tests:
gcloud builds submit --config retrieval_service/alloydb.tests.cloudbuild.yaml \
--substitutions _DATABASE_NAME=$DB_NAME,_DATABASE_USER=$DB_USER,_ALLOYDB_REGION=$DB_REGION,_ALLOYDB_CLUSTER=$DB_CLUSTER,_ALLOYDB_INSTANCE=$DB_INSTANCE
Where $DB_NAME
,$DB_USER
,$DB_REGION
,$DB_CLUSTER
,$DB_INSTANCE
are environment variables with your database values.
If you get the following error:
failed to access secret version for secret projects/<PROJECT_NUMBER>/secrets/alloy_db_user/versions/1: rpc error: code = PermissionDenied desc = Permission 'secretmanager.versions.access' denied for resource 'projects/<PROJECT_NUMBER>/secrets/alloy_db_user/versions/1' (or it may not exist).
Go to Cloud Build > Settings
, and make sure that the GCP Service Secret Manager
is enabled for your Service Account.
You can find the Service Account under History > <BUILD VERSION> > Execution Details > Service Account
.
Eg. <PROJECT_NUMBER>[email protected]