ComponentsChat loadersDiscordOn this pageDiscordThis notebook shows how to create your own chat loader that works on copy-pasted messages (from dms) to a list of LangChain messages.The process has four steps:Create the chat .txt file by copying chats from the Discord app and pasting them in a file on your local computerCopy the chat loader definition from below to a local file.Initialize the DiscordChatLoader with the file path pointed to the text file.Call loader.load() (or loader.lazy_load()) to perform the conversion.1. Creat message dump​Currently (2023/08/23) this loader only supports .txt files in the format generated by copying messages in the app to your clipboard and pasting in a file. Below is an example.discord_chats.txttalkingtower — 08/15/2023 11:10 AMLove music! Do you like jazz?reporterbob — 08/15/2023 9:27 PMYes! Jazz is fantastic. Ever heard this one?WebsiteListen to classic jazz track...talkingtower — Yesterday at 5:03 AMIndeed! Great choice. 🎷reporterbob — Yesterday at 5:23 AMThanks! How about some virtual sightseeing?WebsiteVirtual tour of famous landmarks...talkingtower — Today at 2:38 PMSounds fun! Let's explore.reporterbob — Today at 2:56 PMEnjoy the tour! See you around.talkingtower — Today at 3:00 PMThank you! Goodbye! 👋reporterbob — Today at 3:02 PMFarewell! Happy exploring.2. Define chat loader​LangChain currently does not support import loggingimport refrom typing import Iterator, 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]                current_content = [                    line[len(current_sender) + len(current_timestamp) + 4 :].strip()                ]            elif re.match(r"\[\d{1,2}:\d{2} (?:AM|PM)\]", line.strip()):                results.append(                    HumanMessage(                        content="".join(current_content).strip(),                        additional_kwargs={                            "sender": current_sender,                            "events": [{"message_time": current_timestamp}],                        },                    )                )                current_timestamp = line.strip()[1:-1]                current_content = []            else:                current_content.append("\n" + line.strip())        if current_sender and current_content:            results.append(                HumanMessage(                    content="".join(current_content).strip(),                    additional_kwargs={                        "sender": current_sender,                        "events": [{"message_time": current_timestamp}],                    },                )            )        return chat_loaders.ChatSession(messages=results)    def lazy_load(self) -> Iterator[chat_loaders.ChatSession]:        """        Lazy load the messages from the chat file and yield them in the required format.        Yields:            A `ChatSession` object containing the loaded chat messages.        """        yield self._load_single_chat_session_from_txt(self.path)2. Create loader​We will point to the file we just wrote to disk.loader = DiscordChatLoader(    path="./discord_chats.txt",)3. Load Messages​Assuming the format is correct, the loader will convert the chats to langchain messages.from typing import Listfrom langchain.chat_loaders.base import ChatSessionfrom langchain.chat_loaders.utils import (    map_ai_messages,    merge_chat_runs,)raw_messages = loader.lazy_load()# Merge consecutive messages from the same sender into a single messagemerged_messages = merge_chat_runs(raw_messages)# Convert messages from "talkingtower" to AI messagesmessages: List[ChatSession] = list(map_ai_messages(merged_messages, sender="talkingtower"))messages    [{'messages': [AIMessage(content='Love music! Do you like jazz?', additional_kwargs={'sender': 'talkingtower', 'events': [{'message_time': '08/15/2023 11:10 AM\n'}]}, example=False),       HumanMessage(content='Yes! Jazz is fantastic. Ever heard this one?\nWebsite\nListen to classic jazz track...', additional_kwargs={'sender': 'reporterbob', 'events': [{'message_time': '08/15/2023 9:27 PM\n'}]}, example=False),       AIMessage(content='Indeed! Great choice. 🎷', additional_kwargs={'sender': 'talkingtower', 'events': [{'message_time': 'Yesterday at 5:03 AM\n'}]}, example=False),       HumanMessage(content='Thanks! How about some virtual sightseeing?\nWebsite\nVirtual tour of famous landmarks...', additional_kwargs={'sender': 'reporterbob', 'events': [{'message_time': 'Yesterday at 5:23 AM\n'}]}, example=False),       AIMessage(content="Sounds fun! Let's explore.", additional_kwargs={'sender': 'talkingtower', 'events': [{'message_time': 'Today at 2:38 PM\n'}]}, example=False),       HumanMessage(content='Enjoy the tour! See you around.', additional_kwargs={'sender': 'reporterbob', 'events': [{'message_time': 'Today at 2:56 PM\n'}]}, example=False),       AIMessage(content='Thank you! Goodbye! 👋', additional_kwargs={'sender': 'talkingtower', 'events': [{'message_time': 'Today at 3:00 PM\n'}]}, example=False),       HumanMessage(content='Farewell! Happy exploring.', additional_kwargs={'sender': 'reporterbob', 'events': [{'message_time': 'Today at 3:02 PM\n'}]}, example=False)]}]Next Steps​You can then use these messages how you see fit, such as finetuning a model, few-shot example selection, or directly make predictions for the next message  from langchain.chat_models import ChatOpenAIllm = ChatOpenAI()for chunk in llm.stream(messages[0]['messages']):    print(chunk.content, end="", flush=True)    Thank you! Have a wonderful day! 🌟PreviousChat loadersNextFacebook Messenger1. Creat message dump2. Define chat loader2. Create loader3. Load MessagesNext Steps