
    dh[                    4   S r SSKJr  SSKrSSKrSSKJrJrJrJ	r	J
r
JrJrJrJrJrJrJrJr  SSKJr  SSKJrJr  SSKJr  SSKJrJrJr  SS	KJr  SS
K J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/  SSK0J1r1  SSK2J3r3J4r4J5r5  SSK6J7r7  SSK8J9r9  SSK:J;r;J<r<  SSK=J>r>  SSK?J@r@JArA  \R                  " \C5      rD " S S\E5      rF S"     S#S jjrGS$S jrH S"       S%S jjrI      S&S jrJS'S jrKS(S jrL/ SQrM\" SSSS9 " S  S\5      5       rNS)S! jrOg)*u   
Deprecated LiteLLM wrapper.

⭐  Use `pip install langchain-litellm` and import
   `from langchain_litellm import ChatLiteLLM` instead.
    )annotationsN)AnyAsyncIteratorCallableDictIteratorListLiteralMappingOptionalSequenceTupleTypeUnion)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LanguageModelInput)BaseChatModelagenerate_from_streamgenerate_from_stream)create_base_retry_decorator)	AIMessageAIMessageChunkBaseMessageBaseMessageChunkChatMessageChatMessageChunkFunctionMessageFunctionMessageChunkHumanMessageHumanMessageChunkSystemMessageSystemMessageChunkToolCallToolCallChunkToolMessage)UsageMetadata)ChatGenerationChatGenerationChunk
