
    h[                    (   d Z ddlmZ ddlZddlZddlmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZ ddlmZ ddlmZm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!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z;m<Z< ddl=m>Z> ddl?m@Z@mAZA  ej                  eC      ZD G d deE      ZF	 d"	 	 	 	 	 d#dZGd$dZH	 d"	 	 	 	 	 	 	 d%dZI	 	 	 	 	 	 d&dZJd'dZKd(dZLg dZM eddd       G d  de             ZNd)d!ZOy)*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                      e Zd ZdZy)ChatLiteLLMExceptionz$Error with the `LiteLLM I/O` libraryN)__name__
__module____qualname____doc__     e/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/chat_models/litellm.pyr4   r4   H   s    .r:   r4   ChatLiteLLMc                    ddl }|j                  |j                  |j                  |j                  g}t        || j                  |      S )zKReturns a tenacity retry decorator, preconfigured to handle PaLM exceptionsr   N)error_typesmax_retriesrun_manager)litellmTimeoutAPIErrorAPIConnectionErrorRateLimitErrorr   r?   )llmr@   rA   errorss       r;   _create_retry_decoratorrH   L   sL      	""	F '[ r:   c                v   | d   }|dk(  rt        | d         S |dk(  r`| j                  dd      xs d}i }| j                  d      rt        | d         |d<   | j                  d      r| d   |d<   t        ||	      S |d
k(  rt	        | d         S |dk(  rt        | d   | d         S t        | d   |      S )NroleusercontentrL   	assistant function_call
tool_calls)rL   additional_kwargssystemfunctionnamerL   rU   rL   rJ   )r!   getdictr   r#   r   r   )_dictrJ   rL   rR   s       r;   _convert_dict_to_messager[   `   s    =Dv~E)$455		 ))Ir*0b99_%15eO6L1Mo.99\".3L.Al+<MNN		U9%566		uY'7eFmLL5#3$??r:   c                `    K   t         |      }|d fd       } |di | d{   S 7 w)z0Use tenacity to retry the async completion call.r@   c                 X   K    j                   j                  di |  d {   S 7 wNr9   )clientacreate)kwargsrF   s    r;   _completion_with_retryz6acompletion_with_retry.<locals>._completion_with_retry   s)      (SZZ''1&1111s    *(*Nrb   r   returnr   r9   rH   )rF   r@   rb   retry_decoratorrc   s   `    r;   acompletion_with_retryrh   y   s=      .c{KO2 2 (1&1111s   $.,.c           
        | j                  d      }| j                  d      xs d}| j                  d      rdt        | d         i}n| j                  d      rd| d   i}ni }g }| j                  d      x}rX||d<   	 |D cg c]G  }t        |d   j                  d      |d   j                  d	      |j                  d
      |d         I }}|dk(  s	|t        k(  rt	        |      S |dk(  s	|t
        k(  rt        |||      S |dk(  s	|t        k(  rt        |      S |dk(  s	|t        k(  rt        || d         S |s	|t        k(  rt        ||      S  ||      S c c}w # t        $ r Y w xY w)NrJ   rL   rO   rP   reasoning_contentrQ   rT   rU   	argumentsidindex)rU   argsrl   rm   rK   rM   rN   )rL   rR   tool_call_chunksrS   rV   rW   )	rX   rY   r&   KeyErrorr"   r   r$   r    r   )rZ   default_classrJ   rL   rR   ro   raw_tool_callsrtcs           r;   _convert_delta_to_message_chunkrt      s    99VDii	"(bGyy!,d53I.JK	&	'0%8K2LM<00~0*8,'	 *   Z,,V4Z,,[9wwt}g,	    v~*;; 11		 ?/-
 	

 
	].@@!'22		}0DD#G%-HH	"22d;;W--7   		s%   ?E% AE E%  E% %	E10E1c                L    d| d   | d   t        j                  | d         ddS )NrT   rl   rU   rn   )rU   rk   )typerl   rT   )jsondumps)	tool_calls    r;   !_lc_tool_call_to_openai_tool_callrz      s4    of%If$56
 r:   c                   d| j                   i}t        | t              r| j                  |d<   nt        | t              rd|d<   nt        | t
              ryd|d<   d| j                  v r| j                  d   |d<   | j                  r'| j                  D cg c]  }t        |       c}|d<   nd| j                  v r| j                  d   |d<   nnt        | t              rd|d<   nXt        | t              rd|d<   | j                  |d	<   n3t        | t              rd
|d<   | j                  |d<   nt        d|        d	| j                  v r| j                  d	   |d	<   |S c c}w )NrL   rJ   rK   rN   rP   rQ   rS   rT   rU   tooltool_call_idzGot unknown type )rL   
isinstancer   rJ   r!   r   rR   rQ   rz   r#   r   rU   r'   r}   
ValueError)messagemessage_dicttcs      r;   _convert_message_to_dictr      sb   $-w#?L';'&||V	G\	*%V	GY	'*Vg777,3,E,Eo,VL)@G@R@R*:<1"5*L& W666)0)B)B<)PL&	G]	+'V	G_	-)V&||V	G[	)%V'.';';^$,WI677***&88@V#*s   E)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                      e Zd ZU dZdZded<   dZded<   dZded	<   	 dZded
<   dZ	ded<   dZ
ded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   	  ee      Zded<   	 dZded<   	 dZded <   	 dZded!<   	 dZded"<   d#Zd$ed%<   ed3d&       Zed3d'       Z	 d4	 	 	 	 	 d5d(Z e!d6d)       Z"	 	 	 d7	 	 	 	 	 	 	 	 	 	 	 d8d*Z#d9d+Z$	 	 	 	 	 	 d:d,Z%	 	 d;	 	 	 	 	 	 	 	 	 d<d-Z&	 	 d;	 	 	 	 	 	 	 	 	 d=d.Z'	 	 	 d7	 	 	 	 	 	 	 	 	 	 	 d>d/Z(	 d4	 	 	 	 	 	 	 d? fd0Z)ed3d1       Z*ed@d2       Z+ xZ,S )Ar<   u;   DEPRECATED – use `langchain_litellm.ChatLiteLLM` instead.Nr   r`   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   intr?   c                    | j                   }| j                  | j                  }|| j                  | j                  | j                  | j
                  | j                  | j                  d| j                  S )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_paramszChatLiteLLM._default_params&  sm     **??&"ooO$!11//nn++#'#;#;	
 	
 		
r:   c                   | j                   }| j                  | j                  }| j                  | j                  _        | j                  | j                  _        dD ]8  }t        | |      x}st        | j                  |j                  dd      |       : | j                  | j                  _        || j                  | j                  d}i | j                  |S )z.Get the parameters used for the openai client.)r   r   r   r   r   r   _api_key_key)r   r   r   )r   r   r   r`   r   getattrsetattrreplacer   r   r   )r   r   named_api_keyapi_key_valuecredss        r;   _client_paramszChatLiteLLM._client_params7  s     **??&"ooO#}}"ll
 	M !(m <<}<KK!))*f=!	 $(#4#4 $!11!

 1$&&0%00r:   c                D     t         |      }|d fd       } |di |S )z*Use tenacity to retry the completion call.r]   c                 <     j                   j                  di | S r_   )r`   
