LangChain Google Community

3.0.5 · active · verified Thu Apr 09

langchain-google-community is an integration package that connects LangChain with various miscellaneous Google products and services, such as BigQuery, Document AI, Firestore, and Google Cloud Storage. As of version 3.0.5, it provides components like chat message histories, document loaders, and vector stores for these services. The library follows a frequent release cadence, often aligning with updates in the broader LangChain ecosystem.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates using `BigQueryChatMessageHistory` to store and retrieve chat messages in Google BigQuery. It requires Google Cloud project, dataset, and table names, and assumes appropriate GCP authentication and enabled BigQuery API.

import os
from langchain_google_community.chat_message_histories import BigQueryChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage

# Ensure you have GOOGLE_CLOUD_PROJECT, BIGQUERY_DATASET_NAME, BIGQUERY_TABLE_NAME set as environment variables
# or pass them directly to the constructor.
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-gcp-project-id')
dataset_name = os.environ.get('BIGQUERY_DATASET_NAME', 'langchain_chat_history')
table_name = os.environ.get('BIGQUERY_TABLE_NAME', 'chat_sessions')
session_id = 'test_session_123'

if not all([project_id, dataset_name, table_name]) or project_id == 'your-gcp-project-id':
    print("Warning: Skipping BigQuery history quickstart. Please set GOOGLE_CLOUD_PROJECT, BIGQUERY_DATASET_NAME, and BIGQUERY_TABLE_NAME environment variables.")
    print("Alternatively, replace 'your-gcp-project-id' with a valid project ID.")
else:
    try:
        # Initialize BigQuery Chat Message History
        history = BigQueryChatMessageHistory(
            project_id=project_id,
            dataset_name=dataset_name,
            table_name=table_name,
            session_id=session_id
        )

        # Add messages
        history.add_user_message("Hi there!")
        history.add_ai_message("Hello! How can I help you today?")
        history.add_message(HumanMessage(content="Tell me a joke."))
        history.add_message(AIMessage(content="Why don't scientists trust atoms? Because they make up everything!"))

        # Retrieve messages
        messages = history.messages
        print(f"Retrieved {len(messages)} messages for session '{session_id}':")
        for msg in messages:
            print(f"  {type(msg).__name__}: {msg.content}")

        # Clear messages (optional, for cleanup)
        # history.clear()
        # print("History cleared.")
    except Exception as e:
        print(f"Error running BigQuery history quickstart: {e}")
        print("Ensure you have authenticated with Google Cloud (e.g., `gcloud auth application-default login`) and BigQuery API is enabled in project: {project_id}.")

view raw JSON →