
    g@                        d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	Z	d dl
Z
d dlZ
d dlZ
d dlmZ d dlmZmZ d dlmZmZ d dlmZ d dlmZmZmZmZmZ d	d
lmZ dedede dedede!deee"e	jF                  f      fdZ$ G d d      Z%y)    N)partial)CallableOptionalUnion)map_finish_reason)custom_promptprompt_factory)AsyncHTTPHandlerget_async_httpx_client)LiteLLMLoggingBaseClass)ChoicesCustomStreamWrapperMessageModelResponseUsage   )PredibaseErrorclientapi_baseheadersdatamodelmessagestimeoutc                    K   | j                  |||d|       d {   }|j                  dk7  r!t        |j                  |j                        |j	                         }	|j                  |d|	d|i       |	S 7 ]w)NTr   r   streamr      status_codemessage complete_input_dictinputapi_keyoriginal_responseadditional_args)postr    r   textaiter_lines	post_call)
r   r   r   r   r   r   logging_objr   responsecompletion_streams
             X/var/www/openai/venv/lib/python3.12/site-packages/litellm/llms/predibase/chat/handler.py	make_callr1      s      [['T7 !  H s")=)=x}}UU ,,.+.5	   !s   A<A:AA<c                       e Zd Zd fdZdefdZdedej                  ded	e	d
e
dededeeef   dedefdZdddi fdedededededededededeeej"                  f   dedeeef   fdZddi fdedededededededeeej"                  f   defdZdddi fdededededededeeej"                  f   defdZd Z xZS )PredibaseChatCompletionreturnNc                 "    t         |           y N)super__init__)self	__class__s    r0   r8   z PredibaseChatCompletion.__init__<   s        generated_textc                     g d}|D ]k  }|j                         j                  |      r|j                  |dd      }|j                  |      sG|ddd   j                  |ddd   dd      ddd   }m |S )z
        Parse the output text to remove any special characters. In our current approach we just check for ChatML tokens.

        Initial issue that prompted this - https://github.com/BerriAI/litellm/issues/763
        )z<|assistant|>z
<|system|>z<|user|>z<s>z</s>r"      N)strip
startswithreplaceendswith)r9   r<   chat_template_tokenstokens       r0   output_parserz%PredibaseChatCompletion.output_parser?   s     
 *E##%007!/!7!7r1!E&&u-!/"!5!=!=eDbDk2q!QRVTVRV!W	 *
 r;   r   r.   model_responser   r-   optional_paramsr&   r   r   c                 
   |j                  |	||j                  d|i        |
d|j                          	 |j                         }d|v r#t	        t        |d         |j                        t        |t              st	        dd|       d	|vrt	        dd
|       t        |d	         dkD  r0| j                  |d	         |j                  d   j                  _        d|v rkd|d   v rdt        |d   d         |j                  d   _        d}|d   d   D ]  }|d   	||d   z  } t!        |j                  d   j                  d|       d|v r|d   dkD  rd|v rd|d   v rg }t#        |d   d         D ]  \  }}d}|d   D ]  }|d   	||d   z  } t        |d	         dkD  r t%        | j                  |d	         |      }nt%        d       }t'        t        |d         |dz   |      }|j)                  |        |j                  j+                  |       d}	 t-        j.                  |	      }|d   d   d   j1                  dd      }|Et        |      dkD  r7d}	 t        |j3                  |d   d   d   j1                  dd                  }nd}||z   }t5        t7        j6                               |_        ||_        t=        |||      }||_        |j@                  }i }|jC                         D ]+  \  }}|jE                  d      s||djG                  |      <   - ||jH                  d<   |S # t        $ r t	        |j                  d      w xY w# t        $ r Y 4w xY w# t        $ r Y w xY w) Nr#   r$   zraw model_response: i  )r!   r    errorz,'completion_response' is not a dictionary - r   r<   z4'generated_text' is not a key response dictionary - r   detailstokensfinish_reasonlogprob_logprobbest_ofr>   best_of_sequences)contentlogprobs)rR   )rM   indexr!   )r   choicesr!   rR   r"   )prompt_tokenscompletion_tokenstotal_tokenszx-zllm_provider-{}additional_headers)%r,   r*   json	Exceptionr   strr    
isinstancedictlenrF   rU   r!   rR   r   rM   setattr	enumerater   r   appendextendlitellmtoken_countergetencodeinttimecreatedr   r   usager   itemsrA   format_hidden_params)r9   r   r.   rG   r   r-   rH   r&   r   r   print_verboseencodingcompletion_responsesum_logprobrE   choices_listidxitemmessage_obj
choice_objrV   output_textrW   rX   rk   predibase_headersresponse_headerskvs                                r0   process_responsez(PredibaseChatCompletion.process_responseS   sJ    	&mm2D9	 	 	
 	,X]]O<=	I"*--/ )) /89$00 
 148$ #JK^J_`  ")<<$ #RSfRgh  &'789A=<@<N<N'(89=&&q)119
 00 3I >>:K'	2?C;&&q)7  0;HEEY'3#uY'77 F "**1-55
 O+	0JQ0N!44+/B9/MM#%L%.+I67JK&	T '(%)(^E$Y/; +uY/? ? &4 t$456:*1(,(:(:4@P;Q(R)4+K
 +2$*?K%,*;D<Q*R"%'$/&

 %++J7'&( #**11,? 	#118DM %Y/29=AA)RP"s;'7!'; !$'OO&y1!4Y?CCIrR%! !"$'88!$TYY[!1$'/%

  % %,,%++-DAq||D!@A !2!9!9!!<= . ?O%%&:;[  	I CHH	IN  		  s/   M (M& ,3M6 !M#&	M32M36	NNr   custom_prompt_dictro   	tenant_idr   r   c                 `   t        j                         j                  ||||
|      }d}d}d}d|v r|}n-|r|}n(dt        j                  v rt        j
                  dd      }| d| d| }|
