o
    ZhT                     @  s  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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
l.m/Z/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= e>e?Z@G dd deAZB	d3d4ddZCd5ddZD	d3d6d"d#ZEd7d'd(ZFd8d,d-ZGd9d/d0ZHg d1ZIG d2d deZJdS ):z+Wrapper around LiteLLM's model I/O library.    )annotationsN)AnyAsyncIteratorCallableDictIteratorListLiteralMappingOptionalSequenceTupleTypeUnion)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)ChatGenerationChatGenerationChunk
ChatResult)Runnable)BaseTool)get_from_dict_or_envpre_initconvert_to_openai_tool)	BaseModelFieldc                   @  s   e Zd ZdZdS )ChatLiteLLMExceptionz$Error with the `LiteLLM I/O` libraryN)__name__
__module____qualname____doc__ r6   r6   ^/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/chat_models/litellm.pyr1   A   s    r1   llmChatLiteLLMrun_managerHOptional[Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun]]returnCallable[[Any], Any]c                 C  s,   ddl }|j|j|j|jg}t|| j|dS )zKReturns a tenacity retry decorator, preconfigured to handle PaLM exceptionsr   N)Zerror_typesmax_retriesr:   )litellmTimeoutZAPIErrorZAPIConnectionErrorZRateLimitErrorr   r>   )r8   r:   r?   errorsr6   r6   r7   _create_retry_decoratorE   s   rB   _dictMapping[str, Any]r   c                 C  s   | d }|dkrt | d dS |dkr;| ddpd}i }| dr*t| d |d< | dr5| d |d< t||d	S |d
krFt| d dS |dkrTt| d | d dS t| d |dS )NroleusercontentrG   	assistant function_call
tool_calls)rG   additional_kwargssystemfunctionnamerG   rP   rG   rE   )r   getdictr   r!   r   r   )rC   rE   rG   rM   r6   r6   r7   _convert_dict_to_messageY   s    

rU   'Optional[AsyncCallbackManagerForLLMRun]kwargsr   c                   s4   t  |d}|d fdd}|d	i |I dH S )
z0Use tenacity to retry the async completion call.r:   rW   r   r<   c                    s    j jdi | I d H S Nr6   )clientZacreaterW   r8   r6   r7   _completion_with_retryz   s   z6acompletion_with_retry.<locals>._completion_with_retryNrW   r   r<   r   r6   rB   )r8   r:   rW   retry_decoratorr]   r6   r\   r7   acompletion_with_retryr   s
   ra   default_classType[BaseMessageChunk]r   c                 C  s  |  d}|  dpd}|  drdt| d i}ni }g }|  d }r<||d< z	dd |D }W n	 ty;   Y nw |dksD|tkrIt|d	S |d
ksQ|tkrXt|||dS |dks`|tkret|d	S |dksm|tkrut|| d dS |s{|tkrt||dS ||d	S )NrE   rG   rJ   rK   rL   c                 S  s:   g | ]}t |d  d|d  d|d|d dqS )rO   rP   	argumentsidindex)rP   argsre   rf   )r$   rS   ).0Zrtcr6   r6   r7   
<listcomp>   s    z3_convert_delta_to_message_chunk.<locals>.<listcomp>rF   rH   rI   )rG   rM   tool_call_chunksrN   rO   rP   rQ   rR   )rS   rT   KeyErrorr    r   r"   r   r   )rC   rb   rE   rG   rM   rj   Zraw_tool_callsr6   r6   r7   _convert_delta_to_message_chunk   s<   


	


rl   	tool_callr#   rT   c                 C  s$   d| d | d t | d ddS )NrO   re   rP   rg   )rP   rd   )typere   rO   )jsondumps)rm   r6   r6   r7   !_lc_tool_call_to_openai_tool_call   s   rq   messagec                 C  s  d| j i}t| tr| j|d< nit| trd|d< n_t| trJd|d< d| jv r/| jd |d< | jr=dd | jD |d< n<d| jv rI| jd |d< n/t| trTd	|d< n%t| t	rcd
|d< | j
|d< nt| trrd|d< | j|d< ntd|  d| jv r| jd |d< |S )NrG   rE   rF   rI   rK   c                 S     g | ]}t |qS r6   )rq   )rh   Ztcr6   r6   r7   ri      s    z,_convert_message_to_dict.<locals>.<listcomp>rL   rN   rO   rP   tooltool_call_idzGot unknown type )rG   
isinstancer   rE   r   r   rM   rL   r!   r   rP   r%   ru   
ValueError)rr   Zmessage_dictr6   r6   r7   _convert_message_to_dict   s8   











rx   )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-0613c                      s  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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dYd(d)ZedYd*d+Z	dZd[d/d0Z e!d\d3d4Z"			d]d^d<d=Z#d_d@dAZ$d`dCdDZ%		dadbdFdGZ&		dadcdJdKZ'			d]dddLdMZ(	dZde fdSdTZ)edYdUdVZ*edfdWdXZ+  Z,S )gr9   z%Chat model that uses the LiteLLM API.Nr   rZ   ry   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_timeout   zOptional[float]temperature)default_factoryDict[str, Any]model_kwargstop_pzOptional[int]top_kintn
max_tokens   r>   r<   c                 C  s<   | j }| jdur| j}|| j| j| j| j| j| jd| jS )z2Get the default parameters for calling OpenAI API.N)r{   force_timeoutr   streamr   r   r   )	r{   r|   r   r   r   r   r   r   r   selfset_model_valuer6   r6   r7   _default_params  s   
zChatLiteLLM._default_paramsc                 C  s   | j }| jdur| j}| j| j_| j| j_dD ]}t| | }r+t| j|dd| q| j| j_|| j	| jd}i | j
|S )z.Get the parameters used for the openai client.N)r}   r~   r   r   r   r   Z_api_key_key)r{   r   r   )r{   r|   r   rZ   r   getattrsetattrreplacer   r   r   )r   r   Znamed_api_keyZapi_key_valuecredsr6   r6   r7   _client_params)  s&   




