o
    i%                     @   s  d dl 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 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mZ zd dlmZ d dlm Z  d dl!m"Z"m#Z# eryd 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j0ej1dZ2ej3ej4ej5dZ6ej7ej8ej9ej:ej;dZ<G dd deZ=d'ddZ>d d!d"e?dd!fd#d$Z@d(d%d&ZAdS ))    Nwraps)consts)record_token_usage)SPANDATA)set_data_normalized)TYPE_CHECKING)set_span_errored)AnyCallableIterator)Span)should_send_default_pii)DidNotEnableIntegration)capture_internal_exceptionsevent_from_exceptionreraise)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                   @   s@   e Zd ZdZde Zddd deddfdd	Zedd
dZdS )CohereIntegrationcoherezauto.ai.Tselfinclude_promptsreturnNc                 C   s
   || _ d S )N)r/   )r.   r/    r1   [/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/sentry_sdk/integrations/cohere.py__init__L   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_streamr1   r1   r1   r2   
setup_onceO   s   zCohereIntegration.setup_once)T)r0   N)	__name__
__module____qualname__
identifieroriginboolr3   staticmethodr:   r1   r1   r1   r2   r,   H   s    
r,   excr
   r0   c                 C   s6   t   t| t jdddd\}}tj||d d S )Nr-   F)typehandled)client_options	mechanism)hint)r	   r   
sentry_sdk
get_clientoptionscapture_event)rB   eventrG   r1   r1   r2   _capture_exceptionV   s   
rM   fCallable[..., Any]r4   c                    s8   dddddt dd fdd td fdd}|S )Nspanr   resr   include_piir0   c                 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   AI_RESPONSESrS   COLLECTED_PII_CHAT_RESP_ATTRSgetattrCOLLECTED_CHAT_RESP_ATTRSrT   r   rU   rV   rW   rX   AI_WARNINGSrY   )rP   rQ   rR   pii_attrattrr1   r1   r2   collect_chat_response_fieldsb   s@   



z0_wrap_chat.<locals>.collect_chat_response_fieldsargsr
   kwargsc            	         s0  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 n8 tys } z,t }t  t| d d d  W d    n1 s`w   Y  t|  W Y d }~nd }~ww t  t r jrttjttdd |dg d|dg  t D ]\}}||v rt|||  qt D ]\}}||v rt|||  qttjd	 r|d 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 sw   Y  d S )Nmessagezcohere.client.Chatopnamer?   c                 S   s   t | dd t | dddS )Nrole re   ri   content)r]   lowerxr1   r1   r2   <lambda>   s   
z._wrap_chat.<locals>.new_chat.<locals>.<lambda>chat_historyuserrk   Fr0   Iterator[StreamedChatResponse]c                  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rR   )r   
isinstancer   r   responser   r/   __exit__rn   )rb   integrationold_iteratorrP   r1   r2   new_iterator   s    z2_wrap_chat.<locals>.new_chat.<locals>.new_iteratorrt   unknown_responseT)r0   rs   ) rH   rI   get_integrationr,   ru   getstr
start_spanr   OPCOHERE_CHAT_COMPLETIONS_CREATEr?   	__enter__	Exceptionsysexc_infor   rM   rw   r   r   r/   r   r   AI_INPUT_MESSAGESlistmapCOLLECTED_PII_CHAT_PARAMSitemsCOLLECTED_CHAT_PARAMSAI_STREAMINGr   )	rc   rd   re   rQ   er   r   vrz   rb   rN   r4   )rx   ry   rP   r2   new_chat   s   


	
-&z_wrap_chat.<locals>.new_chatrc   r
   rd   r
   r0   r
   )r@   r   )rN   r4   r   r1   r   r2   r5   a   s   
#Ur5   c                    s   t  d fdd}|S )Nrc   r
   rd   r0   c                     s  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 n1 ty } z%t }t  t| W d    n1 sw   Y  t|  W Y d }~n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 Creationrf   textsr   r   rT   rU   rV   )rV   total_tokens)rH   rI   r|   r,   r   r   r   COHERE_EMBEDDINGS_CREATEr?   r   r/   ru   r~   r   r   AI_TEXTSr   lenr   AI_MODEL_IDr   r   r   r   rM   r   rZ   rT   rU   r   rV   )rc   rd   rx   rP   rQ   r   r   rN   r1   r2   	new_embed   sb   

$z_wrap_embed.<locals>.new_embedr   r   )rN   r   r1   r   r2   r7      s   -r7   )rB   r
   r0   N)rN   rO   r0   rO   )Br   	functoolsr   rH   r   sentry_sdk.ai.monitoringr   sentry_sdk.constsr   sentry_sdk.ai.utilsr   typingr   sentry_sdk.tracing_utilsr	   r
   r   r   sentry_sdk.tracingr   sentry_sdk.scoper   sentry_sdk.integrationsr   r   sentry_sdk.utilsr   r   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_REASONr^   AI_CITATIONSAI_DOCUMENTSAI_SEARCH_QUERIESAI_SEARCH_RESULTSAI_TOOL_CALLSr\   r,   rM   r@   r5   r7   r1   r1   r1   r2   <module>   sn    	
}