From 2d2b097fab8eb6f3626c86cb958d8cc762257061 Mon Sep 17 00:00:00 2001 From: Bagatur <22008038+baskaryan@users.noreply.github.com> Date: Tue, 29 Aug 2023 21:41:32 -0700 Subject: [PATCH] mv chat history (#9725) --- libs/langchain/langchain/schema/__init__.py | 5 +- .../langchain/schema/chat_history.py | 67 +++++++++++++++++++ libs/langchain/langchain/schema/memory.py | 62 ----------------- 3 files changed, 70 insertions(+), 64 deletions(-) create mode 100644 libs/langchain/langchain/schema/chat_history.py diff --git a/libs/langchain/langchain/schema/__init__.py b/libs/langchain/langchain/schema/__init__.py index 15557f5f3c..aec8a1e391 100644 --- a/libs/langchain/langchain/schema/__init__.py +++ b/libs/langchain/langchain/schema/__init__.py @@ -1,8 +1,9 @@ """**Schemas** are the LangChain Base Classes and Interfaces.""" from langchain.schema.agent import AgentAction, AgentFinish +from langchain.schema.chat_history import BaseChatMessageHistory from langchain.schema.document import BaseDocumentTransformer, Document from langchain.schema.exceptions import LangChainException -from langchain.schema.memory import BaseChatMessageHistory, BaseMemory +from langchain.schema.memory import BaseMemory from langchain.schema.messages import ( AIMessage, BaseMessage, @@ -40,10 +41,10 @@ Memory = BaseMemory __all__ = [ "BaseMemory", "BaseStore", - "BaseChatMessageHistory", "AgentFinish", "AgentAction", "Document", + "BaseChatMessageHistory", "BaseDocumentTransformer", "BaseMessage", "ChatMessage", diff --git a/libs/langchain/langchain/schema/chat_history.py b/libs/langchain/langchain/schema/chat_history.py new file mode 100644 index 0000000000..1f74ed0cd2 --- /dev/null +++ b/libs/langchain/langchain/schema/chat_history.py @@ -0,0 +1,67 @@ +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import List + +from langchain.schema.messages import AIMessage, BaseMessage, HumanMessage + + +class BaseChatMessageHistory(ABC): + """Abstract base class for storing chat message history. + + See `ChatMessageHistory` for default implementation. + + Example: + .. code-block:: python + + class FileChatMessageHistory(BaseChatMessageHistory): + storage_path: str + session_id: str + + @property + def messages(self): + with open(os.path.join(storage_path, session_id), 'r:utf-8') as f: + messages = json.loads(f.read()) + return messages_from_dict(messages) + + def add_message(self, message: BaseMessage) -> None: + messages = self.messages.append(_message_to_dict(message)) + with open(os.path.join(storage_path, session_id), 'w') as f: + json.dump(f, messages) + + def clear(self): + with open(os.path.join(storage_path, session_id), 'w') as f: + f.write("[]") + """ + + messages: List[BaseMessage] + """A list of Messages stored in-memory.""" + + def add_user_message(self, message: str) -> None: + """Convenience method for adding a human message string to the store. + + Args: + message: The string contents of a human message. + """ + self.add_message(HumanMessage(content=message)) + + def add_ai_message(self, message: str) -> None: + """Convenience method for adding an AI message string to the store. + + Args: + message: The string contents of an AI message. + """ + self.add_message(AIMessage(content=message)) + + @abstractmethod + def add_message(self, message: BaseMessage) -> None: + """Add a Message object to the store. + + Args: + message: A BaseMessage object to store. + """ + raise NotImplementedError() + + @abstractmethod + def clear(self) -> None: + """Remove all messages from the store""" diff --git a/libs/langchain/langchain/schema/memory.py b/libs/langchain/langchain/schema/memory.py index fe1b784440..cd4d572f98 100644 --- a/libs/langchain/langchain/schema/memory.py +++ b/libs/langchain/langchain/schema/memory.py @@ -4,7 +4,6 @@ from abc import ABC, abstractmethod from typing import Any, Dict, List from langchain.load.serializable import Serializable -from langchain.schema.messages import AIMessage, BaseMessage, HumanMessage class BaseMemory(Serializable, ABC): @@ -58,64 +57,3 @@ class BaseMemory(Serializable, ABC): @abstractmethod def clear(self) -> None: """Clear memory contents.""" - - -class BaseChatMessageHistory(ABC): - """Abstract base class for storing chat message history. - - See `ChatMessageHistory` for default implementation. - - Example: - .. code-block:: python - - class FileChatMessageHistory(BaseChatMessageHistory): - storage_path: str - session_id: str - - @property - def messages(self): - with open(os.path.join(storage_path, session_id), 'r:utf-8') as f: - messages = json.loads(f.read()) - return messages_from_dict(messages) - - def add_message(self, message: BaseMessage) -> None: - messages = self.messages.append(_message_to_dict(message)) - with open(os.path.join(storage_path, session_id), 'w') as f: - json.dump(f, messages) - - def clear(self): - with open(os.path.join(storage_path, session_id), 'w') as f: - f.write("[]") - """ - - messages: List[BaseMessage] - """A list of Messages stored in-memory.""" - - def add_user_message(self, message: str) -> None: - """Convenience method for adding a human message string to the store. - - Args: - message: The string contents of a human message. - """ - self.add_message(HumanMessage(content=message)) - - def add_ai_message(self, message: str) -> None: - """Convenience method for adding an AI message string to the store. - - Args: - message: The string contents of an AI message. - """ - self.add_message(AIMessage(content=message)) - - @abstractmethod - def add_message(self, message: BaseMessage) -> None: - """Add a Message object to the store. - - Args: - message: A BaseMessage object to store. - """ - raise NotImplementedError() - - @abstractmethod - def clear(self) -> None: - """Remove all messages from the store"""