a
    bŠÝgõ  ã                   @   s8   d Z ddlmZmZmZ ddlmZ G dd„ deƒZdS )z+Wrapper around Xinference embedding models.é    )ÚAnyÚListÚOptional)Ú
Embeddingsc                       s„   e Zd ZU dZeed< ee ed< ee ed< dee ee dœ‡ fdd„Ze	e e	e	e
  d	œd
d„Zee	e
 dœdd„Z‡  ZS )ÚXinferenceEmbeddingsaq  Xinference embedding models.

    To use, you should have the xinference library installed:

    .. code-block:: bash

        pip install xinference

    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

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

        .. code-block:: bash

            pip install xinference_client

        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 Embedding with LangChain.

    Example:

    .. code-block:: python

        from langchain_community.embeddings import XinferenceEmbeddings

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

    ÚclientÚ
server_urlÚ	model_uidN)r   r	   c                    s¨   zddl m} W nR tyb   zddlm} W n. ty\ } ztdƒ|‚W Y d }~n
d }~0 0 Y n0 tƒ  ¡  |d u r~tdƒ‚|d u rŽtdƒ‚|| _|| _||ƒ| _	d S )Nr   )ÚRESTfulClientzƒCould not import RESTfulClient from xinference. Please install it with `pip install xinference` or `pip install xinference_client`.zPlease provide server URLzPlease provide the model UID)
Zxinference.clientr
   ÚImportErrorZxinference_clientÚsuperÚ__init__Ú
ValueErrorr   r	   r   )Úselfr   r	   r
   Úe©Ú	__class__© úw/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/xinference.pyr   R   s&    ÿý
zXinferenceEmbeddings.__init__)ÚtextsÚreturnc                    s.   | j  | j¡‰ ‡ fdd„|D ƒ}dd„ |D ƒS )zµEmbed a list of documents using Xinference.
        Args:
            texts: The list of texts to embed.
        Returns:
            List of embeddings, one for each text.
        c                    s"   g | ]}ˆ   |¡d  d d ‘qS )Údatar   Ú	embedding)Úcreate_embedding)Ú.0Útext©Úmodelr   r   Ú
<listcomp>x   s   z8XinferenceEmbeddings.embed_documents.<locals>.<listcomp>c                 S   s   g | ]}t tt|ƒƒ‘qS r   )ÚlistÚmapÚfloat)r   r   r   r   r   r   {   ó    )r   Ú	get_modelr	   )r   r   Z
embeddingsr   r   r   Úembed_documentsn   s
    
ÿz$XinferenceEmbeddings.embed_documents)r   r   c                 C   s6   | j  | j¡}| |¡}|d d d }ttt|ƒƒS )zžEmbed a query of documents using Xinference.
        Args:
            text: The text to embed.
        Returns:
            Embeddings for the text.
        r   r   r   )r   r#   r	   r   r   r    r!   )r   r   r   Zembedding_resr   r   r   r   Úembed_query}   s    
z XinferenceEmbeddings.embed_query)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   Ústrr   r   r!   r$   r%   Ú__classcell__r   r   r   r   r      s   
C ÿÿr   N)r)   Útypingr   r   r   Zlangchain_core.embeddingsr   r   r   r   r   r   Ú<module>   s   