Add StepProfile and LLM port with errors
Domain enum for step profiles and the LlmPort protocol with request, response, usage and the LlmError hierarchy. Step names are validated because they later become part of file paths. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d4416e4e74
commit
140014f8bb
4 files changed
+166
No files matched your search
@@ -0,0 +1,10 @@
|
||||
from chemenu.domain.step_profile import StepProfile
|
||||
|
||||
|
||||
def test_profile_values() -> None:
|
||||
assert {p.name: p.value for p in StepProfile} == {
|
||||
"ANALYSE": "analyse",
|
||||
"WRITE": "write",
|
||||
"CLASSIFY": "classify",
|
||||
}
|
||||
assert StepProfile("write") is StepProfile.WRITE
|
||||
@@ -0,0 +1,53 @@
|
||||
import pytest
|
||||
from pydantic import BaseModel
|
||||
|
||||
from chemenu.domain.step_profile import StepProfile
|
||||
from chemenu.ports import llm
|
||||
from chemenu.ports.llm import LlmError, LlmRequest, Usage
|
||||
|
||||
|
||||
class _Out(BaseModel):
|
||||
x: int
|
||||
|
||||
|
||||
def _request(step: str) -> LlmRequest:
|
||||
return LlmRequest(step=step, profile=StepProfile.ANALYSE, prompt="p", output_model=_Out)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("step", ["", "Note", "note_inventory", "../x", "a/b", "note-inventory\n"])
|
||||
def test_invalid_step_name_is_rejected(step: str) -> None:
|
||||
with pytest.raises(ValueError, match="invalid step name"):
|
||||
_request(step)
|
||||
|
||||
|
||||
def test_valid_step_name_is_accepted() -> None:
|
||||
assert _request("note-inventory2").step == "note-inventory2"
|
||||
|
||||
|
||||
def test_error_carries_usage() -> None:
|
||||
usage = Usage(input_tokens=3, output_tokens=4)
|
||||
error = llm.TruncatedError("cut off", usage)
|
||||
assert error.usage == usage
|
||||
assert str(error) == "cut off"
|
||||
assert LlmError("plain").usage is None
|
||||
assert usage.cache_read_tokens == 0
|
||||
assert usage.cache_write_tokens == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"name",
|
||||
[
|
||||
"ProfileNotConfiguredError",
|
||||
"MissingApiKeyError",
|
||||
"MissingRecordingError",
|
||||
"RefusedError",
|
||||
"TruncatedError",
|
||||
"ModelMismatchError",
|
||||
"LlmTimeoutError",
|
||||
"ProviderError",
|
||||
],
|
||||
)
|
||||
def test_all_errors_are_llm_errors(name: str) -> None:
|
||||
cls = getattr(llm, name)
|
||||
assert issubclass(cls, LlmError)
|
||||
assert str(cls("msg")) == "msg"
|
||||
Reference in new issue
Block a user