Files
fegger 081084816b Improve VLM client lifecycle, retries, metrics, and cache keys
- Adds abstract aclose() and shared load_prompt() helper to BaseVLMClient.

- OllamaClient now logs latency, tokens, and prompt hash per call.

- Retries cover HTTP 429 and 5xx in addition to network/timeout errors.

- Cache key now includes response_format and max_tokens.

- Warns when JSON schema generation falls back to plain 'json' format.

- Adds FakeVLMClient.aclose() and cache unit tests.
2026-08-21 16:44:14 +02:00

59 lines
1.7 KiB
Python

"""Shared pytest fixtures."""
from __future__ import annotations
import json
from typing import Any
import pytest
from PIL import Image
from odoo_ocr.clients.base import BaseVLMClient
from odoo_ocr.config import Settings
from odoo_ocr.schemas import VLMResponse
@pytest.fixture
def settings() -> Settings:
"""Default test settings with cache disabled."""
return Settings(
ollama_base_url="http://test-ollama:11435",
cache=Settings().model_dump()["cache"] | {"enabled": False},
)
class FakeVLMClient(BaseVLMClient):
"""Test double that returns canned responses based on prompt content."""
def __init__(self, responses: dict[str, str] | None = None) -> None:
super().__init__(Settings())
self.responses = responses or {}
self.calls: list[dict[str, Any]] = []
async def complete(
self,
system_prompt: str,
user_prompt: str,
images: list[Image.Image],
response_format: type[Any] | None = None,
temperature: float = 0.1,
max_tokens: int = 4096,
model: str | None = None,
) -> VLMResponse:
self.calls.append(
{
"system_prompt": system_prompt,
"user_prompt": user_prompt,
"image_count": len(images),
"model": model,
}
)
for key, value in self.responses.items():
if key in system_prompt or key in user_prompt:
return VLMResponse(content=json.dumps(value), model="fake")
return VLMResponse(content=json.dumps({}), model="fake")
async def aclose(self) -> None:
"""The fake client holds no resources."""
return None