ChatResult)Runnable)BaseTool)get_from_dict_or_envpre_init)convert_to_openai_tool)	BaseModelFieldc                      \ rS rSrSrSrg)ChatLiteLLMExceptionH   z$Error with the `LiteLLM I/O` library N)__name__
__module____qualname____firstlineno____doc____static_attributes__r6       _/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/chat_models/litellm.pyr4   r4   H   s    .r=   r4   ChatLiteLLMc                    SSK nUR                  UR                  UR                  UR                  /n[        X0R                  US9$ )zKReturns a tenacity retry decorator, preconfigured to handle PaLM exceptionsr   N)error_typesmax_retriesrun_manager)litellmTimeoutAPIErrorAPIConnectionErrorRateLimitErrorr   rB   )llmrC   rD   errorss       r>   _create_retry_decoratorrK   L   sJ      	""	F '[ r=   c                |   U S   nUS:X  a  [        U S   S9$ US:X  ak  U R                  SS5      =(       d    Sn0 nU R                  S5      (       a  [        U S   5      US'   U R                  S5      (       a  U S   US'   [        X#S	9$ US
:X  a  [	        U S   S9$ US:X  a  [        U S   U S   S9$ [        U S   US9$ )NroleusercontentrO   	assistant function_call
tool_calls)rO   additional_kwargssystemfunctionnamerO   rX   rO   rM   )r!   getdictr   r#   r   r   )_dictrM   rO   rU   s       r>   _convert_dict_to_messager^   `   s    =Dv~E)$455		 ))Ir*0b99_%%15eO6L1Mo.99\"".3L.Al+NN		U9%566		uY'7eFmLL5#3$??r=   c                ^   ^ #    [        T US9nUSU 4S jj5       nU" S0 UD6I Sh  vN $  N7f)z0Use tenacity to retry the async completion call.rC   c                 X   >#    TR                   R                  " S0 U D6I S h  vN $  N7fNr6   )clientacreate)kwargsrI   s    r>   _completion_with_retry6acompletion_with_retry.<locals>._completion_with_retry   s&      ZZ''1&1111s    *(*Nre   r   returnr   r6   rK   )rI   rC   re   retry_decoratorrf   s   `    r>   acompletion_with_retryrl   y   s<      .c{KO2 2 (1&1111s   #-+-c           
        U R                  S5      nU R                  S5      =(       d    SnU R                  S5      (       a  S[        U S   5      0nO U R                  S5      (       a  SU S   0nO0 n/ nU R                  S5      =n(       aV  XdS'    U Vs/ sHE  n[        US   R                  S5      US   R                  S	5      UR                  S
5      US   S9PMG     nnUS:X  d
  U[        :X  a	  [	        US9$ US:X  d
  U[
        :X  a  [        UUUS9$ US:X  d
  U[        :X  a	  [        US9$ US:X  d
  U[        :X  a  [        X0S   S9$ U(       d
  U[        :X  a	  [        X2S9$ U" US9$ s  snf ! [         a     Nf = f)NrM   rO   rR   rS   reasoning_contentrT   rW   rX   	argumentsidindex)rX   argsrp   rq   rN   rP   rQ   )rO   rU   tool_call_chunksrV   rY   rZ   )	r[   r\   r&   KeyErrorr"   r   r$   r    r   )r]   default_classrM   rO   rU   rs   raw_tool_callsrtcs           r>   _convert_delta_to_message_chunkrx      s    99VDii	"(bGyy!!,d53I.JK	&	'	'0%8K2LM<00~0*8,'	 *  *C Z,,V4Z,,[9wwt}g,	 *    v~*;; 11		 ?/-
 	

 
	].@@!'22		}0DD#G-HH	"22;;W--7   		s%   E1 AE,!E1 ,E1 1
E>=E>c                N    SU S   U S   [         R                  " U S   5      S.S.$ )NrW   rp   rX   rr   )rX   ro   )typerp   rW   )jsondumps)	tool_calls    r>   !_lc_tool_call_to_openai_tool_callr~      s4    of%If$56
 r=   c                    SU R                   0n[        U [        5      (       a  U R                  US'   GO2[        U [        5      (       a  SUS'   GO[        U [
        5      (       a  SUS'   SU R                  ;   a  U R                  S   US'   U R                  (       a)  U R                   Vs/ sH  n[        U5      PM     snUS'   OSU R                  ;   a  U R                  S   US'   O}[        U [        5      (       a  SUS'   Ob[        U [        5      (       a  SUS'   U R                  US	'   O8[        U [        5      (       a  S
US'   U R                  US'   O[        SU  35      eS	U R                  ;   a  U R                  S	   US	'   U$ s  snf )NrO   rM   rN   rQ   rS   rT   rV   rW   rX   tooltool_call_idzGot unknown type )rO   
isinstancer   rM   r!   r   rU   rT   r~   r#   r   rX   r'   r   
ValueError)messagemessage_dicttcs      r>   _convert_message_to_dictr      sz   $-w#?L';''&||V	G\	*	*%V	GY	'	'*Vg777,3,E,Eo,VL)@G@R@R*@R"1"5@R*L& W666)0)B)B<)PL&	G]	+	+'V	G_	-	-)V&||V	G[	)	)%V'.';';^$,WI677***&88@V#*s   ,F)zo1-miniz
o1-previewzgpt-4o-minizgpt-4o-mini-2024-07-18zgpt-4ozgpt-4o-2024-08-06zgpt-4o-2024-05-13zgpt-4-turbozgpt-4-turbo-previewzgpt-4-0125-previewzgpt-4-1106-previewzgpt-3.5-turbo-1106gpt-3.5-turbozgpt-3.5-turbo-0301zgpt-3.5-turbo-0613zgpt-3.5-turbo-16kzgpt-3.5-turbo-16k-0613zgpt-4z
gpt-4-0314z
gpt-4-0613z	gpt-4-32kzgpt-4-32k-0314zgpt-4-32k-0613z0.3.24z1.0zlangchain_litellm.ChatLiteLLM)sinceremovalalternative_importc                    ^  \ rS rSr% SrSrS\S'   SrS\S'   SrS	\S
'    Sr	S	\S'   Sr
S	\S'   SrS	\S'   SrS	\S'   SrS	\S'   SrS	\S'   SrS	\S'   SrS\S'   SrS	\S'   SrS	\S'   SrS	\S'   SrS\S'   SrS\S'    \" \S9rS\S'    SrS\S'    SrS \S!'    SrS \S"'    SrS \S#'   S$rS%\S&'   \S5S' j5       r\S5S( j5       r  S6     S7S) jjr!\"S8S* j5       r#   S9           S:S+ jjr$S;S, jr%      S<S- jr&  S=         S>S. jjr'  S=         S?S/ jjr(   S9           S@S0 jjr) S6       SAU 4S1 jjjr*\S5S2 j5       r+\SBS3 j5       r,S4r-U =r.$ )Cr?      u;   DEPRECATED – use `langchain_litellm.ChatLiteLLM` instead.Nr   rc   r   strmodelzOptional[str]
model_nameopenai_api_keyazure_api_keyanthropic_api_keyreplicate_api_keycohere_api_keyopenrouter_api_keyapi_keyFbool	streamingapi_baseorganizationcustom_llm_providerz+Optional[Union[float, Tuple[float, float]]]request_timeoutzOptional[float]temperature)default_factoryDict[str, Any]model_kwargstop_pzOptional[int]top_kn
max_tokens   intrB   c                    U R                   nU R                  b  U R                  nUU R                  U R                  U R                  U R
                  U R                  U R                  S.U R                  E$ )z2Get the default parameters for calling OpenAI API.)r   force_timeoutr   streamr   r   r   )	r   r   r   r   r   r   r   r   r   selfset_model_values     r>   _default_paramsChatLiteLLM._default_params&  sm     **??&"ooO$!11//nn++#'#;#;	
 	
 		
r=   c                   U R                   nU R                  b  U R                  nU R                  U R                  l        U R                  U R                  l        S H>  n[        X5      =n(       d  M  [        U R                  UR                  SS5      U5        M@     U R                  U R                  l        UU R                  U R                  S.n0 U R                  EUE$ )z.Get the parameters used for the openai client.)r   r   r   r   r   r   _api_key_key)r   r   r   )r   r   r   rc   r   getattrsetattrreplacer   r   r   )r   r   named_api_keyapi_key_valuecredss        r>   _client_paramsChatLiteLLM._client_params7  s     **??&"ooO#}}"ll
M !( <<}<KK!))*f=!
 $(#4#4 $!11!

 1$&&0%00r=   c                B   ^  [        T US9nUSU 4S jj5       nU" S0 UD6$ )z*Use tenacity to retry the completion call.r`   c                 <   > TR                   R                  " S0 U D6$ rb   )rc   
completion)re   r   s    r>   rf   AChatLiteLLM.completion_with_retry.<locals>._completion_with_retry[  s    ;;))3F33r=   rh   r6   rj   )r   rC   re   rk   rf   s   `    r>   completion_with_retry!ChatLiteLLM.completion_with_retryU  s3     2$KP		4 
	4 &///r=   c                    SSK n[        USSSS9US'   [        USS	SS9US'   [        US
SSS9US
'   [        USSSS9US'   [        USSSS9US'   [        USSSS9US'   [        USSSS9US'   [        USSSS9US'   X!S'   US   b  SUS   s=::  a  S::  d  O  [	        S5      eUS   b  SUS   s=::  a  S::  d  O  [	        S5      eUS   b  US   S::  a  [	        S5      eU$ ! [         a    [        S5      ef = f)zGValidate api key, python package exists, temperature, top_p, and top_k.r   NzUCould not import litellm python package. Please install it with `pip install litellm`r   OPENAI_API_KEYrR   )defaultr   AZURE_API_KEYr   ANTHROPIC_API_KEYr   REPLICATE_API_KEYr   OPENROUTER_API_KEYr   COHERE_API_KEYhuggingface_api_keyHUGGINGFACE_API_KEYtogether_ai_api_keyTOGETHERAI_API_KEYrc   r   r   z+temperature must be in the range [0.0, 1.0]r   z%top_p must be in the range [0.0, 1.0]r   ztop_k must be positive)rD   ImportErrorr4   r.   r   )clsvaluesrD   s      r>   validate_environment ChatLiteLLM.validate_environmenta  s   	 $8$&6$
  #7O_b#
 ';')<b'
"# ';')<b'
"# (<(*>(
#$ $8$&6$
  )=)+@")
$% )=)+?)
$% #x- ,Q&:O5TST5TJKK'?&qF7O/Hq/HDEE'?&6'?a+?566S  	&? 	s   C$ $C:c                    Ub  UOU R                   nU(       a   U R                  " U4X#S.UD6n[        U5      $ U R                  X5      u  p0 U	EUEn	U R                  " SXS.U	D6n
U R                  U
5      $ )N)stoprC   messagesrC   r6   )r   _streamr   _create_message_dictsr   _create_chat_resultr   r   r   rC   r   re   should_streamstream_itermessage_dictsparamsresponses              r>   	_generateChatLiteLLM._generate  s     #)"4$..,,#@FK (44 $ : :8 J%F%f%-- 
"
?E
 ''11r=   c           	        / nUR                  S0 5      nUS    H  n[        US   5      n[        U[        5      (       a6  SU R                  =(       d    U R
                  0Ul        [        U5      Ul        [        U[        UR                  S5      S9S9nUR                  U5        M     U R
                  nU R                  b  U R                  nX7S.n[        X(S	9$ )
Nusagechoicesr   r   finish_reason)r   )r   generation_info)token_usager   )generations
llm_output)r[   r^   r   r   r   r   response_metadata_create_usage_metadatausage_metadatar)   r\   appendr+   )	r   r   r   r   resr   genr   r   s	            r>   r   ChatLiteLLM._create_chat_result  s    ll7B/I&C.s9~>G'9-- $//"?TZZ-) *@)L&  $377?3K LC s# ' **??&"ooO%0K
kIIr=   c                    U R                   nUb  SU;   a  [        S5      eX#S'   U Vs/ sH  n[        U5      PM     nnXS4$ s  snf )Nr   z2`stop` found in both the input and default params.)r   r   r   )r   r   r   r   mr   s         r>   r   !ChatLiteLLM._create_message_dicts  sY     $$ !UVV!6N>FGh1!4hG$$ Hs   Ac              +  j  #    U R                  X5      u  pV0 UEUESS0En[        nSnU R                  " SXSS.UD6 H  n	[        U	[        5      (       d  U	R                  5       n	[        U	S   5      S:X  a  M<  U	S   S   S   n
U	R                  S0 5      n[        X5      n	[        U	[        5      (       a?  U(       d(  S	U R                  =(       d    U R                  0U	l        Sn[        U5      U	l        U	R                  n[        U	S
9nU(       a  UR!                  U	R"                  US9  Uv   M     g 7f)Nr   TFr   r   r   deltar   r   r   chunkr6   )r   r   r   r   r\   
model_dumplenr[   rx   r   r   r   r   r   	__class__r*   on_llm_new_tokenrO   r   r   r   rC   re   r   r   default_chunk_classadded_model_namer   r   r   cg_chunks                r>   r   ChatLiteLLM._stream  s5     !% : :8 J5F5f5h5, // 
"
?E
E eT**((*5#$))$Q'0EIIgr*E3EOE%00'$doo&C/E+ (,$'=e'D$"'//*59H,,U]](,KN+
s   D1D3c                 #    U R                  X5      u  pV0 UEUESS0En[        nSn[        U 4XSS.UD6I S h  vN   S h  vN n	[        U	[        5      (       d  U	R                  5       n	[        U	S   5      S:X  a  MB  U	S   S   S   n
U	R                  S0 5      n[        X5      n	[        U	[        5      (       a?  U(       d(  S	U R                  =(       d    U R                  0U	l        Sn[        U5      U	l        U	R                  n[        U	S
9nU(       a"  UR!                  U	R"                  US9I S h  vN   U7v   GM   GN	 GN N
 g 7f)Nr   TFr   r   r   r   r   r   r   r   )r   r   rl   r   r\   r   r   r[   rx   r   r   r   r   r   r   r*   r   rO   r   s                r>   _astreamChatLiteLLM._astream  sW     !% : :8 J5F5f5h5, !7"
("
EK"
 
 
 	% eT**((*5#$))$Q'0EIIgr*E3EOE%00'$doo&C/E+ (,$'=e'D$"'//*59H!225==2QQQN+
 	( R)
sE   7EEEE
EE
C/E5E6EE
E
Ec                  #    Ub  UOU R                   nU(       a(  U R                  " SXUS.UD6n[        U5      I S h  vN $ U R                  X5      u  p0 U	EUEn	[	        U 4XS.U	D6I S h  vN n
U R                  U
5      $  ND N7f)N)r   r   rC   r   r6   )r   r  r   r   rl   r   r   s              r>   
_agenerateChatLiteLLM._agenerate  s      #)"4$..-- !+IOK /{;;; $ : :8 J%F%f%/
(
EK
 
 ''11 <
s!   <BB.B-B.BBc                  >^ U Vs/ sH  n[        U5      PM     nnU R                  b  SU R                  ;   d_  U R                  b  SU R                  ;   dB  U R                  b  U R                  [        ;   d!  U R                  b2  U R                  [        ;   a  TS:X  d  [	        T[
        5      (       a  SmOo[	        T[
        5      (       a  SmOW[	        T[        5      (       aB  U Vs/ sH
  ofS   S   PM     nn[        U4S jU 5       5      (       d  [        ST SU S	35      e[        TU ](  " SUTS
.UD6$ s  snf s  snf )a  Bind tool-like objects to this chat model.

LiteLLM expects tools argument in OpenAI format.

Args:
    tools: A list of tool definitions to bind to this chat model.
        Can be  a dictionary, pydantic model, callable, or BaseTool. Pydantic
        models, callables, and BaseTools will be automatically converted to
        their schema dictionary representation.
    tool_choice: Which tool to require the model to call. Options are:
        - str of the form ``"<<tool_name>>"``: calls <<tool_name>> tool.
        - ``"auto"``:
            automatically selects a tool (including no tool).
        - ``"none"``:
            does not call a tool.
        - ``"any"`` or ``"required"`` or ``True``:
            forces least one tool to be called.
        - dict of the form:
        ``{"type": "function", "function": {"name": <<tool_name>>}}``
        - ``False`` or ``None``: no effect
    **kwargs: Any additional parameters to pass to the
        :class:`~langchain.runnable.Runnable` constructor.
azureanyrequiredrW   rX   c              3  6   >#    U H  oTS    S   :H  v   M     g7f)rW   rX   Nr6   ).0	tool_nametool_choices     r>   	<genexpr>)ChatLiteLLM.bind_tools.<locals>.<genexpr>U  s!      NX[4V<<js   zTool choice z1 was specified, but the only provided tools were .)toolsr  r6   )r0   r   r   _OPENAI_MODELSr   r   r\   r	  r   superbind)	r   r  r  re   r   formatted_toolsformatted_tool
tool_namesr   s	     `     r>   
bind_toolsChatLiteLLM.bind_tools#  s=   @ EJJED1$7EJ
 ZZ#4::(=+4??0J

&4::+G+>0Qe#z+t'D'D$KT**KT**IXIX~z*62    NX   !";- 0++5,a9  w|U/{UfUU3 Ks   D9(D>c                    U R                   nU R                  b  U R                  nUU R                  U R                  U R                  U R
                  S.$ )zGet the identifying parameters.)r   r   r   r   r   )r   r   r   r   r   r   r   s     r>   _identifying_paramsChatLiteLLM._identifying_params^  sK     **??&"ooO$++ZZZZ
 	
r=   c                    g)Nzlitellm-chatr6   )r   s    r>   	_llm_typeChatLiteLLM._llm_typel  s    r=   r6   )ri   r   N)rC   "Optional[CallbackManagerForLLMRun]re   r   ri   r   )r   r   ri   r   )NNN)r   List[BaseMessage]r   Optional[List[str]]rC   r"  r   Optional[bool]re   r   ri   r+   )r   Mapping[str, Any]ri   r+   )r   r#  r   r$  ri   z+Tuple[List[Dict[str, Any]], Dict[str, Any]])NN)
r   r#  r   r$  rC   r"  re   r   ri   zIterator[ChatGenerationChunk])
r   r#  r   r$  rC   'Optional[AsyncCallbackManagerForLLMRun]re   r   ri   z"AsyncIterator[ChatGenerationChunk])r   r#  r   r$  rC   r'  r   r%  re   r   ri   r+   )r  zDSequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]]r  zLOptional[Union[dict, str, Literal['auto', 'none', 'required', 'any'], bool]]re   r   ri   z)Runnable[LanguageModelInput, BaseMessage])ri   r   )/r7   r8   r9   r:   r;   rc   __annotations__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r2   r\   r   r   r   r   r   rB   propertyr   r   r   r/   r   r   r   r   r   r  r  r  r  r  r<   __classcell__)r   s   @r>   r?   r?      s    FFC E3  $J$$(NM(#'M=''+}+'+}+$(NM((,,!G]!It"Hm""&L-&)--CGO@G#'K'#(#>L.>Q!E?!XE=A}I $J$K
 
  1 1< AE
0=
0PS
0	
0 - -d %):>!%2#2 "2 8	2
 2 2 
2,J*	%)	%1D	%	4	% %):>	!#! "! 8	!
 ! 
'!L %)?C	!#! "! =	!
 ! 
,!L %)?C!%2#2 "2 =	2
 2 2 