completion)rb   r   s    r;   rc   zAChatLiteLLM.completion_with_retry.<locals>._completion_with_retry[  s    )4;;))3F33r:   rd   r9   rf   )r   r@   rb   rg   rc   s   `    r;   completion_with_retryz!ChatLiteLLM.completion_with_retryU  s3     2$KP		4 
	4 &///r:   c                B   	 ddl }t        |ddd      |d<   t        |dd	d      |d<   t        |d
dd      |d
<   t        |ddd      |d<   t        |ddd      |d<   t        |ddd      |d<   t        |ddd      |d<   t        |ddd      |d<   ||d<   |d   %d|d   cxk  rdk  st	        d       t	        d      |d   %d|d   cxk  rdk  st	        d       t	        d      |d   |d   dk  rt	        d      |S # t        $ r t        d      w xY w)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_KEYrO   )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_KEYr`   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)rA   ImportErrorr4   r.   r   )clsvaluesrA   s      r;   validate_environmentz ChatLiteLLM.validate_environmenta  s   	 $8$&6$
  #7O_b#
 ';')<b'
"# ';')<b'
"# (<(*>(
#$ $8$&6$
  )=)+@")
$% )=)+?)
$% #x- ,Q&:O5TST5TJKK 6UJKK'?&qF7O/Hq/HDEE 0IDEE'?&6'?a+?566S  	&? 	s   D	 	Dc                    ||n| j                   }|r! | j                  |f||d|}t        |      S | j                  ||      \  }}	i |	|}	 | j                  d||d|	}
| j                  |
      S )N)stopr@   messagesr@   r9   )r   _streamr   _create_message_dictsr   _create_chat_resultr   r   r   r@   r   rb   should_streamstream_itermessage_dictsparamsresponses              r;   	_generatezChatLiteLLM._generate  s     #)"4$..&$,,#@FK (44 $ : :8T Jv%F%f%-4-- 