j                  dd	      d
u r|dz  }n|dz  }||v r||   }t        |d   |d   |d   |      }nt        ||      }t         j                  j                         }|j                         D ]  \  }}||
vs||
|<    |
j                  dd	      }||
d}|}|	j                  ||||||d       |d
u rC|d
u r| j                  |||||||||	|
||||      S | j                  |||||||||	|
d	||||      S |d
u rVt         j                  j!                  ||t#        j$                  |      ||      }t'        |j)                         |d|	      }|S t         j                  j!                  ||t#        j$                  |      |      }| j+                  ||||
j                  dd	      |	|
|||||      S )N)r&   r   r   rH   r   r"   z!https://serving.app.predibase.comhttpsPREDIBASE_API_BASE/z/deployments/v2/llms/r   FTz/generate_streamz	/generaterolesinitial_prompt_valuefinal_prompt_value)	role_dictr   r   r   )r   r   )inputs
parameters)r#   r   r   acompletion)r%   r&   r(   )r   r   r   r   rG   ro   rp   r&   r-   rH   litellm_params	logger_fnr   r   )r   r   r   r   rG   ro   rp   r&   r-   rH   r   r   r   r   r   r   	predibase)custom_llm_providerr-   )urlr   r   r   )r   r.   rG   r   r-   rH   r&   r   r   ro   rp   )rd   PredibaseConfigvalidate_environmentosenvirongetenvrf   r   r	   
get_configrl   poppre_callasync_streamingasync_completionmodule_level_clientr)   rZ   dumpsr   
iter_linesr}   )r9   r   r   r   r~   rG   ro   rp   r&   r-   rH   r   r   r   r   r   r   completion_url
input_textbase_urlmodel_prompt_detailspromptconfigr{   r|   r   r   r.   	_responses                                r0   
completionz"PredibaseChatCompletion.completion   s   & ))+@@+ A 
 
6e"NH!RZZ/yy!5r:H$:Qyk1FugNx/4700Nk)N&&#5e#< ".w7%9:P%Q#78L#M!	F $%(CF ((335LLNDAq(%&"	 # !$$Xu5 )
 
'+"**	 	 		
 $~++%+#1"/%# +$3#1'## ,  $ ,,%+#1"/%# +$3 #1'## -  & T>2277ZZ% 8 H ,##%$/'	I  2277"ZZ%	 8 H $$)"&&x7#+' % 
 	
r;   c                   K   t        t        j                  j                  d|i      }	 |j	                  ||t        j                  |
             d {   }| j%                  ||||	|||
||||      S 7  # t        j                  $ r^}t        |j                  j                  dj                  |j                  j                  |j                  j                              d }~wt        $ rM}t        j                  D ]  }t!        ||      s| t        ddj                  t#        |                  d }~ww xY ww)	Nr   )llm_providerparams)r   r   z;HTTPStatusError - received status_code={}, error_message={}r   i  z{})r   r.   rG   r   r-   r&   r   r   ro   rH   rp   )r   rd   LlmProviders	PREDIBASEr)   rZ   r   httpxHTTPStatusErrorr   r.   r    rm   r*   r[   LITELLM_EXCEPTION_TYPESr]   r\   r}   )r9   r   r   r   rG   ro   rp   r&   r-   r   r   rH   r   r   r   r   async_handlerr.   e	exceptions                       r0   r   z(PredibaseChatCompletion.async_completion{  s9    & / --77w'
	*//'

40@ 0  H" $$)#'+ % 
 	
# $$ 	 JJ22U\\JJ**AJJOO   	$<<	a+G = !SV)< 		sM   (E *A8 A6A8 E 6A8 8D=AC$$D=0D8(D88D==E c                    K   d|	d<   t        d t        t        ||t        j                  |	      ||||
      |d|      }|S w)NTr   )r   r   r   r   r   r-   r   r   )r/   r1   r   r   r-   )r   r   r1   rZ   r   )r9   r   r   r   rG   ro   rp   r&   r-   r   r   rH   r   r   r   streamwrappers                   r0   r   z'PredibaseChatCompletion.async_streaming  sZ     " X+"!ZZ%!'	  +#
  s   A Ac                      y r6    )r9   argskwargss      r0   	embeddingz!PredibaseChatCompletion.embedding  s    r;   )r4   N)__name__
__module____qualname__r8   r\   rF   r   Responser   boolr   r^   r   listr}   r   floatTimeoutr   r   r   r   r   __classcell__)r:   s   @r0   r3   r3   ;   s0   C (FF ..F &	F
 F -F F F D#IF F 
Fl #^
^
 ^
 	^

 !^
 &^
  ^
 ^
 ^
 ^
 uemm+,^
" #^
$ 
}11	2%^
\ !5
5
 5
 	5

 &5
  5
 5
 5
 uemm+,5
" 
#5
F ## # 	#
 &#  # # uemm+,#  
!#Jr;   r3   )&rZ   r   ri   	functoolsr   typingr   r   r   r   rd   litellm.litellm_core_utils*litellm.litellm_core_utils.litellm_logging'litellm.litellm_core_utils.core_helpersr   3litellm.litellm_core_utils.prompt_templates.factoryr   r	   &litellm.llms.custom_httpx.http_handlerr
   r   litellm.types.utilsr   litellm.utilsr   r   r   r   r   common_utilsr   r\   r^   r   r   r   r1   r3   r   r;   r0   <module>r      s     	   , ,   ! 1 E 8 U U )  	
   eE5==012:] ]r;   