-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[ENH] Add muvera and colBERT support to python client #5744
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
Open
jairad26
wants to merge
1
commit into
main
Choose a base branch
from
jai/muvera
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
chromadb/utils/embedding_functions/pylate_colbert_embedding_function.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| from chromadb.api.types import Embeddings, Documents, EmbeddingFunction, Space | ||
| from typing import List, Dict, Any | ||
| from chromadb.utils.embedding_functions.schemas import validate_config_schema | ||
| from chromadb.utils.muvera import create_fdes | ||
|
|
||
|
|
||
| class PylateColBERTEmbeddingFunction(EmbeddingFunction[Documents]): | ||
| """ | ||
| This class is used to get embeddings for a list of texts using the ColBERT API. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| model_name: str, | ||
| ): | ||
| """ | ||
| Initialize the PylateColBERTEmbeddingFunction. | ||
|
|
||
| Args: | ||
| model_name (str): The name of the model to use for text embeddings. | ||
| Examples: "mixedbread-ai/mxbai-edge-colbert-v0-17m", "mixedbread-ai/mxbai-edge-colbert-v0-32m", "lightonai/colbertv2.0", "answerdotai/answerai-colbert-small-v1", "jinaai/jina-colbert-v2", "GTE-ModernColBERT-v1" | ||
| """ | ||
| try: | ||
| from pylate import models | ||
| except ImportError: | ||
| raise ValueError( | ||
| "The pylate colbert python package is not installed. Please install it with `pip install pylate-colbert`" | ||
| ) | ||
|
|
||
| self.model_name = model_name | ||
| self.model = models.ColBERT(model_name_or_path=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. | ||
| """ | ||
| multivec = self.model.encode(input, batch_size=32, is_query=False) | ||
| if not multivec or not multivec[0]: | ||
| raise ValueError("Model returned empty multivector embeddings") | ||
| return create_fdes( | ||
| multivec, | ||
| dims=len(multivec[0][0]), | ||
propel-code-bot[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| is_query=False, | ||
| fill_empty_partitions=True, | ||
| ) | ||
|
|
||
| def embed_query(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. | ||
| """ | ||
| multivec = self.model.encode(input, batch_size=32, is_query=True) | ||
| if not multivec or not multivec[0]: | ||
| raise ValueError("Model returned empty multivector embeddings") | ||
| return create_fdes( | ||
| multivec, | ||
| dims=len(multivec[0][0]), | ||
propel-code-bot[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| is_query=True, | ||
| fill_empty_partitions=False, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def name() -> str: | ||
| return "pylate_colbert" | ||
|
|
||
| def default_space(self) -> Space: | ||
| return "ip" # muvera uses dot product to approximate multivec similarity | ||
|
|
||
| def supported_spaces(self) -> List[Space]: | ||
| return [ | ||
| "ip" | ||
| ] # no cosine bc muvera does not normalize the fde, no l2 bc muvera uses dot product | ||
|
|
||
| @staticmethod | ||
| def build_from_config(config: Dict[str, Any]) -> "EmbeddingFunction[Documents]": | ||
| model_name = config.get("model_name") | ||
|
|
||
| if model_name is None: | ||
| assert False, "This code should not be reached" | ||
|
|
||
| return PylateColBERTEmbeddingFunction(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, "pylate_colbert") | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.