o
    ZhK                     @  s  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ZK	dGdHddZLdId"d#ZMdJd%d&ZNdKd*d+ZOdLd/d0ZPdMd2d3ZQG d4d deZRdNd8d9ZSdOd<d=ZTdPdAdBZUdQdEdFZVdS )Rz!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;   `/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/chat_models/deepinfra.pyr6   F   s    r6   llmChatDeepInfrarun_managerHOptional[Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun]]returnCallable[[Any], Any]c                 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   rC   )r=   r?   r;   r;   r<   _create_retry_decoratorL   s
   
rG   r&   dictr$   c              	   C  sZ   | d  dd}zt| d d }W n ttfy    i }Y nw |  d}t|||dS )zw
    Convert a tool calling response from server to a ToolCall object.
    Args:
        tool_call:

    Returns:

    functionname 	argumentsid)rJ   argsrM   )getjsonloadsr   	TypeErrorcreate_tool_call)r&   rJ   rN   rM   r;   r;   r<   _parse_tool_callingZ   s   	
rT   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:

    rI   rN   rJ   )rL   rJ   rM   )typerI   rM   )rP   dumpsrO   r%   r;   r;   r<   _convert_to_tool_callingl   s   
rX   _dictMapping[str, Any]r   c                 C  s   | d }|dkrt | d dS |dkr0| ddpd}| dg p"g }dd	 |D }t||d
S |dkr;t| d dS |dkrIt| d | d dS t| d |dS )Nroleusercontentr]   	assistantrK   
tool_callsc                 S     g | ]}t |qS r;   rT   .0r&   r;   r;   r<   
<listcomp>       z,_convert_dict_to_message.<locals>.<listcomp>r]   r`   systemrI   rJ   r]   rJ   r]   r[   )r   rO   r   r!   r   r   )rY   r[   r]   Ztool_calls_contentr`   r;   r;   r<   _convert_dict_to_message   s   rk   default_classType[BaseMessageChunk]r   c                 C  s   |  d}|  dpd}|  dpg }|dks|tkr t|dS |dks(|tkr5dd	 |D }t||d
S |dks=|tkrBt|dS |dksJ|tkrRt|| d dS |sX|tkr^t||dS ||dS )Nr[   r]   rK   r`   r\   r^   r_   c                 S  ra   r;   rb   rc   r;   r;   r<   re          z3_convert_delta_to_message_chunk.<locals>.<listcomp>rg   rh   rI   rJ   ri   rj   )rO   r    r   r"   r   r   )rY   rl   r[   r]   r`   r;   r;   r<   _convert_delta_to_message_chunk   s   



