Skip to main content
Couchbase is an award-winning distributed NoSQL cloud database that delivers unmatched versatility, performance, scalability, and financial value for all of your cloud, mobile, AI, and edge computing applications. Couchbase embraces AI with coding assistance for developers and vector search for their applications.
This notebook goes over how to use the CouchbaseChatMessageHistory class to store the chat message history in a Couchbase cluster

Set Up Couchbase Cluster

To run this demo, you need a Couchbase Cluster. You can work with both Couchbase Capella and your self-managed Couchbase Server.

Install Dependencies

CouchbaseChatMessageHistory lives inside the langchain-couchbase package.

Create Couchbase Connection Object

We create a connection to the Couchbase cluster initially and then pass the cluster object to the Vector Store. Here, we are connecting using the username and password. You can also connect using any other supported way to your cluster. For more information on connecting to the Couchbase cluster, please check the Python SDK documentation.
We will now set the bucket, scope, and collection names in the Couchbase cluster that we want to use for storing the message history. Note that the bucket, scope, and collection need to exist before using them to store the message history.

Usage

In order to store the messages, you need the following:
  • Couchbase Cluster object: Valid connection to the Couchbase cluster
  • bucket_name: Bucket in cluster to store the chat message history
  • scope_name: Scope in bucket to store the message history
  • collection_name: Collection in scope to store the message history
  • session_id: Unique identifier for the session
Optionally you can configure the following:
  • session_id_key: Field in the chat message documents to store the session_id
  • message_key: Field in the chat message documents to store the message content
  • create_index: Used to specify if the index needs to be created on the collection. By default, an index is created on the message_key and the session_id_key of the documents
  • ttl: Used to specify a time in timedelta to live for the documents after which they will get deleted automatically from the storage.

Specifying a Time to Live (TTL) for the Chat Messages

The stored messages can be deleted after a specified time automatically by specifying a ttl parameter along with the initialization of the chat message history store.

Chaining

The chat message history class can be used with LCEL Runnables