a
    bg3T                     @  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ZBd1ddddddZCdddddZDd2dddddd d!ZEdd"d#d$d%d&ZFd'd(d)d*d+ZGdd(d,d-d.ZHg d/ZIG d0d deZJdS )3z+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   u/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/chat_models/litellm.pyr1   A   s   r1   ChatLiteLLMzHOptional[Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun]]zCallable[[Any], Any])llmrun_managerreturnc                 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<   )r9   r:   r=   errorsr6   r6   r7   _create_retry_decoratorE   s    r@   Mapping[str, Any]r   )_dictr;   c                 C  s   | d }|dkrt | d dS |dkrv| ddp4d}i }| drTt| d |d< | drj| d |d< t||d	S |d
krt| d dS |dkrt| d | d dS t| d |dS d S )NroleusercontentrE   	assistant function_call
tool_calls)rE   additional_kwargssystemfunctionnamerE   rN   rE   rC   )r   getdictr   r!   r   r   )rB   rC   rE   rK   r6   r6   r7   _convert_dict_to_messageY   s     

rS   'Optional[AsyncCallbackManagerForLLMRun]r   )r9   r:   kwargsr;   c                   s8   t  |d}|ddd fdd}|f i |I dH S )z0Use tenacity to retry the async completion call.r:   r   rU   r;   c                    s    j jf i | I d H S N)clientZacreaterU   r9   r6   r7   _completion_with_retryz   s    z6acompletion_with_retry.<locals>._completion_with_retryNr@   )r9   r:   rU   retry_decoratorr\   r6   r[   r7   acompletion_with_retryr   s    r_   zType[BaseMessageChunk]r   )rB   default_classr;   c                 C  s  |  d}|  dpd}|  dr4dt| d i}ni }g }|  d }rx||d< zdd |D }W n tyv   Y n0 |dks|tkrt|d	S |d
ks|tkrt|||dS |dks|tkrt|d	S |dks|tkrt|| d dS | s|tkrt||dS ||d	S d S )NrC   rE   rH   rI   rJ   c                 S  s:   g | ]2}t |d  d|d  d|d|d dqS )rM   rN   	argumentsidindex)rN   argsrb   rc   )r$   rQ   ).0Zrtcr6   r6   r7   
<listcomp>   s   z3_convert_delta_to_message_chunk.<locals>.<listcomp>rD   rF   rG   )rE   rK   tool_call_chunksrL   rM   rN   rO   rP   )rQ   rR   KeyErrorr    r   r"   r   r   )rB   r`   rC   rE   rK   rg   Zraw_tool_callsr6   r6   r7   _convert_delta_to_message_chunk   s:    


	

ri   r#   rR   )	tool_callr;   c                 C  s$   d| d | d t | d ddS )NrM   rb   rN   rd   )rN   ra   )typerb   rM   )jsondumps)rj   r6   r6   r7   !_lc_tool_call_to_openai_tool_call   s    rn   )messager;   c                 C  s  d| j i}t| tr | j|d< nt| tr4d|d< nt| trd|d< d| jv r^| jd |d< | jrzdd | jD |d< qd| jv r| jd |d< n^t| trd	|d< nJt| t	rd
|d< | j
|d< n,t| trd|d< | j|d< ntd|  d| jv r| jd |d< |S )NrE   rC   rD   rG   rI   c                 S  s   g | ]}t |qS r6   )rn   )re   Ztcr6   r6   r7   rf      s   z,_convert_message_to_dict.<locals>.<listcomp>rJ   rL   rM   rN   tooltool_call_idzGot unknown type )rE   
isinstancer   rC   r   r   rK   rJ   r!   r   rN   r%   rq   
ValueError)ro   Zmessage_dictr6   r6   r7   _convert_message_to_dict   s6    










rt   )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                      s4  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dd'd(d)Zedd'd*d+ZdWd,ddd-d.d/Z e!d0d0d1d2d3Z"dXd4d5d,d6dd7d8d9d:Z#d;d7d<d=d>Z$d4d5d?d@dAdBZ%dYd4d5d,ddCdDdEdFZ&dZd4d5dGddHdDdIdJZ'd[d4d5dGd6dd7d8dKdLZ(d\dMdNddOdP fdQdRZ)edd'dSdTZ*edd'dUdVZ+  Z,S )]r8   z%Chat model that uses the LiteLLM API.Nr   rY   ru   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_factoryz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)rw   force_timeoutr   streamr   r   r   )	rw   rx   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)ry   rz   r{   r|   r}   r~   Z_api_key_key)rw   r   r   )rw   rx   r   rY   r   getattrsetattrreplacer   r   r   )r   r   Znamed_api_keyZapi_key_valuecredsr6   r6   r7   _client_params(  s$    




