o
    ưi                     @   sn   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	m
Z
 ddlmZ eG dd dZG d	d
 d
e	ZdS )zQ
AgentOps integration for LiteLLM - Provides OpenTelemetry tracing for LLM calls
    N)	dataclass)OptionalDictAny)OpenTelemetryOpenTelemetryConfig)_get_httpx_clientc                   @   sb   e Zd ZU dZeed< dZee ed< dZee ed< dZ	ee ed< dZ
eed< ed	d
 ZdS )AgentOpsConfig%https://otlp.agentops.cloud/v1/tracesendpointNapi_keyservice_namedeployment_environment%https://api.agentops.ai/v3/auth/tokenauth_endpointc                 C   s(   | dt dt ddt ddddS )	Nr
   ZAGENTOPS_API_KEYZAGENTOPS_SERVICE_NAMEagentopsZAGENTOPS_ENVIRONMENT
productionr   )r   r   r   r   r   )osgetenv)cls r   ]/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/agentops/agentops.pyfrom_env   s   

zAgentOpsConfig.from_env)__name__
__module____qualname__r   str__annotations__r   r   r   r   r   classmethodr   r   r   r   r   r	   
   s   
 r	   c                       sL   e Zd ZdZ	ddee f fddZdededeee	f fd	d
Z
  ZS )AgentOpsa`  
    AgentOps integration - built on top of OpenTelemetry

    Example usage:
        ```python
        import litellm
        
        litellm.success_callback = ["agentops"]

        response = litellm.completion(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": "Hello, how are you?"}],
        )
        ```
    Nconfigc                    s   |d u rt  }d }d }|jr-z| |j|j}|d}|d}W n	 ty,   Y nw |r4d| nd }td|j|d}t	 j
|dd |jpJd|jpNd	dd
}|rX||d< || _d S )Ntoken
project_idzAuthorization=Bearer Z	otlp_http)Zexporterr   headersr   )r    Zcallback_nameZlitellmr   )zservice.namezdeployment.environmentztelemetry.sdk.namez
project.id)r	   r   r   _fetch_auth_tokenr   get	Exceptionr   r   super__init__r   r   Zresource_attributes)selfr    Z	jwt_tokenr"   responser#   Zotel_configZresource_attrs	__class__r   r   r(   ,   s:   

zAgentOps.__init__r   r   returnc                 C   s^   ddd}t  }z!|j||d|idd}|jdkr!td|j | W |  S |  w )	a  
        Fetch JWT authentication token from AgentOps API
        
        Args:
            api_key: AgentOps API key
            auth_endpoint: Authentication endpoint
            
        Returns:
            Dict containing JWT token and project ID
        zapplication/jsonz
keep-alive)zContent-Type
Connectionr   
   )urlr#   jsontimeout   zFailed to fetch auth token: )r   poststatus_coder&   textr1   close)r)   r   r   r#   clientr*   r   r   r   r$   X   s   
zAgentOps._fetch_auth_token)N)r   r   r   __doc__r   r	   r(   r   r   r   r$   __classcell__r   r   r+   r   r      s    &,r   )r9   r   dataclassesr   typingr   r   r   Z"litellm.integrations.opentelemetryr   r   Z&litellm.llms.custom_httpx.http_handlerr   r	   r   r   r   r   r   <module>   s    