a
    bgO                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZ eeZG dd deZdS )	    N)Path)DictIteratorListUnion)BaseChatLoader)ChatSession)	AIMessageHumanMessagec                   @   sl   e Zd ZdZeeef dddZee	e
 edddZeejee	e dd	d
Zee dddZdS )SlackChatLoaderz0Load `Slack` conversations from a dump zip file.)pathc                 C   s8   t |tr|nt|| _| j s4td| j ddS )z
        Initialize the chat loader with the path to the exported Slack dump zip file.

        :param path: Path to the exported Slack dump zip file.
        zFile z
 not foundN)
isinstancer   zip_pathexistsFileNotFoundError)selfr    r   t/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/chat_loaders/slack.py__init__   s    	
zSlackChatLoader.__init__)messagesreturnc              	   C   s   g }d }| D ]}t |tsq|dd}|dd}|dd}|sFqtjdtjd}||rbq||kr|d  jd| 7  _|d jd	 	d
|i n |	t
|||d
|igdd |}qt|dS )Ntext tsuserz<@U\d+> has joined the channel)flagsz

eventsZmessage_time)senderr   )Zrolecontentadditional_kwargs)r   )r   dictgetrecompile
IGNORECASEmatchr   r    appendr
   r   )r   resultsZprevious_sendermessager   	timestampr   Zskip_patternr   r   r   _load_single_chat_session   s>    


z)SlackChatLoader._load_single_chat_session)zip_file	file_pathr   c                 C   sV   |  |d}t|}W d   n1 s,0    Y  t|tsRtdt| |S )z"Read JSON data from a zip subfile.rNz#Expected list of dictionaries, got )openjsonloadr   list
ValueErrortype)r,   r-   fdatar   r   r   
_read_jsonC   s
    (
zSlackChatLoader._read_json)r   c                 c   sf   t t| jd@}| D ]&}|dr| ||}| |V  qW d   n1 sX0    Y  dS )z
        Lazy load the chat sessions from the Slack dump file and yield them
        in the required format.

        :return: Iterator of chat sessions containing messages.
        r.   z.jsonN)zipfileZipFilestrr   namelistendswithr7   r+   )r   r,   r-   r   r   r   r   	lazy_loadL   s
    
zSlackChatLoader.lazy_loadN)__name__
__module____qualname____doc__r   r:   r   r   staticmethodr   r   r   r+   r8   r9   r!   r7   r   r=   r   r   r   r   r      s   
#r   )r0   loggingr#   r8   pathlibr   typingr   r   r   r   Zlangchain_core.chat_loadersr   Zlangchain_core.chat_sessionsr   Zlangchain_core.messagesr	   r
   	getLoggerr>   loggerr   r   r   r   r   <module>   s   
