langchain-ai / langchain-google

MIT License
117 stars 147 forks source link

Don't create a gcs storage client on every interaction with blobs #594

Open david-gang opened 2 weeks ago

david-gang commented 2 weeks ago

The following code creates a client per function call at https://github.com/langchain-ai/langchain-google/blob/main/libs/vertexai/langchain_google_vertexai/_image_utils.py

 def _blob_from_gcs(self, gcs_uri: str) -> storage.Blob:
        """Gets image Blob from a Google Cloud Storage uri.

        Args:
            gcs_uri: Valid gcs uri.

        Raises:
            ValueError if there are more than one blob matching the uri.

        Returns:
            storage.Blob
        """

        gcs_client = storage.Client(project=self._project)
        blob = storage.Blob.from_string(gcs_uri, gcs_client)
        blob.reload(client=gcs_client)
        return blob

It would make more sense to make client as a property of ImageBytesLoader

lkuligin commented 2 days ago

good point, would you be open to send a PR and refactor this piece, please?

david-gang commented 1 day ago

hi @lkuligin , I opened a pr https://github.com/langchain-ai/langchain-google/pull/609 but i have failing tests. I don't understand how they are connected to my code. May you please take a look there and explain me ?