
    7|h                     >    d Z ddlmZmZmZ ddlmZ  G d de      Zy)z+Wrapper around Xinference embedding models.    )AnyListOptional)
Embeddingsc                        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Zde	e   de	e	e
      fdZd	ede	e
   fd
Z x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_uidc                    	 ddl m} t        |           |t        d      |t        d      || _        || _         ||      | _	        y # t        $ r( 	 ddlm} n# t        $ r}t        d      |d }~ww xY wY tw xY w)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)
xinference.clientr   ImportErrorxinference_clientsuper__init__
ValueErrorr
   r   r	   )selfr
   r   r   e	__class__s        h/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/embeddings/xinference.pyr   zXinferenceEmbeddings.__init__R   s    		7 	899;<<$"#J/+  	; !Y  <	s5   A 	A>AA>	A8'A33A88A>=A>textsreturnc           	          | j                   j                  | j                        }|D cg c]  }|j                  |      d   d   d    }}|D cg c]  }t	        t        t        |             c}S c c}w c c}w )zEmbed a list of documents using Xinference.
        Args:
            texts: The list of texts to embed.
        Returns:
            List of embeddings, one for each text.
        datar   	embeddingr	   	get_modelr   create_embeddinglistmapfloat)r   r   modeltext
embeddingsr   s         r   embed_documentsz$XinferenceEmbeddings.embed_documentsn   s|     %%dnn5 NS
EIE""4(03K@

 
 .88S]#88
 9s   !A4 A9r$   c                     | j                   j                  | 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#   embedding_resr   s        r   embed_queryz XinferenceEmbeddings.embed_query}   sR     %%dnn5..t4!&)!,[9	Cy)**    )NN)__name__
__module____qualname____doc__r   __annotations__r   strr   r   r"   r&   r)   __classcell__)r   s   @r   r   r      s    AF K&}# LP0"3-0;CC=089T#Y 94U3D 9+ +U +r*   r   N)r.   typingr   r   r   langchain_core.embeddingsr   r    r*   r   <module>r5      s    1 & & 0C+: C+r*   