o
    Zh+                     @  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
rVddlZddlmZ dZdZdZdZdZ 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returnarthuraic               
   C  s2   zddl } W | S  ty } ztd|d}~ww )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   d/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/callbacks/arthur_callback.py_lazy_load_arthur   s   
	r   c                      s   e Zd ZdZdB fddZe					dCdDddZdEddZdFddZdGdd Z	dHd"d#Z
dId&d'ZdJd)d*ZdId+d,ZdKd.d/ZdLd2d3Z				dMdNd7d8ZdId9d:ZdOd<d=ZdPd@dAZ  ZS )Q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/
    arthur_modelr   r   Nonec                   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rg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   s    z2ArthurCallbackHandler.__init__.<locals>.<listcomp>c                   (   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       r   c                   r!   r   )r"   ZPredictedValuer#   r$   r%   r'   r   r   r    K   r*   Nc                      g | ]
}|j  jkr|qS r   r#   ZTokenLikelihoodsr%   r)   r   r   r    T   
    c                   r+   r   r,   r%   r-   r   r   r    \   r.   )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   r0   8   sH   




	zArthurCallbackHandler.__init__https://app.arthur.aiNmodel_idstr
arthur_urlOptional[str]arthur_loginarthur_passwordc                 C  s   t  }|j}|jjj}|du r)ztjd }W n ty!   tdw |||d}	n|du r4|||d}	n||||d}	z
|		|}
W | |
S  |yT   td| dw )	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)rE   login)rE   rF   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   ArthurAIr1   
exceptionsResponseClientErrorosenvironKeyError
ValueErrorZ	get_model)clsr?   rA   rC   rD   r   rH   rJ   Zarthur_api_keyZarthurr   r   r   r   from_credentialsd   s2   

z&ArthurCallbackHandler.from_credentials
serializedDict[str, Any]prompts	List[str]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)r:   r   )r;   rQ   rS   rU   rV   r   r   r   on_llm_start   s   z"ArthurCallbackHandler.on_llm_startresponser   c              
     s  zddl }W n ty } ztd|d}~ww |d }z| j| }W n ty4 } ztd|d}~ww t |d  }g }t|jD ]\}	}
|
D ]}dtt	 dt
j|jd	| j|d
 |	 | j|ji}|jdurt|jv r{t| jv r{|jt |t< |jd }|dur| jdur|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< || qIqC| j| dS )z On LLM end, send data to Arthur.r   NzACould not import pytz. Please install it with 'pip install pytz'.rV   z~This function has been called with a run_id that was never registered in on_llm_start(). Restart and try running the LLM againrX   Zpartner_inference_idZinference_timestamp)tzrW   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>   s    z?ArthurCallbackHandler.on_llm_end.<locals>.<listcomp>.<dictcomp>)items)r   ir\   r   r   r       s    z4ArthurCallbackHandler.on_llm_end.<locals>.<listcomp>) pytzr   r:   rM   r   	enumerategenerationsr@   uuiduuid4r   nowUTCr5   r6   textZgeneration_infoFINISH_REASONr4   r7   ranger8   
isinstanceZ
llm_outputr9   TOKEN_USAGEPROMPT_TOKENSCOMPLETION_TOKENSDURATIONappendr   Zsend_inferences)r;   rZ   rU   re   r   rV   Zrun_map_dataZtime_from_start_to_endZ
inferencesrc   rg   Z
generationZ	inferenceZlogprobs_dataZlikelihoodsr   r   rd   r   
on_llm_end   sv   













<z ArthurCallbackHandler.on_llm_endinputsc                 K     dS )zOn chain start, do nothing.Nr   )r;   rQ   rv   rU   r   r   r   on_chain_start       z$ArthurCallbackHandler.on_chain_startoutputsc                 K  rw   )zOn chain end, do nothing.Nr   )r;   rz   rU   r   r   r   on_chain_end  ry   z"ArthurCallbackHandler.on_chain_enderrorBaseExceptionc                 K  rw   )z%Do nothing when LLM outputs an error.Nr   r;   r|   rU   r   r   r   on_llm_error  ry   z"ArthurCallbackHandler.on_llm_errortokenc                 K  rw   )zOn new token, pass.Nr   )r;   r   rU   r   r   r   on_llm_new_token  ry   z&ArthurCallbackHandler.on_llm_new_tokenc                 K  rw   )z+Do nothing when LLM chain outputs an error.Nr   r~   r   r   r   on_chain_error  ry   z$ArthurCallbackHandler.on_chain_error	input_strc                 K  rw   )zDo nothing when tool starts.Nr   )r;   rQ   r   rU   r   r   r   on_tool_start  ry   z#ArthurCallbackHandler.on_tool_startactionr   c                 K  rw   )z.Do nothing when agent takes a specific action.Nr   )r;   r   rU   r   r   r   on_agent_action  ry   z%ArthurCallbackHandler.on_agent_actionoutputobservation_prefix
llm_prefixc                 K  rw   )zDo nothing when tool ends.Nr   )r;   r   r   r   rU   r   r   r   on_tool_end  ry   z!ArthurCallbackHandler.on_tool_endc                 K  rw   )z&Do nothing when tool outputs an error.Nr   r~   r   r   r   on_tool_error"  ry   z#ArthurCallbackHandler.on_tool_errorrl   c                 K  rw   z
Do nothingNr   )r;   rl   rU   r   r   r   on_text%  ry   zArthurCallbackHandler.on_textfinishr   c                 K  rw   r   r   )r;   r   rU   r   r   r   on_agent_finish(  ry   z%ArthurCallbackHandler.on_agent_finish)r   r   r   r   )r>   NN)
r?   r@   rA   rB   rC   rB   rD   rB   r   r   )rQ   rR   rS   rT   rU   r   r   r   )rZ   r   rU   r   r   r   )rQ   rR   rv   rR   rU   r   r   r   )rz   rR   rU   r   r   r   )r|   r}   rU   r   r   r   )r   r@   rU   r   r   r   )rQ   rR   r   r@   rU   r   r   r   )r   r   rU   r   r   r   )NN)
r   r   r   rB   r   rB   rU   r   r   r   )rl   r@   rU   r   r   r   )r   r   rU   r   r   r   )__name__
__module____qualname____doc__r0   classmethodrP   rY   ru   rx   r{   r   r   r   r   r   r   r   r   r   __classcell__r   r   r<   r   r   +   s.    ,
6

Z






	
r   )r   r   )#r   
__future__r   rK   rh   collectionsr   r   r   typingr   r   r   r	   r
   r   numpyr]   Zlangchain_core.agentsr   r   Zlangchain_core.callbacksr   Zlangchain_core.outputsr   r   Zarthurai.core.modelsr   rq   rr   rp   rm   rs   r   r   r   r   r   r   <module>   s,     
