Add recorded LLM provider replaying answers from files
Key is a hash of step, prompt and output schema, so recordings survive profile changes. Broken recordings raise MissingRecordingError with the path of the affected file. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
63a612c0d6
commit
57c1120b7a
2 files changed
+210
No files matched your search
@@ -0,0 +1,129 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from pydantic import BaseModel
|
||||
|
||||
from chemenu.adapters.llm.config import ModelTarget
|
||||
from chemenu.adapters.llm.recorded import RecordedProvider, request_key
|
||||
from chemenu.domain.step_profile import StepProfile
|
||||
from chemenu.ports.llm import LlmRequest, MissingRecordingError, Usage
|
||||
|
||||
_TARGET = ModelTarget(
|
||||
provider="r", model="claude-x", effort="high", max_tokens=1000, timeout_seconds=60
|
||||
)
|
||||
_META = '{"model": "m", "usage": {"input_tokens": 1, "output_tokens": 1}}'
|
||||
|
||||
|
||||
class _Out(BaseModel):
|
||||
x: int
|
||||
|
||||
|
||||
class _Other(BaseModel):
|
||||
y: str
|
||||
|
||||
|
||||
def _request(
|
||||
step: str = "s",
|
||||
prompt: str = "SECRET",
|
||||
output_model: type[BaseModel] = _Out,
|
||||
profile: StepProfile = StepProfile.ANALYSE,
|
||||
) -> LlmRequest:
|
||||
return LlmRequest(step=step, profile=profile, prompt=prompt, output_model=output_model)
|
||||
|
||||
|
||||
def _write(directory: Path, request: LlmRequest, raw: bytes, meta: str | None) -> tuple[Path, Path]:
|
||||
key = request_key(request)
|
||||
folder = directory / request.step
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
raw_path = folder / f"{key}.json"
|
||||
meta_path = folder / f"{key}.meta.json"
|
||||
raw_path.write_bytes(raw)
|
||||
if meta is not None:
|
||||
meta_path.write_bytes(meta.encode())
|
||||
return raw_path, meta_path
|
||||
|
||||
|
||||
def test_returns_raw_text_model_and_usage(tmp_path: Path) -> None:
|
||||
request = _request(prompt="Größe\r\nZeile")
|
||||
text = '{ "x":1 }\r\nÄöü\n'
|
||||
meta = {"model": "claude-x", "usage": {"input_tokens": 3, "output_tokens": 5}}
|
||||
_write(tmp_path, request, text.encode(), json.dumps(meta))
|
||||
response = RecordedProvider(tmp_path).complete(request, _TARGET)
|
||||
assert response.text == text
|
||||
assert response.model == "claude-x"
|
||||
assert response.usage == Usage(3, 5, 0, 0)
|
||||
|
||||
|
||||
def test_cache_fields_are_read(tmp_path: Path) -> None:
|
||||
request = _request()
|
||||
usage = {
|
||||
"input_tokens": 1,
|
||||
"output_tokens": 2,
|
||||
"cache_read_tokens": 3,
|
||||
"cache_write_tokens": 4,
|
||||
}
|
||||
_write(tmp_path, request, b"raw", json.dumps({"model": "m", "usage": usage}))
|
||||
response = RecordedProvider(tmp_path).complete(request, _TARGET)
|
||||
assert response.usage == Usage(1, 2, 3, 4)
|
||||
|
||||
|
||||
def test_missing_recording_names_path(tmp_path: Path) -> None:
|
||||
request = _request()
|
||||
provider = RecordedProvider(tmp_path)
|
||||
raw_path, meta_path = _write(tmp_path, request, b"raw", None)
|
||||
with pytest.raises(MissingRecordingError) as meta_info:
|
||||
provider.complete(request, _TARGET)
|
||||
assert str(meta_path) in str(meta_info.value)
|
||||
assert "SECRET" not in str(meta_info.value)
|
||||
|
||||
raw_path.unlink()
|
||||
meta_path.write_text(_META)
|
||||
with pytest.raises(MissingRecordingError) as raw_info:
|
||||
provider.complete(request, _TARGET)
|
||||
assert str(raw_path) in str(raw_info.value)
|
||||
|
||||
|
||||
def test_non_utf8_recording_raises(tmp_path: Path) -> None:
|
||||
request = _request()
|
||||
raw_path, _ = _write(tmp_path, request, b"\xff\xfe", _META)
|
||||
with pytest.raises(MissingRecordingError) as info:
|
||||
RecordedProvider(tmp_path).complete(request, _TARGET)
|
||||
assert str(raw_path) in str(info.value)
|
||||
|
||||
|
||||
def test_key_changes_with_prompt_and_schema() -> None:
|
||||
base = request_key(_request())
|
||||
assert request_key(_request(prompt="other")) != base
|
||||
assert request_key(_request(output_model=_Other)) != base
|
||||
assert request_key(_request(step="t")) != base
|
||||
|
||||
|
||||
def test_key_ignores_profile() -> None:
|
||||
assert request_key(_request(profile=StepProfile.ANALYSE)) == request_key(
|
||||
_request(profile=StepProfile.WRITE)
|
||||
)
|
||||
|
||||
|
||||
def test_key_is_stable() -> None:
|
||||
assert request_key(_request()) == "f27cdc9af61eba8c"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"meta",
|
||||
[
|
||||
"not json",
|
||||
"{}",
|
||||
'{"model": "m"}',
|
||||
'{"model": "m", "usage": {"input_tokens": -1, "output_tokens": 1}}',
|
||||
'{"model": "m", "usage": {"input_tokens": 1, "output_tokens": 1, "cache_read_tokens": -1}}',
|
||||
'{"model": "m", "usage": {"input_tokens": "3", "output_tokens": 1}}',
|
||||
'{"model": "m", "usage": {"input_tokens": 1, "output_tokens": 1}, "extra": 1}',
|
||||
],
|
||||
)
|
||||
def test_invalid_meta_raises(tmp_path: Path, meta: str) -> None:
|
||||
request = _request()
|
||||
_, meta_path = _write(tmp_path, request, b"raw", meta)
|
||||
with pytest.raises(MissingRecordingError) as info:
|
||||
RecordedProvider(tmp_path).complete(request, _TARGET)
|
||||
assert str(meta_path) in str(info.value)
|
||||
Reference in new issue
Block a user