o
    ưi5                     @   s  d Z ddlZddlmZmZm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 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+ ddl,m-Z. e Z/e% Z0e! Z1e+ Z2e) Z3e Z4de5de5fddZ6e.							d+de7dedee7 dee7 d ee7 d!ee7 d"ee d#ee8 d$ee fd%d&Z9			d,de7d'e7dee7 dee7 d ee7 d(ee7 fd)d*Z:dS )-z.Abstraction function for OpenAI's realtime API    N)AnyOptionalcast))REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES)get_llm_provider)BaseRealtimeConfig)BaseLLMHTTPHandler)get_secret_str)RealtimeQueryParams)GenericLiteLLMParams)LlmProviders)ProviderConfigManager   )get_litellm_params)Logging)AzureOpenAIRealtime)BedrockRealtime)get_shared_realtime_ssl_context)OpenAIRealtime)VertexAIRealtimeConfig)
VertexBase)XAIRealtime)clientkwargsreturnc                 C   sD   i |  dpi }|  dpi  dp|  dpg }|r ||d< |S )zbBuild the litellm_metadata dict for guardrail checking (internal only, not forwarded to provider).litellm_metadatametadata
guardrails)get)r   r   r    r   P/home/app/Keep/.python/lib/python3.10/site-packages/litellm/realtime_api/main.py_build_litellm_metadata$   s
   "r!   model	websocketapi_baseapi_keyapi_versionazure_ad_tokenr   timeoutquery_paramsc	           '         s  t tt |	d}
t tt |	d}|
du ri }
|dur$|
| |	d}|	dd}td;i |	}td;i |	}t| ||d\} }}}|durTi |d| i}|j| |i ||d d}|t	j
 v rptj| t	|d	}|durtj| ||||||||
|	d
t|	dI dH  dS |dkr|p|jptjptd}|ptjptjptd}|p|jpd}|	dp|dptjdpd}tj| ||||dd||||	d
t|	dI dH  dS |dkr|p|jptjpd}|ptjptjptd}tj| ||||d|||	d
t|	d
I dH  dS |dkr|	d}|	d}|	d}|	d}|	d}|	d}|	d}|	d }|	d!}|	d"}|	d#}tjd;i d| d$|d%|d&|p`|d'|p|d(|d|d|d|d|d|d|d|d |d!|d"|d#|I dH  dS d(|d|d|d|d|d|d|d|d |d!|d"|d#|I dH  dS |d)kr|p|jptd*pd+}|ptjptd,}tj| ||||d|||	d
t|	d
I dH  dS |d-krk|	d.p	|	d/p	td0} |	d1p|	d2ptjptd3}!|	d4p1|	d5p1tjp1td6}"tj |"| d7}#tj!| |!d-d8I dH \}$}%t"|$|%|#d9}&tj| |||&|pX|jd|||
|	d
t|	dI dH  dS t#d:|  )<zT
    Private function to handle the realtime API call.

    For PROXY use only.
    headersextra_headersNlitellm_logging_objuser)r"   r$   r%   r"   )r"   r-   Zoptional_paramslitellm_paramscustom_llm_provider)r"   provideruser_api_key_dict)r"   r#   logging_objprovider_configr$   r%   r   r(   r*   r1   r   azureZAZURE_API_BASEZAZURE_API_KEY2024-10-01-previewrealtime_protocolZLITELLM_AZURE_REALTIME_PROTOCOLbeta)r"   r#   r$   r%   r&   r'   r   r(   r2   r6   r1   r   openaihttps://api.openai.com/ZOPENAI_API_KEY)
