
    Ng                     4   d dl Z d dlmZ d dlmZmZmZmZmZ d dl	m
Z
 defdZdefdZdefdZ	 ddeeef         dededeeeef                  fdZ	 ddeeef         dededeeef         fdZdedefdZdeeef         defdZ G d d          ZdS )    N)Path)AnyDictIterableTupleUnionguard_importreturnc                       t          d          S )zJImport the spacy python package and raise an error if it is not installed.spacyr	        _/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/callbacks/utils.pyimport_spacyr      s       r   c                       t          d          S )zKImport the pandas python package and raise an error if it is not installed.pandasr	   r   r   r   import_pandasr      s    !!!r   c                       t          d          S )zMImport the textstat python package and raise an error if it is not installed.textstatr	   r   r   r   import_textstatr      s    
###r    _nested_dict
parent_keysepc              #      K   |                                  D ]D\  }}|r||z   |z   n|}t          |t                    rt          |||          E d{V  >||fV  EdS )a  
    Generator that yields flattened items from a nested dictionary for a flat dict.

    Parameters:
        nested_dict (dict): The nested dictionary to flatten.
        parent_key (str): The prefix to prepend to the keys of the flattened dict.
        sep (str): The separator to use between the parent key and the key of the
            flattened dictionary.

    Yields:
        (str, any): A key-value pair from the flattened dictionary.
    N)items
isinstancedict_flatten_dict)r   r   r   keyvaluenew_keys         r   r!   r!      s       "'')) ! !
U,6?*s"S((CeT"" 	!$UGS99999999995.    ! !r   c                 <    d t          | ||          D             }|S )a  Flatten a nested dictionary into a flat dictionary.

    Parameters:
        nested_dict (dict): The nested dictionary to flatten.
        parent_key (str): The prefix to prepend to the keys of the flattened dict.
        sep (str): The separator to use between the parent key and the key of the
            flattened dictionary.

    Returns:
        (dict): A flat dictionary.

    c                     i | ]\  }}||	S r   r   ).0kvs      r   
<dictcomp>z flatten_dict.<locals>.<dictcomp>=   s    NNN$!QANNNr   )r!   )r   r   r   	flat_dicts       r   flatten_dictr,   .   s*     ON-Z"M"MNNNIr   sc                 t    t          j        |                     d                                                    S )zHash a string using sha1.

    Parameters:
        s (str): The string to hash.

    Returns:
        (str): The hashed string.
    zutf-8)hashlibsha1encode	hexdigest)r-   s    r   hash_stringr3   A   s,     <))**44666r   	json_pathc                 ~    t          | d          5 }|                                }ddd           n# 1 swxY w Y   |S )zLoad json file to a string.

    Parameters:
        json_path (str): The path to the json file.

    Returns:
        (str): The string representation of the json file.
    rN)openread)r4   fdatas      r   	load_jsonr;   M   sx     
