§
    šŠtj�3  ã                  ó¾   — d dl m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 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 erd dlmZmZ d dlmZ  G d	„ d
e¦  «        ZdS )é    )ÚannotationsN)
ÚTYPE_CHECKINGÚAnyÚAsyncIteratorÚDictÚ	GeneratorÚIteratorÚListÚMappingÚOptionalÚUnion)ÚAsyncCallbackManagerForLLMRunÚCallbackManagerForLLMRun)ÚLLM)ÚGenerationChunk)ÚRESTfulChatModelHandleÚRESTfulGenerateModelHandle)ÚLlamaCppGenerateConfigc                  ó   ‡ — e Zd ZU dZdZded<   ded<   	 ded<   	 ded	<   	 	 	 	 d.d/ˆ fd„Zed0d„¦   «         Zed1d„¦   «         Z	d2d„Z
	 	 d3d4d„Z	 	 d3d5d „Z	 	 d3d6d"„Z	 d7d8d%„Zed9d(„¦   «         Z	 	 d3d:d+„Z	 d7d;d-„Zˆ xZS )<Ú
Xinferencea	  `Xinference` large-scale model inference service.

    To use, you should have the xinference library installed:

    .. code-block:: bash

       pip install "xinference[all]"

    If you're simply using the services provided by Xinference, you can utilize the xinference_client package:

    .. code-block:: bash

        pip install xinference_client

    Check out: https://github.com/xorbitsai/inference
    To run, you need to start a Xinference supervisor on one server and Xinference workers on the other servers

    Example:
        To start a local instance of Xinference, run

        .. code-block:: bash

           $ xinference

        You can also deploy Xinference in a distributed cluster. Here are the steps:

        Starting the supervisor:

        .. code-block:: bash

           $ xinference-supervisor

        Starting the worker:

        .. code-block:: bash

           $ xinference-worker

    Then, launch a model using command line interface (CLI).

    Example:

    .. code-block:: bash

       $ xinference launch -n orca -s 3 -q q4_0

    It will return a model UID. Then, you can use Xinference with LangChain.

    Example:

    .. code-block:: python

        from langchain_community.llms import Xinference

        llm = Xinference(
            server_url="http://0.0.0.0:9997",
            model_uid = {model_uid} # replace model_uid with the model UID return from launching the model
        )

        llm.invoke(
            prompt="Q: where can we visit in the capital of France? A:",
            generate_config={"max_tokens": 1024, "stream": True},
        )

    Example:

    .. code-block:: python

        from langchain_community.llms import Xinference
        from langchain_classic.prompts import PromptTemplate

        llm = Xinference(
            server_url="http://0.0.0.0:9997",
            model_uid={model_uid}, # replace model_uid with the model UID return from launching the model
            stream=True
        )
        prompt = PromptTemplate(
            input=['country'],
            template="Q: where can we visit in the capital of {country}? A:"
        )
        chain = prompt | llm
        chain.stream(input={'country': 'France'})


    To view all the supported builtin models, run:

    .. code-block:: bash

        $ xinference list --all

    NzOptional[Any]ÚclientúOptional[str]Ú
server_urlÚ	model_uidzDict[str, Any]Úmodel_kwargsÚapi_keyr   c                ó¸  •— 	 ddl m} n:# t          $ r- 	 ddlm} n"# t          $ r}t          d¦  «        |‚d }~ww xY wY nw xY w|pi } t	          ¦   «         j        d
i |||dœ¤Ž | j        €t          d¦  «        ‚| j        €t          d¦  «        ‚i | _	        d| _
        |                      ¦   «          |�| j
        rd|› �| j	        d	<    |||¦  «        | _        d S )Nr   )ÚRESTfulClientzƒCould not import RESTfulClient from xinference. Please install it with `pip install xinference` or `pip install xinference_client`.©r   r   r   zPlease provide server URLzPlease provide the model UIDFzBearer ÚAuthorization© )Úxinference.clientr   ÚImportErrorÚxinference_clientÚsuperÚ__init__r   Ú
