
    7|h                     X    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ee      Z
y)    N)AnyList)
Embeddings)	BaseModel
ConfigDictc                        e Zd ZU dZdZeed<   	 	 ddededef fdZ e	d      Z
d	ee   d
eee      fdZded
ee   fdZ x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hardware_targetkwargsc                 :   t        |   di | 	 ddlm} ddlm} 	 t        j                  t        j                  d<   t        j                  t        j                  d<   |j                  |       	 t        |t              r|j                  |      | _        yt        ||      r|| _        y|j#                  |      | _        y# t        $ r}t        d      |d}~ww xY w# t        $ r}t        d	      |d}~ww xY w# t        $ r}t        d
      |d}~ww xY w)z"Initialize the johnsnowlabs model.r   )nlp)NLUPipelinez`Could not import johnsnowlabs python package. Please install it with `pip install johnsnowlabs`.NPYSPARK_PYTHONPYSPARK_DRIVER_PYTHON)r   zFailure starting Spark SessionzFailure loading model )super__init__johnsnowlabsr   nlu.pipe.pipeliner   ImportErrorsys
executableosenvironstart	Exception
isinstancestrloadr   to_nlu_pipe)selfr   r   r   r   r   exc	__class__s          j/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/embeddings/johnsnowlabs.pyr   zJohnSnowLabsEmbeddings.__init__   s    	"6"	(5	G+.>>BJJ'(25..BJJ./IIoI6
	>%% XXe_
E;/"
 __U3
+  	E 	  	G<=3F	G  	>34#=	>sS   C AC# 4&D  D  /D  	C CC #	C=,C88C= 	D	DDforbid)extratextsreturnc                     | j                   j                  |d      }d}|j                  D ]	  }d|v s|} ||   j                         D cg c]  }|j                          c}S c c}w )zCompute doc embeddings using a JohnSnowLabs transformer model.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        document)output_levelN	embedding)r   predictcolumnstolist)r#   r)   dfemb_colcvecs         r&   embed_documentsz&JohnSnowLabsEmbeddings.embed_documentsA   sk     ZZJ? 	Aa	 )+7(:(:(<=

===s   A'textc                 ,    | j                  |g      d   S )zCompute query embeddings using a JohnSnowLabs transformer model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r6   )r#   r7   s     r&   embed_queryz"JohnSnowLabsEmbeddings.embed_queryR   s     ##TF+A..    )r
   cpu)__name__
__module____qualname____doc__r   r   __annotations__r    r   r   model_configr   floatr6   r9   __classcell__)r%   s   @r&   r	   r	   	   s    
 'E3& +$#>#> #> 	#>J L>T#Y >4U3D >"	/ 	/U 	/r:   r	   )r   r   typingr   r   langchain_core.embeddingsr   pydanticr   r   r	   r   r:   r&   <module>rG      s$    	 
  0 *R/Y
 R/r:   