|
|
|
@ -36,6 +36,8 @@ class HuggingFaceEmbeddings(BaseModel, Embeddings):
|
|
|
|
|
Can be also set by SENTENCE_TRANSFORMERS_HOME enviroment variable."""
|
|
|
|
|
model_kwargs: Dict[str, Any] = Field(default_factory=dict)
|
|
|
|
|
"""Key word arguments to pass to the model."""
|
|
|
|
|
encode_kwargs: Dict[str, Any] = Field(default_factory=dict)
|
|
|
|
|
"""Key word arguments to pass when calling the `encode` method of the model."""
|
|
|
|
|
|
|
|
|
|
def __init__(self, **kwargs: Any):
|
|
|
|
|
"""Initialize the sentence_transformer."""
|
|
|
|
@ -68,7 +70,7 @@ class HuggingFaceEmbeddings(BaseModel, Embeddings):
|
|
|
|
|
List of embeddings, one for each text.
|
|
|
|
|
"""
|
|
|
|
|
texts = list(map(lambda x: x.replace("\n", " "), texts))
|
|
|
|
|
embeddings = self.client.encode(texts)
|
|
|
|
|
embeddings = self.client.encode(texts, **self.encode_kwargs)
|
|
|
|
|
return embeddings.tolist()
|
|
|
|
|
|
|
|
|
|
def embed_query(self, text: str) -> List[float]:
|
|
|
|
@ -81,7 +83,7 @@ class HuggingFaceEmbeddings(BaseModel, Embeddings):
|
|
|
|
|
Embeddings for the text.
|
|
|
|
|
"""
|
|
|
|
|
text = text.replace("\n", " ")
|
|
|
|
|
embedding = self.client.encode(text)
|
|
|
|
|
embedding = self.client.encode(text, **self.encode_kwargs)
|
|
|
|
|
return embedding.tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -89,7 +91,7 @@ class HuggingFaceInstructEmbeddings(BaseModel, Embeddings):
|
|
|
|
|
"""Wrapper around sentence_transformers embedding models.
|
|
|
|
|
|
|
|
|
|
To use, you should have the ``sentence_transformers``
|
|
|
|
|
and ``InstructorEmbedding`` python package installed.
|
|
|
|
|
and ``InstructorEmbedding`` python packages installed.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
.. code-block:: python
|
|
|
|
@ -108,7 +110,7 @@ class HuggingFaceInstructEmbeddings(BaseModel, Embeddings):
|
|
|
|
|
"""Model name to use."""
|
|
|
|
|
cache_folder: Optional[str] = None
|
|
|
|
|
"""Path to store models.
|
|
|
|
|
Can be also set by SENTENCE_TRANSFORMERS_HOME enviroment variable."""
|
|
|
|
|
Can be also set by SENTENCE_TRANSFORMERS_HOME environment variable."""
|
|
|
|
|
model_kwargs: Dict[str, Any] = Field(default_factory=dict)
|
|
|
|
|
"""Key word arguments to pass to the model."""
|
|
|
|
|
embed_instruction: str = DEFAULT_EMBED_INSTRUCTION
|
|
|
|
|