"
?E
 ''11r:   c           	        g }|j                  di       }|d   D ]  }t        |d         }t        |t              r1d| j                  xs | j
                  i|_        t        |      |_        t        |t        |j                  d                  }|j                  |        | j
                  }| j                  | j                  }||d}t        ||	      S )
Nusagechoicesr   r   finish_reason)r   )r   generation_info)token_usager   )generations
llm_output)rX   r[   r~   r   r   r   response_metadata_create_usage_metadatausage_metadatar)   rY   appendr+   )	r   r   r   r   resr   genr   r   s	            r;   r   zChatLiteLLM._create_chat_result  s    ll7B/I& 	$C.s9~>G'9- $//"?TZZ-) *@)L&  $377?3K LC s#	$ **??&"ooO%0?K
kjIIr:   c                    | j                   }|d|v rt        d      ||d<   |D cg c]  }t        |       }}||fS c c}w )Nr   z2`stop` found in both the input and default params.)r   r   r   )r   r   r   r   mr   s         r;   r   z!ChatLiteLLM._create_message_dicts  s\     $$ !UVV!F6N>FG1!4GGf$$ Hs   ?c              +  D  K   | j                  ||      \  }}i ||ddi}t        }d} | j                  d||d|D ]  }	t        |	t              s|	j                         }	t        |	d         dk(  r5|	d   d   d   }
|	j                  di       }t        |
|      }	t        |	t              r5|s#d	| j                  xs | j                  i|	_        d}t        |      |	_        |	j                  }t        |	
      }|r|j!                  |	j"                  |       |  y w)Nr   TFr   r   r   deltar   r   r   chunkr9   )r   r   r   r~   rY   
model_dumplenrX   rt   r   r   r   r   r   	__class__r*   on_llm_new_tokenrL   r   r   r   r@   rb   r   r   default_chunk_classadded_model_namer   r   r   cg_chunks                r;   r   zChatLiteLLM._stream  s@     !% : :8T Jv5F5f5h5, /T// 
"
?E
 	E eT*((*5#$))$Q'0EIIgr*E3E;NOE%0'$doo&C/E+ (,$'=e'D$"'//*59H,,U]](,KN+	s   DD c               r  K   | j                  ||      \  }}i ||ddi}t        }d}t        | f||d| d {   2 3 d {   }	t        |	t              s|	j                         }	t        |	d         dk(  r;|	d   d   d   }
|	j                  di       }t        |
|      }	t        |	t              r5|s#d	| j                  xs | j                  i|	_        d}t        |      |	_        |	j                  }t        |	
      }|r%|j!                  |	j"                  |       d {    | 7 7 7 6 y w)Nr   TFr   r   r   r   r   r   r   r   )r   r   rh   r~   rY   r   r   rX   rt   r   r   r   r   r   r   r*   r   rL   r   s                r;   _astreamzChatLiteLLM._astream  s^     !% : :8T Jv5F5f5h5, !7"
(k"
EK"
 
 	 	% eT*((*5#$))$Q'0EIIgr*E3E;NOE%0'$doo&C/E+ (,$'=e'D$"'//*59H!225==2QQQN+
 	( R)
sF   :D7D/D7D5D1D5	CD7$D3%D71D53D75D7c                  K   ||n| j                   }|r) | j                  d|||d|}t        |       d {   S | j                  ||      \  }}	i |	|}	t	        | f||d|	 d {   }
| j                  |
      S 7 G7 w)N)r   r   r@   r   r9   )r   r   r   r   rh   r   r   s              r;   
