97e91a83f3
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
from __future__ import annotations
|
|
import base64
|
|
import pytest
|
|
from instructor.v2.providers.bedrock.handlers import (
|
|
_openai_image_part_to_bedrock,
|
|
_to_bedrock_content_items,
|
|
)
|
|
|
|
|
|
def test_openai_image_part_to_bedrock_data_url(tiny_png_data_url: str):
|
|
part = {"type": "image_url", "image_url": {"url": tiny_png_data_url}}
|
|
out = _openai_image_part_to_bedrock(part)
|
|
assert "image" in out
|
|
assert out["image"]["format"] in {"png", "jpeg", "gif", "webp"} # png expected
|
|
assert out["image"]["source"]["bytes"] == base64.b64decode(
|
|
tiny_png_data_url.split(",", 1)[1]
|
|
)
|
|
|
|
|
|
def test_openai_image_part_to_bedrock_https(image_url: str):
|
|
part = {"type": "image_url", "image_url": {"url": image_url}}
|
|
out = _openai_image_part_to_bedrock(part)
|
|
assert "image" in out
|
|
# GitHub raw returns jpeg for the sample. Normalize is handled in utils.
|
|
assert out["image"]["format"] in {"jpeg", "png", "gif", "webp"}
|
|
assert isinstance(out["image"]["source"]["bytes"], (bytes, bytearray))
|
|
assert len(out["image"]["source"]["bytes"]) > 0
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"text_part",
|
|
[
|
|
{"type": "text", "text": "What is in this image?"},
|
|
{"type": "input_text", "text": "Describe the image."},
|
|
],
|
|
)
|
|
@pytest.mark.parametrize("image_kind", ["data", "https"])
|
|
def test_to_bedrock_content_items_openai_combo(
|
|
text_part, image_kind, tiny_png_data_url: str, image_url: str
|
|
):
|
|
if image_kind == "data":
|
|
image_part = {"type": "image_url", "image_url": {"url": tiny_png_data_url}}
|
|
else:
|
|
image_part = {"type": "image_url", "image_url": {"url": image_url}}
|
|
|
|
content = [text_part, image_part]
|
|
items = _to_bedrock_content_items(content)
|
|
|
|
assert items[0] == {"text": text_part["text"]}
|
|
assert "image" in items[1]
|
|
assert isinstance(items[1]["image"]["source"]["bytes"], (bytes, bytearray))
|
|
assert len(items[1]["image"]["source"]["bytes"]) > 0
|