o
    i                     @   s   d dl Z 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 d d	lmZ d d
lmZ edi  defddZdeeef fddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  ZdS )!    N)GenerativeModel)config)unwrap)wrap)VertexAIAsyncStreamHandler)VertexAIStreamHandler)VertexAIIntegration)make_traced_stream)extract_provider_and_model_namevertexaireturnc                   C   s   t tddS )N__version__ )getattrr    r   r   [/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/vertexai/patch.pyget_version   s   r   c                   C   s   ddiS )Nr   z>=1.71.1r   r   r   r   r   _supported_versions   s   r   c                 C   s   t | ||||dS NF)_traced_generatefuncinstanceargskwargsr   r   r   traced_generate   s   r   c                    s   t | ||||dI d H S r   )_traced_agenerater   r   r   r   traced_agenerate!   s   r   c                 C   s   t | ||||jdS NT)r   _modelr   r   r   r   traced_send_message%   s   r    c                    s   t | ||||jdI d H S r   )r   r   r   r   r   r   traced_send_message_async)   s   r!   c                 C   sZ  t j}|dd}d }t|dd\}	}
|jd|jj| jf |	|
dd}t|dg d d  }zcz6| |i |}|rct|t	|||||||d	W W |j
sL|sb||d
< ||d< |j||||d |  S S W n tys   |jt    w W |j
sz|s||d
< ||d< |j||||d |  |S |j
s|s||d
< ||d< |j||||d |  w w NstreamF_model_name)r   model_name_attrz%s.%sT)providermodelsubmit_to_llmobshistory)is_chatr)   model_instancer   )r   r   response)r   _datadog_integrationgetr
   trace	__class____name__r   r	   r   errorllmobs_set_tagsfinish	Exceptionset_exc_infosysexc_infor   r   r   r   r+   r*   integrationr#   generationsprovider_name
model_namespanr)   r   r   r   r   -   sX   




r   c                    sb  t j}|dd}d }t|dd\}	}
|jd|jj| jf |	|
dd}t|dg d d  }zfz9| |i |I d H }|rgt|t	|||||||d	W W |j
sP|sf||d
< ||d< |j||||d |  S S W n tyw   |jt    w W |j
s~|s||d
< ||d< |j||||d |  |S |j
s|s||d
< ||d< |j||||d |  w w r"   )r   r-   r.   r
   r/   r0   r1   r   r	   r   r2   r3   r4   r5   r6   r7   r8   r9   r   r   r   r   P   sZ   




r   c                  C   s\   t tddrd S dt_ttjd} | t_tddt tddt tddt	 tdd	t
 d S )
N_datadog_patchFT)integration_configr   z2generative_models.GenerativeModel.generate_contentz8generative_models.GenerativeModel.generate_content_asyncz*generative_models.ChatSession.send_messagez0generative_models.ChatSession.send_message_async)r   r   r?   r   r   r-   r   r   r   r    r!   )r:   r   r   r   patchs   s   rA   c                   C   s\   t tddsd S dt_ttjjd ttjjd ttjjd ttjjd ttd d S )Nr?   Fgenerate_contentgenerate_content_asyncsend_messagesend_message_asyncr-   )r   r   r?   r   generative_modelsr   ChatSessiondelattrr   r   r   r   unpatch   s   rI   )r7   r   vertexai.generative_modelsr   ddtracer   $ddtrace.contrib.internal.trace_utilsr   r   (ddtrace.contrib.internal.vertexai._utilsr   r   ddtrace.llmobs._integrationsr   0ddtrace.llmobs._integrations.base_stream_handlerr	   )ddtrace.llmobs._integrations.google_utilsr
   _addstrr   dictr   r   r   r    r!   r   r   rA   rI   r   r   r   r   <module>   s,    ##