
    Ng+                        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
r
ddlZddlmZ dZdZdZdZdZ ddZ! G 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)ArthurModelprompt_tokenscompletion_tokenstoken_usagefinish_reasondurationreturnarthuraic                 V    	 ddl } n"# t          $ r}t          d|          d}~ww xY w| 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   es     i/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/callbacks/arthur_callback.py_lazy_load_arthurr      sT    
 
 
 
' 	
 
 	

 Os    
&!&c                       e Zd ZdZd3 fdZe	 	 	 d4d5d            Zd6dZd7dZd8dZ	d9dZ
d:d Zd;d"Zd:d#Zd<d%Zd=d(Z	 	 d>d?d,Zd:d-Zd@d/ZdAd2Z xZS )B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                   t                                                       t                      }|j        j        j        |j        j        j        || _        t          d | j        	                                D                       | _
        fd| j        	                                D             d         j        | _        fd| j        	                                D             d         j        | _        d| _        t          fd| j        	                                D                       dk    r5fd| j        	                                D             d         j        | _        t!          t"                    | _        dS )zInitialize callback handler.c                    g | ]	}|j         
S  )name).0as     r   
<listcomp>z2ArthurCallbackHandler.__init__.<locals>.<listcomp>D   s    RRR!qvRRR    c                T    g | ]$}|j         j        k    r|j        j        k    "|%S r$   )stageModelPipelineInput
value_typeUnstructured_Textr&   xStage	ValueTypes     r   r(   z2ArthurCallbackHandler.__init__.<locals>.<listcomp>E   sE     
 
 
w%222	 ;;;  <;;r)   r   c                T    g | ]$}|j         j        k    r|j        j        k    "|%S r$   )r+   PredictedValuer-   r.   r/   s     r   r(   z2ArthurCallbackHandler.__init__.<locals>.<listcomp>K   sE     
 
 
w%...	 ;;;  <;;r)   Nc                4    g | ]}|j         j        k    |S r$   r-   TokenLikelihoodsr&   r0   r2   s     r   r(   z2ArthurCallbackHandler.__init__.<locals>.<listcomp>T   s3       |y'AAA AAAr)   c                4    g | ]}|j         j        k    |S r$   r6   r8   s     r   r(   z2ArthurCallbackHandler.__init__.<locals>.<listcomp>\   s3     * * *<9#=== ===r)   )super__init__r   common	constantsr1   r2   r    setget_attributes
attr_namesr%   
input_attroutput_attrtoken_likelihood_attrlenr   dictrun_map)selfr    r   r1   r2   	__class__s      @@r   r;   zArthurCallbackHandler.__init__8   s   
 	$&&)/O-7	( RRt/@/O/O/Q/QRRRSS
 
 
 
 
&5577
 
 

 
  	
 
 
 
 
&5577
 
 

 
  	 &*"   !.==??     * * * **99;;* * * 	* 	 & /:$.?.?r)   https://app.arthur.aiNmodel_idstr
arthur_urlOptional[str]arthur_loginarthur_passwordc                   t                      }|j        }|j        j        j        }|?	 t
          j        d         }n# t          $ r t          d          w xY w |||          }	n| |||          }	n ||||          }		 |		                    |          }
n# |$ r t          d| d          w xY w | |
          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.
        NARTHUR_API_KEYzNo Arthur authentication provided. Either give a login to the ArthurCallbackHandler or set an ARTHUR_API_KEY as an environment variable.)url
access_key)rR   login)rR   rT   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
ValueError	get_model)clsrJ   rL   rN   rO   r   rV   rX   arthur_api_keyarthurr    s              r   from_credentialsz&ArthurCallbackHandler.from_credentialsd   s4   , %&&$&o8L !#,<!=    L   X*HHHFF&!jEEE!",  	!++H55LL" 	 	 	8 8 8 8  	 s<   s   = AB B6
serializedDict[str, Any]prompts	List[str]kwargsr   c                n    |d         }|| j         |         d<   t                      | j         |         d<   dS )z$On LLM start, save the input promptsrun_idinput_texts
start_timeN)rF   r   )rG   rb   rd   rf   rh   s        r   on_llm_startz"ArthurCallbackHandler.on_llm_start   s:     !.5V]+-1VVV\***r)   responser   c           
        	 ddl }n"# t          $ r}t          d          |d}~ww xY w|d         }	 | j        |         }n"# t          $ r}t          d          |d}~ww xY wt	                      |d         z
  }g }t          |j                  D ]\  }	}
|
D ]}dt          t          j	                              dt          j        |j        	          | j        |d
         |	         | j        |j        i}|j        t"          |j        v r(t"          | j        v r|j        t"                   |t"          <   |j        d         }|A| j        :|d         fdt)          t+                              D             }||| j        <   t-          |j        t0                    rxt2          |j        v rj|j        t2                   }t4          |v r#t4          | j        v r|t4                   |t4          <   t6          |v r#t6          | j        v r|t6                   |t6          <   t8          | j        v r