r"   r#   r2   r$   r%   r   r(   r)   r1   r   Zbedrockaws_region_nameaws_access_key_idaws_secret_access_keyaws_session_tokenaws_role_nameaws_session_nameaws_profile_nameaws_web_identity_tokenaws_sts_endpointaws_bedrock_runtime_endpointaws_external_idr#   r2   r$   r%   r(   xaiZXAI_API_BASEhttps://api.x.ai/v1ZXAI_API_KEY	vertex_aivertex_credentialsZvertex_ai_credentialsZVERTEXAI_CREDENTIALSvertex_projectZvertex_ai_projectVERTEXAI_PROJECTvertex_locationZvertex_ai_locationVERTEXAI_LOCATIONZvertex_regionr"   credentialsZ
project_idr/   access_tokenprojectlocationUnsupported model: r   )$r   r   dictr   updater   r   r   Zupdate_environment_variablesr   _member_map_valuesr   Zget_provider_realtime_configbase_llm_http_handlerZasync_realtimer!   r$   litellmr	   r%   Z
openai_keyr&   osenvironazure_realtimeopenai_realtimebedrock_realtimexai_realtimerI   rK   vertex_llm_baseget_vertex_region_ensure_access_token_asyncr   
ValueError)'r"   r#   r$   r%   r&   r'   r   r(   r)   r   r*   r+   r,   r-   r.   Zlitellm_params_dictZ_custom_llm_providerZdynamic_api_keyZdynamic_api_baser3   r6   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rH   rI   rK   resolved_locationrQ   resolved_projectvertex_realtime_configr   r   r    
_arealtime-   s  


















	
	



rh   r/   r6   c              	      s  ddl }d}|dkrtj|pd| |pd|d}n|dkr)tj|p"dd	| id
}nz|dkr9tj|p2dd	| id
}nj|dkrtjpCtd}tj	|| d}	tj
dtjpTtdddI dH \}
}t|
||	d}|j|| d}t }|ji | dd}|j||t|d4 I dH  	 W d  I dH  dS 1 I dH sw   Y  ntd|  t }|j|d|it|d4 I dH  	 W d  I dH  dS 1 I dH sw   Y  dS )a-  
    Health check for realtime API - tries connection to the realtime API websocket

    Args:
        model: str - model name
        api_base: str - api base
        api_version: Optional[str] - api version
        api_key: str - api key
        custom_llm_provider: str - custom llm provider
        realtime_protocol: Optional[str] - protocol version ("GA"/"v1" for GA path, "beta"/None for beta path)

    Returns:
        bool - True if connection is successful, False otherwise
    Raises:
        Exception - if the connection is not successful
    r   Nr4    r5   )r$   r"   r&   r6   r8   r9   r"   )r$   r)   rE   rF   rG   rL   rM   rJ   rN   rP   )r$   r"   )r*   r"   r%   )Zadditional_headersmax_sizesslTrT   zapi-key)
websocketsr]   Z_construct_urlr^   r`   rZ   rK   r	   ra   rb   rc   rI   r   Zget_complete_urlr   Zvalidate_environmentconnectr   rd   )r"   r/   r%   r$   r&   r6   rl   urlrK   re   rQ   rf   rg   ssl_contextr*   r   r   r    _realtime_health_check$  sr   .0rp   )NNNNNNN)NNN);__doc__r[   typingr   r   r   rZ   Zlitellm.constantsr   Z1litellm.litellm_core_utils.get_llm_provider_logicr   Z-litellm.llms.base_llm.realtime.transformationr   Z*litellm.llms.custom_httpx.llm_http_handlerr   Zlitellm.secret_managers.mainr	   Zlitellm.types.realtimer
   Zlitellm.types.routerr   Zlitellm.types.utilsr   Zlitellm.utilsr   Z%litellm_core_utils.get_litellm_paramsr   Z"litellm_core_utils.litellm_loggingr   ZLiteLLMLoggingZllms.azure.realtime.handlerr   Zllms.bedrock.realtime.handlerr   Zllms.custom_httpx.http_handlerr   Zllms.openai.realtime.handlerr   Z&llms.vertex_ai.realtime.transformationr   Zllms.vertex_ai.vertex_llm_baser   Zllms.xai.realtime.handlerr   utilsr   Zwrapper_clientr]   r^   r_   r`   ra   rY   rU   r!   strfloatrh   rp   r   r   r   r    <module>   s    		 {