a
    bgpK                     @  sL  d Z ddlmZ ddlZddl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 ddlZddl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- ddl.m/Z/ ddl.m0Z1 ddl2m3Z3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z?m@Z@mAZAmBZB ddlCmDZD ddlEmFZF eGeHZIG dd deJZKdEddddddZLdddd d!ZMdd"dd#d$ZNd%d&d'd(d)ZOd%d*d+d,d-d.ZPd&dd/d0d1ZQG d2d deZRd3d4d5d6d7ZSd8d9d5d:d;ZTd<d=d>d?d@ZUdAdBd>dCdDZVdS )Fz!deepinfra.com chat models wrapper    )annotationsN)JSONDecodeError)AnyAsyncIteratorCallableDictIteratorList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ToolMessage)ToolCall	tool_call)ChatGenerationChatGenerationChunk
ChatResult)Runnable)BaseToolget_from_dict_or_envconvert_to_openai_tool)	BaseModel
ConfigDictFieldmodel_validator)Self)Requestsc                   @  s   e Zd ZdZdS )ChatDeepInfraExceptionz9Exception raised when the DeepInfra API returns an error.N)__name__
__module____qualname____doc__ r;   r;   w/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/chat_models/deepinfra.pyr6   F   s   r6   ChatDeepInfrazHOptional[Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun]]zCallable[[Any], Any])llmrun_managerreturnc                 C  s   t tjjtg| j|dS )zLReturns a tenacity retry decorator, preconfigured to handle PaLM exceptions.)Zerror_typesmax_retriesr?   )r   requests
exceptionsConnectTimeoutr6   rA   )r>   r?   r;   r;   r<   _create_retry_decoratorL   s
    
rE   dictr$   )r&   r@   c              	   C  sZ   | d  dd}zt| d d }W n ttfy@   i }Y n0 |  d}t|||dS )zw
    Convert a tool calling response from server to a ToolCall object.
    Args:
        tool_call:

    Returns:

    functionname 	argumentsid)rH   argsrK   )getjsonloadsr   	TypeErrorcreate_tool_call)r&   rH   rL   rK   r;   r;   r<   _parse_tool_callingZ   s    	

rR   Dict[str, Any]c                 C  s&   dt | d | d d| ddS )zu
    Convert a ToolCall object to a tool calling request for server.
    Args:
        tool_call:

    Returns:

    rG   rL   rH   )rJ   rH   rK   )typerG   rK   )rN   dumpsrM   r%   r;   r;   r<   _convert_to_tool_callingl   s    
rV   Mapping[str, Any]r   )_dictr@   c                 C  s   | d }|dkrt | d dS |dkr`| ddp4d}| dg pDg }dd	 |D }t||d
S |dkrvt| d dS |dkrt| d | d dS t| d |dS d S )Nroleusercontentr[   	assistantrI   
tool_callsc                 S  s   g | ]}t |qS r;   rR   .0r&   r;   r;   r<   
<listcomp>   s   z,_convert_dict_to_message.<locals>.<listcomp>r[   r^   systemrG   rH   r[   rH   r[   rY   )r   rM   r   r!   r   r   )rX   rY   r[   Ztool_calls_contentr^   r;   r;   r<   _convert_dict_to_message   s    rg   zType[BaseMessageChunk]r   )rX   default_classr@   c                 C  s   |  d}|  dpd}|  dp$g }|dks6|tkr@t|dS |dksP|tkrjdd	 |D }t||d
S |dksz|tkrt|dS |dks|tkrt|| d dS |s|tkrt||dS ||dS d S )NrY   r[   rI   r^   rZ   r\   r]   c                 S  s   g | ]}t |qS r;   r_   r`   r;   r;   r<   rb          z3_convert_delta_to_message_chunk.<locals>.<listcomp>rc   rd   rG   rH   re   rf   )rM   r    r   r"   r   r   )rX   rh   rY   r[   r^   r;   r;   r<   _convert_delta_to_message_chunk   s    


