o
    Zh                     @   sR   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	 G dd deeZ
dS )    N)AnyList)
Embeddings)	BaseModel
ConfigDictc                       s   e Zd ZU dZdZeed< 		ddededef fddZe	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 )JohnSnowLabsEmbeddingsay  JohnSnowLabs embedding models

    To use, you should have the ``johnsnowlabs`` python package installed.
    Example:
        .. code-block:: python

            from langchain_community.embeddings.johnsnowlabs import JohnSnowLabsEmbeddings

            embedding = JohnSnowLabsEmbeddings(model='embed_sentence.bert')
            output = embedding.embed_query("foo bar")
    embed_sentence.bertmodelcpuhardware_targetkwargsc              
      s  t  jdi | zddlm} ddlm} W n ty( } ztd|d}~ww ztjt	j
d< tjt	j
d< |j|d W n tyN } ztd	|d}~ww z"t|tr^||| _W dS t||ri|| _W dS ||| _W dS  ty } ztd
|d}~ww )z"Initialize the johnsnowlabs model.r   )nlp)NLUPipelinez`Could not import johnsnowlabs python package. Please install it with `pip install johnsnowlabs`.NZPYSPARK_PYTHONZPYSPARK_DRIVER_PYTHON)r   zFailure starting Spark SessionzFailure loading model )super__init__Zjohnsnowlabsr   Znlu.pipe.pipeliner   ImportErrorsys
executableosenvironstart	Exception
isinstancestrloadr	   Zto_nlu_pipe)selfr	   r   r   r   r   exc	__class__r   b/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/johnsnowlabs.pyr      s<   



zJohnSnowLabsEmbeddings.__init__Zforbid)extratextsreturnc                 C   sB   | j j|dd}d}|jD ]}d|v r|}qdd ||  D S )zCompute doc embeddings using a JohnSnowLabs transformer model.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        Zdocument)Zoutput_levelNZ	embeddingc                 S   s   g | ]}|  qS r   )tolist).0Zvecr   r   r    
<listcomp>P   s    z:JohnSnowLabsEmbeddings.embed_documents.<locals>.<listcomp>)r	   Zpredictcolumnsr$   )r   r"   dfZemb_colcr   r   r    embed_documentsA   s   

z&JohnSnowLabsEmbeddings.embed_documentstextc                 C   s   |  |gd S )zCompute query embeddings using a JohnSnowLabs transformer model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r*   )r   r+   r   r   r    embed_queryR   s   	z"JohnSnowLabsEmbeddings.embed_query)r   r
   )__name__
__module____qualname____doc__r	   r   __annotations__r   r   r   Zmodel_configr   floatr*   r,   __classcell__r   r   r   r    r   	   s"   
 %r   )r   r   typingr   r   Zlangchain_core.embeddingsr   Zpydanticr   r   r   r   r   r   r    <module>   s    