ValueErrorr   Ú_headersÚ_cluster_authedÚ_check_cluster_authenticatedr   )Úselfr   r   r   r   r   ÚeÚ	__class__s          €úa/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/llms/xinference.pyr&   zXinference.__init__„   sd  ø€ ð		Ø7Ð7Ð7Ð7Ð7Ð7Ð7øÝð 	ð 	ð 	ðØ;Ð;Ð;Ð;Ð;Ð;Ð;øÝð ð ð Ý!ðYñô ð ðøøøøðøøøð <Ð;ð	øøøð $Ð) rˆà�‰ŒÔð 	
ð 	
à(Ø&Ø ,ðð ð	
ð 	
ð 	
ð Œ?Ð"ÝÐ8Ñ9Ô9Ð9àŒ>Ð!ÝÐ;Ñ<Ô<Ð<à(*ˆŒØ$ˆÔØ×)Ò)Ñ+Ô+Ð+ØÐ 4Ô#7ÐØ-@°wÐ-@Ð-@ˆDŒM˜/Ñ*à#�m J°Ñ8Ô8ˆŒˆˆs)   ƒ
 Š
A•›Aœ
;¦6¶;»AÁ AÚreturnÚstrc                ó   — dS )zReturn type of llm.Ú
xinferencer!   ©r+   s    r.   Ú	_llm_typezXinference._llm_type®   s	   € ð ˆ|ó    úMapping[str, Any]c                ó<   — i d| j         i¥d| j        i¥d| j        i¥S )zGet the identifying parameters.r   r   r   r   r3   s    r.   Ú_identifying_paramszXinference._identifying_params³   s<   € ð
Ø˜Tœ_Ð-ð
à˜DœNÐ+ð
ð ˜tÔ0Ð1ð
ð 	
r5   ÚNonec                ó0  — | j         › d�}t          j        |¦  «        }|j        dk    r	d| _        d S |j        dk    r*t          d|                     ¦   «         d         › �¦  «        ‚|                     ¦   «         }t          |d         ¦  «        | _        d S )Nz/v1/cluster/authé”  FéÈ   z+Failed to get cluster information, detail: ÚdetailÚauth)r   ÚrequestsÚgetÚstatus_coder)   ÚRuntimeErrorÚjsonÚbool)r+   ÚurlÚresponseÚresponse_datas       r.   r*   z'Xinference._check_cluster_authenticated¼   s¦   € Ø”Ð2Ð2Ð2ˆÝ”< Ñ$Ô$ˆØÔ 3Ò&Ð&Ø#(ˆDÔ Ð Ð àÔ# sÒ*Ð*Ý"ð;Ø'Ÿ}š}™œ¨xÔ8ð;ð ;ñô ð ð %ŸMšM™OœOˆMÝ#'¨°fÔ(=Ñ#>Ô#>ˆDÔ Ð Ð r5   ÚpromptÚstopúOptional[List[str]]Úrun_managerú"Optional[CallbackManagerForLLMRun]Úkwargsc                óŠ  — | j         €t          d¦  «        ‚| j                              | j        ¦  «        }|                     di ¦  «        }i | j        ¥|¥}|r||d<   |r:|                     d¦  «        r%d}|                      ||||¬¦  «        D ]}||z  }Œ|S |                     ||¬¦  «        }	|	d	         d
         d         S )aq  Call the xinference model and return the output.

        Args:
            prompt: The prompt to use for generation.
            stop: Optional list of stop words to use when generating.
            generate_config: Optional dictionary for the configuration used for
                generation.

        Returns:
            The generated string by the model.
        NúClient is not initialized!Úgenerate_configrI   ÚstreamÚ )ÚmodelrH   rK   rP   ©rH   rP   Úchoicesr   Útext)r   r'   Ú	get_modelr   r@   r   Ú_stream_generateÚgenerate)
