o
    Zh                     @   s  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dZdefdd	Zdefd
dZ	ddeeef dededeeeef  fddZ	ddeeef dededeeef fddZdedefddZdeeef defddZG dd dZdS )    N)Path)AnyDictIterableTupleUnionguard_importreturnc                   C      t dS )zJImport the spacy python package and raise an error if it is not installed.Zspacyr    r   r   Z/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/callbacks/utils.pyimport_spacy      r   c                   C   r   )zKImport the pandas python package and raise an error if it is not installed.Zpandasr   r   r   r   r   import_pandas   r   r   c                   C   r   )zMImport the textstat python package and raise an error if it is not installed.Ztextstatr   r   r   r   r   import_textstat   r   r    _nested_dict
parent_keysepc                 c   sT    |   D ]"\}}|r|| | n|}t|tr"t|||E dH  q||fV  qd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valueZnew_keyr   r   r   r      s   
r   c                 C   s   d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                 S   s   i | ]\}}||qS r   r   ).0kvr   r   r   
<dictcomp>=   s    z flatten_dict.<locals>.<dictcomp>)r   )r   r   r   Z	flat_dictr   r   r   flatten_dict.   s   r!   sc                 C   s   t | 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"   r   r   r   hash_stringA   s   	r'   	json_pathc                 C   s8   t | d}| }W d   |S 1 s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)r(   fdatar   r   r   	load_jsonM   s   	

r.   c                   @   s   e Zd ZdZdddZedefddZedefdd	Zedefd
dZ	edefddZ
deeef fddZd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                 C   s   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_Z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selfr   r   r   __init__{   s8   
z$BaseMetadataCallbackHandler.__init__c                 C      | j S )z;Whether to call verbose callbacks even if verbose is False.)r8   rK   r   r   r   always_verbose      z*BaseMetadataCallbackHandler.always_verbosec                 C   rN   )z Whether to ignore LLM callbacks.)r5   rK   r   r   r   
ignore_llm   rP   z&BaseMetadataCallbackHandler.ignore_llmc                 C   rN   )z"Whether to ignore chain callbacks.)r6   rK   r   r   r   ignore_chain   rP   z(BaseMetadataCallbackHandler.ignore_chainc                 C   rN   )z"Whether to ignore agent callbacks.)r7   rK   r   r   r   ignore_agent   rP   z(BaseMetadataCallbackHandler.ignore_agentc                 C   s:   | j | j| j| j| j| j| j| j| j| j	| j
| j| jdS )Nr0   r1   r2   r3   r4   r9   r:   r;   r<   r=   r>   r?   r@   rT   rK   r   r   r   get_custom_callback_meta   s   z4BaseMetadataCallbackHandler.get_custom_callback_metac                 C   s   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)r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   r   r   r   reset_callback_meta   s8   z/BaseMetadataCallbackHandler.reset_callback_meta)r
   N)__name__
__module____qualname____doc__rM   propertyboolrO   rQ   rR   rS   r   strr   rU   rV   r   r   r   r   r/   [   s    
(r/   )r   r   )r#   pathlibr   typingr   r   r   r   r   Zlangchain_core.utilsr	   r   r   r   r]   r   r!   r'   r.   r/   r   r   r   r   <module>   s<    




