o
    0 i$                     @   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 d dlmZ er<d dl
mZmZmZ d d	l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mZ d dlmZ d dlm Z m!Z! ersd dlm"Z" W n e#y   edw zd dlm$Z$ W n e#y   d dlm%Z$ Y nw ej&ej'ej(ej)ej*ej+ej,dZ-ej.ej/dZ0ej1ej2ej3dZ4ej5ej6ej7ej8ej9dZ:G dd deZ;dd Z<dd Z=dd  Z>d
S )!    wraps)consts)record_token_usage)SPANDATA)set_data_normalized)TYPE_CHECKING)set_span_errored)AnyCallableIterator)SpanN)should_send_default_pii)DidNotEnableIntegration)capture_internal_exceptionsevent_from_exception)Client)
BaseCohere)ChatStreamEndEventNonStreamedChatResponse)StreamedChatResponsezCohere not installed)StreamEndStreamedChatResponse)StreamedChatResponse_StreamEnd)modelkpseedZfrequency_penaltyZpresence_penaltyZraw_prompting)Ztoolspreamble)Zgeneration_idZis_search_requiredZfinish_reason)Z	citationsZ	documentsZsearch_queriesZsearch_resultsZ
tool_callsc                   @   s0   e Zd ZdZde Zd	ddZedd ZdS )
CohereIntegrationcoherezauto.ai.Tc                 C   s
   || _ d S )N)include_prompts)selfr!    r#   j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/integrations/cohere.py__init__K   s   
zCohereIntegration.__init__c                   C   s0   t tjddt_ttjt_t tjddt_d S )NF)	streamingT)
_wrap_chatr   Zchat_wrap_embedr   ZembedZchat_streamr#   r#   r#   r$   
setup_onceO   s   zCohereIntegration.setup_onceN)T)__name__
__module____qualname__
identifieroriginr%   staticmethodr)   r#   r#   r#   r$   r   G   s    

r   c                 C   s6   t   t| t jdddd\}}tj||d d S )Nr    F)typeZhandled)Zclient_optionsZ	mechanism)hint)r	   r   
sentry_sdk
get_clientoptionsZcapture_event)exceventr1   r#   r#   r$   _capture_exceptionW   s   
r7   c                    s$   dd  t  fdd}|S )Nc                 S   s   |r%t |drt| tj|jg tD ]}t ||r$t| d| t|| qtD ]}t ||r9t| d| t|| q't |drwt |jdrSt	| |jj
j|jj
jd nt |jdrft	| |jjj|jjjd t |jdryt| tj|jj d S d S d S )Ntextzai.metabilled_units)input_tokensoutput_tokenstokenswarnings)hasattrr   r   ZAI_RESPONSESr8   COLLECTED_PII_CHAT_RESP_ATTRSgetattrCOLLECTED_CHAT_RESP_ATTRSr9   r   r:   r;   r<   r=   ZAI_WARNINGSr>   )spanresinclude_piiZpii_attrattrr#   r#   r$   collect_chat_response_fieldsf   s@   



z0_wrap_chat.<locals>.collect_chat_response_fieldsc                     s  t  t  d u sd|vst|dts| i |S |d}t jtj	j
dtjd  z	| i |}W n tyU } zt| d d d  |d d }~ww t  t r jrttjttdd |dg d|dg  t D ]\}}||v rt|||  q|t D ]\}}||v rt|||  qttjd	 r| fd
d}| W  d    S t|tr؈|t o͈ jd d d d  ntdd d d d  |W  d    S 1 sw   Y  d S )Nmessagezcohere.client.Chatopnamer.   c                 S   s   t | dd t | dddS )Nrole rH   rL   content)rA   lowerxr#   r#   r$   <lambda>   s   
z._wrap_chat.<locals>.new_chat.<locals>.<lambda>Zchat_historyuserrN   Fc                  3   sv    t  & D ]} t| tst| tr | jt ojd | V  qW d    n1 s-w   Y  d d d  d S )NrE   )r   
isinstancer   r   responser   r!   __exit__rQ   )rG   integrationold_iteratorrC   r#   r$   new_iterator   s    z2_wrap_chat.<locals>.new_chat.<locals>.new_iteratorrU   Zunknown_responseT)r2   r3   get_integrationr   rV   getstr
start_spanr   OPZCOHERE_CHAT_COMPLETIONS_CREATEr.   	__enter__	Exceptionr7   rX   r   r   r!   r   r   AI_INPUT_MESSAGESlistmapCOLLECTED_PII_CHAT_PARAMSitemsCOLLECTED_CHAT_PARAMSZAI_STREAMINGr   )argskwargsrH   rD   er   vr[   rG   fr&   )rY   rZ   rC   r$   new_chat   sz   


	
/$z_wrap_chat.<locals>.new_chatr   )rn   r&   ro   r#   rm   r$   r'   c   s   "Vr'   c                    s   t   fdd}|S )Nc                     sn  t  t}|d u r | i |S t jtjjdtjd}d|v r[t	 r[|j
r[t|d tr:t|tj|d g n!t|d tr[t|d dkr[t|d d tr[t|tj|d  d|v rht|tj|d  z	 | i |}W n ty } zt| |d d }~ww t|drt|jdrt|jjdrt||jjj|jjjd	 |W  d    S 1 sw   Y  d S )
NzCohere Embedding CreationrI   Ztextsr   r   r9   r:   r;   )r;   Ztotal_tokens)r2   r3   r\   r   r_   r   r`   ZCOHERE_EMBEDDINGS_CREATEr.   r   r!   rV   r^   r   r   ZAI_TEXTSrd   lenrc   AI_MODEL_IDrb   r7   r?   r9   r:   r   r;   )ri   rj   rY   rC   rD   rk   rn   r#   r$   	new_embed   s\   
$z_wrap_embed.<locals>.new_embedr   )rn   rs   r#   rr   r$   r(      s   ,r(   )?	functoolsr   r2   r   Zsentry_sdk.ai.monitoringr   Zsentry_sdk.constsr   Zsentry_sdk.ai.utilsr   typingr   Zsentry_sdk.tracing_utilsr	   r
   r   r   Zsentry_sdk.tracingr   Zsentry_sdk.scoper   Zsentry_sdk.integrationsr   r   Zsentry_sdk.utilsr   r   Zcohere.clientr   Zcohere.base_clientr   r    r   r   r   ImportErrorr   r   rq   ZAI_TOP_KZAI_TOP_PZAI_SEEDZAI_FREQUENCY_PENALTYZAI_PRESENCE_PENALTYZAI_RAW_PROMPTINGrh   ZAI_TOOLSZAI_PREAMBLErf   ZAI_GENERATION_IDZAI_SEARCH_REQUIREDZAI_FINISH_REASONrB   ZAI_CITATIONSZAI_DOCUMENTSZAI_SEARCH_QUERIESZAI_SEARCH_RESULTSZAI_TOOL_CALLSr@   r   r7   r'   r(   r#   r#   r#   r$   <module>   sl    	