
    h                     b    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  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   )Logging)RealTimeStreaming   )OpenAIChatCompletion)RealtimeQueryParamsc                   r    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	   dee   fdZ
y)OpenAIRealtimeapi_basequery_paramsreturnc                 &   ddl m} |j                  dd      }|j                  dd      } ||      }|j                  d      }|j	                         D ci c]  \  }}|d	k7  s|| }}}|r|j                  |
      }t        |      S c c}}w )zo
        Construct the backend websocket URL with all query parameters (excluding 'model' if present).
        r   )URLzhttps://zwss://zhttp://zws://z/v1/realtime)pathmodel)params)httpxr   replace	copy_withitemsstr)selfr   r   r   urlkv
query_dicts           `/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/openai/realtime/handler.py_construct_urlzOpenAIRealtime._construct_url   s     	##J9##Iw7(mmmm0'3'9'9';Ltq!qG|adL
L--z-2C3x Ms   B&BNr   	websocketlogging_objapi_keyclienttimeoutc	                 &  K   dd l }	ddlm}
 |t        d      |t        d      |d|i}| j	                  ||      }	 |	j                  |d| dd	      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        $ 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   )ClientConnectionz+api_base is required for Azure OpenAI callsz*api_key is required for Azure OpenAI callsr   z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websockets.asyncio.clientr(   
ValueErrorr!   connectr   r   bidirectional_forward
exceptionsInvalidStatusCodeclosestatus_coder   	ExceptionRuntimeError)r   r   r"   r#   r   r$   r%   r&   r   r-   r(   r   
backend_wsrealtime_streamingeclose_errors                   r    async_realtimezOpenAIRealtime.async_realtime!   s     	>JKK?IJJ #U+L!!(L9	!))'.wi%8#0 *  
A 
A %6t$4jA;&" )>>@@@
A 
A 
A A
A 
A 
A 
A $$66 	E//q}}SV/DDD 	oo(?Ax&H &      
&#k*::>OSVT ?  $D[MR  

 	s   =F B= B"B= !+B(B$B(B= B&B= !F"B= $B(&B= (B:.B1/B:6B= 9F:B= =F)D
?D D
F
F"E 9D<:E ?F	 	E<	)E72F	7E<<F	?
F	FF)NNNNN)__name__
__module____qualname__r   r   r!   r   LiteLLMLoggingr   floatr<        r    r   r      s    s :M RU , #'!% $#'6:66 6 $	6
 3-6 #6 6 %6 236rC   r   N)__doc__typingr   r   r   "litellm_core_utils.litellm_loggingr   r@   %litellm_core_utils.realtime_streamingr   openair
   litellm.types.realtimer   r   rB   rC   r    <module>rJ      s-    ' & L G ) 6H) HrC   