r+   rH   rI   rK   rM   rS   rP   Úcombined_text_outputÚtokenÚ
completions
             r.   Ú_callzXinference._callÊ   s   € ð$ Œ;ÐÝÐ9Ñ:Ô:Ð:Ø”×%Ò% d¤nÑ5Ô5ˆà4:·J²JÐ?PÐRTÑ4UÔ4UˆàB˜TÔ.ÐB°/ÐBˆàð 	+Ø&*ˆO˜FÑ#àð 	4˜×2Ò2°8Ñ<Ô<ð 	4Ø#%Ð Ø×.Ò.ØØØ'Ø /ð	 /ñ ô ð .ð .�ð %¨Ñ-Ð$Ð$Ø'Ð'ð Ÿš¨vÀ˜ÑWÔWˆJØ˜iÔ(¨Ô+¨FÔ3Ð3r5   rS   ú=Union['RESTfulGenerateModelHandle', 'RESTfulChatModelHandle']rP   ú"Optional['LlamaCppGenerateConfig']úGenerator[str, None, None]c              #  ór  K  — |                      ||¬¦  «        }|D ]š}t          |t          ¦  «        rƒ|                     dg ¦  «        }|rk|d         }t          |t          ¦  «        rN|                     dd¦  «        }	|                     d¦  «        }
|r|                     |	| j        |
¬¦  «         |	V — Œ›dS )	a^  
        Args:
            prompt: The prompt to use for generation.
            model: The model used for generation.
            stop: Optional list of stop words to use when generating.
            generate_config: Optional dictionary for the configuration used for
                generation.

        Yields:
            A string token.
        rT   rU   r   rV   rR   Úlogprobs)r[   ÚverboseÚ	log_probsN)rY   Ú
isinstanceÚdictr@   Úon_llm_new_tokenrc   )r+   rS   rH   rK   rP   Ústreaming_responseÚchunkrU   Úchoicer[   rd   s              r.   rX   zXinference._stream_generateö   sç   è è € ð$ #Ÿ^š^Ø¨?ð ,ñ 
ô 
Ðð (ð 	$ð 	$ˆEÝ˜%¥Ñ&Ô&ð $ØŸ)š) I¨rÑ2Ô2�Øð 	$Ø$ QœZ�FÝ! &­$Ñ/Ô/ð $Ø &§
¢
¨6°2Ñ 6Ô 6˜Ø$*§J¢J¨zÑ$:Ô$:˜	Ø&ð Ø'×8Ò8Ø&+°T´\ÈYð 9ñ ô ð ð $˜˜˜øð	$ð 	$r5   úIterator[GenerationChunk]c              +  ó  K  — |                      di ¦  «        }i | j        ¥|¥}|r||d<   |                      ||¦  «        D ]@}|r<|                      |¦  «        }|r!|                     |j        | j        ¬¦  «         |V — ŒAd S ©NrP   rI   )rc   )r@   r   Ú_create_generate_streamÚ$_stream_response_to_generation_chunkrg   rV   rc   ©r+   rH   rI   rK   rM   rP   Ústream_respri   s           r.   Ú_streamzXinference._stream  s¿   è è € ð !Ÿ*š*Ð%6¸Ñ;Ô;ˆØB˜TÔ.ÐB°/ÐBˆØð 	+Ø&*ˆO˜FÑ#Ø×7Ò7¸ÀÑPÔPð 	ð 	ˆKØð Ø×AÒAÀ+ÑNÔN�Øð Ø×0Ò0Øœ
Ø $¤ð 1ñ ô ð ð ���øð	ð 	r5   úOptional[Dict[str, List[str]]]úIterator[str]c              #  ó®   K  — | j         €t          d¦  «        ‚| j                              | j        ¦  «        }|                     ||¬¦  «        E d {V —† d S )NrO   rT   )r   r'   rW   r   rY   )r+   rH   rP   rS   s       r.   rn   z"Xinference._create_generate_stream.  sd   è è € ð Œ;ÐÝÐ9Ñ:Ô:Ð:Ø”×%Ò% d¤nÑ5Ô5ˆØ—>’>¨À�>ÑQÔQÐQÐQÐQÐQÐQÐQÐQÐQÐQr5   Ústream_responser   c           
     ó²  — d}t          | t          ¦  «        r²|                      dg ¦  «        }|rŠ|d         }t          |t          ¦  «        r^|                     dd¦  «        }t          |t          |                     dd¦  «        |                     dd¦  «        ¬¦  «        ¬	¦  «        S t	          d
