Skip to content

Commit

Permalink
genai[refactoring]: Remove Pillow support, adjust dependencies, and c…
Browse files Browse the repository at this point in the history
…lean up unused code (#603)
  • Loading branch information
maxmet91 authored Nov 19, 2024
1 parent 3347d6f commit 28a478d
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 179 deletions.
7 changes: 0 additions & 7 deletions libs/genai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ This package contains the LangChain integrations for Gemini through their genera
pip install -U langchain-google-genai
```

### Image utilities
To use image utility methods, like loading images from GCS urls, install with extras group 'images':

```bash
pip install -e "langchain-google-genai[images]"
```

## Chat Models

Expand Down Expand Up @@ -61,7 +55,6 @@ The value of `image_url` can be any of the following:
- A public image URL
- An accessible gcs file (e.g., "gcs://path/to/file.png")
- A base64 encoded image (e.g., `data:image/png;base64,abcd124`)
- A PIL image



Expand Down
52 changes: 0 additions & 52 deletions libs/genai/langchain_google_genai/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import logging
import uuid
import warnings
from io import BytesIO
from operator import itemgetter
from typing import (
Any,
Expand All @@ -22,7 +21,6 @@
Union,
cast,
)
from urllib.parse import urlparse

import google.api_core

Expand Down Expand Up @@ -114,16 +112,6 @@

from . import _genai_extension as genaix

IMAGE_TYPES: Tuple = ()
try:
import PIL
from PIL.Image import Image

IMAGE_TYPES = IMAGE_TYPES + (Image,)
except ImportError:
PIL = None # type: ignore
Image = None # type: ignore

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -245,46 +233,6 @@ def _is_openai_parts_format(part: dict) -> bool:
return "type" in part


def _is_vision_model(model: str) -> bool:
return "vision" in model


def _is_url(s: str) -> bool:
try:
result = urlparse(s)
return all([result.scheme, result.netloc])
except Exception as e:
logger.debug(f"Unable to parse URL: {e}")
return False


def _is_b64(s: str) -> bool:
return s.startswith("data:image")


def _load_image_from_gcs(path: str, project: Optional[str] = None) -> Image:
try:
from google.cloud import storage # type: ignore[attr-defined]
except ImportError:
raise ImportError(
"google-cloud-storage is required to load images from GCS."
" Install it with `pip install google-cloud-storage`"
)
if PIL is None:
raise ImportError(
"PIL is required to load images. Please install it "
"with `pip install pillow`"
)

gcs_client = storage.Client(project=project)
pieces = path.split("/")
blobs = list(gcs_client.list_blobs(pieces[2], prefix="/".join(pieces[3:])))
if len(blobs) > 1:
raise ValueError(f"Found more than one candidate for {path}!")
img_bytes = blobs[0].download_as_bytes()
return PIL.Image.open(BytesIO(img_bytes))


def _convert_to_parts(
raw_content: Union[str, Sequence[Union[str, dict]]],
) -> List[Part]:
Expand Down
113 changes: 1 addition & 112 deletions libs/genai/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions libs/genai/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ license = "MIT"
python = ">=3.9,<4.0"
langchain-core = ">=0.3.15,<0.4"
google-generativeai = "^0.8.0"
pillow = { version = "^10.1.0", optional = true }
pydantic = ">=2,<3"

[tool.poetry.extras]
images = ["pillow"]

[tool.poetry.group.test]
optional = true

Expand Down Expand Up @@ -48,7 +44,7 @@ codespell = "^2.2.0"
optional = true

[tool.poetry.group.test_integration.dependencies]
pillow = "^10.1.0"
pytest = "^7.3.0"


[tool.poetry.group.lint]
Expand All @@ -62,7 +58,6 @@ ruff = "^0.1.5"
mypy = "^1.10"
types-requests = "^2.28.11.5"
types-google-cloud-ndb = "^2.2.0.1"
types-pillow = "^10.1.0.2"
types-protobuf = "^4.24.0.20240302"
numpy = "^1.26.2"

Expand All @@ -71,9 +66,7 @@ numpy = "^1.26.2"
optional = true

[tool.poetry.group.dev.dependencies]
pillow = "^10.1.0"
types-requests = "^2.31.0.10"
types-pillow = "^10.1.0.2"
types-google-cloud-ndb = "^2.2.0.1"

[tool.ruff.lint]
Expand Down

0 comments on commit 28a478d

Please sign in to comment.