o
    Zh                     @   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dee dee f fddZde	e d	e	e	e
  fd
dZded	e	e
 f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_uidNc                    s   zddl m} W n# ty+   zddlm} W n ty( } ztd|d }~ww Y nw t   |d u r9td|d u rAtd|| _|| _||| _	d S )Nr   )RESTfulClientzCould 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__ `/var/www/html/lang_env/lib/python3.10/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 )zEmbed 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   {   s    )r   	get_modelr	   )r   r   Z
embeddingsr   r   r   embed_documentsn   s
   
z$XinferenceEmbeddings.embed_documentsr   c                 C   s6   | j | j}||}|d d d }ttt|S )zEmbed 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    