rj   )messager@   c                 C  s   t | tr| j| jd}nt | tr2d| jd}nt | tr\dd | jD }d| j|d}nbt | trtd| jd}nJt | trd| j| j	d	}n.t | t
rd
| j| j	| jd}ntd|  d| jv r| jd |d< |S )N)rY   r[   rZ   c                 S  s   g | ]}t |qS r;   )rV   r`   r;   r;   r<   rb      s   z,_convert_message_to_dict.<locals>.<listcomp>r]   )rY   r[   r^   rd   rG   )rY   r[   rH   tool)rY   r[   rH   tool_call_idzGot unknown type rH   )
isinstancer   rY   r[   r   r   r^   r!   r   rH   r#   rm   
ValueErrorZadditional_kwargs)rk   Zmessage_dictr^   r;   r;   r<   _convert_message_to_dict   s:    






rp   c                      sF  e Zd ZU dZedddZded< dZded< d	Zd
ed< ed	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< dZded< edd Zedd!d"d#Zedd!d$d%Zddd&d'd'd(d)d*Zded+d'd'd(d,d-Zed.d/ed0d'd1d2d3Zed4d/d5d!d6d7Zdfd8d9d&d:d'd;d<d=d>Zd?d;d@dAdBZd8d9dCdDdEdFZ dgd8d9d&d'dGdHdIdJZ!dhd8d9d+d'dKdHdLdMZ"did8d9d+d:d'd;d<dNdOZ#edd!dPdQZ$edd!dRdSZ%dd'dTdUdVdWZ&dd!dXdYZ'd0d!dZd[Z(d'd0d\d]d^Z)d_d'd`da fdbdcZ*  Z+S )jr=   z)A chat model that uses the DeepInfra API.zmeta-llama/Llama-2-70b-chat-hfmodel)defaultaliasstr
model_namez4https://api.deepinfra.com/v1/openai/chat/completionsurlNOptional[str]deepinfra_api_tokentimeoutzOptional[float]request_timeout   temperature)default_factoryrS   model_kwargstop_pzOptional[int]top_kintn   
max_tokensFbool	streamingrA   T)Zpopulate_by_name)r@   c                 C  s$   | j | j| j| j| j| jd| jS )z2Get the default parameters for calling OpenAI API.)rq   r   streamr   r|   rz   )ru   r   r   r   r|   rz   r~   selfr;   r;   r<   _default_params   s    zChatDeepInfra._default_paramsc                 C  s
   i | j S )z.Get the parameters used for the openai client.)r   r   r;   r;   r<   _client_params   s    zChatDeepInfra._client_paramsz"Optional[CallbackManagerForLLMRun]r   )r?   kwargsr@   c                   s2   t  |d}|ddd fdd}|f i |S )z*Use tenacity to retry the completion call.r?   r   r   r@   c               
     s~   zH|  d}t  d}|j   | |d} |j|j |W S  t	yx } zt
d|  W Y d }~n
d }~0 0 d S Nrz   headersrv   datary   ZEX)popr5   _headerspost_url_body_handle_statusstatus_codetext	Exceptionprintr   rz   requestresponseer   r;   r<   _completion_with_retry  s    

zCChatDeepInfra.completion_with_retry.<locals>._completion_with_retryrE   r   r?   r   Zretry_decoratorr   r;   r   r<   completion_with_retry   s    z#ChatDeepInfra.completion_with_retryz'Optional[AsyncCallbackManagerForLLMRun]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   r   c               
     s   z|  d}t  d}|j   | |d4 I d H >} |j| I d H  |	 I d H W  d   I d H  W S 1 I d H s0    Y  W n0 t
