Skip to content
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

image_url and contextual_text in mm embeddings #85

Merged
merged 1 commit into from
Mar 25, 2024
Merged
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
14 changes: 10 additions & 4 deletions libs/vertexai/langchain_google_vertexai/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)

from langchain_google_vertexai._base import _VertexAICommon
from langchain_google_vertexai._image_utils import ImageBytesLoader
from langchain_google_vertexai._utils import get_user_agent

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -396,21 +397,26 @@ def embed_query(self, text: str) -> List[float]:
"""
return self.embed([text], 1, "RETRIEVAL_QUERY")[0]

def embed_image(self, image_path: str) -> List[float]:
def embed_image(
self, image_path: str, contextual_text: Optional[str] = None
) -> List[float]:
"""Embed an image.

Args:
image_path: Path to image (local or Google Cloud Storage) to generate
image_path: Path to image (local, Google Cloud Storage or web) to generate
embeddings for.
contextual_text: Text to generate embeddings for.

Returns:
Embedding for the image.
"""
if self.model_type != GoogleEmbeddingModelType.MULTIMODAL:
raise NotImplementedError("Only supported for multimodal models")

image = Image.load_from_file(image_path)
image_loader = ImageBytesLoader()
bytes_image = image_loader.load_bytes(image_path)
image = Image(bytes_image)
result: MultiModalEmbeddingResponse = self.instance[
"get_embeddings_with_retry"
](image=image)
](image=image, contextual_text=contextual_text)
return result.image_embedding
Loading