a
    bg+                     @  s   d Z ddlmZ ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZmZmZ ddlZddlmZmZ dd	lmZ dd
lmZ e
rddlZddlmZ dZdZdZdZdZ ddddZ!G dd deZ"dS )zArthurAI's Callback Handler.    )annotationsN)defaultdict)datetime)time)TYPE_CHECKINGAnyDefaultDictDictListOptional)AgentActionAgentFinish)BaseCallbackHandler)	LLMResult)ArthurModelZprompt_tokensZcompletion_tokenstoken_usageZfinish_reasondurationarthurai)returnc               
   C  s@   zddl } W n. ty: } ztd|W Y d}~n
d}~0 0 | S )zLazy load Arthur.r   NzpTo use the ArthurCallbackHandler you need the `arthurai` package. Please install it with `pip install arthurai`.)r   ImportError)r   e r   {/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/callbacks/arthur_callback.py_lazy_load_arthur   s    r   c                      s:  e Zd ZdZddd fddZed:d	d
d
d
d dddZdddddddZddddddZdddddddZ	ddddddZ
ddddd d!Zd	ddd"d#d$Zddddd%d&Zdd	ddd'd(d)Zd*ddd+d,d-Zd;dd
d
ddd.d/d0Zddddd1d2Zd	ddd3d4d5Zd6ddd7d8d9Z  ZS )<ArthurCallbackHandlera  Callback Handler that logs to Arthur platform.

    Arthur helps enterprise teams optimize model operations
    and performance at scale. The Arthur API tracks model
    performance, explainability, and fairness across tabular,
    NLP, and CV models. Our API is model- and platform-agnostic,
    and continuously scales with complex and dynamic enterprise needs.
    To learn more about Arthur, visit our website at
    https://www.arthur.ai/ or read the Arthur docs at
    https://docs.arthur.ai/
    r   None)arthur_modelr   c                   s   t    t }|jjj |jjj|| _tdd | j	 D | _
 fdd| j	 D d j| _ fdd| j	 D d j| _d| _tfdd| j	 D dkr·fdd| j	 D d j| _tt| _dS )	zInitialize callback handler.c                 S  s   g | ]
}|j qS r   )name).0ar   r   r   
<listcomp>D       z2ArthurCallbackHandler.__init__.<locals>.<listcomp>c                   s(   g | ] }|j  jkr|jjkr|qS r   )stageZModelPipelineInput
value_typeUnstructured_Textr   xStage	ValueTyper   r   r    E   s   r   c                   s(   g | ] }|j  jkr|jjkr|qS r   )r"   ZPredictedValuer#   r$   r%   r'   r   r   r    K   s   Nc                   s   g | ]}|j  jkr|qS r   r#   ZTokenLikelihoodsr%   r)   r   r   r    T   s   c                   s   g | ]}|j  jkr|qS r   r*   r%   r+   r   r   r    \   s   )super__init__r   common	constantsr(   r)   r   setZget_attributes
attr_namesr   
input_attroutput_attrtoken_likelihood_attrlenr   dictrun_map)selfr   r   	__class__r'   r   r-   8   s>    





zArthurCallbackHandler.__init__https://app.arthur.aiNstrzOptional[str])model_id
arthur_urlarthur_loginarthur_passwordr   c                 C  s   t  }|j}|jjj}|du rVztjd }W n tyF   tdY n0 |||d}	n$|du rl|||d}	n||||d}	z|		|}
W n" |y   td| dY n0 | |
S )	aX  Initialize callback handler from Arthur credentials.

        Args:
            model_id (str): The ID of the arthur model to log to.
            arthur_url (str, optional): The URL of the Arthur instance to log to.
                Defaults to "https://app.arthur.ai".
            arthur_login (str, optional): The login to use to connect to Arthur.
                Defaults to None.
            arthur_password (str, optional): The password to use to connect to
                Arthur. Defaults to None.

        Returns:
            ArthurCallbackHandler: The initialized callback handler.
        NZARTHUR_API_KEYzNo Arthur authentication provided. Either give a login to the ArthurCallbackHandler or set an ARTHUR_API_KEY as an environment variable.)urlZ
access_key)rA   login)rA   rB   passwordz%Was unable to retrieve model with id zl from Arthur. Make sure the ID corresponds to a model that is currently registered with your Arthur account.)
r   ArthurAIr.   
exceptionsResponseClientErrorosenvironKeyError
ValueErrorZ	get_model)clsr=   r>   r?   r@   r   rD   rF   Zarthur_api_keyZarthurr   r   r   r   from_credentialsd   s.    



z&ArthurCallbackHandler.from_credentialszDict[str, Any]z	List[str]r   )
serializedpromptskwargsr   c                 K  s*   |d }|| j | d< t | j | d< dS )z$On LLM start, save the input promptsrun_idinput_texts
start_timeN)r7   r   )r8   rM   rN   rO   rP   r   r   r   on_llm_start   s    z"ArthurCallbackHandler.on_llm_startr   )responserO   r   c              
     s  zddl }W n. ty: } ztd|W Y d}~n
