Google Cloud AlloyDB for PostgreSQL is a fully managedThis notebook goes over how to usePostgreSQLcompatible database service for your most demanding enterprise workloads.AlloyDBcombines the best ofGoogle CloudwithPostgreSQL, for superior performance, scale, and availability. Extend your database application to build AI-powered experiences leveragingAlloyDBLangChain integrations.
Google Cloud AlloyDB for PostgreSQL to store chat message history with the AlloyDBChatMessageHistory class.
Learn more about the package on GitHub.
Before You Begin
To run this notebook, you will need to do the following:- Create a Google Cloud Project
- Enable the AlloyDB API
- Create a AlloyDB instance
- Create a AlloyDB database
- Add an IAM database user to the database (Optional)
π¦π Library Installation
The integration lives in its ownlangchain-google-alloydb-pg package, so we need to install it.
π Authentication
Authenticate to Google Cloud as the IAM user logged into this notebook in order to access your Google Cloud Project.- If you are using Colab to run this notebook, use the cell below and continue.
- If you are using Vertex AI Workbench, check out the setup instructions here.
β Set Your Google Cloud Project
Set your Google Cloud project so that you can leverage Google Cloud resources within this notebook. If you donβt know your project ID, try the following:- Run
gcloud config list. - Run
gcloud projects list. - See the support page: Locate the project ID.
π‘ API Enablement
Thelangchain-google-alloydb-pg package requires that you enable the AlloyDB Admin API in your Google Cloud Project.
Basic Usage
Set AlloyDB database values
Find your database values, in the AlloyDB cluster page.AlloyDBEngine Connection Pool
One of the requirements and arguments to establish AlloyDB as a ChatMessageHistory memory store is aAlloyDBEngine object. The AlloyDBEngine configures a connection pool to your AlloyDB database, enabling successful connections from your application and following industry best practices.
To create a AlloyDBEngine using AlloyDBEngine.from_instance() you need to provide only 5 things:
project_id: Project ID of the Google Cloud Project where the AlloyDB instance is located.region: Region where the AlloyDB instance is located.cluster: The name of the AlloyDB cluster.instance: The name of the AlloyDB instance.database: The name of the database to connect to on the AlloyDB instance.
user and password arguments to AlloyDBEngine.from_instance():
user: Database user to use for built-in database authentication and loginpassword: Database password to use for built-in database authentication and login.
Initialize a table
TheAlloyDBChatMessageHistory class requires a database table with a specific schema in order to store the chat message history.
The AlloyDBEngine engine has a helper method init_chat_history_table() that can be used to create a table with the proper schema for you.
AlloyDBChatMessageHistory
To initialize theAlloyDBChatMessageHistory class you need to provide only 3 things:
engine- An instance of aAlloyDBEngineengine.session_id- A unique identifier string that specifies an id for the session.table_name: The name of the table within the AlloyDB database to store the chat message history.