Source code for langchain.embeddings.spacy_embeddings
import importlib.util
from typing import Any, Dict, List
from langchain.pydantic_v1 import BaseModel, Extra, root_validator
from langchain.schema.embeddings import Embeddings
[docs]class SpacyEmbeddings(BaseModel, Embeddings):
    """Embeddings by SpaCy models.
    It only supports the 'en_core_web_sm' model.
    Attributes:
        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.
    """
    nlp: Any  # The Spacy model loaded into memory
    class Config:
        """Configuration for this pydantic object."""
        extra = Extra.forbid  # Forbid extra attributes during model initialization
    @root_validator(pre=True)
    def validate_environment(cls, values: Dict) -> Dict:
        """
        Validates that the Spacy package and the 'en_core_web_sm' 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 'en_core_web_sm'
            model are not installed.
        """
        # Check if the Spacy package is installed
        if importlib.util.find_spec("spacy") is None:
            raise ValueError(
                "Spacy package not found. "
                "Please install it with `pip install spacy`."
            )
        try:
            # Try to load the 'en_core_web_sm' Spacy model
            import spacy