Source code for langchain.chat_loaders.facebook_messenger
import json
import logging
from pathlib import Path
from typing import Iterator, Union
from langchain.chat_loaders.base import BaseChatLoader
from langchain.schema.chat import ChatSession
from langchain.schema.messages import HumanMessage
logger = logging.getLogger(__file__)
[docs]class SingleFileFacebookMessengerChatLoader(BaseChatLoader):
    """Load `Facebook Messenger` chat data from a single file.
    Args:
        path (Union[Path, str]): The path to the chat file.
    Attributes:
        path (Path): The path to the chat file.
    """
[docs]    def __init__(self, path: Union[Path, str]) -> None:
        super().__init__()
        self.file_path = path if isinstance(path, Path) else Path(path)
[docs]    def lazy_load(self) -> Iterator[ChatSession]:
        """Lazy loads the chat data from the file.
        Yields:
            ChatSession: A chat session containing the loaded messages.
        """
        with open(self.file_path) as f:
            data = json.load(f)
        sorted_data = sorted(data["messages"], key=lambda x: x["timestamp_ms"])
        messages = []
        for m in sorted_data:
            messages.append(
                HumanMessage(
                    content=m["content"], additional_kwargs={"sender": m["sender_name"]}
                )
            )
        yield ChatSession(messages=messages)
[docs]class FolderFacebookMessengerChatLoader(BaseChatLoader):
    """Load `Facebook Messenger` chat data from a folder.
    Args:
        path (Union[str, Path]): The path to the directory
            containing the chat files.
    Attributes:
        path (Path): The path to the directory containing the chat files.