mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +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
92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
import json
|
|
import logging
|
|
from typing import List
|
|
|
|
from langchain_core.chat_history import BaseChatMessageHistory
|
|
from langchain_core.messages import (
|
|
BaseMessage,
|
|
message_to_dict,
|
|
messages_from_dict,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
DEFAULT_DBNAME = "chat_history"
|
|
DEFAULT_COLLECTION_NAME = "message_store"
|
|
|
|
|
|
class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
|
"""Chat message history that stores history in MongoDB.
|
|
|
|
Args:
|
|
connection_string: connection string to connect to MongoDB
|
|
session_id: arbitrary key that is used to store the messages
|
|
of a single chat session.
|
|
database_name: name of the database to use
|
|
collection_name: name of the collection to use
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
connection_string: str,
|
|
session_id: str,
|
|
database_name: str = DEFAULT_DBNAME,
|
|
collection_name: str = DEFAULT_COLLECTION_NAME,
|
|
):
|
|
from pymongo import MongoClient, errors
|
|
|
|
self.connection_string = connection_string
|
|
self.session_id = session_id
|
|
self.database_name = database_name
|
|
self.collection_name = collection_name
|
|
|
|
try:
|
|
self.client: MongoClient = MongoClient(connection_string)
|
|
except errors.ConnectionFailure as error:
|
|
logger.error(error)
|
|
|
|
self.db = self.client[database_name]
|
|
self.collection = self.db[collection_name]
|
|
self.collection.create_index("SessionId")
|
|
|
|
@property
|
|
def messages(self) -> List[BaseMessage]: # type: ignore
|
|
"""Retrieve the messages from MongoDB"""
|
|
from pymongo import errors
|
|
|
|
try:
|
|
cursor = self.collection.find({"SessionId": self.session_id})
|
|
except errors.OperationFailure as error:
|
|
logger.error(error)
|
|
|
|
if cursor:
|
|
items = [json.loads(document["History"]) for document in cursor]
|
|
else:
|
|
items = []
|
|
|
|
messages = messages_from_dict(items)
|
|
return messages
|
|
|
|
def add_message(self, message: BaseMessage) -> None:
|
|
"""Append the message to the record in MongoDB"""
|
|
from pymongo import errors
|
|
|
|
try:
|
|
self.collection.insert_one(
|
|
{
|
|
"SessionId": self.session_id,
|
|
"History": json.dumps(message_to_dict(message)),
|
|
}
|
|
)
|
|
except errors.WriteError as err:
|
|
logger.error(err)
|
|
|
|
def clear(self) -> None:
|
|
"""Clear session memory from MongoDB"""
|
|
from pymongo import errors
|
|
|
|
try:
|
|
self.collection.delete_many({"SessionId": self.session_id})
|
|
except errors.WriteError as err:
|
|
logger.error(err)
|