mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-25 16:13:25 +00:00
adding parameter for changing the language in SpacyEmbeddings (#15743)
Description: Added the parameter for a possibility to change a language model in SpacyEmbeddings. The default value is still the same: "en_core_web_sm", so it shouldn't affect a code which previously did not specify this parameter, but it is not hard-coded anymore and easy to change in case you want to use it with other languages or models. Issue: At Barcelona Supercomputing Center in Aina project (https://github.com/projecte-aina), a project for Catalan Language Models and Resources, we would like to use Langchain for one of our current projects and we would like to comment that Langchain, while being a very powerful and useful open-source tool, is pretty much focused on English language. We would like to contribute to make it a bit more adaptable for using with other languages. Dependencies: This change requires the Spacy library and a language model, specified in the model parameter. Tag maintainer: @dev2049 Twitter handle: @projecte_aina --------- Co-authored-by: Marina Pliusnina <marina.pliusnina@bsc.es> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
This commit is contained in:
parent
744070ee85
commit
a1ce7ab672
@ -52,7 +52,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"embedder = SpacyEmbeddings()"
|
"embedder = SpacyEmbeddings(model_name=\"en_core_web_sm\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1,17 +1,16 @@
|
|||||||
import importlib.util
|
import importlib.util
|
||||||
from typing import Any, Dict, List
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from langchain_core.embeddings import Embeddings
|
from langchain_core.embeddings import Embeddings
|
||||||
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
|
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
|
||||||
|
|
||||||
|
|
||||||
class SpacyEmbeddings(BaseModel, Embeddings):
|
class SpacyEmbeddings(BaseModel, Embeddings):
|
||||||
"""Embeddings by SpaCy models.
|
"""Embeddings by spaCy models.
|
||||||
|
|
||||||
It only supports the 'en_core_web_sm' model.
|
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
nlp (Any): The Spacy model loaded into memory.
|
model_name (str): Name of a spaCy model.
|
||||||
|
nlp (Any): The spaCy model loaded into memory.
|
||||||
|
|
||||||
Methods:
|
Methods:
|
||||||
embed_documents(texts: List[str]) -> List[List[float]]:
|
embed_documents(texts: List[str]) -> List[List[float]]:
|
||||||
@ -20,7 +19,8 @@ class SpacyEmbeddings(BaseModel, Embeddings):
|
|||||||
Generates an embedding for a single piece of text.
|
Generates an embedding for a single piece of text.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
nlp: Any # The Spacy model loaded into memory
|
model_name: str = "en_core_web_sm"
|
||||||
|
nlp: Optional[Any] = None
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
"""Configuration for this pydantic object."""
|
"""Configuration for this pydantic object."""
|
||||||
@ -30,7 +30,7 @@ class SpacyEmbeddings(BaseModel, Embeddings):
|
|||||||
@root_validator(pre=True)
|
@root_validator(pre=True)
|
||||||
def validate_environment(cls, values: Dict) -> Dict:
|
def validate_environment(cls, values: Dict) -> Dict:
|
||||||
"""
|
"""
|
||||||
Validates that the Spacy package and the 'en_core_web_sm' model are installed.
|
Validates that the spaCy package and the model are installed.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
values (Dict): The values provided to the class constructor.
|
values (Dict): The values provided to the class constructor.
|
||||||
@ -39,26 +39,32 @@ class SpacyEmbeddings(BaseModel, Embeddings):
|
|||||||
The validated values.
|
The validated values.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
ValueError: If the Spacy package or the 'en_core_web_sm'
|
ValueError: If the spaCy package or the
|
||||||
model are not installed.
|
model are not installed.
|
||||||
"""
|
"""
|
||||||
# Check if the Spacy package is installed
|
if values.get("model_name") is None:
|
||||||
|
values["model_name"] = "en_core_web_sm"
|
||||||
|
|
||||||
|
model_name = values.get("model_name")
|
||||||
|
|
||||||
|
# Check if the spaCy package is installed
|
||||||
if importlib.util.find_spec("spacy") is None:
|
if importlib.util.find_spec("spacy") is None:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Spacy package not found. "
|
"SpaCy package not found. "
|
||||||
"Please install it with `pip install spacy`."
|
"Please install it with `pip install spacy`."
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
# Try to load the 'en_core_web_sm' Spacy model
|
# Try to load the spaCy model
|
||||||
import spacy
|
import spacy
|
||||||
|
|
||||||
values["nlp"] = spacy.load("en_core_web_sm")
|
values["nlp"] = spacy.load(model_name)
|
||||||
except OSError:
|
except OSError:
|
||||||
# If the model is not found, raise a ValueError
|
# If the model is not found, raise a ValueError
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Spacy model 'en_core_web_sm' not found. "
|
f"SpaCy model '{model_name}' not found. "
|
||||||
"Please install it with"
|
f"Please install it with"
|
||||||
" `python -m spacy download en_core_web_sm`."
|
f" `python -m spacy download {model_name}`"
|
||||||
|
"or provide a valid spaCy model name."
|
||||||
)
|
)
|
||||||
return values # Return the validated values
|
return values # Return the validated values
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user