
    gC                         d dl mZmZmZ d dlmZmZ d dlZd dlm	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  e       ZdefdZ G d de      Zy)    )AnyCallableOptional)AsyncAzureOpenAIAzureOpenAIN)prompt_factory)CustomStreamWrapperModelResponseTextCompletionResponse   )BaseLLM)OpenAITextCompletionConfig   )AzureOpenAIErrorazure_client_paramsc                 b    | j                  dd       }|d|v r|| d<   | j                  d       | S )Nazure_endpointz/openai/deploymentsbase_url)getpop)r   r   s     Z/var/www/openai/venv/lib/python3.12/site-packages/litellm/llms/azure/completion/handler.py!select_azure_base_url_or_endpointr      sC    (,,-=tDN! N2.<
+##$45    c                       e Zd Zd fdZd Z	 	 	 ddedededededed	ed
edede	de
e   f fdZ	 	 ddedededededededed
e
e   fdZ	 	 ddedededededed
e
e   fdZ	 	 ddedededededed
e
e   fdZ xZS )AzureTextCompletionc                 "    t         |           y )N)super__init__)self	__class__s    r   r   zAzureTextCompletion.__init__   s    r   c                 4    ddi}|||d<   |S |d| |d<   |S )Nzcontent-typezapplication/jsonzapi-keyBearer Authorization )r   api_keyazure_ad_tokenheaderss       r   validate_environmentz(AzureTextCompletion.validate_environment    sG    .
 !(GI  ')00@'AGO$r   modelmessagesmodel_responser%   api_baseapi_versionapi_typer&   print_verboseacompletionr'   c                    t         |           	 ||t        dd      |j                  dd      }t	        ||d      }d|v rg|]|j                  d	      s|d	z  }|| z  }|| t        j                  ||
d
}|||d<   n|||d<   |du rt        d#i |}nt        d#i |}d |d|}n||d|}|du rG|j                  dd      r| j                  ||||||||
|	      S | j                  ||||||||
||
      S d|v r!|d   du r| j                  ||||||||
|	      S |j                  ||||d|||d       t        |t               st        dd      |||t        j                  ||
d}t#        |      }|||d<   n|||d<   |t        d#i |}n:|}|6t        |j$                  t&              r|j$                  j)                  d|        |j*                  j,                  j.                  d#i |d|
i}|j1                         }|j3                         }|j5                  ||||||d       t6        j9                  t;        d#i ||      S # t        $ r}|d }~wt<        $ rT}t?        |dd      }t?        |d d       }t?        |d!d       }||rt?        |d d       }t        |tA        |      |"      d }~ww xY w)$N  zMissing model or messagesstatus_codemessagemax_retriesr   
azure_text)r*   r)   custom_llm_providerzgateway.ai.cloudflare.com/)r-   r   http_clientr6   timeoutr%   r&   T)r)   promptstreamF)	logging_objr,   datar)   r%   r-   r&   r;   client)
r,   r?   r+   r%   r-   r)   r&   r;   r@   r>   )r%   r&   )r'   r-   r,   complete_input_dictinputr%   additional_argsmax retries must be an intr-   r   azure_deploymentr:   r6   r;   r   api-versionr;   )r'   r-   r,   )rC   r%   original_responserD   response_objectmodel_response_objectr4     r'   responser4   r5   r'   r$   )!r   
completionr   r   r   endswithlitellmclient_sessionr   r   r   async_streamingr0   	streamingpre_call
isinstanceintr   _custom_querydict
setdefaultcompletionswith_raw_responsecreateparse
model_dump	post_callopenai_text_completion_config%convert_to_chat_model_response_objectr   	Exceptiongetattrstr)r   r)   r*   r+   r%   r,   r-   r.   r&   r/   r;   r>   optional_paramslitellm_params	logger_fnr0   r'   r@   r6   r<   r   r?   azure_clientraw_responserO   stringified_responseer4   error_headerserror_responser    s                                 r   rQ   zAzureTextCompletion.completion*   s   ( 	b	} 0& #-H  *--mQ?K#!LF +h6>#,,S1 C5'*H (3'/j'.'='='2#*+' *9@+I6'3@N+,<="d*!1!H4G!H!,!C/B!C!%K?K #$ & d""&&x7//$/!)!# '$/'5 '% 0 
 
  ++!)!'5 '$/#'5 '%$/ ,   _,1Jd1R~~ +%# +#1#! & 
 
 $$ # (/.<$ (3$,/3% %  "+s3*$'1M 
 $/&.(-#*#9#9#.&'# 'H(;'# &5<'	2#/<J'(89>#.#E1D#EL#)L".:$22D4 %22==);  Q|77IIPP   $+  (--/'/':':'<$%% #&:#*'2$,%	 & 	 2WW(>(VAU(V.< X    	G 	!!]C8K#Ay$7M$Q
D9N$ '	4 H"'Q 	s8   CI $I ?$I $D5I 	K#I%%K1AK  Kr?   r;   r>   c           	        K   d }	 |j                  dd      }t        |t              st        dd      |||t        j
                  ||d}t        |      }|||d<   n|	|	|d	<   |