||t8          <   |                    |           | j                            |           dS )z On LLM end, send data to Arthur.r   NzACould not import pytz. Please install it with 'pip install pytz'.rh   z~This function has been called with a run_id that was never registered in on_llm_start(). Restart and try running the LLM againrj   partner_inference_idinference_timestamp)tzri   logprobstop_logprobsc                X    g | ]&}d  |                                          D             'S )c                >    i | ]\  }}|t          j        |          S r$   )npexp)r&   kvs      r   
<dictcomp>z?ArthurCallbackHandler.on_llm_end.<locals>.<listcomp>.<dictcomp>   s&    JJJdaQq		JJJr)   )items)r&   irq   s     r   r(   z4ArthurCallbackHandler.on_llm_end.<locals>.<listcomp>   sH     ' ' ' ! KJhqk6G6G6I6IJJJ' ' 'r)   ) pytzr   rF   r[   r   	enumerategenerationsrK   uuiduuid4r   nowUTCrA   rB   textgeneration_infoFINISH_REASONr@   rC   rangerD   
isinstance
llm_outputrE   TOKEN_USAGEPROMPT_TOKENSCOMPLETION_TOKENSDURATIONappendr    send_inferences)rG   rl   rf   r|   r   rh   run_map_datatime_from_start_to_end
inferencesr{   r~   
generation	inferencelogprobs_datalikelihoodsr   rq   s                   @r   
on_llm_endz ArthurCallbackHandler.on_llm_end   s   	KKKK 	 	 	S 	
 !	</LL 	 	 	9  		 "&,|*D!D 
'(<== :	- :	-NA{) 9- 9-
*C
,=,=)8<48+D+D+DO\-%@%C$jo		 -9
 &)CCC)T_<<3=3M)4	-0 %/$>z$JM%1 6B#0#@' ' ' '%*3x==%9%9' ' ' AL	$"<=
 x2D99V#x':::"*"5k"BK%44)T_<<3>}3M	-0)[88-@@7BCT7U	"34 t..*@Ih'!!),,,,s9-x 	))*55555s'    
'"'A 
A AA inputsc                    dS )zOn chain start, do nothing.Nr$   )rG   rb   r   rf   s       r   on_chain_startz$ArthurCallbackHandler.on_chain_start         r)   outputsc                    dS )zOn chain end, do nothing.Nr$   )rG   r   rf   s      r   on_chain_endz"ArthurCallbackHandler.on_chain_end  r   r)   errorBaseExceptionc                    dS )z%Do nothing when LLM outputs an error.Nr$   rG   r   rf   s      r   on_llm_errorz"ArthurCallbackHandler.on_llm_error  r   r)   tokenc                    dS )zOn new token, pass.Nr$   )rG   r   rf   s      r   on_llm_new_tokenz&ArthurCallbackHandler.on_llm_new_token  r   r)   c                    dS )z+Do nothing when LLM chain outputs an error.Nr$   r   s      r   on_chain_errorz$ArthurCallbackHandler.on_chain_error  r   r)   	input_strc                    dS )zDo nothing when tool starts.Nr$   )rG   rb   r   rf   s       r   on_tool_startz#ArthurCallbackHandler.on_tool_start  r   r)   actionr   c                    dS )z.Do nothing when agent takes a specific action.Nr$   )rG   r   rf   s      r   on_agent_actionz%ArthurCallbackHandler.on_agent_action  r   r)   outputobservation_prefix
llm_prefixc                    dS )zDo nothing when tool ends.Nr$   )rG   r   r   r   rf   s        r   on_tool_endz!ArthurCallbackHandler.on_tool_end  r   r)   c                    dS )z&Do nothing when tool outputs an error.Nr$   r   s      r   on_tool_errorz#ArthurCallbackHandler.on_tool_error"  r   r)   r   c                    dS z
Do nothingNr$   )rG   r   rf   s      r   on_textzArthurCallbackHandler.on_text%  r   r)   finishr   c                    dS r   r$   )rG   r   rf   s      r   on_agent_finishz%ArthurCallbackHandler.on_agent_finish(  r   r)   )r    r   r   r!   )rI   NN)
rJ   rK   rL   rM   rN   rM   rO   rM   r   r   )rb   rc   rd   re   rf   r   r   r!   )rl   r   rf   r   r   r!   )rb   rc   r   rc   rf   r   r   r!   )r   rc   rf   r   r   r!   )r   r   rf   r   r   r!   )r   rK   rf   r   r   r!   )rb   rc   r   rK   rf   r   r   r!   )r   r   rf   r   r   r   )NN)
r   r   r   rM   r   rM   rf   r   r   r!   )r   rK   rf   r   r   r!   )r   r   rf   r   r   r!   )__name__
__module____qualname____doc__r;   classmethodra   rk   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)rH   s   @r   r   r   +   s       
 
*@ *@ *@ *@ *@ *@X  %<&*)-4! 4! 4! 4! [4!l4 4 4 4X6 X6 X6 X6t* * * *
( ( ( (4 4 4 4" " " ": : : :+ + + += = = = -1$(	) ) ) ) )5 5 5 5          r)   r   )r   r   )#r   
__future__r   rY   r   collectionsr   r   r   typingr   r   r	   r
   r   r   numpyru   langchain_core.agentsr   r   langchain_core.callbacksr   langchain_core.outputsr   r   arthurai.core.modelsr   r   r   r   r   r   r   r   r$   r)   r   <module>r      sm   " " " " " " " " 				  # # # # # #             H H H H H H H H H H H H H H H H     : : : : : : : : 8 8 8 8 8 8 , , , , , , 1OOO000000'    ~ ~ ~ ~ ~/ ~ ~ ~ ~ ~r)   