zChatLiteLLM._client_paramsz"Optional[CallbackManagerForLLMRun])r:   rU   r;   c                   s2   t  |d}|ddd fdd}|f i |S )z*Use tenacity to retry the completion call.rV   r   rW   c                    s    j jf i | S rX   )rY   
completionrZ   r   r6   r7   r\   L  s    zAChatLiteLLM.completion_with_retry.<locals>._completion_with_retryr]   )r   r:   rU   r^   r\   r6   r   r7   completion_with_retryF  s    z!ChatLiteLLM.completion_with_retryr   )valuesr;   c                 C  sZ  zddl }W n ty&   tdY n0 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rdksn td|d dur2d|d   kr(dks2n td|d durV|d dkrVt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`ry   ZOPENAI_API_KEYrH   )defaultrz   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_KEYrY   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+   rs   )clsr   r=   r6   r6   r7   validate_environmentR  sL    








&,z ChatLiteLLM.validate_environmentzList[BaseMessage]zOptional[List[str]]zOptional[bool]r(   )messagesstopr:   r   rU   r;   c                 K  sr   |d ur|n| j }|r6| j|f||d|}t|S | ||\}}	i |	|}	| jf ||d|	}
| |
S )N)r   r:   r   r:   )r   _streamr   _create_message_dictsr   _create_chat_resultr   r   r   r:   r   rU   Zshould_streamZstream_itermessage_dictsparamsresponser6   r6   r7   	_generate  s$    zChatLiteLLM._generaterA   )r   r;   c           	      C  sx   g }|d D ]2}t |d }t|t|ddd}|| q|di }| j}| jd urb| j}||d}t||dS )	Nchoicesro   finish_reason)r   )ro   Zgeneration_infousage)token_usagerw   )generations
llm_output)rS   r&   rR   rQ   appendrw   rx   r(   )	r   r   r   resro   genr   r   r   r6   r6   r7   r     s    

zChatLiteLLM._create_chat_resultz+Tuple[List[Dict[str, Any]], Dict[str, Any]])r   r   r;   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  s   g | ]}t |qS r6   )rt   )re   mr6   r6   r7   rf         z5ChatLiteLLM._create_message_dicts.<locals>.<listcomp>)r   rs   )r   r   r   r   r   r6   r6   r7   r     s    z!ChatLiteLLM._create_message_dictszIterator[ChatGenerationChunk])r   r   r:   rU   r;   c                 k  s   |  ||\}}i ||ddi}t}| jf ||d|D ]l}t|tsT| }t|d dkrfq>|d d d }	t|	|}|j}t	|d}
|r|j
|j|
d |
V  q>d S 	Nr   Tr   r   r   delta)ro   )chunk)r   r   r   rr   rR   
model_dumplenri   	__class__r'   on_llm_new_tokenrE   r   r   r   r:   rU   r   r   Zdefault_chunk_classr   r   Zcg_chunkr6   r6   r7   r     s&    



zChatLiteLLM._streamrT   z"AsyncIterator[ChatGenerationChunk]c                 K s   |  ||\}}i ||ddi}t}t| f||d|I d H 2 zz3 d H W }t|tsb| }t|d dkrtqD|d d d }	t|	|}|j}t	|d}
|r|j
|j|
dI d H  |
V  qD6 d S r   )r   r   r_   rr   rR   r   r   ri   r   r'   r   rE   r   r6   r6   r7   _astream  s*    


zChatLiteLLM._astreamc                   s~   |d ur|n| j }|r<| jf |||d|}t|I d H S | ||\}}	i |	|}	t| f||d|	I d H }
| |
S )N)r   r   r:   r   )r   r   r   r   r_   r   r   r6   r6   r7   
_agenerate  s$    zChatLiteLLM._ageneratezDSequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]]zLOptional[Union[dict, str, Literal['auto', 'none', 'required', 'any'], bool]]z)Runnable[LanguageModelInput, BaseMessage])toolstool_choicerU   r;   c                   s   dd |D }| j dur"d| j v s^| jdur6d| jv s^| j durJ| j tv s^| jdurv| jtv rv dkspt trvd nTt trd nDt trdd |D }t fdd	|D std
  d| dt j	f | 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  s   g | ]}t |qS r6   r-   )re   rp   r6   r6   r7   rf     r   z*ChatLiteLLM.bind_tools.<locals>.<listcomp>NZazureanyrequiredc                 S  s   g | ]}|d  d qS )rM   rN   r6   )re   Zformatted_toolr6   r6   r7   rf   ,  s   c                 3  s   | ]}| d  d kV  qdS )rM   rN   Nr6   )re   Z	tool_namer   r6   r7   	<genexpr>/  s   z)ChatLiteLLM.bind_tools.<locals>.<genexpr>zTool choice z1 was specified, but the only provided tools were .)r   r   )
rw   rx   _OPENAI_MODELSrr   r   rR   r   rs   superbind)r   r   r   rU   Zformatted_toolsZ
tool_namesr   r   r7   
bind_tools  sJ     

zChatLiteLLM.bind_toolsc                 C  s.   | j }| jdur| j}|| j| j| j| jdS )zGet the identifying parameters.N)rw   r   r   r   r   )rw   rx   r   r   r   r   r   r6   r6   r7   _identifying_params8  s    
zChatLiteLLM._identifying_paramsc                 C  s   dS )Nzlitellm-chatr6   r   r6   r6   r7   	_llm_typeF  s    zChatLiteLLM._llm_type)N)NNN)NN)NN)NNN)N)-r2   r3   r4   r5   rY   __annotations__rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r0   rR   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   r8      sj   
 2           ;)N)N)Kr5   
__future__r   rl   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   r@   rS   r_   ri   rn   rt   r   r8   r6   r6   r6   r7   <module>   s6   <D
  ,