t        di |}n:|
}|6t        |j                  t              r|j                  j                  d
|       |j                  |d   |j                  dd|j                   i|j                  j                  d|d        |j                  j                   j"                  di |d|i d {   }|j%                         }t&        j)                  |j+                         |      S 7 9# t        $ r}|d }~wt,        $ rT}t/        |dd      }t/        |dd       }t/        |dd       }||rt/        |dd       }t        |t1        |      |      d }~ww xY ww)Nr6   r   r2   rE   r3   rF   rH   r%   r&   rI   r<   r#   r"   Tr'   r,   r0   rA   rB   r;   rK   r4   rN   r'   rO   rP   r$   )r   rX   rY   r   rS   rT   r   r   rZ   r[   r\   rW   r%   	_base_url_uri_referencer]   r^   r_   r`   rc   rd   ra   re   rf   rg   )r   r%   r-   r)   r,   r?   r;   r+   r>   r&   r@   rO   r6   r   rk   rl   rn   r4   ro   rp   s                       r   r0   zAzureTextCompletion.acompletion   s     ?	((=!4Kk3/& #-I   +"*$)&55*"# #D$7# "18#I.+8F#$45 ~/F2EF%*z ..0 !..99-U  8n$,, /7<;O;O:P1QR , 6 6 E E#'+/	! ! 	 "S!9!9!K!K!R!R "" '" L $))+H0VV ( 3 3 5&4 W  	   	G 	!!]C8K#Ay$7M$Q
D9N$ '	4 H"'Q 	sH   GDE! %E&8E! GE! !	G*E,,G8AGGGc
           	      |   |j                  dd      }
t        |
t              st        dd      |||t        j
                  |
|d}t        |      }|||d<   n|||d	<   |	t        di |}n:|	}|6t        |j                  t              r|j                  j                  d
|       |j                  |d   |j                  dd|j                   i|j                  j                  d|d        |j                  j                   j"                  di |d|i}|j%                         }t'        ||d|      }|S )Nr6   r   r2   rE   r3   rF   rH   r%   r&   rI   r<   r#   r"   Trr   rB   r;   r7   completion_streamr)   r8   r>   r$   )r   rX   rY   r   rS   rT   r   r   rZ   r[   r\   rW   r%   rs   rt   r]   r^   r_   r`   r	   )r   r>   r,   r%   r-   r?   r)   r;   r&   r@   r6   r   rk   rl   rO   streamwrappers                   r   rV   zAzureTextCompletion.streaming2  s    hh}a0+s+")E 
 '& %"11&
 @ 3
 -4	*'4B 01>&=)<=L!L&:l6P6PRV+W**55m[Qx. ((+w|7K7K6L-MN(22AA#'+	 	 		
 I|//AAHH 

#
  %%'+& ,#	
 r   c
           	        K   	 |||t         j                  |j                  dd      |d}
t        |
      }
|||
d<   n|||
d<   |	t	        di |
}n:|	}|6t        |j                  t              r|j                  j                  d|       |j                  |d   |j                  d	d
|j                   i|j                  j                  d|d        |j                  j                  j                  di |d|i d {   }|j!                         }t#        ||d|      }|S 7 %# t$        $ rT}t'        |dd      }t'        |dd       }t'        |dd       }||rt'        |dd       }t)        |t+        |      |      d }~ww xY ww)Nr6   r   rF   rH   r%   r&   rI   r<   r#   r"   Trr   rB   r;   r7   rv   r4   rN   r'   rO   rP   r$   )rS   rT   r   r   r   rX   rZ   r[   r\   rW   r%   rs   rt   r]   r^   r_   r`   r	   re   rf   r   rg   )r   r>   r,   r%   r-   r?   r)   r;   r&   r@   r   rk   rl   rO   rx   rn   r4   ro   rp   s                      r   rU   z#AzureTextCompletion.async_streamingq  s    9	  +"*$)&55#xxq9"# #D$7# "18#I.+8F#$45~/F2EF%*z ..0 !..99-U  8n$,, /7<;O;O:P1QR , 6 6 E E#'+/	! ! 	 "S!9!9!K!K!R!R "" '" L $))+H/"*$0'	M !   	!!]C8K#Ay$7M$Q
D9N$ '	4 H"'Q 	s<   FD D, D*$D, )F*D, ,	F	5AFF		F)returnN)FNN)NN)__name__
__module____qualname__r   r(   rg   listr
   r   boolr   r[   rQ   r   r0   rV   rU   __classcell__)r    s   @r   r   r      s   4 ""&%ww w &	w
 w w w w w  w  !w" $#wF )-MM M 	M
 M M M &M M !Mp )-= = 	=
 = = = = !=P )-E E 	E
 E E E E !Er   r   )typingr   r   r   openair   r   rS   3litellm.litellm_core_utils.prompt_templates.factoryr   litellm.utilsr	   r
   r   baser    openai.completion.transformationr   common_utilsr   rc   r[   r   r   r$   r   r   <module>r      sF    * * 0  N T T  J + : < 	4 	Z' Zr   