
    ih                        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y)    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	        a/var/www/html/dev/engine/venv/lib/python3.12/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   | j                         D ]=  \  }}|r||z   |z   n|}t        |t              rt        |||      E d{    8||f ? y7 w)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      sb      "'') !
U,6*s"S(CeT"$UGS9995. ! :s   AAAAc                 R    t        | ||      D ci c]  \  }}||
 }}}|S c c}}w )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.

    )r   )r   r   r   kv	flat_dicts         r   flatten_dictr'   .   s4     #0Z"MN$!QANIN Os   #sc                 f    t        j                  | j                  d            j                         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_stringr.   A   s%     <<)*4466r   	json_pathc                 j    t        | d      5 }|j                         }ddd       |S # 1 sw Y   S xY w)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)r/   fdatas      r   	load_jsonr6   M   s5     
i	 vvxKKs   (2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y)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 | _        y )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.)rC   rV   s    r   always_verbosez*BaseMetadataCallbackHandler.always_verbose   s     ###r   c                     | j                   S )z Whether to ignore LLM callbacks.)r?   rV   s    r   
ignore_llmz&BaseMetadataCallbackHandler.ignore_llm   s     r   c                     | j                   S )z"Whether to ignore chain callbacks.)r@   rV   s    r   ignore_chainz(BaseMetadataCallbackHandler.ignore_chain        !!!r   c                     | j                   S )z"Whether to ignore agent callbacks.)rA   rV   s    r   ignore_agentz(BaseMetadataCallbackHandler.ignore_agent   r_   r   c                 &   | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  dS )Nr:   r;   r<   r=   r>   rD   rE   rF   rG   rH   rI   rJ   rK   rc   rV   s    r   get_custom_callback_metaz4BaseMetadataCallbackHandler.get_custom_callback_meta   sm    IIkkIIkk --////++++//
 	
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 | _        y)zReset the callback metadata.r   FN)r:   r;   r<   r=   r>   r?   r@   rA   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   s    r   reset_callback_metaz/BaseMetadataCallbackHandler.reset_callback_meta   s    		 ""$$&!$&!"$&(#$&!%'"#% !')$')$r   )r   N)__name__
__module____qualname____doc__rX   propertyboolrZ   r\   r^   ra   r   strr   rd   rf   r   r   r   r8   r8   [   s    >&0P $ $ $  D     "d " " "d " "
$sCx. 
"'r   r8   ) _)r*   pathlibr   typingr   r   r   r   r   langchain_core.utilsr
   r   r   r   rm   r   r'   r.   r6   r8   r   r   r   <module>rs      s      4 4 -!c !
"s "
$ $ CF!c3h!-0!<?!eCHo!0 CFc3h-0<?	#s(^&	73 	73 	7sDy) c T Tr   