ro   messagec                 C  s   t | tr| j| jd}nRt | trd| jd}nFt | tr.dd | jD }d| j|d}n1t | tr:d| jd}n%t | trHd| j| j	d	}nt | t
rXd
| j| j	| jd}ntd|  d| jv rk| jd |d< |S )N)r[   r]   r\   c                 S  ra   r;   )rX   rc   r;   r;   r<   re      rf   z,_convert_message_to_dict.<locals>.<listcomp>r_   )r[   r]   r`   rh   rI   )r[   r]   rJ   tool)r[   r]   rJ   tool_call_idzGot unknown type rJ   )
isinstancer   r[   r]   r   r   r`   r!   r   rJ   r#   rr   
ValueErrorZadditional_kwargs)rp   Zmessage_dictr`   r;   r;   r<   _convert_message_to_dict   s:   






ru   c                      s  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ded"d#Zeded$d%Z		dfdgd*d+Z		dfdhd-d.Zed/d0edid3d4Zed5d0djd7d8Z						dkdld@dAZdmdDdEZdndGdHZ 				dodpdJdKZ!				dodqdMdNZ"						dkdrdOdPZ#ededQdRZ$edsdSdTZ%dtdXdYZ&dsdZd[Z'dud\d]Z(dvd^d_Z)dw fdcddZ*  Z+S )xr>   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_factoryrU   model_kwargstop_pzOptional[int]top_kintn   
max_tokensFbool	streamingrC   T)Zpopulate_by_namerA   c                 C  s$   | j | j| j| j| j| jd| jS )z2Get the default parameters for calling OpenAI API.)rv   r   streamr   r   r   )rz   r   r   r   r   r   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_paramsr?   "Optional[CallbackManagerForLLMRun]kwargsr   c                   s,   t  |d}|d fdd}|d	i |S )
z*Use tenacity to retry the completion call.r?   r   r   rA   c               
     sn   z$|  d}t  d}|j   | |d} |j|j |W S  t	y6 } zt
d|  d }~ww Nr   headersr{   datar~   ZEX)popr5   _headerspost_url_body_handle_statusstatus_codetext	Exceptionprintr   r   requestresponseer   r;   r<   _completion_with_retry  s   

zCChatDeepInfra.completion_with_retry.<locals>._completion_with_retryNr   r   rA   r   r;   rG   r   r?   r   Zretry_decoratorr   r;   r   r<   completion_with_retry   s   z#ChatDeepInfra.completion_with_retry'Optional[AsyncCallbackManagerForLLMRun]c                   s4   t  |d}|d fdd}|d	i |I dH S )
z0Use tenacity to retry the async completion call.r   r   r   rA   c               
     s   zJ|  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 sDw   Y  W d S  t
y] } ztd|  d }~ww r   )r   r5   r   apostr   r   r   statusr   rP   r   r   r   r   r;   r<   r     s    
4
zDChatDeepInfra.acompletion_with_retry.<locals>._completion_with_retryNr   r;   r   r   r;   r   r<   acompletion_with_retry  s
   z$ChatDeepInfra.acompletion_with_retrybefore)mode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_KEYrK   )rw   r}   ZDEEPINFRA_API_TOKENr,   )clsr   Zapi_keyr;   r;   r<   init_defaults.  s   
zChatDeepInfra.init_defaultsafterr4   c                 C  s   | j d urd| j   krdkstd td| jd ur0d| j  kr+dks0td td| jd ur>| jdkr>t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   rt   r   r   r   r;   r;   r<   validate_environmentA  s   z"ChatDeepInfra.validate_environmentmessagesList[BaseMessage]stopOptional[List[str]]r   Optional[bool]r)   c                 K  sv   |d ur|n| j }|r| j|f||d|}t|S | ||\}}	i |	|}	| jd||d|	}
| |
 S )Nr   r?   r   r?   r;   )r   _streamr   _create_message_dictsr   _create_chat_resultrP   )r   r   r   r?   r   r   should_streamstream_itermessage_dictsparamsr   r;   r;   r<   	_generateN  s$   zChatDeepInfra._generater   rZ   c                 C  sh   g }|d D ]}t |d }t|t|ddd}|| q|di }|| jd}t||d}|S )	Nchoicesrp   finish_reason)r   rp   Zgeneration_infousage)token_usagerv   )generations
llm_output)rk   r'   rH   rO   appendrz   r)   )r   r   r   resrp   genr   r   r;   r;   r<   r   d  s   z!ChatDeepInfra._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  ra   r;   )ru   )rd   mr;   r;   r<   re   z  rn   z7ChatDeepInfra._create_message_dicts.<locals>.<listcomp>)r   rt   )r   r   r   r   r   r;   r;   r<   r   r  s   z#ChatDeepInfra._create_message_dictsIterator[ChatGenerationChunk]c                 k  s    |  ||\}}i ||ddi}| jd||d|}t| D ]}t|}	|	rAt|	d d}
|r>|jt|	j|
d |
V  q$d S )Nr   Tr   r   chunkr;   )	r   r   _parse_stream
iter_lines_handle_sse_liner(   on_llm_new_tokenry   r]   )r   r   r   r?   r   r   r   r   liner   cg_chunkr;   r;   r<   r   }  s"   zChatDeepInfra._stream"AsyncIterator[ChatGenerationChunk]c              	   K s   |  ||\}}|dd||}|d}t|  d}|j|  | ||d4 I d H 7}	t|	j2 z$3 d H W }
t	|
}|rYt
|d d}|rV|jt|j|dI d H  |V  q56 W d   I d H  d S 1 I d H slw   Y  d S )NT)r   r   r   r   r   r   r   )r   r   r5   r   r   r   r   _parse_stream_asyncr]   r   r(   r   ry   )r   r   r   r?   r   r   r   r   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d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;   r<   
_agenerate  s   
zChatDeepInfra._ageneratec                 C  s   | j | j| j| j| jdS )zGet the identifying parameters.)rv   r   r   r   r   )rz   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coder   Nonec                 C  sR   |dkrt d| d| |dkrtd| |dkr't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   rt   r   )r   r   r   r;   r;   r<   r     s   zChatDeepInfra._handle_statusc                 C  s   | j S N)r{   r   r;   r;   r<   r     s   zChatDeepInfra._urlc                 C  s   d| j  ddS )Nzbearer zapplication/json)AuthorizationzContent-Type)r}   r   r;   r;   r<   r     s   
zChatDeepInfra._headersc                 C  s   |S r   r;   )r   r   r;   r;   r<   r     s   zChatDeepInfra._bodytoolsDSequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]])Runnable[LanguageModelInput, BaseMessage]c                   s$   dd |D }t  jd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  ra   r;   r.   )rd   rq   r;   r;   r<   re     rn   z,ChatDeepInfra.bind_tools.<locals>.<listcomp>r   Nr;   )superbind)r   r   r   Zformatted_tools	__class__r;   r<   
bind_tools  s   zChatDeepInfra.bind_tools)rA   rU   r   )r?   r   r   r   rA   r   )r?   r   r   r   rA   r   )r   r   rA   r   )rA   r4   )NNN)r   r   r   r   r?   r   r   r   r   r   rA   r)   )r   rZ   rA   r)   )r   r   r   r   rA   r   )NN)
r   r   r   r   r?   r   r   r   rA   r   )
r   r   r   r   r?   r   r   r   rA   r   )r   r   r   r   r?   r   r   r   r   r   rA   r)   )rA   ry   )r   r   r   r   rA   r   )rA   r   )r   r   rA   r   )r   r   r   r   rA   r   ),r7   r8   r9   r:   r2   rz   __annotations__r{   r}   r   r   rH   r   r   r   r   r   r   rC   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>      s|   
 






rbodyIterator[bytes]Iterator[str]c                 c  s&    | D ]}t |}|d ur|V  qd S r   _parse_stream_helperr   r   _liner;   r;   r<   r     s   r   aiohttp.StreamReaderAsyncIterator[str]c                 C s0   | 2 z3 d H W }t |}|d ur|V  q6 d S r   r   r   r;   r;   r<   r     s   r   r   bytesr|   c                 C  sX   | r*|  dr*|  dr| tdd  } n| tdd  } |  dkr%d 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   ry   Optional[BaseMessageChunk]c                 C  sJ   zt | }t}|di gd di }t||W S  ty$   Y d S w )Nr   r   delta)rP   rQ   r   rO   ro   r   )r   objZdefault_chunk_classr  r;   r;   r<   r     s   
r   r   )r=   r>   r?   r@   rA   rB   )r&   rH   rA   r$   )r&   r$   rA   rU   )rY   rZ   rA   r   )rY   rZ   rl   rm   rA   r   )rp   r   rA   rH   )r   r   rA   r   )r   r   rA   r   )r   r   rA   r|   )r   ry   rA   r   )Wr:   
__future__r   rP   loggingr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   ZaiohttprD   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&   rS   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   rG   rT   rX   rk   ro   ru   r>   r   r   r   r   r;   r;   r;   r<   <module>   sN    8<






$  
3

