U
    h                     @   sR   d dl Zd dlmZmZ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DictListOptional)
Embeddings)	BaseModelroot_validatorc                   @   s   e Zd ZU dZdZeed< dZee	 ed< G dd dZ
edd	eed
ddZee eee  dddZeee dddZee eee  dddZeee dddZdS )SpacyEmbeddingsa  Embeddings by spaCy models.

    Attributes:
        model_name (str): Name of a spaCy model.
        nlp (Any): The spaCy model loaded into memory.

    Methods:
        embed_documents(texts: List[str]) -> List[List[float]]:
            Generates embeddings for a list of documents.
        embed_query(text: str) -> List[float]:
            Generates an embedding for a single piece of text.
    en_core_web_sm
model_nameNnlpc                   @   s   e Zd ZdZdS )zSpacyEmbeddings.ConfigZforbidN)__name__
__module____qualname__extra r   r   S/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/spacy_embeddings.pyConfig   s   r   T)pre)valuesreturnc              	   C   s   | ddkrd|d< | d}tjddkr8tdzddl}|||d< W n* tk
r|   td| d	| d
Y nX |S )aE  
        Validates that the spaCy package and the model are installed.

        Args:
            values (Dict): The values provided to the class constructor.

        Returns:
            The validated values.

        Raises:
            ValueError: If the spaCy package or the
            model are not installed.
        r   Nr
   spacyzDSpaCy package not found. Please install it with `pip install spacy`.r   r   zSpaCy model 'z>' not found. Please install it with `python -m spacy download z%`or provide a valid spaCy model name.)get	importlibutil	find_spec
ValueErrorr   loadOSError)clsr   r   r   r   r   r   validate_environment   s    

z$SpacyEmbeddings.validate_environment)textsr   c                    s    fdd|D S )z
        Generates embeddings for a list of documents.

        Args:
            texts (List[str]): The documents to generate embeddings for.

        Returns:
            A list of embeddings, one for each document.
        c                    s   g | ]}  |j qS r   r   Zvectortolist).0textselfr   r   
<listcomp>O   s     z3SpacyEmbeddings.embed_documents.<locals>.<listcomp>r   r'   r!   r   r&   r   embed_documentsE   s    
zSpacyEmbeddings.embed_documents)r%   r   c                 C   s   |  |j S )z
        Generates an embedding for a single piece of text.

        Args:
            text (str): The text to generate an embedding for.

        Returns:
            The embedding for the text.
        r"   r'   r%   r   r   r   embed_queryQ   s    
zSpacyEmbeddings.embed_queryc                    s   t ddS )aA  
        Asynchronously generates embeddings for a list of documents.
        This method is not implemented and raises a NotImplementedError.

        Args:
            texts (List[str]): The documents to generate embeddings for.

        Raises:
            NotImplementedError: This method is not implemented.
        3Asynchronous embedding generation is not supported.NNotImplementedErrorr)   r   r   r   aembed_documents]   s    z SpacyEmbeddings.aembed_documentsc                    s   t ddS )a<  
        Asynchronously generates an embedding for a single piece of text.
        This method is not implemented and raises a NotImplementedError.

        Args:
            text (str): The text to generate an embedding for.

        Raises:
            NotImplementedError: This method is not implemented.
        r-   Nr.   r+   r   r   r   aembed_queryj   s    zSpacyEmbeddings.aembed_query)r   r   r   __doc__r   str__annotations__r   r   r   r   r   r   r    r   floatr*   r,   r0   r1   r   r   r   r   r	      s   
(r	   )importlib.utilr   typingr   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r   r   r	   r   r   r   r   <module>   s   