o
    Zh                     @   s^   d dl Z d dlZd dlmZ d dlmZ d dlmZ dede	de	fdd	Z
G d
d deZdS )    N)List)Document)
BaseLoadermessagetitlereturnc                 C   sb   | sdS | d r| d d nd}| d d d }t j | d d	}| d
| d| d| dS )z
    Combine message information in a readable format ready to be used.
    Args:
        message: Message to be concatenated
        title: Title of the conversation

    Returns:
        Concatenated message
     authorroleunknowncontentpartsr   Zcreate_timez%Y-%m-%d %H:%M:%Sz - z on z: z

)datetimefromtimestampstrftime)r   r   Zsendertextdate r   c/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/chatgpt.pyconcatenate_rows
   s   
r   c                   @   s6   e Zd ZdZddedefddZdee fdd	Z	d
S )ChatGPTLoaderz0Load conversations from exported `ChatGPT` data.log_filenum_logsc                 C   s   || _ || _dS )zInitialize a class object.

        Args:
            log_file: Path to the log file
            num_logs: Number of logs to load. If 0, load all logs.
        N)r   r   )selfr   r   r   r   r   __init__"   s   
zChatGPTLoader.__init__r   c                    s   t | jdd}| jrt|d | j nt|}W d    n1 s$w   Y  g }|D ])}|d |d  d fddt D }dt| ji}|t	||d	 q-|S )
Nutf8)encodingr   mappingr   c                    sB   g | ]\}}|d kr | d d d dkst  | d qS )r   r   r	   r
   system)r   ).0idxkeymessagesr   r   r   
<listcomp>5   s    z&ChatGPTLoader.load.<locals>.<listcomp>source)Zpage_contentmetadata)
openr   r   jsonloadjoin	enumeratestrappendr   )r   fdataZ	documentsdr   r'   r   r#   r   r*   ,   s   &
zChatGPTLoader.loadN)r   )
__name__
__module____qualname____doc__r-   intr   r   r   r*   r   r   r   r   r      s    
r   )r   r)   typingr   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   dictr-   r   r   r   r   r   r   <module>   s    