i		 vvxx              Ks   266c                       e Zd ZdZddZedefd            Zedefd            Zedefd            Z	edefd            Z
deeef         fd	Zdd
ZdS )BaseMetadataCallbackHandlera'  Handle the metadata and associated function states for callbacks.

    Attributes:
        step (int): The current step.
        starts (int): The number of times the start method has been called.
        ends (int): The number of times the end method has been called.
        errors (int): The number of times the error method has been called.
        text_ctr (int): The number of times the text method has been called.
        ignore_llm_ (bool): Whether to ignore llm callbacks.
        ignore_chain_ (bool): Whether to ignore chain callbacks.
        ignore_agent_ (bool): Whether to ignore agent callbacks.
        ignore_retriever_ (bool): Whether to ignore retriever callbacks.
        always_verbose_ (bool): Whether to always be verbose.
        chain_starts (int): The number of times the chain start method has been called.
        chain_ends (int): The number of times the chain end method has been called.
        llm_starts (int): The number of times the llm start method has been called.
        llm_ends (int): The number of times the llm end method has been called.
        llm_streams (int): The number of times the text method has been called.
        tool_starts (int): The number of times the tool start method has been called.
        tool_ends (int): The number of times the tool end method has been called.
        agent_ends (int): The number of times the agent end method has been called.
        on_llm_start_records (list): A list of records of the on_llm_start method.
        on_llm_token_records (list): A list of records of the on_llm_token method.
        on_llm_end_records (list): A list of records of the on_llm_end method.
        on_chain_start_records (list): A list of records of the on_chain_start method.
        on_chain_end_records (list): A list of records of the on_chain_end method.
        on_tool_start_records (list): A list of records of the on_tool_start method.
        on_tool_end_records (list): A list of records of the on_tool_end method.
        on_agent_finish_records (list): A list of records of the on_agent_end method.
    r   Nc                    d| _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        d| _        d| _        d| _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        d S )Nr   F)stepstartsendserrorstext_ctrignore_llm_ignore_chain_ignore_agent_ignore_retriever_always_verbose_chain_starts
chain_ends
llm_startsllm_endsllm_streamstool_starts	tool_ends
agent_endson_llm_start_recordson_llm_token_recordson_llm_end_recordson_chain_start_recordson_chain_end_recordson_tool_start_recordson_tool_end_recordson_text_recordson_agent_finish_recordson_agent_action_recordsselfs    r   __init__z$BaseMetadataCallbackHandler.__init__{   s    		 ""!&$*,!*,!(*,.#*,!+-")+ %'-/$-/$$$r   c                     | j         S )z;Whether to call verbose callbacks even if verbose is False.)rH   r[   s    r   always_verbosez*BaseMetadataCallbackHandler.always_verbose   s     ##r   c                     | j         S )z Whether to ignore LLM callbacks.)rD   r[   s    r   
ignore_llmz&BaseMetadataCallbackHandler.ignore_llm   s     r   c                     | j         S )z"Whether to ignore chain callbacks.)rE   r[   s    r   ignore_chainz(BaseMetadataCallbackHandler.ignore_chain        !!r   c                     | j         S )z"Whether to ignore agent callbacks.)rF   r[   s    r   ignore_agentz(BaseMetadataCallbackHandler.ignore_agent   rd   r   c                     | j         | j        | j        | j        | j        | j        | j        | j        | j        | j	        | j
        | j        | j        dS )Nr?   r@   rA   rB   rC   rI   rJ   rK   rL   rM   rN   rO   rP   rh   r[   s    r   get_custom_callback_metaz4BaseMetadataCallbackHandler.get_custom_callback_meta   sU    IkIk -//++/
 
 	
r   c                    d| _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        d| _        d| _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        dS )zReset the callback metadata.r   FN)r?   r@   rA   rB   rC   rD   rE   rF   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   s    r   reset_callback_metaz/BaseMetadataCallbackHandler.reset_callback_meta   s    		 ""$$&!$&!"$&(#$&!%'"#% !')$')$tr   )r   N)__name__
__module____qualname____doc__r]   propertyboolr_   ra   rc   rf   r   strr   ri   rk   r   r   r   r=   r=   [   s        >&0 &0 &0 &0P $ $ $ $ X$  D       X  "d " " " X" "d " " " X"
$sCx. 
 
 
 
"' ' ' ' ' 'r   r=   )r   r   )r/   pathlibr   typingr   r   r   r   r   langchain_core.utilsr
   r   r   r   rr   r!   r,   r3   r;   r=   r   r   r   <module>rv      s          4 4 4 4 4 4 4 4 4 4 4 4 4 4 - - - - - -!c ! ! ! !
"s " " " "
$ $ $ $ $ CF! !c3h!-0!<?!eCHo! ! ! !0 CF c3h-0<?	#s(^   &	73 	73 	7 	7 	7 	7sDy) c    T T T T T T T T T Tr   