forked from chroma-core/chroma
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Validate response of embedding function to be in the expected fo…
…rmat during runtime (chroma-core#1615) ## Description of changes Requested in chroma-core#1488 - Improvements & Bug fixes - Raise an exception when an external embedding function doesn't return embeddings in the expected format ## Test plan - [X] Tests pass locally with `pytest` for python, `yarn test` for js ## Documentation Changes *Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the [docs repository](https://github.com/chroma-core/docs)?*
- Loading branch information
1 parent
28aa64c
commit caa10f6
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,40 @@ | ||
import pytest | ||
from typing import List, cast | ||
from chromadb.api.types import EmbeddingFunction, Documents, Image, Document, Embeddings | ||
import numpy as np | ||
|
||
|
||
def random_embeddings() -> Embeddings: | ||
return cast(Embeddings, np.random.random(size=(10, 10)).tolist()) | ||
|
||
|
||
def random_image() -> Image: | ||
return np.random.randint(0, 255, size=(10, 10, 3), dtype=np.int32) | ||
|
||
|
||
def random_documents() -> List[Document]: | ||
return [str(random_image()) for _ in range(10)] | ||
|
||
|
||
def test_embedding_function_results_format_when_response_is_valid() -> None: | ||
valid_embeddings = random_embeddings() | ||
|
||
class TestEmbeddingFunction(EmbeddingFunction[Documents]): | ||
def __call__(self, input: Documents) -> Embeddings: | ||
return valid_embeddings | ||
|
||
ef = TestEmbeddingFunction() | ||
assert valid_embeddings == ef(random_documents()) | ||
|
||
|
||
def test_embedding_function_results_format_when_response_is_invalid() -> None: | ||
invalid_embedding = {"error": "test"} | ||
|
||
class TestEmbeddingFunction(EmbeddingFunction[Documents]): | ||
def __call__(self, input: Documents) -> Embeddings: | ||
return cast(Embeddings, invalid_embedding) | ||
|
||
ef = TestEmbeddingFunction() | ||
with pytest.raises(ValueError) as e: | ||
ef(random_documents()) | ||
assert e.type is ValueError |