Listfrom langchain.schema import BaseMessage, HumanMessagefrom langchain.chat_loaders import base as chat_loaderslogger = logging.getLogger()class DiscordChatLoader(chat_loaders.BaseChatLoader):        def __init__(self, path: str):        """        Initialize the Discord chat loader.        Args:            path: Path to the exported Discord chat text file.        """        self.path = path        self._message_line_regex = re.compile(            r"(.+?) ‚Äî (\w{3,9} \d{1,2}(?:st|nd|rd|th)?(?:, \d{4})? \d{1,2}:\d{2} (?:AM|PM)|Today at \d{1,2}:\d{2} (?:AM|PM)|Yesterday at \d{1,2}:\d{2} (?:AM|PM))",  # noqa            flags=re.DOTALL,        )    def _load_single_chat_session_from_txt(        self, file_path: str    ) -> chat_loaders.ChatSession:        """        Load a single chat session from a text file.        Args:            file_path: Path to the text file containing the chat messages.        Returns:            A `ChatSession` object containing the loaded chat messages.        """        with open(file_path, "r", encoding="utf-8") as file:            lines = file.readlines()        results: List[BaseMessage] = []        current_sender = None        current_timestamp = None        current_content = []        for line in lines:            if re.match(                r".+? ‚Äî (\d{2}/\d{2}/\d{4} \d{1,2}:\d{2} (?:AM|PM)|Today at \d{1,2}:\d{2} (?:AM|PM)|Yesterday at \d{1,2}:\d{2} (?:AM|PM))",  # noqa                line,            ):                if current_sender and current_content:                    results.append(                        HumanMessage(                            content="".join(current_content).strip(),                            additional_kwargs={                                "sender": current_sender,                                "events": [{"message_time": current_timestamp}],                            },                        )                    )                current_sender, current_timestamp = line.split(" ‚Äî ")[:2]
This notebook shows how to create your own chat loader that works on copy-pasted messages (from dms) to a list of LangChain messages.
This notebook shows how to create your own chat loader that works on copy-pasted messages (from dms) to a list of LangChain messages. ->: Listfrom langchain.schema import BaseMessage, HumanMessagefrom langchain.chat_loaders import base as chat_loaderslogger = logging.getLogger()class DiscordChatLoader(chat_loaders.BaseChatLoader):        def __init__(self, path: str):        """        Initialize the Discord chat loader.        Args:            path: Path to the exported Discord chat text file.        """        self.path = path        self._message_line_regex = re.compile(            r"(.+?) ‚Äî (\w{3,9} \d{1,2}(?:st|nd|rd|th)?(?:, \d{4})? \d{1,2}:\d{2} (?:AM|PM)|Today at \d{1,2}:\d{2} (?:AM|PM)|Yesterday at \d{1,2}:\d{2} (?:AM|PM))",  # noqa            flags=re.DOTALL,        )    def _load_single_chat_session_from_txt(        self, file_path: str    ) -> chat_loaders.ChatSession:        """        Load a single chat session from a text file.        Args:            file_path: Path to the text file containing the chat messages.        Returns:            A `ChatSession` object containing the loaded chat messages.        """        with open(file_path, "r", encoding="utf-8") as file:            lines = file.readlines()        results: List[BaseMessage] = []        current_sender = None        current_timestamp = None        current_content = []        for line in lines:            if re.match(                r".+? ‚Äî (\d{2}/\d{2}/\d{4} \d{1,2}:\d{2} (?:AM|PM)|Today at \d{1,2}:\d{2} (?:AM|PM)|Yesterday at \d{1,2}:\d{2} (?:AM|PM))",  # noqa                line,            ):                if current_sender and current_content:                    results.append(                        HumanMessage(                            content="".join(current_content).strip(),                            additional_kwargs={                                "sender": current_sender,                                "events": [{"message_time": current_timestamp}],                            },                        )                    )                current_sender, current_timestamp = line.split(" ‚Äî ")[:2]