zChatLiteLLM._client_paramsr:   "Optional[CallbackManagerForLLMRun]rW   c                   s,   t  |d}|d fdd}|d	i |S )
z*Use tenacity to retry the completion call.rX   rW   r   r<   c                    s    j jdi | S rY   )rZ   
completionr[   r   r6   r7   r]   M  s   zAChatLiteLLM.completion_with_retry.<locals>._completion_with_retryNr^   r6   r_   )r   r:   rW   r`   r]   r6   r   r7   completion_with_retryG  s   z!ChatLiteLLM.completion_with_retryvaluesr   c                 C  sX  zddl }W n ty   tdw t|dddd|d< t|dd	dd|d< t|d
ddd|d
< t|dddd|d< t|dddd|d< t|dddd|d< t|dddd|d< t|dddd|d< ||d< |d durd|d   kr{dkstd td|d durd|d   krdkstd td|d dur|d dkrtd|S )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}   ZOPENAI_API_KEYrJ   )defaultr~   ZAZURE_API_KEYr   ZANTHROPIC_API_KEYr   ZREPLICATE_API_KEYr   ZOPENROUTER_API_KEYr   ZCOHERE_API_KEYZhuggingface_api_keyZHUGGINGFACE_API_KEYZtogether_ai_api_keyZTOGETHERAI_API_KEYrZ   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)r?   ImportErrorr1   r+   rw   )clsr   r?   r6   r6   r7   validate_environmentS  sV   







""z ChatLiteLLM.validate_environmentmessagesList[BaseMessage]stopOptional[List[str]]r   Optional[bool]r(   c                 K  sr   |d ur|n| j }|r| j|f||d|}t|S | ||\}}	i |	|}	| jd||d|	}
| |
S )N)r   r:   r   r:   r6   )r   _streamr   _create_message_dictsr   _create_chat_resultr   r   r   r:   r   rW   Zshould_streamZstream_itermessage_dictsparamsresponser6   r6   r7   	_generate  s$   
zChatLiteLLM._generater   rD   c           	      C  sx   g }|d D ]}t |d }t|t|ddd}|| q|di }| j}| jd ur1| j}||d}t||dS )	Nchoicesrr   finish_reason)r   )rr   Zgeneration_infousage)token_usager{   )generations
llm_output)rU   r&   rT   rS   appendr{   r|   r(   )	r   r   r   resrr   genr   r   r   r6   r6   r7   r     s   

zChatLiteLLM._create_chat_result+Tuple[List[Dict[str, Any]], Dict[str, Any]]c                 C  s<   | j }|d urd|v rtd||d< dd |D }||fS )Nr   z2`stop` found in both the input and default params.c                 S  rs   r6   )rx   )rh   mr6   r6   r7   ri         z5ChatLiteLLM._create_message_dicts.<locals>.<listcomp>)r   rw   )r   r   r   r   r   r6   r6   r7   r     s   z!ChatLiteLLM._create_message_dictsIterator[ChatGenerationChunk]c                 k  s    |  ||\}}i ||ddi}t}| jd	||d|D ]6}t|ts+| }t|d dkr4q |d d d }	t|	|}|j}t	|d}
|rS|j
|j|
d |
V  q d S )
Nr   Tr   r   r   deltarr   chunkr6   )r   r   r   rv   rT   
model_dumplenrl   	__class__r'   on_llm_new_tokenrG   r   r   r   r:   rW   r   r   Zdefault_chunk_classr   r   Zcg_chunkr6   r6   r7   r     s*   



zChatLiteLLM._streamrV   "AsyncIterator[ChatGenerationChunk]c                 K s   |  ||\}}i ||ddi}t}t| f||d|I d H 2 z=3 d H W }t|ts2| }t|d dkr;q#|d d d }	t|	|}|j}t	|d}
|r]|j
|j|
dI d H  |
V  q#6 d S )	Nr   Tr   r   r   r   r   r   )r   r   ra   rv   rT   r   r   rl   r   r'   r   rG   r   r6   r6   r7   _astream  s.   


zChatLiteLLM._astreamc                   s   |d ur|n| j }|r| jd|||d|}t|I d H S | ||\}}	i |	|}	t| f||d|	I d H }
| |
S )N)r   r   r:   r   r6   )r   r   r   r   ra   r   r   r6   r6   r7   
_agenerate  s&   
zChatLiteLLM._ageneratetoolsDSequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]]tool_choiceLOptional[Union[dict, str, Literal['auto', 'none', 'required', 'any'], bool]])Runnable[LanguageModelInput, BaseMessage]c                   s   dd |D }| j durd| j v s/| jdurd| jv s/| j dur%| j tv s/| jdur;| jtv r; dks8t tr;d n*t trCd n"t tredd |D }t fdd	|D setd
  d| dt j	d| d|S )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.
        c                 S  rs   r6   r-   )rh   rt   r6   r6   r7   ri     r   z*ChatLiteLLM.bind_tools.<locals>.<listcomp>NZazureanyrequiredc                 S  s   g | ]}|d  d qS )rO   rP   r6   )rh   Zformatted_toolr6   r6   r7   ri   -  s    c                 3  s     | ]}| d  d kV  qdS )rO   rP   Nr6   )rh   Z	tool_namer   r6   r7   	<genexpr>0  s    
z)ChatLiteLLM.bind_tools.<locals>.<genexpr>zTool choice z1 was specified, but the only provided tools were .)r   r   r6   )
r{   r|   _OPENAI_MODELSrv   r   rT   r   rw   superbind)r   r   r   rW   Zformatted_toolsZ
tool_namesr   r   r7   
bind_tools  s,    

zChatLiteLLM.bind_toolsc                 C  s.   | j }| jdur| j}|| j| j| j| jdS )zGet the identifying parameters.N)r{   r   r   r   r   )r{   r|   r   r   r   r   r   r6   r6   r7   _identifying_params9  s   
zChatLiteLLM._identifying_paramsc                 C  s   dS )Nzlitellm-chatr6   r   r6   r6   r7   	_llm_typeG  s   zChatLiteLLM._llm_type)r<   r   N)r:   r   rW   r   r<   r   )r   r   r<   r   )NNN)r   r   r   r   r:   r   r   r   rW   r   r<   r(   )r   rD   r<   r(   )r   r   r   r   r<   r   )NN)
r   r   r   r   r:   r   rW   r   r<   r   )
r   r   r   r   r:   rV   rW   r   r<   r   )r   r   r   r   r:   rV   r   r   rW   r   r<   r(   )r   r   r   r   rW   r   r<   r   )r<   rz   )-r2   r3   r4   r5   rZ   __annotations__r{   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r0   rT   r   r   r   r   r   r>   propertyr   r   r   r,   r   r   r   r   r   r   r   r   r   r   __classcell__r6   r6   r   r7   r9      sx   
 2

;r   )r8   r9   r:   r;   r<   r=   )rC   rD   r<   r   )r8   r9   r:   rV   rW   r   r<   r   )rC   rD   rb   rc   r<   r   )rm   r#   r<   rT   )rr   r   r<   rT   )Kr5   
__future__r   ro   loggingtypingr   r   r   r   r   r   r	   r
   r   r   r   r   r   Zlangchain_core.callbacksr   r   Zlangchain_core.language_modelsr   Z*langchain_core.language_models.chat_modelsr   r   r   Z#langchain_core.language_models.llmsr   Zlangchain_core.messagesr   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   Zlangchain_core.outputsr&   r'   r(   Zlangchain_core.runnablesr)   Zlangchain_core.toolsr*   Zlangchain_core.utilsr+   r,   Z%langchain_core.utils.function_callingr.   Zpydanticr/   r0   	getLoggerr2   logger	Exceptionr1   rB   rU   ra   rl   rq   rx   r   r9   r6   r6   r6   r7   <module>   s8    <D



,
