Skip to content

Commit

Permalink
added reduced dimensionanility for multimodal embeddings (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkuligin authored Sep 6, 2024
1 parent e200310 commit 638b5e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
7 changes: 5 additions & 2 deletions libs/vertexai/langchain_google_vertexai/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,10 @@ def embed_query(self, text: str) -> List[float]:
return self.embed([text], 1, "RETRIEVAL_QUERY")[0]

def embed_image(
self, image_path: str, contextual_text: Optional[str] = None
self,
image_path: str,
contextual_text: Optional[str] = None,
dimensions: Optional[int] = None,
) -> List[float]:
"""Embed an image.
Expand All @@ -479,5 +482,5 @@ def embed_image(
image = Image(bytes_image)
result: MultiModalEmbeddingResponse = self.instance[
"get_embeddings_with_retry"
](image=image, contextual_text=contextual_text)
](image=image, contextual_text=contextual_text, dimension=dimensions)
return result.image_embedding
15 changes: 12 additions & 3 deletions libs/vertexai/tests/integration_tests/test_embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ def test_langchain_google_vertexai_large_batches() -> None:


@pytest.mark.release
def test_langchain_google_vertexai_image_embeddings(tmp_image) -> None:
@pytest.mark.parametrize(
"dim, expected_dim",
[(None, 1408), (512, 512)],
)
def test_langchain_google_vertexai_image_embeddings(
dim, expected_dim, tmp_image
) -> None:
model = VertexAIEmbeddings(model_name="multimodalembedding")
output = model.embed_image(tmp_image)
assert len(output) == 1408
kwargs = {}
if dim:
kwargs["dimensions"] = dim
output = model.embed_image(tmp_image, **kwargs)
assert len(output) == expected_dim


@pytest.mark.release
Expand Down

0 comments on commit 638b5e0

Please sign in to comment.