a
    bg                     @   sZ   d dl Z d dlZd dlmZ d dlmZ d dlmZ ee	e	d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   z/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/chatgpt.pyconcatenate_rows
   s    
r   c                   @   s4   e Zd ZdZd
eedddZee d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.
        Nr   )selfr   r   r   r   r   __init__"   s    zChatGPTLoader.__init__)r   c                    s   t | jdd4}| jr*t|d | j nt|}W d    n1 sH0    Y  g }|D ]R}|d |d  d fddt D }dt| ji}|t	||d	 qZ|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    B
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   