o
    BDiu$                     @   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 er6d 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rmd dlm Z  W n e!yy   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Z.ej/ej0ej1dZ2ej3ej4ej5ej6ej7dZ8G dd deZ9dd Z:dd Z;dd Z<d	S )     wraps)consts)record_token_usage)SPANDATA)set_data_normalized)TYPE_CHECKING)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seedfrequency_penaltypresence_penaltyraw_prompting)toolspreamble)generation_idis_search_requiredfinish_reason)	citations	documentssearch_queriessearch_results
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.   W/var/www/Datamplify/venv/lib/python3.10/site-packages/sentry_sdk/integrations/cohere.py__init__I   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   chat_wrap_embedr   embedchat_streamr.   r.   r.   r/   
setup_onceM   s   zCohereIntegration.setup_onceN)T)__name__
__module____qualname__
identifieroriginr0   staticmethodr7   r.   r.   r.   r/   r*   E   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eventrB   r.   r.   r/   _capture_exceptionU   s   
rI   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)prompt_tokenscompletion_tokenstokenswarnings)hasattrr   r   AI_RESPONSESrJ   COLLECTED_PII_CHAT_RESP_ATTRSgetattrCOLLECTED_CHAT_RESP_ATTRSrK   r   rL   input_tokensoutput_tokensrO   AI_WARNINGSrP   )spanresinclude_piipii_attrattrr.   r.   r/   collect_chat_response_fieldsb   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 r_   rc   content)rT   lowerxr.   r.   r/   <lambda>   s   
z._wrap_chat.<locals>.new_chat.<locals>.<lambda>chat_historyuserre   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r[   )r   
isinstancer   r   responser   r,   __exit__rh   )r^   integrationold_iteratorrY   r.   r/   new_iterator   s    z2_wrap_chat.<locals>.new_chat.<locals>.new_iteratorrm   unknown_responseT)rC   rD   get_integrationr*   rn   getstr
start_spanr   OPCOHERE_CHAT_COMPLETIONS_CREATEr<   	__enter__	ExceptionrI   rp   r   r   r,   r   r   AI_INPUT_MESSAGESlistmapCOLLECTED_PII_CHAT_PARAMSitemsCOLLECTED_CHAT_PARAMSAI_STREAMINGr   )argskwargsr_   rZ   er   vrs   r^   fr1   )rq   rr   rY   r/   new_chat   sz   


	
/$z_wrap_chat.<locals>.new_chatr   )r   r1   r   r.   r   r/   r2   _   s   "Vr2   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 Creationr`   textsr   r   rK   rL   rV   )rM   total_tokens)rC   rD   ru   r*   rx   r   ry   COHERE_EMBEDDINGS_CREATEr<   r   r,   rn   rw   r   r   AI_TEXTSr~   lenr}   AI_MODEL_IDr|   rI   rQ   rK   rL   r   rV   )r   r   rq   rY   rZ   r   r   r.   r/   	new_embed   s\   
$z_wrap_embed.<locals>.new_embedr   )r   r   r.   r   r/   r4      s   ,r4   )=	functoolsr   rC   r   sentry_sdk.ai.monitoringr   sentry_sdk.constsr   sentry_sdk.ai.utilsr   typingr   r	   r
   r   sentry_sdk.tracingr   sentry_sdk.scoper   sentry_sdk.integrationsr   r   sentry_sdk.utilsr   r   cohere.clientr   cohere.base_clientr   r+   r   r   r   ImportErrorr   r   r   AI_TOP_KAI_TOP_PAI_SEEDAI_FREQUENCY_PENALTYAI_PRESENCE_PENALTYAI_RAW_PROMPTINGr   AI_TOOLSAI_PREAMBLEr   AI_GENERATION_IDAI_SEARCH_REQUIREDAI_FINISH_REASONrU   AI_CITATIONSAI_DOCUMENTSAI_SEARCH_QUERIESAI_SEARCH_RESULTSAI_TOOL_CALLSrS   r*   rI   r2   r4   r.   r.   r.   r/   <module>   sj    	
