Skip to content

The agent engineering platform.

community[patch]: support fastembed v1 and v2 (#19125)

Sergey Kozlov1a55e950aaaf
diff --git a/libs/community/langchain_community/embeddings/fastembed.py b/libs/community/langchain_community/embeddings/fastembed.py
index 65ed5b16c2..4f03ab70fe 100644
--- a/libs/community/langchain_community/embeddings/fastembed.py
+++ b/libs/community/langchain_community/embeddings/fastembed.py
@@ -57,13 +57,15 @@ class FastEmbedEmbeddings(BaseModel, Embeddings):
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that FastEmbed has been installed."""
+ model_name = values.get("model_name")
+ max_length = values.get("max_length")
+ cache_dir = values.get("cache_dir")
+ threads = values.get("threads")
+
try:
+ # >= v0.2.0
from fastembed import TextEmbedding
- model_name = values.get("model_name")
- max_length = values.get("max_length")
- cache_dir = values.get("cache_dir")
- threads = values.get("threads")
values["_model"] = TextEmbedding(
model_name=model_name,
max_length=max_length,
@@ -71,10 +73,21 @@ class FastEmbedEmbeddings(BaseModel, Embeddings):
threads=threads,
)
except ImportError as ie:
- raise ImportError(
- "'FastEmbedEmbeddings' requires 'fastembed==v0.2.0' or above. "
- "Please install it with `pip install fastembed`."
- ) from ie
+ try:
+ # < v0.2.0
+ from fastembed.embedding import FlagEmbedding
+
+ values["_model"] = FlagEmbedding(
+ model_name=model_name,
+ max_length=max_length,
+ cache_dir=cache_dir,
+ threads=threads,
+ )
+ except ImportError:
+ raise ImportError(
+ "Could not import 'fastembed' Python package. "
+ "Please install it with `pip install fastembed`."
+ ) from ie
return values
def embed_documents(self, texts: List[str]) -> List[List[float]]: