Skip to content

Commit

Permalink
test: update multimodal tests for autodetect_images parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
devin-ai-integration[bot] and jxnl committed Dec 15, 2024
1 parent ea8b186 commit 48a17fd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
15 changes: 13 additions & 2 deletions tests/llm/test_mistral/test_multimodal.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
"gif": "https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif",
}


class ImageDescription(BaseModel):
objects: list[str] = Field(..., description="The objects in the image")
scene: str = Field(..., description="The scene of the image")
colors: list[str] = Field(..., description="The colors in the image")


@pytest.mark.requires_mistral
@pytest.mark.parametrize("model, mode", product(models, modes))
def test_multimodal_image_description(model: str, mode: Mode, client: Any) -> None:
Expand Down Expand Up @@ -50,6 +52,7 @@ def test_multimodal_image_description(model: str, mode: Mode, client: Any) -> No
assert response.scene != ""
assert len(response.colors) > 0


def test_image_size_validation(tmp_path: Path) -> None:
"""Test that images over 10MB are rejected."""
large_image: Path = tmp_path / "large_image.jpg"
Expand All @@ -58,9 +61,13 @@ def test_image_size_validation(tmp_path: Path) -> None:
typed_file: IO[bytes] = cast(IO[bytes], file_obj)
typed_file.write(b"0" * (10 * 1024 * 1024 + 1))

with pytest.raises(ValueError, match=r"Image file size \(10\.0MB\) exceeds Mistral's limit of 10\.0MB"):
with pytest.raises(
ValueError,
match=r"Image file size \(10\.0MB\) exceeds Mistral's limit of 10\.0MB",
):
Image.from_path(large_image).to_mistral()


def test_image_format_validation() -> None:
"""Test validation of supported image formats."""
# Test valid formats
Expand All @@ -73,6 +80,7 @@ def test_image_format_validation() -> None:
with pytest.raises(ValueError, match="Unsupported image format"):
Image(source="test.bmp", media_type="image/bmp", data="fake_data").to_mistral()


@pytest.mark.requires_mistral
@pytest.mark.parametrize("model, mode", product(models, modes))
def test_multiple_images(model: str, mode: Mode, client: Any) -> None:
Expand Down Expand Up @@ -107,6 +115,7 @@ def test_multiple_images(model: str, mode: Mode, client: Any) -> None:
],
)


def test_image_downscaling() -> None:
"""Test automatic downscaling of large images."""
large_image_url = "https://example.com/large_image.jpg" # Mock URL
Expand All @@ -125,18 +134,20 @@ def test_image_downscaling() -> None:
assert mistral_format is not None
# Note: Actual downscaling verification would require PIL/image processing


def test_base64_image_handling(base64_image: str) -> None:
"""Test handling of base64-encoded images."""
image = Image(
source="data:image/jpeg;base64," + base64_image,
media_type="image/jpeg",
data=base64_image
data=base64_image,
)

mistral_format = image.to_mistral()
assert mistral_format["type"] == "image_url"
assert mistral_format["data"].startswith("data:image/jpeg;base64,")


@pytest.fixture
def base64_image() -> str:
"""Fixture providing a valid base64-encoded test image."""
Expand Down
12 changes: 10 additions & 2 deletions tests/llm/test_openai/test_multimodal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

image_url = "https://retail.degroot-inc.com/wp-content/uploads/2024/01/AS_Blueberry_Patriot_1-605x605.jpg"


class ImageDescription(BaseModel):
objects: list[str] = Field(..., description="The objects in the image")
scene: str = Field(..., description="The scene of the image")
colors: list[str] = Field(..., description="The colors in the image")


@pytest.mark.requires_openai
@pytest.mark.parametrize("model, mode", product(models, modes))
def test_multimodal_image_description(model: str, mode: Mode, client: Any) -> None:
Expand Down Expand Up @@ -42,9 +44,12 @@ def test_multimodal_image_description(model: str, mode: Mode, client: Any) -> No
assert response.scene != ""
assert len(response.colors) > 0


@pytest.mark.requires_openai
@pytest.mark.parametrize("model, mode", product(models, modes))
def test_multimodal_image_description_autodetect(model: str, mode: Mode, client: Any) -> None:
def test_multimodal_image_description_autodetect(
model: str, mode: Mode, client: Any
) -> None:
client = instructor.from_openai(client, mode=mode)
response = client.chat.completions.create(
model=model, # Ensure this is a vision-capable model
Expand All @@ -71,9 +76,12 @@ def test_multimodal_image_description_autodetect(model: str, mode: Mode, client:
assert response.scene != ""
assert len(response.colors) > 0


@pytest.mark.requires_openai
@pytest.mark.parametrize("model, mode", product(models, modes))
def test_multimodal_image_description_autodetect_no_response_model(model: str, mode: Mode, client: Any) -> None:
def test_multimodal_image_description_autodetect_no_response_model(
model: str, mode: Mode, client: Any
) -> None:
client = instructor.from_openai(client, mode=mode)
response = client.chat.completions.create(
response_model=None,
Expand Down
4 changes: 1 addition & 3 deletions tests/test_multimodal.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,7 @@ def test_image_autodetect(input_data, expected_type, expected_media_type, reques


def test_image_autodetect_invalid_input():
with pytest.raises(
ValueError, match="Invalid or unsupported base64 image data"
):
with pytest.raises(ValueError, match="Invalid or unsupported base64 image data"):
Image.autodetect("not_an_image_input")

# Test safely converting an invalid image
Expand Down

0 comments on commit 48a17fd

Please sign in to comment.