Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ export CHROMA_SSL="true"

# Optional: Specify path to .env file (defaults to .chroma_env)
export CHROMA_DOTENV_PATH="/path/to/your/.env"

# with Huggingface model
export CHROMA_HF_MODEL="hf-model-name"
```

#### Embedding Function Environment Variables
Expand Down
10 changes: 6 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@ dependencies = [
"chromadb>=1.0.10",
"cohere>=5.14.2",
"httpx>=0.28.1",
"huggingface-hub[hf-xet]>=0.30.1",
"mcp[cli]>=1.2.1",
"openai>=1.70.0",
"pillow>=11.1.0",
"pytest>=8.3.5",
"pytest-asyncio>=0.26.0",
"python-dotenv>=0.19.0",
"sentence-transformers>=4.1.0",
"typing-extensions>=4.13.1",
"voyageai>=0.3.2",
]

[project.urls]
Homepage = "https://github.com/chroma-core/chroma-mcp"
Documentation = "https://github.com/chroma-core/chroma-mcp#readme"
Issues = "https://github.com/chroma-core/chroma-mcp/issues"
# [project.urls]
# Homepage = "https://github.com/chroma-core/chroma-mcp"
# Documentation = "https://github.com/chroma-core/chroma-mcp#readme"
# Issues = "https://github.com/chroma-core/chroma-mcp/issues"

[build-system]
requires = ["hatchling"]
Expand Down
96 changes: 96 additions & 0 deletions src/chroma_mcp/embedding/local_huggingface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
Local huggingface embedding function.
Model should be download to local to run offline first. Follow: https://huggingface.co/docs/hub/en/models-downloading
"""

from chromadb.api.types import Embeddings, Documents, EmbeddingFunction, Space
from typing import List, Dict, Any, Optional
import os
import numpy as np
from chromadb.utils.embedding_functions.schemas import validate_config_schema
from sentence_transformers import SentenceTransformer
from typing import cast


class HuggingFaceLocalEmbeddingFunction(EmbeddingFunction[Documents]):
"""
This class is used to get embeddings for a list of texts using the HuggingFace API.
It requires an API key and a model name. The default model name is "sentence-transformers/all-MiniLM-L6-v2".
"""

def __init__(
self,
model_name: str = os.getenv('CHROMA_HF_MODEL', "sentence-transformers/all-MiniLM-L6-v2" )
):
"""
Initialize the HuggingFaceEmbeddingFunction.

Args:
model_name (str, optional): The name of the model to use for text embeddings.
Defaults to "sentence-transformers/all-MiniLM-L6-v2".
"""

self.model = SentenceTransformer(model_name)
self.model_name = model_name

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

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

Returns:
Embeddings: The embeddings for the texts.

Example:
>>> hugging_face = HuggingFaceEmbeddingFunction(api_key_env_var="CHROMA_HUGGINGFACE_API_KEY")
>>> texts = ["Hello, world!", "How are you?"]
>>> embeddings = hugging_face(texts)
"""
response = self.model.encode(
input,
)
# Convert to numpy arrays
return cast(Embeddings, response.tolist())

@staticmethod
def name() -> str:
return "huggingface"

def default_space(self) -> Space:
return "cosine"

def supported_spaces(self) -> List[Space]:
return ["cosine", "l2", "ip"]

@staticmethod
def build_from_config(config: Dict[str, Any]) -> "EmbeddingFunction[Documents]":
model_name = config.get("model_name")
assert model_name is not None, "Model name must be provided in the config"

return HuggingFaceLocalEmbeddingFunction(model_name=model_name)

def get_config(self) -> Dict[str, Any]:
return {"model_name": self.model_name}

def validate_config_update(
self, old_config: Dict[str, Any], new_config: Dict[str, Any]
) -> None:
if "model_name" in new_config:
raise ValueError(
"The model name cannot be changed after the embedding function has been initialized."
)

@staticmethod
def validate_config(config: Dict[str, Any]) -> None:
"""
Validate the configuration using the JSON schema.

Args:
config: Configuration to validate

Raises:
ValidationError: If the configuration does not match the schema
"""
validate_config_schema(config, "huggingface")
Loading