o
    BDi                     @   s   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 zd dlZd dlmZmZ W n ey[   edw G dd deZdd Zdd ZdS )    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                   @   s0   e Zd ZdZde Zd	ddZedd ZdS )
HuggingfaceHubIntegrationhuggingface_hubzauto.ai.Tc                 C   s
   || _ d S N)include_prompts)selfr    r   `/var/www/Datamplify/venv/lib/python3.10/site-packages/sentry_sdk/integrations/huggingface_hub.py__init__   s   
z"HuggingfaceHubIntegration.__init__c                   C   s   t tjjjjtjjj_d S r   )_wrap_text_generationr   	inference_clientInferenceClienttext_generationr   r   r   r   
setup_once"   s   
z$HuggingfaceHubIntegration.setup_onceN)T)__name__
__module____qualname__
identifieroriginr   staticmethodr   r   r   r   r   r      s    

r   c                 C   s0   t | t jdddd\}}tj||d d S )Nr   F)typehandled)client_options	mechanism)hint)r   
sentry_sdk
get_clientoptionscapture_event)exceventr*   r   r   r   _capture_exception,   s   
r1   c                    s   t   fdd}|S )Nc               
      s  t  t  d u r| i |S d|v r|d }n#t| dkr7| d |d< |d }| d f| dd   } n| i |S |d}|d}t jtjj	dtj
d  z	| i |W n tyz } zt| d d d  |d d }~ww t  t r jrttj| ttj| ttj| ttrt r jrttjg d d d  W  d    S ttrt rш jrttjjg jd urjjdkrtjjd	 d d d  W  d    S ttstd
d d d d  W  d    S |ddr) fdd}| W  d    S  fdd}| W  d    S 1 s?w   Y  d S )Nprompt      r   modelstreamzText Generation)opnamer$   total_tokensunknown_responseTdetailsFc                  3   s    t  U d} g }D ]&}t|drt|jdr||jj t|dr.t|jdr.|jj} |V  qt|dkrHt rH j	rHt
tjd| | dkrRt| d W d    n1 s\w   Y  d d d  d S )Nr   tokentextr<   generated_tokens r9   )r   hasattrr=   appendr>   r<   r?   lenr   r   r   r   AI_RESPONSESjoinr   __exit__)tokens_useddata_bufxintegrationresspanr   r   new_details_iterator~   s2   zP_wrap_text_generation.<locals>.new_text_generation.<locals>.new_details_iteratorc                  3   s    g } t  8 D ]}t|tr| | |V  q	t| dkr/t r/ jr/ttj	d
|  d d d  W d    d S 1 sAw   Y  d S )Nr   r@   )r   
isinstancestrrB   rC   r   r   r   r   rD   rE   rF   )rH   srJ   r   r   new_iterator   s"   

"zH_wrap_text_generation.<locals>.new_text_generation.<locals>.new_iterator) r+   r,   get_integrationr   rC   get
start_spanr   OP'HUGGINGFACE_HUB_CHAT_COMPLETIONS_CREATEr$   	__enter__	Exceptionr1   rF   r   r   r   r   r   AI_INPUT_MESSAGESAI_MODEL_IDAI_STREAMINGrO   rP   rD   r   generated_textr<   r?   r   r	   )argskwargsr2   r5   	streamingerN   rR   frJ   r   new_text_generation8   s   




#B&z2_wrap_text_generation.<locals>.new_text_generationr   )rc   rd   r   rb   r   r   6   s   vr   )	functoolsr   r+   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   r1   r   r   r   r   r   <module>   s&    