y } ztd|  W Y d }~n
d }~0 0 d S r   )r   r5   r   apostr   r   r   statusr   rN   r   r   r   r   r;   r<   r     s    
>
zDChatDeepInfra.acompletion_with_retry.<locals>._completion_with_retryNr   r   r;   r   r<   acompletion_with_retry  s    z$ChatDeepInfra.acompletion_with_retrybefore)moder   )valuesr@   c                 C  s(   t |dddd}t |dd|d|d< |S )zGValidate api key, python package exists, temperature, top_p, and top_k.Zdeepinfra_api_keyZDEEPINFRA_API_KEYrI   )rr   rx   ZDEEPINFRA_API_TOKENr,   )clsr   Zapi_keyr;   r;   r<   init_defaults-  s    
zChatDeepInfra.init_defaultsafterr4   c                 C  st   | j d ur*d| j   kr dks*n td| jd urTd| j  krJdksTn td| jd urp| jdkrptd| S )Nr   r{   z+temperature must be in the range [0.0, 1.0]z%top_p must be in the range [0.0, 1.0]ztop_k must be positive)r|   ro   r   r   r   r;   r;   r<   validate_environment@  s    ""z"ChatDeepInfra.validate_environmentzList[BaseMessage]zOptional[List[str]]zOptional[bool]r)   )messagesstopr?   r   r   r@   c                 K  sv   |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_resultrN   )r   r   r   r?   r   r   should_streamstream_itermessage_dictsparamsr   r;   r;   r<   	_generateM  s$    zChatDeepInfra._generaterW   )r   r@   c                 C  sh   g }|d D ]2}t |d }t|t|ddd}|| q|di }|| jd}t||d}|S )	Nchoicesrk   finish_reason)r   rk   Zgeneration_infousage)token_usagerq   )generations
llm_output)rg   r'   rF   rM   appendru   r)   )r   r   r   resrk   genr   r   r;   r;   r<   r   c  s    z!ChatDeepInfra._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 r;   )rp   )ra   mr;   r;   r<   rb   y  ri   z7ChatDeepInfra._create_message_dicts.<locals>.<listcomp>)r   ro   )r   r   r   r   r   r;   r;   r<   r   q  s    z#ChatDeepInfra._create_message_dictszIterator[ChatGenerationChunk])r   r   r?   r   r@   c                 k  s   |  ||\}}i ||ddi}| jf ||d|}t| D ]:}t|}	|	rFt|	d d}
|rz|jt|	j|
d |
V  qFd S )Nr   Tr   r   chunk)	r   r   _parse_stream
iter_lines_handle_sse_liner(   on_llm_new_tokenrt   r[   )r   r   r   r?   r   r   r   r   liner   cg_chunkr;   r;   r<   r   |  s    zChatDeepInfra._streamz"AsyncIterator[ChatGenerationChunk]c              	   K s   |  ||\}}|dd||}|d}t|  d}|j|  | ||d4 I d H l}	t|	j2 zH3 d H W }
t	|
}|rht
|d d}|r|jt|j|dI d H  |V  qh6 W d   I d H  q1 I d H s0    Y  d S )NT)r   r   rz   r   r   r   r   )r   r   r5   r   r   r   r   _parse_stream_asyncr[   r   r(   r   rt   )r   r   r   r?   r   r   r   rz   r   r   r   r   r   r;   r;   r<   _astream  s"    

zChatDeepInfra._astreamc                   s   |d ur|n| j }|r<| j|f||d|}t|I d H S | ||\}}	d|i|	|}	| jf d|i|	I d H }
| |
S )Nr   r   r?   )r   r   r   r   r   r   )r   r   r   r?   r   r   r   r   r   r   r   r;   r;   r<   
_agenerate  s    zChatDeepInfra._ageneratec                 C  s   | j | j| j| j| jdS )zGet the identifying parameters.)rq   r|   r   r   r   )ru   r|   r   r   r   r   r;   r;   r<   _identifying_params  s    z!ChatDeepInfra._identifying_paramsc                 C  s   dS )Nzdeepinfra-chatr;   r   r;   r;   r<   	_llm_type  s    zChatDeepInfra._llm_typeNone)coder   r@   c                 C  sV   |dkrt d| d| n4|dkr6td| n|dkrRtd| d| d S )Ni  zDeepInfra Server error status z: i  z'DeepInfra received an invalid payload:    z6DeepInfra returned an unexpected response with status )r6   ro   r   )r   r   r   r;   r;   r<   r     s    zChatDeepInfra._handle_statusc                 C  s   | j S N)rv   r   r;   r;   r<   r     s    zChatDeepInfra._urlc                 C  s   d| j  ddS )Nzbearer zapplication/json)AuthorizationzContent-Type)rx   r   r;   r;   r<   r     s    
zChatDeepInfra._headersr   c                 C  s   |S r   r;   )r   r   r;   r;   r<   r     s    zChatDeepInfra._bodyzDSequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]]z)Runnable[LanguageModelInput, BaseMessage])toolsr   r@   c                   s$   dd |D }t  jf d|i|S )a3  Bind tool-like objects to this chat model.

        Assumes model is compatible with OpenAI tool-calling API.

        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.
            **kwargs: Any additional parameters to pass to the
                :class:`~langchain.runnable.Runnable` constructor.
        c                 S  s   g | ]}t |qS r;   r.   )ra   rl   r;   r;   r<   rb     ri   z,ChatDeepInfra.bind_tools.<locals>.<listcomp>r   )superbind)r   r   r   Zformatted_tools	__class__r;   r<   
bind_tools  s    zChatDeepInfra.bind_tools)N)N)NNN)NN)NN)NNN),r7   r8   r9   r:   r2   ru   __annotations__rv   rx   rz   r|   rF   r~   r   r   r   r   r   rA   r1   Zmodel_configpropertyr   r   r   r   r3   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r;   r;   r   r<   r=      sl   
            
zIterator[bytes]zIterator[str])rbodyr@   c                 c  s$   | D ]}t |}|d ur|V  qd S r   _parse_stream_helperr   r   _liner;   r;   r<   r     s    r   zaiohttp.StreamReaderzAsyncIterator[str]c                 C s.   | 2 z"3 d H W }t |}|d ur|V  q6 d S r   r   r   r;   r;   r<   r     s    r   bytesrw   )r   r@   c                 C  sX   | rT|  drT|  dr*| tdd  } n| tdd  } |  dkrJd S | dS d S )Ns   data:s   data: s   [DONE]zutf-8)
startswithlenstripdecode)r   r;   r;   r<   r     s    

r   rt   zOptional[BaseMessageChunk]c                 C  sN   z4t | }t}|di gd di }t||W S  tyH   Y d S 0 d S )Nr   r   delta)rN   rO   r   rM   rj   r   )r   objZdefault_chunk_classr   r;   r;   r<   r     s    
r   )N)Wr:   
__future__r   rN   loggingr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   ZaiohttprB   Z langchain_core.callbacks.managerr   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#   Zlangchain_core.messages.toolr$   r&   rQ   Zlangchain_core.outputsr'   r(   r)   Zlangchain_core.runnablesr*   Zlangchain_core.toolsr+   Zlangchain_core.utilsr-   Z%langchain_core.utils.function_callingr/   Zpydanticr0   r1   r2   r3   Ztyping_extensionsr4   Z&langchain_community.utilities.requestsr5   	getLoggerr7   loggerr   r6   rE   rR   rV   rg   rj   rp   r=   r   r   r   r   r;   r;   r;   r<   <module>   sL   8<

 $  3