
    dh                        S SK r S SKJr  S SKJrJrJrJrJr  S SK	J
r
  S\4S jrS\4S jrS\4S jr SS	\\\4   S
\S\S\\\\4      4S jjr SS	\\\4   S
\S\S\\\4   4S jjrS\S\4S jrS\\\4   S\4S jr " S S5      rg)    N)Path)AnyDictIterableTupleUnionguard_importreturnc                      [        S5      $ )zJImport the spacy python package and raise an error if it is not installed.spacyr	        [/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/callbacks/utils.pyimport_spacyr      s      r   c                      [        S5      $ )zKImport the pandas python package and raise an error if it is not installed.pandasr	   r   r   r   import_pandasr      s    !!r   c                      [        S5      $ )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              #      #    U R                  5        HE  u  p4U(       a  X-   U-   OUn[        U[        5      (       a  [        XEU5       Sh  vN   M@  XT4v   MG     g N7f)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      sW      "'')
,6*"S(CeT""$US999.  * :s   A
AAAc                 P    [        XU5       VVs0 sH  u  p4X4_M	     nnnU$ s  snnf )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'   .   s1     #0"MN"M$!"MIN Os   "sc                 h    [         R                  " U R                  S5      5      R                  5       $ )zpHash 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                 t    [        U S5       nUR                  5       nSSS5        U$ ! , (       d  f       W$ = f)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   s3     
i	vvx 
K 
	Ks   (
7c                       \ rS rSrSrSS jr\S\4S j5       r\S\4S j5       r	\S\4S j5       r
\S\4S	 j5       rS\\\4   4S
 jrSS jrSrg)BaseMetadataCallbackHandler[   a  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                    SU l         SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l	        SU l
        SU l        SU l        SU l        SU l        SU l        SU l        SU l        / U l        / U l        / U l        / U l        / U l        / U l        / U l        / U l        / U l        / U l        g )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__$BaseMetadataCallbackHandler.__init__{   s    		 ""!&$*,!*,!(*,.#*,!+-")+ %'-/$-/$r   c                     U R                   $ )z;Whether to call verbose callbacks even if verbose is False.)rD   rW   s    r   always_verbose*BaseMetadataCallbackHandler.always_verbose   s     ###r   c                     U R                   $ )z Whether to ignore LLM callbacks.)r@   rW   s    r   
ignore_llm&BaseMetadataCallbackHandler.ignore_llm   s     r   c                     U R                   $ )z"Whether to ignore chain callbacks.)rA   rW   s    r   ignore_chain(BaseMetadataCallbackHandler.ignore_chain        !!!r   c                     U R                   $ )z"Whether to ignore agent callbacks.)rB   rW   s    r   ignore_agent(BaseMetadataCallbackHandler.ignore_agent   rd   r   c                 &   U R                   U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S.$ )Nr;   r<   r=   r>   r?   rE   rF   rG   rH   rI   rJ   rK   rL   ri   rW   s    r   get_custom_callback_meta4BaseMetadataCallbackHandler.get_custom_callback_meta   sm    IIkkIIkk --////++++//
 	
r   c                 ~   SU l         SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l	        SU l
        SU l        SU l        SU l        SU l        SU l        SU l        / U l        / U l        / U l        / U l        / U l        / U l        / U l        / U l        / U l        / U l        g)zReset the callback metadata.r   FN)r;   r<   r=   r>   r?   r@   rA   rB   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   s    r   reset_callback_meta/BaseMetadataCallbackHandler.reset_callback_meta   s    		 ""$$&!$&!"$&(#$&!%'"#% !')$')$r   )rL   rD   rF   rE   r=   r>   rB   rA   r@   rC   rH   rG   rI   rV   rU   rQ   rP   rO   rM   rN   rT   rS   rR   r<   r;   r?   rK   rJ   )r   N)__name__
__module____qualname____firstlineno____doc__rY   propertyboolr\   r_   rb   rf   r   strr   rj   rm   __static_attributes__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   rv   r   r'   r.   r6   r8   r   r   r   <module>r}      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   