Skip to main content

Google Cloud Firestore

Cloud Firestore is a NoSQL document database built for automatic scaling, high performance, and ease of application development.

This notebook goes over how to use Firestore to store chat message history.

Setting up​

To run this notebook, you will need a Google Cloud Project, a Firestore database instance in Native Mode, and Google credentials, see Firestore Quickstarts.

!pip install firebase-admin

Basic Usage​

from langchain_community.chat_message_histories.firestore import (
FirestoreChatMessageHistory,
)

message_history = FirestoreChatMessageHistory(
collection_name="langchain-chat-history",
session_id="user-session-id",
user_id="user-id",
)

message_history.add_user_message("hi!")
message_history.add_ai_message("whats up?")
message_history.messages
[HumanMessage(content='hi!'),
HumanMessage(content='hi!'),
AIMessage(content='whats up?')]

Custom Firestore Client​

import firebase_admin
from firebase_admin import credentials, firestore

# Use a service account.
cred = credentials.Certificate("path/to/serviceAccount.json")

app = firebase_admin.initialize_app(cred)
client = firestore.client(app=app)

message_history = FirestoreChatMessageHistory(
collection_name="langchain-chat-history",
session_id="user-session-id",
user_id="user-id",
firestore_client=client,
)