
    dhKP                     6   S SK rS SKrS SKrS SKr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 SKrS SKJrJr  S SKJr  S SKJr  S SKJr  S S	KJr  \R8                  " \5      rS
r\\\ S4      " SSS9r!\\\ S4      " SSS9r"/ SQr# " S S5      r$S'S\ S\	S\$4S jjr%S\	S\\
\ \	4   \\	   \	4   4S jr&S\	S\	4S jr'S\(S\	4S jr)S\ S\ 4S jr*S\	S\	4S jr+S\	S\	4S jr,S \S\
\ \	4   4S! jr-S"\\\   \	4   S\\
\ \	4      4S# jr. " S$ S%\5      r/S%S&/r0g)(    N)
ContextVar)AnyDictListUnioncast)UUID)AgentActionAgentFinish)BaseCallbackHandler)BaseMessage)	LLMResult)parsezhttps://app.llmonitor.comuser_ctx)defaultuser_props_ctx)temperaturetop_ptop_kstoppresence_penaltyfrequence_penaltyseedfunction_call	functionstoolstool_choiceresponse_format
max_tokens
logit_biasc                   Z    \ rS rSrSrSS\S\SS4S jjrS\4S jrS	\S
\S\S\4S jr	Sr
g)UserContextManager*   z+Context manager for LLMonitor user context.Nuser_id
user_propsreturnc                 X    [         R                  U5        [        R                  U5        g Nr   setr   )selfr$   r%   s      h/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/callbacks/llmonitor_callback.py__init__UserContextManager.__init__-   s    W:&    c                     g r(    )r+   s    r,   	__enter__UserContextManager.__enter__1   s    r/   exc_type	exc_valueexc_tbc                 X    [         R                  S 5        [        R                  S 5        g r(   r)   )r+   r4   r5   r6   s       r,   __exit__UserContextManager.__exit__4   s    T4 r/   r1   r(   )__name__
__module____qualname____firstlineno____doc__strr   r-   r2   r8   __static_attributes__r1   r/   r,   r"   r"   *   sL    5' ' ' '3 ! ! !c !c !r/   r"   r$   r%   r&   c                     [        X5      $ )zBuilds an LLMonitor UserContextManager

Parameters:
    - `user_id`: The user id.
    - `user_props`: The user properties.

Returns:
    A context manager that sets the user context.
)r"   )r$   r%   s     r,   identifyrB   9   s     g22r/   objc                 D   [        U S5      (       a  U R                  5       $ [        U [        5      (       a.  U R	                  5        VVs0 sH  u  pU[        U5      _M     snn$ [        U [        5      (       a  U  Vs/ sH  n[        U5      PM     sn$ U $ s  snnf s  snf )Nto_json)hasattrrE   
isinstancedictitems
_serializelist)rC   keyvalueelements       r,   rJ   rJ   F   s    sI{{}#t9<E:3Z&&EE#t3673
7#377J F 8s   
B>B	raw_inputc                    U (       d  g [        U [        5      (       a  [        U 5      S:X  a  [        U S   5      $ [        U [        5      (       d  [        U 5      $ U R                  S5      nU R                  S5      nU R                  S5      nU R                  S5      nU(       a  U$ U(       a  U$ U(       a  U$ U(       a  U$ [        U 5      $ )N   r   inputinputsquestionquery)rG   rK   len_parse_inputrH   rJ   get)rO   input_valueinputs_valuequestion_valuequery_values        r,   rW   rW   S   s     )T""s9~':IaL))i&&)$$--(K==*L]]:.N--(Ki  r/   
raw_outputc                 l   U (       d  g [        U [        5      (       d  [        U 5      $ U R                  S5      nU R                  S5      nU R                  S5      nU R                  S5      nU R                  S5      nU(       a  U$ U(       a  U$ U(       a  U$ U(       a  U$ U(       a  U$ [        U 5      $ )Ntextoutputoutput_textanswerresult)rG   rH   rJ   rX   )r]   
text_valueoutput_valueoutput_text_valueanswer_valueresult_values         r,   _parse_outputri   o   s    j$''*%%'J>>(+L"}5>>(+L>>(+L  j!!r/   rolec                     U S:X  a  gU $ )Nhumanuserr1   )rj   s    r,   _parse_lc_rolern      s     wr/   metadatac                     [         R                  5       b  [         R                  5       $ U =(       d    0 n U R                  S5      nUc  U R                  S5      nU$ )Nr$   userId)r   rX   )ro   r$   s     r,   _get_user_idrr      sI    ||~!||~~2Hll9%G,,x(Nr/   c                     [         R                  5       b  [         R                  5       $ U =(       d    0 n U R                  SS 5      $ )Nr%   )r   rX   )ro   s    r,   _get_user_propsrt      s:    '!!##~2H<<d++r/   messagec                 *   / SQnU R                   [        U R                  5      S.nUR                  U Vs0 sHK  nU R                  R                  U5      c  M!  U[        [        U R                  R                  U5      5      _MM     sn5        U$ s  snf )N)r   
tool_callstool_call_idname)r_   rj   )contentrn   typeupdateadditional_kwargsrX   r   r   )ru   keysparsedrL   s       r,   _parse_lc_messager      s    BDoo~gll/KLF
MM 	
((,,S1 ?Cc74488=>>	
 M	
s   B.Bmessagesc                 B    U  Vs/ sH  n[        U5      PM     sn$ s  snf r(   )r   )r   ru   s     r,   _parse_lc_messagesr      s     6>?h7g&h???s   c                     ^  \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   \\S'      S.S	\\S4   S
\\S4   S\SS4U 4S jjjr	SSSS.S\
\\4   S\\   S\S\\S4   S\\\   S4   S\\
\\4   S4   S\SS4S jjrSSSS.S\
\\4   S\\\      S\S\\S4   S\\\   S4   S\\
\\4   S4   S\S\4S jjrSS.S\S\S\\S4   S\SS4
S jjrSSSS.S\
\\4   S\S\S\\S4   S\\\   S4   S\\
\\4   S4   S\SS4S jjrSSS.S\S\S\\S4   S\\\   S4   S\SS4S  jjrSSSS.S\
\\4   S!\
\\4   S\S\\S4   S\\\   S4   S\\
\\4   S4   S\S\4S" jjrSS.S#\
\\4   S\S\\S4   S\S\4
S$ jjrSS.S%\S\S\\S4   S\S\4
S& jjrSS.S'\S\S\\S4   S\S\4
S( jjrSS.S)\S\S\\S4   S\S\4
S* jjrSS.S)\S\S\\S4   S\S\4
S+ jjrSS.S)\S\S\\S4   S\S\4
S, jjrS-rU =r $ )/LLMonitorCallbackHandler   ab  Callback Handler for LLMonitor`.

#### Parameters:
    - `app_id`: The app id of the app you want to report to. Defaults to
    `None`, which means that `LLMONITOR_APP_ID` will be used.
    - `api_url`: The url of the LLMonitor API. Defaults to `None`,
    which means that either `LLMONITOR_API_URL` environment variable
    or `https://app.llmonitor.com` will be used.

#### Raises:
    - `ValueError`: if `app_id` is not provided either as an
    argument or as an environment variable.
    - `ConnectionError`: if the connection to the API fails.


#### Example:
```python
from langchain_community.llms import OpenAI
from langchain_community.callbacks import LLMonitorCallbackHandler

llmonitor_callback = LLMonitorCallbackHandler()
llm = OpenAI(callbacks=[llmonitor_callback],
             metadata={"userId": "user-123"})
llm.invoke("Hello, how are you?")
```
"_LLMonitorCallbackHandler__api_url!_LLMonitorCallbackHandler__app_id"_LLMonitorCallbackHandler__verbose,_LLMonitorCallbackHandler__llmonitor_version+_LLMonitorCallbackHandler__has_valid_configNapp_idapi_urlverboser&   c                   > [         TU ]  5         SU l         SS Kn[        R
                  R                  S5      U l        UR                  U l	        [        U R                  5      [        S5      :  a*  [        R                  SU R                   S35        SU l        SU l        U=(       d#    [        R                  " S	5      =(       d    [         U l        U=(       d    [%        [        R                  " S
5      5      U l        U=(       d    [        R                  " S5      nUc  [        R                  S5        SU l        OXPl        U R                  SL a  g  [*        R,                  " U R"                   SU R(                   35      nUR.                  (       d
  [1        5       eg ! [         a    [        R                  S5        SU l         g f = f! [2         a%    [        R                  SU R"                   35         g f = f)NTr   	llmonitorz[LLMonitor] To use the LLMonitor callback handler you need to 
                have the `llmonitor` Python package installed. Please install it 
                with `pip install llmonitor`Fz0.0.32zB[LLMonitor] The installed `llmonitor` version is 
                z 
                but `LLMonitorCallbackHandler` requires at least version 0.0.32 
                upgrade `llmonitor` with `pip install --upgrade llmonitor`LLMONITOR_API_URLLLMONITOR_VERBOSELLMONITOR_APP_IDzh[LLMonitor] app_id must be provided either as an argument or 
                as an environment variablez	/api/app/zG[LLMonitor] Could not connect to the LLMonitor API at 
                )superr-   r   r   	importlibro   versionr   track_event&_LLMonitorCallbackHandler__track_eventImportErrorloggerwarningr   osgetenvDEFAULT_API_URLr   boolr   r   requestsrX   okConnectionError	Exception)r+   r   r   r   r   _app_idres	__class__s          r,   r-   !LLMonitorCallbackHandler.__init__   s    	"&	'0'9'9'A'A+'ND$!*!6!6D ))*U8_<NN))* +KN ',D#"& UBII.A$BUo HD3F)G$H9BII&89?NN. ',D##M""e+	,,$..!14==/JKC66%'' M  	NN0
 ',D#	P  	NN $	s$   9F# A
G #&GG,G>=G>)parent_run_idtagsro   
serializedpromptsrun_idr   r   ro   kwargsc                   U R                   SL a  g  [        U5      n[        U5      n	UR                  S0 5      n
U
R	                  UR                  S0 5      5        U
R                  S5      =(       d)    U
R                  S5      =(       d    U
R                  S5      nU(       d  SU
R                  S5      ;   a  S	n[
         Vs0 sH(  nU
R                  U5      c  M  XR                  U5      _M*     nn[        U5      nU R                  S
SU[        U5      U(       a  [        U5      OS UUUUUU	U R                  S9  g s  snf ! [         a#  n[        R                  " SU 35         S nAg S nAff = f)NFinvocation_paramsr   model
model_namemodel_id	anthropic_typeclaude-2llmstart
r$   r   r   ry   rR   r   extraro   r%   r   z/[LLMonitor] An error occurred in on_llm_start: )r   rr   rt   rX   r|   PARAMS_TO_CAPTURErW   r   r?   r   r   warningswarn)r+   r   r   r   r   r   ro   r   r$   r%   paramsry   paramr   rR   es                   r,   on_llm_start%LLMonitorCallbackHandler.on_llm_start  si    ""e+)	Q"8,G(2JZZ 3R8FMMx,
 

7# *::l+*::j)  K6::g+>>! /.E::e$ )zz%((.   !)E6{4Ac-0t!%}}  ,  	QMMKA3OPP	Qs1   B0E D=D=0AE =E 
E/E**E/r   c                   U R                   SL a  g  [        U5      n[        U5      n	UR                  S0 5      n
U
R	                  UR                  S0 5      5        U
R                  S5      =(       d)    U
R                  S5      =(       d    U
R                  S5      nU(       d  SU
R                  S5      ;   a  S	n[
         Vs0 sH(  nU
R                  U5      c  M  XR                  U5      _M*     nn[        US
   5      nU R                  SSU[        U5      U(       a  [        U5      OS UUUUUU	U R                  S9  g s  snf ! [         a"  n[        R                  SU 35         S nAg S nAff = f)NFr   r   r   r   r   r   r   r   r   r   r   r   z6[LLMonitor] An error occurred in on_chat_model_start: )r   rr   rt   rX   r|   r   r   r   r?   r   r   r   error)r+   r   r   r   r   r   ro   r   r$   r%   r   ry   r   r   rR   r   s                   r,   on_chat_model_start,LLMonitorCallbackHandler.on_chat_model_startO  so    ""e+)	W"8,G(2JZZ 3R8FMMx,
 

7# *::l+*::j)  K6::g+>>! /.E::e$ )zz%((.   'x{3E6{4Ac-0t!%}}  ,  	WLLQRSQTUVV	Ws1   B0E E E 0AE  E 
E1E,,E1)r   responsec                R   U R                   SL a  g  UR                  =(       d    0 R                  S0 5      nUR                  S    Vs/ sH5  n[	        US5      (       a  [        UR                  5      OUR                  PM7     nn[        U5      S:X  a  US   nU R                  SS[        U5      U(       a  [        U5      OS UUR                  S5      UR                  S	5      S
.U R                  S9  g s  snf ! [         a"  n[        R                  SU 35         S nAg S nAff = f)NFtoken_usager   ru   rQ   r   endprompt_tokenscompletion_tokens)prompt
completion)r   r   r`   r   r   z-[LLMonitor] An error occurred in on_llm_end: )r   
llm_outputrX   generationsrF   r   ru   r_   rV   r   r?   r   r   r   r   )	r+   r   r   r   r   r   
generationparsed_outputr   s	            r,   
on_llm_end#LLMonitorCallbackHandler.on_llm_end  s.    ""e+	N#..4"99-LK #+"6"6q"9	" #:J :y11 "*"4"45__% #:	  " =!Q& -a 06{4Ac-0t$)ooo>"-//2E"F }}  ".  	NLLHLMM	Ns*   6C: ;C5A1C: 5C: :
D&D!!D&	input_strc                N   U R                   SL a  g  [        U5      n[        U5      n	UR                  S5      n
U R	                  SSU[        U5      U(       a  [        U5      OS U
UUUU	U R                  S9  g ! [         a"  n[        R                  SU 35         S nAg S nAff = f)NFry   toolr   	r$   r   r   ry   rR   r   ro   r%   r   z0[LLMonitor] An error occurred in on_tool_start: )
r   rr   rt   rX   r   r?   r   r   r   r   )r+   r   r   r   r   r   ro   r   r$   r%   ry   r   s               r,   on_tool_start&LLMonitorCallbackHandler.on_tool_start  s     ""e+	Q"8,G(2J>>&)D6{4Ac-0t!%}}    	QLLKA3OPP	Qs   A%A8 8
B$BB$)r   r   r`   c          	         [        U5      nU R                  SL a  g  U R                  SS[        U5      U(       a  [        U5      OS UU R                  S9  g ! [         a"  n[
        R                  SU 35         S nAg S nAff = f)NFr   r   r   r   r`   r   z.[LLMonitor] An error occurred in on_tool_end: )r?   r   r   r   r   r   r   )r+   r`   r   r   r   r   r   s          r,   on_tool_end$LLMonitorCallbackHandler.on_tool_end  s     V""e+
	O6{4Ac-0t}}    	OLLI!MNN	Os   9A 
B!A>>BrS   c                   U R                   SL a  g  UR                  S/ SQ5      S   nSn	U=(       d    0 nUR                  S5      n
U
c  UR                  S5      n
US:X  d  US	:X  a  S
n	U
b  S
n	U
nUb  Sn	[        U5      n[        U5      n[	        U5      nU R                  U	SU[        U5      U(       a  [        U5      OS UUUUUU R                  S9  g ! [         a"  n[        R                  SU 35         S nAg S nAff = f)NFid)NNNN   chain
agent_name	agentNameAgentExecutorPlanAndExecuteagentr   r   z1[LLMonitor] An error occurred in on_chain_start: )r   rX   rr   rt   rW   r   r?   r   r   r   r   )r+   r   rS   r   r   r   ro   r   ry   r{   r   r$   r%   rR   r   s                  r,   on_chain_start'LLMonitorCallbackHandler.on_chain_start  s    ""e+#	R>>$(@A!DDD~2H \2I $LL5	&$2B*B$ ("8,G(2J (E6{4Ac-0t!%}}    	RLLLQCPQQ	Rs   CC 
DC<<Doutputsc          	         U R                   SL a  g  [        U5      nU R                  SS[        U5      U(       a  [        U5      OS UU R                  S9  g ! [
         a"  n[        R                  SU 35         S nAg S nAff = f)NFr   r   r   z/[LLMonitor] An error occurred in on_chain_end: )r   ri   r   r?   r   r   r   r   )r+   r   r   r   r   r`   r   s          r,   on_chain_end%LLMonitorCallbackHandler.on_chain_end  s     ""e+	P"7+F6{4Ac-0t}}    	PLLJ1#NOO	Ps   AA 
B!A>>Bactionc          
      :   U R                   SL a  g  UR                  n[        UR                  5      nU R	                  SS[        U5      U(       a  [        U5      OS UUU R                  S9  g ! [         a"  n[        R                  SU 35         S nAg S nAff = f)NFr   r   )r   r   ry   rR   r   z2[LLMonitor] An error occurred in on_agent_action: )
r   r   rW   
tool_inputr   r?   r   r   r   r   )r+   r   r   r   r   ry   rR   r   s           r,   on_agent_action(LLMonitorCallbackHandler.on_agent_action4  s     ""e+	S;;D !2!23E6{4Ac-0t}}    	SLLMaSQRR	Ss   AA. .
B8BBfinishc          	          U R                   SL a  g  [        UR                  5      nU R                  SS[	        U5      U(       a  [	        U5      OS UU R
                  S9  g ! [         a"  n[        R                  SU 35         S nAg S nAff = f)NFr   r   r   z2[LLMonitor] An error occurred in on_agent_finish: )	r   ri   return_valuesr   r?   r   r   r   r   )r+   r   r   r   r   r`   r   s          r,   on_agent_finish(LLMonitorCallbackHandler.on_agent_finishN  s     ""e+	S"6#7#78F6{4Ac-0t}}    	SLLMaSQRR	Ss   AA! !
B+BBr   c          	      4   U R                   SL a  g  U R                  SS[        U5      U(       a  [        U5      OS [        U5      [        R                  " 5       S.U R
                  S9  g ! [         a"  n[        R                  SU 35         S nAg S nAff = f)NFr   r   ru   stackr   r   r   r   z1[LLMonitor] An error occurred in on_chain_error: 	r   r   r?   	traceback
format_excr   r   r   r   r+   r   r   r   r   r   s         r,   on_chain_error'LLMonitorCallbackHandler.on_chain_errorf  s     ""e+
	R6{4Ac-0t"%e*y7K7K7MN}}    	RLLLQCPQQ	R   AA+ +
B5BBc          	      4   U R                   SL a  g  U R                  SS[        U5      U(       a  [        U5      OS [        U5      [        R                  " 5       S.U R
                  S9  g ! [         a"  n[        R                  SU 35         S nAg S nAff = f)NFr   r   r   r   z0[LLMonitor] An error occurred in on_tool_error: r   r   s         r,   on_tool_error&LLMonitorCallbackHandler.on_tool_error|  s     ""e+
	Q6{4Ac-0t"%e*y7K7K7MN}}    	QLLKA3OPP	Qr  c          	      4   U R                   SL a  g  U R                  SS[        U5      U(       a  [        U5      OS [        U5      [        R                  " 5       S.U R
                  S9  g ! [         a"  n[        R                  SU 35         S nAg S nAff = f)NFr   r   r   r   z/[LLMonitor] An error occurred in on_llm_error: r   r   s         r,   on_llm_error%LLMonitorCallbackHandler.on_llm_error  s     ""e+
	P6{4Ac-0t"%e*y7K7K7MN}}    	PLLJ1#NOO	Pr  )	__api_url__app_id__has_valid_config__llmonitor_version__track_event	__verbose)NNF)!r:   r;   r<   r=   r>   r?   __annotations__r   r   r-   r   r   r   r	   r   r   r   r   r   r   r   r   r   r
   r   r   r   BaseExceptionr   r  r  r@   __classcell__)r   s   @r,   r   r      s[   6 NMO $($(	<c4i < sDy!< 	<
 
< <H ,0'+046QcN6Q c6Q
 6Q T4Z(6Q DItO$6Q S#X,-6Q 6Q 
6Q| ,0'+047WcN7W tK()7W
 7W T4Z(7W DItO$7W S#X,-7W 7W 
7W| ,0&N&N 	&N
 T4Z(&N &N 
&N\ ,0'+04 QcN Q  Q
  Q T4Z( Q DItO$ Q S#X,- Q  Q 
 QN ,0'+OO 	O
 T4Z(O DItO$O O 
O< ,0'+040RcN0R S#X0R
 0R T4Z(0R DItO$0R S#X,-0R 0R 
0Rn ,0Pc3hP 	P
 T4Z(P P 
P: ,0SS 	S
 T4Z(S S 
S> ,0SS 	S
 T4Z(S S 
S: ,0RR 	R
 T4Z(R R 
R6 ,0QQ 	Q
 T4Z(Q Q 
Q6 ,0PP 	P
 T4Z(P P 
P Pr/   r   rB   r(   )1importlib.metadatar   loggingr   r   r   contextvarsr   typingr   r   r   r   r   uuidr	   r   langchain_core.agentsr
   r   langchain_core.callbacksr   langchain_core.messagesr   langchain_core.outputsr   packaging.versionr   	getLoggerr:   r   r   r?   r   r   r   r"   rB   rJ   rW   rH   ri   rn   rr   rt   r   r   r   __all__r1   r/   r,   <module>r     s     	   " / /   : 8 / , #			8	$-eCI&'
DAE#t),-.>M $! !
3c 
3s 
36H 
3
C 
E$sCx.$s)S"@A 
!C !C !8"d "s "6
3 3 ,c ,c ,
{ 
tCH~ 
@tK'8#'=!> @4SRUXCW @oP2 oPd &z
2r/   