Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH]: adding capability for Spacy embeddings #2049

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions chromadb/utils/embedding_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,41 @@ def __call__(self, input: Documents) -> Embeddings:
return cast(Embeddings, self._model.encode(texts_with_instructions).tolist())


class SpacyEmbeddingFunction(EmbeddingFunction[Documents]):
def __init__(self, model_name: str = "lg"):
try:
import spacy
except ImportError:
raise ValueError(
"The spacy python package is not installed. Please install it with `pip install spacy`"
)
self._model_name = model_name

try:
self._nlp = spacy.load("en_core_web_{model}".format(model=self._model_name))
except OSError:
raise ValueError(
"spacy models are not downloaded, please download them using `spacy download en-core-web-lg or en-core-web-md`"
)

def __call__(self, input: Documents) -> Embeddings:
"""
Get the embeddings for a list of texts.

Args:
texts (Documents): A list of texts to get embeddings for.

Returns:
Embeddings: The embeddings for the texts.

Example:
>>> spacy_fn = SpacyEmbeddingFunction(model_name="md")
>>> input = ["Hello, world!", "How are you?"]
>>> embeddings = spacy_fn(input)
"""

return [self._nlp(doc).vector for doc in input]

# In order to remove dependencies on sentence-transformers, which in turn depends on
# pytorch and sentence-piece we have created a default ONNX embedding function that
# implements the same functionality as "all-MiniLM-L6-v2" from sentence-transformers.
Expand Down