
    j
                     r    d Z ddlmZmZmZ ddlmZ ddlmZ	 ddl
mZ ddlmZ d	ed
efdZ G d de      Zy)z
This file contains the calling Azure OpenAI's `/openai/realtime` endpoint.

This requires websockets, and is currently only supported on LiteLLM Proxy.
    )AnyOptionalcast))REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES   )Logging)RealTimeStreaming   )AzureChatCompletion	client_ws
backend_wsc                    K   dd l }	 	 |j                          d {   }| j                  |       d {    27 7 # |j                  j                  $ r Y y w xY ww)Nr   )
websocketsrecv	send_text
exceptionsConnectionClosed)r   r   r   messages       |/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/litellm/llms/azure/realtime/handler.pyforward_messagesr      s]     &OO--G%%g... -.  11 s4   A? ;? =? ? AAAAc                       e Zd ZdedededefdZ	 	 	 	 	 	 ddedededee   d	ee   dee   d
ee   dee   dee   fdZ	y)AzureOpenAIRealtimeapi_basemodelapi_versionreturnc                 :    |j                  dd      }| d| d| S )z
        Example output:
        "wss://my-endpoint-sweden-berri992.openai.azure.com/openai/realtime?api-version=2024-10-01-preview&deployment=gpt-4o-realtime-preview";

        zhttps://zwss://z/openai/realtime?api-version=z&deployment=)replace)selfr   r   r   s       r   _construct_urlz"AzureOpenAIRealtime._construct_url   s0     ##J9j5k],ugV	
    N	websocketlogging_objapi_keyazure_ad_tokenclienttimeoutc
                 4  K   dd l }
ddlm} |t        d      |t        d      | j	                  |||      }	 |
j                  |d|it              4 d {   }t        |t        ||      |      }|j                          d {    d d d       d {    y 7 D7 7 	# 1 d {  7  sw Y   y xY w# |
j                  j                  $ r9}|j                  |j                  t        |             d {  7   Y d }~y d }~wt        $ r Y y w xY ww)Nr   )ClientConnectionz+api_base is required for Azure OpenAI callsz.api_version is required for Azure OpenAI callszapi-key)extra_headersmax_size)codereason)r   websockets.asyncio.clientr)   
ValueErrorr    connectr   r	   r   bidirectional_forwardr   InvalidStatusCodeclosestatus_codestr	Exception)r   r   r"   r#   r   r$   r   r%   r&   r'   r   r)   urlr   realtime_streaminges                   r   async_realtimez"AzureOpenAIRealtime.async_realtime)   s     	>JKKMNN!!(E;?	!))w C *   %6t$4jA;&" )>>@@@   A    $$66 	E//q}}SV/DDD 		s   8DB9 BB9 +B$B 	B$B9 B"B9 DB9  B$"B9 $B6*B-+B62B9 5D6B9 9D)D;C><DDDDDD)NNNNNN)
__name__
__module____qualname__r5   r    r   LiteLLMLoggingr   floatr:    r!   r   r   r      s    	
s 	
3 	
S 	
S 	
  #'!%%)(, $#'&& & $	&
 3-& #& c]& !& & %&r!   r   N)__doc__typingr   r   r   litellm.constantsr   "litellm_core_utils.litellm_loggingr   r>   %litellm_core_utils.realtime_streamingr	   azurer   r   r   r@   r!   r   <module>rG      s>    ' & G L G '
c s 2- 2r!   