-
-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathtest_multimodal.py
377 lines (306 loc) · 12.3 KB
/
test_multimodal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import pytest
from pathlib import Path
from instructor.multimodal import Image, convert_contents, convert_messages
from instructor.mode import Mode
from unittest.mock import patch, MagicMock
@pytest.fixture
def base64_jpeg():
# Source: https://gist.github.com/trymbill/136dfd4bfc0736fae5b959430ec57373
return "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/wAALCAABAAEBAREA/8QAFAABAAAAAAAAAAAAAAAAAAAACf/EABQQAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQEAAD8AKp//2Q==" # noqa: E501
@pytest.fixture
def base64_png():
# Source: https://gist.github.com/ondrek/7413434
return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=" # noqa: E501
def test_image_from_url():
url = "https://example.com/image.jpg"
image = Image.from_url(url)
assert image.source == url
assert image.media_type == "image/jpeg"
assert image.data is None
def test_image_from_path(tmp_path: Path):
image_path = tmp_path / "test_image.jpg"
image_path.write_bytes(b"fake image data")
image = Image.from_path(image_path)
assert image.source == image_path
assert image.media_type == "image/jpeg"
assert image.data is not None
@pytest.mark.skip(reason="Needs to download image")
def test_image_to_anthropic():
image = Image(
source="http://example.com/image.jpg", media_type="image/jpeg", data=None
)
anthropic_format = image.to_anthropic()
assert anthropic_format["type"] == "image"
assert anthropic_format["source"]["type"] == "base64"
assert anthropic_format["source"]["media_type"] == "image/jpeg"
def test_image_to_openai():
image = Image(
source="http://example.com/image.jpg", media_type="image/jpeg", data=None
)
openai_format = image.to_openai()
assert openai_format["type"] == "image_url"
assert openai_format["image_url"]["url"] == "http://example.com/image.jpg"
def test_convert_contents():
contents = ["Hello", Image.from_url("http://example.com/image.jpg")]
converted = list(convert_contents(contents, Mode.TOOLS))
assert len(converted) == 2
assert converted[0] == {"type": "text", "text": "Hello"}
assert converted[1]["type"] == "image_url"
assert converted[1]["image_url"]["url"] == "http://example.com/image.jpg"
def test_convert_messages():
messages = [
{
"role": "user",
"content": ["Hello", Image.from_url("http://example.com/image.jpg")],
},
{"role": "assistant", "content": "Hi there!"},
]
converted = list(convert_messages(messages, Mode.TOOLS))
assert len(converted) == 2
assert converted[0]["role"] == "user"
assert len(converted[0]["content"]) == 2
assert converted[0]["content"][0] == {"type": "text", "text": "Hello"}
assert converted[0]["content"][1]["type"] == "image_url"
assert converted[1]["role"] == "assistant"
assert converted[1]["content"] == "Hi there!"
def test_convert_messages_anthropic():
messages = [
{
"role": "user",
"content": [
"Hello",
Image(source="base64data", media_type="image/jpeg", data="fakedata"),
],
}
]
converted = list(convert_messages(messages, Mode.ANTHROPIC_JSON))
assert len(converted) == 1
assert converted == [
{
"role": "user",
"content": [
{"type": "text", "text": "Hello"},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "fakedata",
},
},
],
}
]
def test_convert_messages_gemini():
messages = [
{
"role": "user",
"content": ["Hello", Image.from_url("http://example.com/image.jpg")],
}
]
with pytest.raises(NotImplementedError):
list(convert_messages(messages, Mode.GEMINI_JSON))
# Additional tests
def test_image_from_path_unsupported_format(tmp_path: Path):
image_path = tmp_path / "test_image.txt"
image_path.write_bytes(b"fake gif data")
with pytest.raises(ValueError, match="Unsupported image format: text/plain"):
Image.from_path(image_path)
def test_image_from_path_empty_file(tmp_path: Path):
image_path = tmp_path / "empty_image.jpg"
image_path.touch()
with pytest.raises(ValueError, match="Image file is empty"):
Image.from_path(image_path)
def test_image_to_openai_base64():
image = Image(
source="local_file.jpg", media_type="image/jpeg", data="base64encodeddata"
)
openai_format = image.to_openai()
assert openai_format["type"] == "image_url"
assert openai_format["image_url"]["url"].startswith("data:image/jpeg;base64,")
def test_convert_contents_single_string():
content = "Hello, world!"
converted = convert_contents(content, Mode.TOOLS)
assert converted == "Hello, world!"
def test_convert_contents_single_image():
image = Image.from_url("http://example.com/image.jpg")
converted = list(convert_contents(image, Mode.TOOLS))
assert len(converted) == 1
assert converted == [
{"type": "image_url", "image_url": {"url": "http://example.com/image.jpg"}}
]
def test_convert_messages_mixed_content():
messages = [
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there!"},
{"role": "user", "content": Image.from_url("http://example.com/image.jpg")},
]
converted = list(convert_messages(messages, Mode.TOOLS))
assert len(converted) == 3
assert converted[0]["content"] == "Hello"
assert converted[1]["content"] == "Hi there!"
assert converted[2]["content"][0]["type"] == "image_url"
def test_convert_contents_invalid_type():
with pytest.raises(ValueError, match="Unsupported content type"):
list(convert_contents([1, 2, 3], Mode.TOOLS))
def test_convert_contents_anthropic_mode():
contents = [
"Hello",
Image(source="base64data", media_type="image/png", data="fakedata"),
]
converted = list(convert_contents(contents, Mode.ANTHROPIC_JSON))
assert converted[1]["type"] == "image"
assert converted[1]["source"]["type"] == "base64"
assert converted[1]["source"]["media_type"] == "image/png"
def test_convert_contents_custom_dict():
contents = {
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,base64_img"},
}
converted = list(convert_contents(contents, Mode.TOOLS))
assert len(converted) == 1
assert converted == [contents]
def test_image_from_base64_url(base64_png):
image = Image.from_url(base64_png)
assert image.source == base64_png
assert image.media_type == "image/png"
assert image.data is not None
assert image.data == base64_png.split(",")[-1]
def test_image_from_url_with_query_params():
url = "https://example.com/image.jpg?param1=value1¶m2=value2"
image = Image.from_url(url)
assert image.source == url
assert image.media_type == "image/jpeg"
assert image.data is None
def test_image_from_url_with_unusual_extension():
url = "https://example.com/image.webp"
image = Image.from_url(url)
assert image.source == url
assert image.media_type == "image/webp"
assert image.data is None
def test_image_to_openai_with_base64_source(base64_png):
base64_data = base64_png.split(",")[-1]
image = Image(
source=f"data:image/png;base64,{base64_data}",
media_type="image/png",
data=base64_data,
)
openai_format = image.to_openai()
assert openai_format["type"] == "image_url"
assert openai_format["image_url"]["url"] == f"data:image/png;base64,{base64_data}"
def test_image_to_anthropic_with_base64_source(base64_png):
base64_data = base64_png.split(",")[-1]
image = Image(
source=f"data:image/png;base64,{base64_data}",
media_type="image/png",
data=base64_data,
)
anthropic_format = image.to_anthropic()
assert anthropic_format["type"] == "image"
assert anthropic_format["source"]["type"] == "base64"
assert anthropic_format["source"]["media_type"] == "image/png"
assert anthropic_format["source"]["data"] == base64_data
@pytest.mark.parametrize(
"url",
[
"http://example.com/image.jpg",
"https://example.com/image.png",
"https://example.com/image.webp",
"https://example.com/image.jpg?param=value",
"base64_png",
],
)
def test_image_from_various_urls(url, request):
if url.startswith("base64"):
url = request.getfixturevalue(url)
image = Image.from_url(url)
assert image.source == url
if image.is_base64(url):
assert image.data is not None
else:
assert image.data is None
def test_convert_contents_with_base64_image(base64_png):
contents = ["Hello", Image.from_url(base64_png)]
converted = list(convert_contents(contents, Mode.TOOLS))
assert len(converted) == 2
assert converted[0] == {"type": "text", "text": "Hello"}
assert converted[1]["type"] == "image_url"
assert converted[1]["image_url"]["url"] == base64_png
@pytest.mark.parametrize(
"input_data, expected_type, expected_media_type",
[
# URL tests
("http://example.com/image.jpg", "url", "image/jpeg"),
("https://example.com/image.png", "url", "image/png"),
("https://example.com/image.webp", "url", "image/webp"),
("https://example.com/image.jpg?param=value", "url", "image/jpeg"),
(
"https://example.com/image",
"url",
"image/jpeg",
), # Default to JPEG if no extension
# Base64 data URI tests
(
"base64_png",
"base64",
"image/png",
),
(
"base64_jpeg",
"base64",
"image/jpeg",
),
# File path tests (mocked)
("/path/to/image.jpg", "file", "image/jpeg"),
("/path/to/image.png", "file", "image/png"),
("/path/to/image.webp", "file", "image/webp"),
],
)
def test_image_autodetect(input_data, expected_type, expected_media_type, request):
with (
patch("pathlib.Path.is_file", return_value=True),
patch("pathlib.Path.stat", return_value=MagicMock(st_size=1000)),
patch("pathlib.Path.read_bytes", return_value=b"fake image data"),
patch("requests.head") as mock_head,
):
mock_head.return_value = MagicMock(
headers={"Content-Type": expected_media_type}
)
if input_data.startswith("base64"):
input_data = request.getfixturevalue(input_data)
image = Image.autodetect(input_data)
if isinstance(image.source, Path):
assert image.source == Path(input_data)
else:
assert image.source == input_data
assert image.media_type == expected_media_type
if expected_type == "url":
assert image.data is None
elif expected_type == "base64":
assert image.data is not None
assert image.data.startswith("iVBOR") or image.data.startswith("/9j/")
elif expected_type == "file":
assert image.data is not None
assert image.data == "ZmFrZSBpbWFnZSBkYXRh" # base64 of 'fake image data'
def test_image_autodetect_invalid_input():
with pytest.raises(
ValueError, match="Invalid or unsupported base64 image data"
):
Image.autodetect("not_an_image_input")
# Test safely converting an invalid image
assert Image.autodetect_safely("hello") == "hello"
def test_image_autodetect_empty_file(tmp_path):
empty_file = tmp_path / "empty.jpg"
empty_file.touch()
with pytest.raises(ValueError, match="Image file is empty"):
Image.autodetect(empty_file)
def test_raw_base64_autodetect_jpeg(base64_jpeg):
raw_base_64 = base64_jpeg.split(",")[-1]
image = Image.autodetect(raw_base_64)
assert image.media_type == "image/jpeg"
assert image.source == image.data == raw_base_64
def test_raw_base64_autodetect_png(base64_png):
raw_base_64 = base64_png.split(",")[-1]
image = Image.autodetect(raw_base_64)
assert image.media_type == "image/png"
assert image.source == image.data == raw_base_64