Bigtable is a key-value and wide-column store, ideal for fast access to structured, semi-structured, or unstructured data. Extend your database application to build AI-powered experiences leveraging Bigtable’s LangChain integrations.This notebook goes over how to use Bigtable to save, load and delete langchain documents with
BigtableLoader
and BigtableSaver
.
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 Bigtable API
- Create a Bigtable instance
- Create a Bigtable table
- Create Bigtable access credentials
🦜🔗 Library Installation
The integration lives in its ownlangchain-google-bigtable
package, so we need to install it.
☁ 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.
🔐 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.
Basic Usage
Using the saver
Save langchain documents withBigtableSaver.add_documents(<documents>)
. To initialize BigtableSaver
class you need to provide 2 things:
instance_id
- An instance of Bigtable.table_id
- The name of the table within the Bigtable to store langchain documents.
Querying for Documents from Bigtable
For more details on connecting to a Bigtable table, please check the Python SDK documentation.Load documents from table
Load langchain documents withBigtableLoader.load()
or BigtableLoader.lazy_load()
. lazy_load
returns a generator that only queries database during the iteration. To initialize BigtableLoader
class you need to provide:
instance_id
- An instance of Bigtable.table_id
- The name of the table within the Bigtable to store langchain documents.
Delete documents
Delete a list of langchain documents from Bigtable table withBigtableSaver.delete(<documents>)
.
Advanced Usage
Limiting the returned rows
There are two ways to limit the returned rows:Custom client
The client created by default is the default client, using only admin=True option. To use a non-default, a custom client can be passed to the constructor.Custom content
The BigtableLoader assumes there is a column family calledlangchain
, that has a column called content
, that contains values encoded in UTF-8. These defaults can be changed like so:
Metadata mapping
By default, themetadata
map on the Document
object will contain a single key, rowkey
, with the value of the row’s rowkey value. To add more items to that map, use metadata_mapping.
Metadata as JSON
If there is a column in Bigtable that contains a JSON string that you would like to have added to the output document metadata, it is possible to add the following parameters to BigtableLoader. Note, the default value formetadata_as_json_encoding
is UTF-8.