a
    bg                     @   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                       st   e Zd ZU dZdZeed< deeed fddZe	dd	Z
ee eee  d
ddZeee d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)r	   hardware_targetkwargsc              
      s&  t  jf i | zddlm} ddlm} W n. ty\ } ztd|W Y d}~n
d}~0 0 z(tjt	j
d< tjt	j
d< |j|d W n. ty } ztd	|W Y d}~n
d}~0 0 z:t|tr||| _nt||r|| _n||| _W n0 ty  } ztd
|W Y d}~n
d}~0 0 dS )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__ y/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/johnsnowlabs.pyr      s0     

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       z:JohnSnowLabsEmbeddings.embed_documents.<locals>.<listcomp>)r	   Zpredictcolumnsr$   )r   r"   ZdfZemb_colcr   r   r    embed_documentsA   s    

z&JohnSnowLabsEmbeddings.embed_documents)textr#   c                 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
   