mirror of
https://github.com/hwchase17/langchain
synced 2024-11-08 07:10:35 +00:00
ed58eeb9c5
Moved the following modules to new package langchain-community in a backwards compatible fashion: ``` mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community ``` Moved the following to core ``` mv langchain/langchain/utils/json_schema.py core/langchain_core/utils mv langchain/langchain/utils/html.py core/langchain_core/utils mv langchain/langchain/utils/strings.py core/langchain_core/utils cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py rm langchain/langchain/utils/env.py ``` See .scripts/community_split/script_integrations.sh for all changes
73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
"""Cassandra-based chat message history, based on cassIO."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import typing
|
|
from typing import List
|
|
|
|
if typing.TYPE_CHECKING:
|
|
from cassandra.cluster import Session
|
|
|
|
from langchain_core.chat_history import BaseChatMessageHistory
|
|
from langchain_core.messages import (
|
|
BaseMessage,
|
|
message_to_dict,
|
|
messages_from_dict,
|
|
)
|
|
|
|
DEFAULT_TABLE_NAME = "message_store"
|
|
DEFAULT_TTL_SECONDS = None
|
|
|
|
|
|
class CassandraChatMessageHistory(BaseChatMessageHistory):
|
|
"""Chat message history that stores history in Cassandra.
|
|
|
|
Args:
|
|
session_id: arbitrary key that is used to store the messages
|
|
of a single chat session.
|
|
session: a Cassandra `Session` object (an open DB connection)
|
|
keyspace: name of the keyspace to use.
|
|
table_name: name of the table to use.
|
|
ttl_seconds: time-to-live (seconds) for automatic expiration
|
|
of stored entries. None (default) for no expiration.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
session_id: str,
|
|
session: Session,
|
|
keyspace: str,
|
|
table_name: str = DEFAULT_TABLE_NAME,
|
|
ttl_seconds: typing.Optional[int] = DEFAULT_TTL_SECONDS,
|
|
) -> None:
|
|
try:
|
|
from cassio.history import StoredBlobHistory
|
|
except (ImportError, ModuleNotFoundError):
|
|
raise ImportError(
|
|
"Could not import cassio python package. "
|
|
"Please install it with `pip install cassio`."
|
|
)
|
|
self.session_id = session_id
|
|
self.ttl_seconds = ttl_seconds
|
|
self.blob_history = StoredBlobHistory(session, keyspace, table_name)
|
|
|
|
@property
|
|
def messages(self) -> List[BaseMessage]: # type: ignore
|
|
"""Retrieve all session messages from DB"""
|
|
message_blobs = self.blob_history.retrieve(
|
|
self.session_id,
|
|
)
|
|
items = [json.loads(message_blob) for message_blob in message_blobs]
|
|
messages = messages_from_dict(items)
|
|
return messages
|
|
|
|
def add_message(self, message: BaseMessage) -> None:
|
|
"""Write a message to the table"""
|
|
self.blob_history.store(
|
|
self.session_id, json.dumps(message_to_dict(message)), self.ttl_seconds
|
|
)
|
|
|
|
def clear(self) -> None:
|
|
"""Clear session memory from DB"""
|
|
self.blob_history.clear_session_id(self.session_id)
|