
    h                         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             Z G d	 d
e	      Zy)zQ
AgentOps integration for LiteLLM - Provides OpenTelemetry tracing for LLM calls
    N)	dataclass)OptionalDictAny)OpenTelemetryOpenTelemetryConfig)_get_httpx_clientc                   v    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	        Zy)
AgentOpsConfig%https://otlp.agentops.cloud/v1/tracesendpointNapi_keyservice_namedeployment_environment%https://api.agentops.ai/v3/auth/tokenauth_endpointc           	           | dt        j                  d      t        j                  dd      t        j                  dd      d      S )	Nr   AGENTOPS_API_KEYAGENTOPS_SERVICE_NAMEagentopsAGENTOPS_ENVIRONMENT
productionr   )r   r   r   r   r   )osgetenv)clss    b/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/integrations/agentops/agentops.pyfrom_envzAgentOpsConfig.from_env   s@    <II01#:JG#%99-C\#RA
 	
    )__name__
__module____qualname__r   str__annotations__r   r   r   r   r   classmethodr    r   r   r   r   
   sS    ;Hc;!GXc]!"&L(3-&,0HSM0@M3@
 
r   r   c                   P     e Zd ZdZ	 ddee   f fdZdededeee	f   fdZ
 x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?"}],
        )
        ```
    configc                    |t         j                         }d }d }|j                  rI	 | j                  |j                  |j                        }|j                  d      }|j                  d      }|rd| nd }t        d|j                  |      }t        | )  |d       |j                  xs d|j                  xs d	dd
}|r||d<   || _        y # t        $ r Y nw xY w)Ntoken
project_idzAuthorization=Bearer 	otlp_http)exporterr   headersr   )r(   callback_namelitellmr   )zservice.namezdeployment.environmentztelemetry.sdk.namez
project.id)r   r   r   _fetch_auth_tokenr   get	Exceptionr   r   super__init__r   r   resource_attributes)	selfr(   	jwt_tokenr+   responser.   otel_configresource_attrs	__class__s	           r   r5   zAgentOps.__init__,   s     >#,,.F 	
>>11&..&BVBVW$LL1	%\\,7
 :C))5) __
 	$ 	 	
 #//<9&,&C&C&S|",
 +5N<(#1 7  s   AC 	C C r   r   returnc                    ddd}t               }	 |j                  ||d|id      }|j                  dk7  rt        d|j                         |j                         |j                          S # |j                          w xY 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_coder3   textrB   close)r7   r   r   r.   clientr9   s         r   r1   zAgentOps._fetch_auth_tokenX   s     /&

 #$	{{!)	 # H ##s*">x}}o NOO==?LLNFLLNs   AA/ /B)N)r   r    r!   __doc__r   r   r5   r"   r   r   r1   __classcell__)r<   s   @r   r'   r'      sB    " ,0*2(*2X S T#s(^ r   r'   )rJ   r   dataclassesr   typingr   r   r   "litellm.integrations.opentelemetryr   r   &litellm.llms.custom_httpx.http_handlerr	   r   r'   r%   r   r   <module>rP      sC    
 ! & & Q D

 
 
"Z} Zr   