
    g
                     R    d Z ddlmZmZ ddlmZ ddlmZ ddl	m
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   )Logging)RealTimeStreaming   )OpenAIChatCompletionc                   f    e Zd Z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   fdZ	y)OpenAIRealtimeapi_basemodelreturnc                 X    |j                  dd      }|j                  dd      }| d| S )z
        Example output:
        "BACKEND_WS_URL = "wss://localhost:8080/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01"";
        zhttps://zwss://zhttp://zws://z/v1/realtime?model=)replace)selfr   r   s      Y/var/www/openai/venv/lib/python3.12/site-packages/litellm/llms/openai/realtime/handler.py_construct_urlzOpenAIRealtime._construct_url   s:    
 ##J9##Iw7.ug66    N	websocketlogging_objapi_keyclienttimeoutc                   K   dd l }|t        d      |t        d      | j                  ||      }		 |j                  |	d| dd      4 d {   }
t	        ||
|      }|j                          d {    d d d       d {    y 7 :7 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        $ rx}	 |j                  d	d
t        |              d {  7   nD# t        $ r3}dt        |      v sdt        |      v rnt        d|       Y d }~nd }~ww xY wY d }~y Y d }~y d }~ww xY ww)Nr   z+api_base is required for Azure OpenAI callsz*api_key is required for Azure OpenAI callszBearer zrealtime=v1)AuthorizationzOpenAI-Beta)extra_headers)codereasoni  zInternal server error: zalready completedzwebsocket.closez*Unexpected error while closing WebSocket: )
websockets
ValueErrorr   connectr   bidirectional_forward
exceptionsInvalidStatusCodeclosestatus_codestr	ExceptionRuntimeError)r   r   r   r   r   r   r   r   r   url
backend_wsrealtime_streamingeclose_errors                 r   async_realtimezOpenAIRealtime.async_realtime   s     	JKK?IJJ!!(E2	!))'.wi%8#0 *   %6z;&" )>>@@@   A    $$66 	E//q}}SV/DDD 	oo(?Ax&H &      
&#k*::>OSVT ?  $D[MR  

 	s   1E;B' BB' !B6B7B;B' BB' E;B' BB' B$BB$ B' #E;$B' 'E8 )C4)C,*C4/E;4E8"D*#D&$D*)E3*	E&3)E!E3!E&&E3)
E;3E88E;)NNNN)
__name__
__module____qualname__r'   r   r   LiteLLMLoggingr   floatr/    r   r   r   r      s    7s 73 73 7 #'!% $#'11 1 $	1
 3-1 #1 1 %1r   r   N)__doc__typingr   r   "litellm_core_utils.litellm_loggingr   r3   %litellm_core_utils.realtime_streamingr   openair	   r   r5   r   r   <module>r;      s%    ! L G );) ;r   