_ageneratezChatLiteLLM._agenerate  s      #)"4$..'$-- !+IOK /{;;; $ : :8T Jv%F%f%/
(k
EK
 
 ''11 <
s!   7BB1B+B,BBc                :   |D cg c]  }t        |       }}| j                  d| j                  v sV| j                  d| j                  v s<| j                  | j                  t        v s| j                  *| j                  t        v rdk(  st	        t
              rdn^t	        t
              rdnKt	        t              r;|D cg c]
  }|d   d    }}t        fd|D              st        d d| d	      t        | (  d|d
|S c c}w c c}w )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requiredrT   rU   c              3  4   K   | ]  }|d    d   k(    yw)rT   rU   Nr9   ).0	tool_nametool_choices     r;   	<genexpr>z)ChatLiteLLM.bind_tools.<locals>.<genexpr>U  s%      AJ	[4V<<s   zTool choice z1 was specified, but the only provided tools were .)toolsr   r9   )r0   r   r   _OPENAI_MODELSr~   r   rY   r   r   superbind)	r   r  r   rb   r|   formatted_toolsformatted_tool
tool_namesr   s	     `     r;   
bind_toolszChatLiteLLM.bind_tools#  s.   @ EJJD1$7JJ
 ZZ#4::(=+4??0J

&4::+G+>0Qe#z+t'D$KT*KT*IX7Ez*62J   NX  !";- 0++5,a9  w|U/{UfUU3 Ks   D
Dc                    | j                   }| j                  | j                  }|| j                  | j                  | j                  | j
                  dS )zGet the identifying parameters.)r   r   r   r   r   )r   r   r   r   r   r   r   s     r;   _identifying_paramszChatLiteLLM._identifying_params^  sK     **??&"ooO$++ZZZZ
 	
r:   c                     y)Nzlitellm-chatr9   )r   s    r;   	_llm_typezChatLiteLLM._llm_typel  s    r:   )re   r   N)r@   "Optional[CallbackManagerForLLMRun]rb   r   re   r   )r   r   re   r   )NNN)r   List[BaseMessage]r   Optional[List[str]]r@   r  r   Optional[bool]rb   r   re   r+   )r   Mapping[str, Any]re   r+   )r   r  r   r  re   z+Tuple[List[Dict[str, Any]], Dict[str, Any]])NN)
r   r  r   r  r@   r  rb   r   re   zIterator[ChatGenerationChunk])
r   r  r   r  r@   'Optional[AsyncCallbackManagerForLLMRun]rb   r   re   z"AsyncIterator[ChatGenerationChunk])r   r  r   r  r@   r  r   r  rb   r   re   r+   )r  zDSequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]]r   zLOptional[Union[dict, str, Literal['auto', 'none', 'required', 'any'], bool]]rb   r   re   z)Runnable[LanguageModelInput, BaseMessage])re   r   )-r5   r6   r7   r8   r`   __annotations__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r2   rY   r   r   r   r   r   r?   propertyr   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v 
 
  r:   c                l    | j                  dd      }| j                  dd      }t        ||||z         S )Nprompt_tokensr   completion_tokens)input_tokensoutput_tokenstotal_tokens)rX   r(   )r   r  r  s      r;   r   r   q  s>    ???A6LOO$7;M!#!M1 r:   r  )rF   r<   r@   zHOptional[Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun]]re   zCallable[[Any], Any])rZ   r  re   r   )rF   r<   r@   r  rb   r   re   r   )rZ   r  rq   zType[BaseMessageChunk]re   r   )ry   r%   re   rY   )r   r   re   rY   )r   r  re   r(   )Pr8   
__future__r   rw   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   	getLoggerr5   logger	Exceptionr4   rH   r[   rh   rt   rz   r   r  r<   r   r9   r:   r;   <module>r0     s[   #       7 > 
 L    " 5 
 . ) ? H %			8	$/9 / 			
 (@6 <@2	282 2 		2 +.+.-C+.+.\>6 
6
m- m
m`r:   