-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for multimodal model and add embed_image (#44)
* feat: add support for multimodal model and add embed_image
- Loading branch information
Showing
6 changed files
with
182 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
__pycache__ | ||
.mypy_cache_test |
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,39 @@ | ||
import base64 | ||
|
||
import pytest | ||
from _pytest.tmpdir import TempPathFactory | ||
from vertexai.vision_models import Image # type: ignore | ||
|
||
|
||
@pytest.fixture | ||
def base64_image() -> str: | ||
return ( | ||
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAA" | ||
"BHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3/OAAAABl0RVh0U29mdHdhcmUAd3" | ||
"d3Lmlua3NjYXBlLm9yZ5vuPBoAAANCSURBVEiJtZZPbBtFFMZ/M7ubXdtdb1xSFyeilBap" | ||
"ySVU8h8OoFaooFSqiihIVIpQBKci6KEg9Q6H9kovIHoCIVQJJCKE1ENFjnAgcaSGC6rEnx" | ||
"BwA04Tx43t2FnvDAfjkNibxgHxnWb2e/u992bee7tCa00YFsffekFY+nUzFtjW0LrvjRXr" | ||
"CDIAaPLlW0nHL0SsZtVoaF98mLrx3pdhOqLtYPHChahZcYYO7KvPFxvRl5XPp1sN3adWiD" | ||
"1ZAqD6XYK1b/dvE5IWryTt2udLFedwc1+9kLp+vbbpoDh+6TklxBeAi9TL0taeWpdmZzQD" | ||
"ry0AcO+jQ12RyohqqoYoo8RDwJrU+qXkjWtfi8Xxt58BdQuwQs9qC/afLwCw8tnQbqYAPs" | ||
"gxE1S6F3EAIXux2oQFKm0ihMsOF71dHYx+f3NND68ghCu1YIoePPQN1pGRABkJ6Bus96Cu" | ||
"tRZMydTl+TvuiRW1m3n0eDl0vRPcEysqdXn+jsQPsrHMquGeXEaY4Yk4wxWcY5V/9scqOM" | ||
"OVUFthatyTy8QyqwZ+kDURKoMWxNKr2EeqVKcTNOajqKoBgOE28U4tdQl5p5bwCw7BWqua" | ||
"ZSzAPlwjlithJtp3pTImSqQRrb2Z8PHGigD4RZuNX6JYj6wj7O4TFLbCO/Mn/m8R+h6rYS" | ||
"Ub3ekokRY6f/YukArN979jcW+V/S8g0eT/N3VN3kTqWbQ428m9/8k0P/1aIhF36PccEl6E" | ||
"hOcAUCrXKZXXWS3XKd2vc/TRBG9O5ELC17MmWubD2nKhUKZa26Ba2+D3P+4/MNCFwg59oW" | ||
"VeYhkzgN/JDR8deKBoD7Y+ljEjGZ0sosXVTvbc6RHirr2reNy1OXd6pJsQ+gqjk8VWFYmH" | ||
"rwBzW/n+uMPFiRwHB2I7ih8ciHFxIkd/3Omk5tCDV1t+2nNu5sxxpDFNx+huNhVT3/zMDz" | ||
"8usXC3ddaHBj1GHj/As08fwTS7Kt1HBTmyN29vdwAw+/wbwLVOJ3uAD1wi/dUH7Qei66Pf" | ||
"yuRj4Ik9is+hglfbkbfR3cnZm7chlUWLdwmprtCohX4HUtlOcQjLYCu+fzGJH2QRKvP3UN" | ||
"z8bWk1qMxjGTOMThZ3kvgLI5AzFfo379UAAAAASUVORK5CYII=" | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def tmp_image(tmp_path_factory: TempPathFactory, base64_image) -> str: | ||
img_data = base64.b64decode(base64_image.split(",")[1]) | ||
image = Image(image_bytes=img_data) | ||
fn = tmp_path_factory.mktemp("data") / "img.png" | ||
image.save(str(fn)) | ||
return str(fn) |
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
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,51 @@ | ||
from typing import Any, Dict | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from pydantic.v1 import root_validator | ||
|
||
from langchain_google_vertexai import VertexAIEmbeddings | ||
from langchain_google_vertexai.embeddings import GoogleEmbeddingModelType | ||
|
||
|
||
def test_langchain_google_vertexai_embed_image_multimodal_only() -> None: | ||
mock_embeddings = MockVertexAIEmbeddings("textembedding-gecko@001") | ||
assert mock_embeddings.model_type == GoogleEmbeddingModelType.TEXT | ||
with pytest.raises(NotImplementedError) as e: | ||
mock_embeddings.embed_image("test") | ||
assert e.value == "Only supported for multimodal models" | ||
|
||
|
||
def test_langchain_google_vertexai_embed_documents_text_only() -> None: | ||
mock_embeddings = MockVertexAIEmbeddings("multimodalembedding@001") | ||
assert mock_embeddings.model_type == GoogleEmbeddingModelType.MULTIMODAL | ||
with pytest.raises(NotImplementedError) as e: | ||
mock_embeddings.embed_documents(["test"]) | ||
assert e.value == "Not supported for multimodal models" | ||
|
||
|
||
def test_langchain_google_vertexai_embed_query_text_only() -> None: | ||
mock_embeddings = MockVertexAIEmbeddings("multimodalembedding@001") | ||
assert mock_embeddings.model_type == GoogleEmbeddingModelType.MULTIMODAL | ||
with pytest.raises(NotImplementedError) as e: | ||
mock_embeddings.embed_query("test") | ||
assert e.value == "Not supported for multimodal models" | ||
|
||
|
||
class MockVertexAIEmbeddings(VertexAIEmbeddings): | ||
""" | ||
A mock class for avoiding instantiating VertexAI and the EmbeddingModel client | ||
instance during init | ||
""" | ||
|
||
def __init__(self, model_name, **kwargs: Any) -> None: | ||
super().__init__(model_name, **kwargs) | ||
|
||
@classmethod | ||
def _init_vertexai(cls, values: Dict) -> None: | ||
pass | ||
|
||
@root_validator() | ||
def validate_environment(cls, values: Dict) -> Dict: | ||
values["client"] = MagicMock() | ||
return values |