120 行
3.4 KiB
Python
120 行
3.4 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import json
|
||
|
|
|
||
|
|
from engines.vision_provider import (
|
||
|
|
OpenAIVisionProvider,
|
||
|
|
VisionProviderConfig,
|
||
|
|
VisionRecognitionRequest,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def _response() -> dict:
|
||
|
|
return {
|
||
|
|
"choices": [
|
||
|
|
{
|
||
|
|
"message": {
|
||
|
|
"content": json.dumps(
|
||
|
|
{
|
||
|
|
"prompt": "shared linework and cel shading",
|
||
|
|
"tags": ["style", "cel shading"],
|
||
|
|
"confidence": 0.92,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
]
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def test_openai_vision_sends_ordered_deduplicated_style_images_with_limit() -> None:
|
||
|
|
captured: dict = {}
|
||
|
|
|
||
|
|
def transport(url, payload, headers, timeout_seconds):
|
||
|
|
captured.update(
|
||
|
|
{
|
||
|
|
"url": url,
|
||
|
|
"payload": payload,
|
||
|
|
"headers": headers,
|
||
|
|
"timeout_seconds": timeout_seconds,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
return _response()
|
||
|
|
|
||
|
|
provider = OpenAIVisionProvider(
|
||
|
|
VisionProviderConfig(
|
||
|
|
provider_id="style-vlm",
|
||
|
|
base_url="https://vision.example",
|
||
|
|
vision_model="vision-model",
|
||
|
|
),
|
||
|
|
transport=transport,
|
||
|
|
)
|
||
|
|
primary = "data:image/png;base64,PRIMARY"
|
||
|
|
request = VisionRecognitionRequest(
|
||
|
|
display_name="style reference set",
|
||
|
|
image_uri=primary,
|
||
|
|
image_uris=tuple(
|
||
|
|
[primary]
|
||
|
|
+ [f"https://images.example/{index}.png" for index in range(1, 10)]
|
||
|
|
),
|
||
|
|
)
|
||
|
|
|
||
|
|
result = provider.recognize_material(request)
|
||
|
|
|
||
|
|
user_content = captured["payload"]["messages"][1]["content"]
|
||
|
|
image_urls = [
|
||
|
|
item["image_url"]["url"]
|
||
|
|
for item in user_content
|
||
|
|
if item.get("type") == "image_url"
|
||
|
|
]
|
||
|
|
assert image_urls == [primary] + [f"https://images.example/{index}.png" for index in range(1, 8)]
|
||
|
|
assert result.prompt == "shared linework and cel shading"
|
||
|
|
|
||
|
|
|
||
|
|
def test_openai_vision_keeps_single_image_contract_unchanged() -> None:
|
||
|
|
captured: dict = {}
|
||
|
|
|
||
|
|
def transport(_url, payload, _headers, _timeout_seconds):
|
||
|
|
captured["payload"] = payload
|
||
|
|
return _response()
|
||
|
|
|
||
|
|
provider = OpenAIVisionProvider(
|
||
|
|
VisionProviderConfig(
|
||
|
|
provider_id="single-vlm",
|
||
|
|
base_url="https://vision.example",
|
||
|
|
vision_model="vision-model",
|
||
|
|
),
|
||
|
|
transport=transport,
|
||
|
|
)
|
||
|
|
provider.recognize_material(
|
||
|
|
VisionRecognitionRequest(
|
||
|
|
display_name="single material",
|
||
|
|
image_uri="data:image/png;base64,SINGLE",
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
user_content = captured["payload"]["messages"][1]["content"]
|
||
|
|
assert [item for item in user_content if item.get("type") == "image_url"] == [
|
||
|
|
{"type": "image_url", "image_url": {"url": "data:image/png;base64,SINGLE"}}
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
def test_multi_image_field_does_not_shift_legacy_positional_arguments() -> None:
|
||
|
|
request = VisionRecognitionRequest(
|
||
|
|
"legacy material",
|
||
|
|
"data:image/png;base64,LEGACY",
|
||
|
|
None,
|
||
|
|
"legacy prompt",
|
||
|
|
"legacy negative",
|
||
|
|
("legacy-tag",),
|
||
|
|
"legacy-model",
|
||
|
|
{"legacy": True},
|
||
|
|
)
|
||
|
|
|
||
|
|
assert request.prompt == "legacy prompt"
|
||
|
|
assert request.negative_prompt == "legacy negative"
|
||
|
|
assert request.tags == ("legacy-tag",)
|
||
|
|
assert request.model == "legacy-model"
|
||
|
|
assert request.metadata == {"legacy": True}
|
||
|
|
assert request.image_uris == ()
|