26 9VS9V
9V 9V 
39V 9Vv 
 
  r=   c                d    U R                  SS5      nU R                  SS5      n[        UUX-   S9$ )Nprompt_tokensr   completion_tokens)input_tokensoutput_tokenstotal_tokens)r[   r(   )r   r.  r/  s      r>   r   r   q  s<    ???A6LOO$7;M!#!1 r=   r!  )rI   r?   rC   zHOptional[Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun]]ri   zCallable[[Any], Any])r]   r&  ri   r   )rI   r?   rC   r'  re   r   ri   r   )r]   r&  ru   zType[BaseMessageChunk]ri   r   )r}   r%   ri   r\   )r   r   ri   r\   )r   r&  ri   r(   )Pr;   
__future__r   r{   loggingtypingr   r   r   r   r   r	   r
   r   r   r   r   r   r   langchain_core._api.deprecationr   langchain_core.callbacksr   r   langchain_core.language_modelsr   *langchain_core.language_models.chat_modelsr   r   r   #langchain_core.language_models.llmsr   langchain_core.messagesr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   langchain_core.messages.air(   langchain_core.outputsr)   r*   r+   langchain_core.runnablesr,   langchain_core.toolsr-   langchain_core.utilsr.   r/   %langchain_core.utils.function_callingr0   pydanticr1   r2   	getLoggerr7   logger	Exceptionr4   rK   r^   rl   rx   r~   r   r  r?   r   r6   r=   r>   <module>rD     sY   #       7 > 
 L    " 5 
 . ) ? H %			8	$/9 / 			
 (@6 <@2	282 2 		2 +.+.-C+.+.\>6 
6
m- m
m`r=   