
    lh=                         d dl mZ d dlm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 d dlZd dlmZ d d	lmZmZ d d
lmZmZ 	 d dlZd dlmZmZ  G d de      Zd Zd Zy# e$ r	  ed      w xY w)    wraps)consts)record_token_usage)set_data_normalized)SPANDATA)AnyIterableCallableN)should_send_default_pii)DidNotEnableIntegration)capture_internal_exceptionsevent_from_exception)ChatCompletionStreamOutputTextGenerationOutputzHuggingface not installedc                   2    e Zd ZdZde ZddZed        Zy)HuggingfaceHubIntegrationhuggingface_hubzauto.ai.c                     || _         y N)include_prompts)selfr   s     y/var/www/pru.catia.catastroantioquia-mas.com/tasa/lib/python3.12/site-packages/sentry_sdk/integrations/huggingface_hub.py__init__z"HuggingfaceHubIntegration.__init__   s
    .    c                      t        t        j                  j                  j                  j
                        t        j                  j                  j                  _        y r   )_wrap_text_generationr   	inference_clientInferenceClienttext_generation r   r   
setup_oncez$HuggingfaceHubIntegration.setup_once"   sA     "))11AAQQ 	!!))99Ir   N)T)__name__
__module____qualname__
identifieroriginr   staticmethodr$   r#   r   r   r   r      s,    "J
|$F/ 
 
r   r   c                     t        | t        j                         j                  ddd      \  }}t        j                  ||       y )Nr   F)typehandled)client_options	mechanism)hint)r   
sentry_sdk
get_clientoptionscapture_event)exceventr0   s      r   _capture_exceptionr7   ,   s?    &!,,.66,?KE4
 U.r   c                 .     t                fd       }|S )Nc                  H  	
 t        j                         j                  t               | i |S d|v r|d   }n0t	        |       dk\  r| d   |d<   |d   }| d   f| dd  z   } n | i |S |j                  d      }|j                  d      }t        j                  t        j                  j                  dt        j                        

j                          	  | i |	t               5  t!               r'j"                  rt%        
t&        j(                  |       t%        
t&        j*                  |       t%        
t&        j,                  |       t/        	t0              rPt!               r(j"                  rt%        
t&        j2                  	g       
j                  d d d        	cd d d        S t/        	t4              rt!               r2j"                  r&t%        
t&        j2                  	j6                  g       	j8                  :	j8                  j:                  dkD  r!t=        
	j8                  j:                  	       
j                  d d d        	cd d d        S t/        	t>              s+t%        
d
d       
j                  d d d        	cd d d        S |j                  dd      r	
fd} |       cd d d        S 	
fd} |       cd d d        S # t        $ r&}t        |       
j                  d d d        |d d }~ww xY w# 1 sw Y   y xY w)Nprompt      r   modelstreamzText Generation)opnamer)   total_tokensunknown_responseTdetailsFc               3   N  K   t               5  d} g }D ]  }t        |d      r;t        |j                  d      r%|j                  |j                  j                         t        |d      r,t        |j
                  d      r|j
                  j                  } |  t        |      dkD  r@t               r6j                  r*t        t        j                  dj                  |             | dkD  rt        |        d d d        j                  d d d        y # 1 sw Y   xY ww)Nr   tokentextrD   generated_tokens rA   )r   hasattrrF   appendrG   rD   rH   lenr   r   r   r   AI_RESPONSESjoinr   __exit__)tokens_useddata_bufxintegrationresspans      r   new_details_iteratorzP_wrap_text_generation.<locals>.new_text_generation.<locals>.new_details_iterator   s     46 &'.0!$ $A&q'2wqww7O ( =&q)4 !		+=: /0ii.H.H"#G$  MA- 7 9 + ; ;/ $h&;&;RWWX=N '?. $-8)0 MM$d31 s   D%C/D=D%D"D%c               3   j  K   g } t               5  D ]'  }t        |t              r| j                  |       | ) t	        |       dkD  r@t               r6j                  r*t        t        j                  dj                  |              j                  d d d        d d d        y # 1 sw Y   y xY ww)Nr   rI   )r   
isinstancestrrK   rL   r   r   r   r   rM   rN   rO   )rQ   srS   rT   rU   s     r   new_iteratorzH_wrap_text_generation.<locals>.new_text_generation.<locals>.new_iterator   s     *,H46 8!$ $A)!S1 ( 2"#G$
  MA- 7 9 + ; ;/ $h&;&;RWWX=N dD$78 8 8s   B3BB'	B3'B0,B3) r1   r2   get_integrationr   rL   get
start_spanr   OP'HUGGINGFACE_HUB_CHAT_COMPLETIONS_CREATEr)   	__enter__	Exceptionr7   rO   r   r   r   r   r   AI_INPUT_MESSAGESAI_MODEL_IDAI_STREAMINGrX   rY   rM   r   generated_textrD   rH   r   r
   )argskwargsr:   r=   	streamingerV   r[   rS   rT   rU   fs           @@@r   new_text_generationz2_wrap_text_generation.<locals>.new_text_generation8   s    !++-==>WXd%f%%vH%FY!^#AwF8H%FG:QR(D d%f%%

7#JJx(	$$yy@@",33

 		T$V$C )* Z	&&([-H-H#D(*D*DfMh&:&:EBh&;&;YG#s#*,1L1L' --
 dD$/Z	& Z	&" #34*,1L1L' --++,
 ;;*s{{/K/Ka/O&%([[%A%A dD$/=Z	& Z	&@ c8,#D*<dCdD$/IZ	& Z	&L zz)U+48 ,-IZ	& Z	&P8$ $~uZ	& Z	&  	q!MM$d+	
Z	& Z	&s>   !K& 3B>L;B&L+1L&LL&	L/!LLL!r   )rk   rl   s   ` r   r   r   6   s$    
1Xz& z&x r   )	functoolsr   r1   r   sentry_sdk.ai.monitoringr   sentry_sdk.ai.utilsr   sentry_sdk.constsr   typingr	   r
   r   sentry_sdk.scoper   sentry_sdk.integrationsr   r   sentry_sdk.utilsr   r   !huggingface_hub.inference._clientr   r   r   ImportErrorr   r7   r   r#   r   r   <module>rw      sh      7 3 & * *  4 =
4,P

 
$/A  4
2
334s   A" "A0