d}~0 0 |d }z| j| }W n. ty } ztd|W Y d}~n
d}~0 0 t |d  }g }t|jD ]L\}	}
|
D ]<}dtt	 dt
j|jd	| j|d
 |	 | j|ji}|jdurdt|jv rt| jv r|jt |t< |jd }|durd| jdurd|d   fddtt D }||| j< t|jtrt|jv r|jt }t|v rt| jv r|t |t< t|v rt| jv r|t |t< t| jv r||t< || qq| j| dS )z On LLM end, send data to Arthur.r   NzACould not import pytz. Please install it with 'pip install pytz'.rP   z~This function has been called with a run_id that was never registered in on_llm_start(). Restart and try running the LLM againrR   Zpartner_inference_idZinference_timestamp)tzrQ   logprobsZtop_logprobsc                   s"   g | ]}d d  |   D qS )c                 S  s   i | ]\}}|t |qS r   )npexp)r   kvr   r   r   
<dictcomp>   r!   z?ArthurCallbackHandler.on_llm_end.<locals>.<listcomp>.<dictcomp>)items)r   irV   r   r   r       s   z4ArthurCallbackHandler.on_llm_end.<locals>.<listcomp>) pytzr   r7   rI   r   	enumerategenerationsr<   uuiduuid4r   nowUTCr2   r3   textZgeneration_infoFINISH_REASONr1   r4   ranger5   
isinstanceZ
llm_outputr6   TOKEN_USAGEPROMPT_TOKENSCOMPLETION_TOKENSDURATIONappendr   Zsend_inferences)r8   rT   rO   r_   r   rP   Zrun_map_dataZtime_from_start_to_endZ
inferencesr]   ra   Z
generationZ	inferenceZlogprobs_dataZlikelihoodsr   r   r^   r   
on_llm_end   s~    






z ArthurCallbackHandler.on_llm_end)rM   inputsrO   r   c                 K  s   dS )zOn chain start, do nothing.Nr   )r8   rM   rp   rO   r   r   r   on_chain_start   s    z$ArthurCallbackHandler.on_chain_start)outputsrO   r   c                 K  s   dS )zOn chain end, do nothing.Nr   )r8   rr   rO   r   r   r   on_chain_end  s    z"ArthurCallbackHandler.on_chain_endBaseException)errorrO   r   c                 K  s   dS )z%Do nothing when LLM outputs an error.Nr   r8   ru   rO   r   r   r   on_llm_error  s    z"ArthurCallbackHandler.on_llm_error)tokenrO   r   c                 K  s   dS )zOn new token, pass.Nr   )r8   rx   rO   r   r   r   on_llm_new_token  s    z&ArthurCallbackHandler.on_llm_new_tokenc                 K  s   dS )z+Do nothing when LLM chain outputs an error.Nr   rv   r   r   r   on_chain_error  s    z$ArthurCallbackHandler.on_chain_error)rM   	input_strrO   r   c                 K  s   dS )zDo nothing when tool starts.Nr   )r8   rM   r{   rO   r   r   r   on_tool_start  s    z#ArthurCallbackHandler.on_tool_startr   )actionrO   r   c                 K  s   dS )z.Do nothing when agent takes a specific action.Nr   )r8   r}   rO   r   r   r   on_agent_action  s    z%ArthurCallbackHandler.on_agent_action)outputobservation_prefix
llm_prefixrO   r   c                 K  s   dS )zDo nothing when tool ends.Nr   )r8   r   r   r   rO   r   r   r   on_tool_end  s    z!ArthurCallbackHandler.on_tool_endc                 K  s   dS )z&Do nothing when tool outputs an error.Nr   rv   r   r   r   on_tool_error"  s    z#ArthurCallbackHandler.on_tool_error)rf   rO   r   c                 K  s   dS z
Do nothingNr   )r8   rf   rO   r   r   r   on_text%  s    zArthurCallbackHandler.on_textr   )finishrO   r   c                 K  s   dS r   r   )r8   r   rO   r   r   r   on_agent_finish(  s    z%ArthurCallbackHandler.on_agent_finish)r;   NN)NN)__name__
__module____qualname____doc__r-   classmethodrL   rS   ro   rq   rs   rw   ry   rz   r|   r~   r   r   r   r   __classcell__r   r   r9   r   r   +   s,   ,   6Z  	r   )#r   
__future__r   rG   rb   collectionsr   r   r   typingr   r   r   r	   r
   r   numpyrW   Zlangchain_core.agentsr   r   Zlangchain_core.callbacksr   Zlangchain_core.outputsr   r   Zarthurai.core.modelsr   rk   rl   rj   rg   rm   r   r   r   r   r   r   <module>   s*    