¦  «        ‚t          |¬¦  «        S t	          d¦  «        ‚)z0Convert a stream response to a generation chunk.rR   rU   r   rV   Úfinish_reasonNrb   )rx   rb   )rV   Úgeneration_infozchoice type error!)rV   zstream_response type error!)re   rf   r@   r   Ú	TypeError)rv   r[   rU   rj   s       r.   ro   z/Xinference._stream_response_to_generation_chunk6  så   € ð
 ˆÝ�o¥tÑ,Ô,ð 	;Ø%×)Ò)¨)°RÑ8Ô8ˆGØð 3Ø  œ�Ý˜f¥dÑ+Ô+ð :Ø"ŸJšJ v¨rÑ2Ô2�Eå*Ø"Ý(,Ø*0¯*ª*°_ÀdÑ*KÔ*KØ%+§Z¢Z°
¸DÑ%AÔ%Að)ñ )ô )ðñ ô ð õ $Ð$8Ñ9Ô9Ð9å&¨EÐ2Ñ2Ô2Ð2åÐ9Ñ:Ô:Ð:r5   ú'Optional[AsyncCallbackManagerForLLMRun]úAsyncIterator[GenerationChunk]c               ó"  K  — |                      di ¦  «        }i | j        ¥|¥}|r||d<   |                      ||¦  «        2 3 d {V —†}|rC|                      |¦  «        }|r'|                     |j        | j        ¬¦  «        ƒ d {V —† |W V — ŒM6 d S rm   )r@   r   Ú_acreate_generate_streamro   rg   rV   rc   rp   s           r.   Ú_astreamzXinference._astreamQ  sú   è è € ð !Ÿ*š*Ð%6¸Ñ;Ô;ˆØB˜TÔ.ÐB°/ÐBˆØð 	+Ø&*ˆO˜FÑ#Ø!%×!>Ò!>¸vÀÑ!WÔ!Wð 	ð 	ð 	ð 	ð 	ð 	ð 	�+Øð Ø×AÒAÀ+ÑNÔN�Øð Ø%×6Ò6Øœ
Ø $¤ð 7ñ ô ð ð ð ð ð ð ð ð ����øð "XÐ!WÐ!Ws   ÁBúAsyncIterator[str]c               ót  K  — | j         |dœ}|�|                     ¦   «         D ]
\  }}|||<   Œt          |o|                     d¦  «        ¦  «        }t	          j        ¦   «         4 ƒd {V —†}|                     | j        › d�|¬¦  «        4 ƒd {V —†	 }|j        dk    r;|j        dk    rt          d¦  «        ‚|j
        }	t          d|j        › d	|	› �¦  «        ‚|j        2 3 d {V —†}
|st          j        |
¦  «        W V — Œ!|
                     d
¦  «        }|
                     d¦  «        rC|t#          d¦  «        d …                              ¦   «         }|sŒwt          j        |¦  «        W V — Œ�6 	 d d d ¦  «        ƒd {V —† n# 1 ƒd {V —†swxY w Y   d d d ¦  «        ƒd {V —† d S # 1 ƒd {V —†swxY w Y   d S )N)rS   rH   rQ   z/v1/completions)rE   rC   r<   r;   z)astream call failed with status code 404.z%astream call failed with status code z. Details: zutf-8s   data:)r   ÚitemsrD   r@   ÚaiohttpÚClientSessionÚpostr   ÚstatusÚFileNotFoundErrorrV   r'   ÚcontentrC   ÚloadsÚdecodeÚ
