Skip to content

Commit

Permalink
Ensure single instance of ImageBytesLoader in Embeddings (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
SauravP97 authored Feb 18, 2025
1 parent 6091071 commit 80257cd
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions libs/vertexai/langchain_google_vertexai/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings
from concurrent.futures import ThreadPoolExecutor, wait
from enum import Enum, auto
from functools import cached_property
from typing import Any, Dict, List, Literal, Optional, Tuple, Type

from google.api_core.exceptions import (
Expand Down Expand Up @@ -194,6 +195,10 @@ def __init__(
self.client.get_embeddings
)

@cached_property
def _image_bytes_loader_client(self):
return ImageBytesLoader(project=self.project)

@property
def model_type(self) -> str:
return GoogleEmbeddingModelType(self.model_name)
Expand Down Expand Up @@ -517,7 +522,7 @@ def embed_image(
if self.model_type != GoogleEmbeddingModelType.MULTIMODAL:
raise NotImplementedError("Only supported for multimodal models")

image_loader = ImageBytesLoader()
image_loader = self._image_bytes_loader_client
bytes_image = image_loader.load_bytes(image_path)
image = Image(bytes_image)
result: MultiModalEmbeddingResponse = self.instance[
Expand All @@ -544,7 +549,7 @@ def embed_images(
if self.model_type != GoogleEmbeddingModelType.MULTIMODAL:
raise NotImplementedError("Only supported for multimodal models")

image_loader = ImageBytesLoader()
image_loader = self._image_bytes_loader_client

embeddings = []
for image_path in uris:
Expand Down

0 comments on commit 80257cd

Please sign in to comment.