startswithÚlenÚstrip)r+   rH   rP   Úrequest_bodyÚkeyÚvaluerQ   ÚsessionrF   Úoptional_detailÚlineÚjson_strs               r.   r~   z#Xinference._acreate_generate_streamf  s>  è è € ð 26´È6Ð'RÐ'RˆØÐ&Ø-×3Ò3Ñ5Ô5ð *ð *‘
��UØ$)�˜SÑ!Ð!å�oÐG¨/×*=Ò*=¸hÑ*GÔ*GÑHÔHˆÝÔ(Ñ*Ô*ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7¨gØ—|’|Ø”Ð7Ð7Ð7Ø!ð $ñ ô ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð Ø”? cÒ)Ð)Ø”¨#Ò-Ð-Ý/ØGñô ð ð +3¬-˜Ý(ð;ÀHÄOð ;ð ;Ø)8ð;ð ;ñô ð ð
 #+Ô"2ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7˜$Ø!ð 7Ý"œj¨Ñ.Ô.Ð.Ð.Ð.Ð.Ð.à#'§;¢;¨wÑ#7Ô#7˜ØŸ?š?¨8Ñ4Ô4ð 7Ø'/µ°H±´°°Ô'@×'FÒ'FÑ'HÔ'H˜HØ#+ð )Ø (Ý"&¤*¨XÑ"6Ô"6Ð6Ð6Ð6Ð6øð #3Ð"2ð!7ð 7ð 7ñ 7ô 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7øøøð 7ð 7ð 7ð 7ð	7ð 	7ð 	7ñ 	7ô 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7ð 	7øøøð 	7ð 	7ð 	7ð 	7ð 	7ð 	7sD   Á+&F'ÂAFÃ E/Ã&B
FÅ1F'Æ
F	ÆF'ÆF	ÆF'Æ'
F1Æ4F1)NNN)r   r   r   r   r   r   r   r   )r/   r0   )r/   r6   )r/   r9   )NN)
rH   r0   rI   rJ   rK   rL   rM   r   r/   r0   )
rS   r^   rH   r0   rK   rL   rP   r_   r/   r`   )
rH   r0   rI   rJ   rK   rL   rM   r   r/   rk   )N)rH   r0   rP   rs   r/   rt   )rv   r0   r/   r   )
rH   r0   rI   rJ   rK   r{   rM   r   r/   r|   )rH   r0   rP   rs   r/   r€   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r&   Úpropertyr4   r8   r*   r]   rX   rr   rn   Ústaticmethodro   r   r~   Ú__classcell__)r-   s   @r.   r   r      sÜ  ø€ € € € € € ðZð Zðx !€FÐ Ð Ð Ñ ØÐÐÑØ&ØÐÐÑØ#Ø Ð Ð Ñ Ø:ð %)Ø#'Ø!%ð	(9ð (9ð (9ð (9ð (9ð (9ð (9ðT ðð ð ñ „Xðð ð
ð 
ð 
ñ „Xð
ð?ð ?ð ?ð ?ð" %)Ø:>ð	*4ð *4ð *4ð *4ð *4ð` ;?Ø>Bð!$ð !$ð !$ð !$ð !$ðL %)Ø:>ð	ð ð ð ð ð, NRðRð Rð Rð Rð Rð ð;ð ;ð ;ñ „\ð;ð: %)Ø?Cð	ð ð ð ð ð, NRð#7ð #7ð #7ð #7ð #7ð #7ð #7ð #7ð #7r5   r   )Ú
__future__r   rC   Útypingr   r   r   r   r   r	   r
   r   r   r   rƒ   r?   Úlangchain_core.callbacksr   r   Ú#langchain_core.language_models.llmsr   Úlangchain_core.outputsr   r"   r   r   Úxinference.model.llm.corer   r   r!   r5   r.   ú<module>r£      sk  ðØ "Ð "Ð "Ð "Ð "Ð "à €€€ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð €€€Ø €€€ðð ð ð ð ð ð ð ð 4Ð 3Ð 3Ð 3Ð 3Ð 3Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2àð AØTÐTÐTÐTÐTÐTÐTÐTØ@Ð@Ð@Ð@Ð@Ð@ðj7ð j7ð j7ð j7ð j7�ñ j7ô j7ð j7ð j7ð j7r5   