wren node: targets from the wiring, on wren 2
Every output is a target and every input besides prompt a reading. A targets setting gives descriptions, values, ranges and per-target guards, model takes a trained pack's artifact digest, min_confidence defaults to the model's threshold. A command is applied whole or not at all, and targets the model was not trained on read degraded. The home path (area/kind ports, groups, brightness, temperature answers) is gone.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
from collections.abc import Generator
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
@@ -12,40 +14,51 @@ def db() -> Generator[None, None, None]:
|
||||
|
||||
|
||||
@dataclass
|
||||
class _Device:
|
||||
kind: str
|
||||
id: str
|
||||
state: str
|
||||
class FakeTarget:
|
||||
name: str
|
||||
type: str
|
||||
value: Any
|
||||
values: list[str] | None = None
|
||||
range: list[float] | None = None
|
||||
description: str = ""
|
||||
unit: str | None = None
|
||||
readonly: bool = False
|
||||
|
||||
|
||||
@dataclass
|
||||
class _House:
|
||||
time: str
|
||||
outdoor_c: float
|
||||
dark: bool
|
||||
user_area: str | None
|
||||
areas: dict[str, list[_Device]]
|
||||
class FakeCall:
|
||||
target: str
|
||||
value: Any = None
|
||||
delay_s: float | None = None
|
||||
confidence: float = 1.0
|
||||
|
||||
|
||||
class FakeWren:
|
||||
"""The model package's surface, answering whatever a test sets ``answer`` to."""
|
||||
"""The model package's surface: ``act`` answers whatever a test sets
|
||||
``answer`` to, and ``load_pack`` returns ``pack``."""
|
||||
|
||||
AREAS = [
|
||||
*("living room", "kitchen", "bathroom", "bedroom", "office"),
|
||||
*("garden", "patio", "garage", "driveway", "balcony"),
|
||||
]
|
||||
KINDS = ["light", "media", "heat", "shutter", "water", "switch"]
|
||||
Device = _Device
|
||||
House = _House
|
||||
Target = FakeTarget
|
||||
Call = FakeCall
|
||||
runtime = SimpleNamespace(MODEL=Path("demo.bin"))
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.answer: tuple[list[Any], float] = ([], 1.0)
|
||||
self.houses: list[_House] = []
|
||||
self.answer: list[FakeCall] = []
|
||||
self.pack: dict[str, Any] = {"meta": {"threshold": 0.9, "targets": []}}
|
||||
self.loaded: list[Path] = []
|
||||
self.commands: list[str] = []
|
||||
self.targets: list[list[FakeTarget]] = []
|
||||
self.models: list[Any] = []
|
||||
|
||||
def act(self, command: str, house: _House) -> tuple[list[Any], float]:
|
||||
self.houses.append(house)
|
||||
def load_pack(self, path: Path) -> dict[str, Any]:
|
||||
self.loaded.append(path)
|
||||
return self.pack
|
||||
|
||||
def act(
|
||||
self, command: str, targets: list[FakeTarget], *, model: Any = None
|
||||
) -> list[FakeCall]:
|
||||
self.commands.append(command)
|
||||
self.targets.append(targets)
|
||||
self.models.append(model)
|
||||
return self.answer
|
||||
|
||||
|
||||
|
||||
+343
-244
@@ -4,79 +4,96 @@ The model is an optional package, so ``fake_wren`` (conftest) answers in its
|
||||
place: what the node does with an answer is what is under test here.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from fluksio.flow.artifacts import ArtifactStore
|
||||
from fluksio.flow.messages import DType, MessageSpec
|
||||
from fluksio.flow.nodes.base import Node
|
||||
from fluksio.flow.nodes.wren import CANT, WrenNode, _plain, _value
|
||||
from fluksio.flow.nodes.wren import CANT, WrenNode, _plain
|
||||
from fluksio.flow.pipeline import Pipeline
|
||||
|
||||
|
||||
@dataclass
|
||||
class FakeCall:
|
||||
tool: str
|
||||
args: dict[str, Any] = field(default_factory=dict)
|
||||
delay_s: float | None = None
|
||||
PROMPT = MessageSpec(name="command", port="prompt", dtype=DType.STR)
|
||||
OUTDOOR = MessageSpec(name="outdoor", port="outdoor_temperature", dtype=DType.FLOAT)
|
||||
LIGHT = MessageSpec(name="kitchen_light", dtype=DType.BOOL)
|
||||
|
||||
|
||||
def _node(*targets: tuple[str, DType], **params: object) -> WrenNode:
|
||||
def _node(
|
||||
fake_wren,
|
||||
*targets: tuple[str, DType],
|
||||
inputs=(),
|
||||
saying=False,
|
||||
trained=None,
|
||||
health=None,
|
||||
app=None,
|
||||
**params: Any,
|
||||
) -> WrenNode:
|
||||
"""A started node, its model trained on its own targets unless ``trained``
|
||||
lists others."""
|
||||
node = WrenNode(
|
||||
requires=[MessageSpec(name="command", port="prompt", dtype=DType.STR)],
|
||||
requires=[PROMPT, *inputs],
|
||||
provides=[
|
||||
MessageSpec(name="actions", dtype=DType.LIST),
|
||||
*(MessageSpec(name=port, dtype=dtype) for port, dtype in targets),
|
||||
*(
|
||||
MessageSpec(name=port, dtype=DType.STR)
|
||||
for port in ("reply", "unhandled")
|
||||
if saying
|
||||
),
|
||||
],
|
||||
params=params,
|
||||
)
|
||||
node.assign_flow("house", "control")
|
||||
if health is not None:
|
||||
node._on_health = lambda _node, status, detail: health.append((status, detail))
|
||||
fake_wren.pack["meta"]["targets"] = (
|
||||
list(node.targets.values()) if trained is None else trained
|
||||
)
|
||||
asyncio.run(node.start(app))
|
||||
return node
|
||||
|
||||
|
||||
def test_targets_of_one_area_and_kind_are_one_device(fake_wren):
|
||||
node = _node(
|
||||
("living_room_setpoint", DType.FLOAT),
|
||||
("living_room_boost", DType.BOOL),
|
||||
("kitchen_lamp", DType.BOOL),
|
||||
("office_plug", DType.BOOL),
|
||||
)
|
||||
assert node.devices == {
|
||||
("living room", "heat"): ["living_room_setpoint", "living_room_boost"],
|
||||
("kitchen", "light"): ["kitchen_lamp"],
|
||||
("office", "switch"): ["office_plug"],
|
||||
}
|
||||
def _record(port: str, value: Any, call: str, **more: Any) -> dict[str, Any]:
|
||||
return {
|
||||
"message": f"house.{port}",
|
||||
"value": value,
|
||||
"applied": True,
|
||||
"delay_s": None,
|
||||
"call": call,
|
||||
"confidence": 1.0,
|
||||
} | more
|
||||
|
||||
|
||||
def test_a_kind_the_model_does_not_know_is_refused(fake_wren, monkeypatch):
|
||||
monkeypatch.setattr(fake_wren, "KINDS", ["light"])
|
||||
with pytest.raises(ValueError, match="switch"):
|
||||
_node(("office_plug", DType.BOOL))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("port", ["hallway_light", "kitchen_fridge", "light"])
|
||||
def test_a_port_naming_no_trained_device_is_refused(fake_wren, port):
|
||||
with pytest.raises(ValueError, match="living_room") as refused:
|
||||
_node((port, DType.BOOL))
|
||||
assert port in str(refused.value)
|
||||
|
||||
|
||||
def test_a_prompt_is_required(fake_wren):
|
||||
with pytest.raises(ValueError, match="prompt"):
|
||||
WrenNode(provides=[MessageSpec(name="kitchen_light", dtype=DType.BOOL)])
|
||||
@pytest.mark.parametrize(
|
||||
("requires", "provides", "params", "refusal"),
|
||||
[
|
||||
([], [LIGHT], {}, "prompt"),
|
||||
([PROMPT], [MessageSpec(name="plan", dtype=DType.JSON)], {}, "plan"),
|
||||
([PROMPT], [LIGHT], {"targets": {"hall_light": {}}}, "hall_light"),
|
||||
(
|
||||
[PROMPT],
|
||||
[LIGHT],
|
||||
{"targets": {"kitchen_light": {"values": ["ON"]}}},
|
||||
"values",
|
||||
),
|
||||
([PROMPT], [LIGHT], {"targets": {"kitchen_light": {"range": [0, 1]}}}, "range"),
|
||||
],
|
||||
)
|
||||
def test_a_wiring_wren_cannot_act_on_is_refused(
|
||||
fake_wren, requires, provides, params, refusal
|
||||
):
|
||||
with pytest.raises(ValueError, match=refusal):
|
||||
WrenNode(requires=requires, provides=provides, params=params)
|
||||
|
||||
|
||||
def test_only_the_prompt_wakes_it(fake_wren):
|
||||
node = WrenNode(
|
||||
requires=[
|
||||
MessageSpec(name="command", port="prompt", dtype=DType.STR),
|
||||
MessageSpec(name="outside", port="outdoor", dtype=DType.FLOAT),
|
||||
],
|
||||
)
|
||||
node = WrenNode(requires=[PROMPT, OUTDOOR])
|
||||
assert {spec.port: spec.trigger for spec in node.input_ports} == {
|
||||
"prompt": True,
|
||||
"outdoor": False,
|
||||
"outdoor_temperature": False,
|
||||
}
|
||||
|
||||
|
||||
@@ -85,83 +102,284 @@ def test_the_model_reads_the_command_plain():
|
||||
assert _plain("Heat it to 22.5, please!") == "heat it to 22.5 please"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("tool", "args", "dtype", "value"),
|
||||
[
|
||||
("lights.on", {}, DType.BOOL, True),
|
||||
("lights.on", {}, DType.FLOAT, 100.0),
|
||||
("lights.off", {}, DType.STR, "off"),
|
||||
("heating.set", {"temp": 22}, DType.FLOAT, 22.0),
|
||||
("heating.set", {"temp": 22}, DType.BOOL, None),
|
||||
("heating.boost", {}, DType.BOOL, True),
|
||||
("heating.boost", {}, DType.INT, None),
|
||||
("switches.off", {}, DType.INT, 0),
|
||||
("sensors.temperature", {"area": "kitchen"}, DType.FLOAT, None),
|
||||
],
|
||||
)
|
||||
def test_a_call_means_what_its_target_type_can_hold(tool, args, dtype, value):
|
||||
assert _value(FakeCall(tool, args), dtype) == value
|
||||
|
||||
|
||||
def test_applied_calls_reach_their_targets(fake_wren):
|
||||
def test_the_targets_reach_the_model_with_their_values_and_settings(fake_wren):
|
||||
node = _node(
|
||||
("living_room_setpoint", DType.FLOAT),
|
||||
("living_room_boost", DType.BOOL),
|
||||
fake_wren,
|
||||
("kitchen_light", DType.BOOL),
|
||||
("living_room_heating", DType.FLOAT),
|
||||
("kitchen_shutter", DType.STR),
|
||||
inputs=[OUTDOOR],
|
||||
targets={
|
||||
"living_room_heating": {"range": [5, 30], "unit": "°C"},
|
||||
"kitchen_shutter": {"values": ["UP", "DOWN"]},
|
||||
"outdoor_temperature": {"description": "temperature outside"},
|
||||
},
|
||||
)
|
||||
Pipeline([node]).state["house.kitchen_light"] = True
|
||||
|
||||
node.execute({"house.command": "warm it up", "house.outdoor": 6.2})
|
||||
|
||||
assert fake_wren.loaded == [fake_wren.runtime.MODEL]
|
||||
assert fake_wren.models == [fake_wren.pack]
|
||||
# Targets nothing has set yet are off, 0 within their range, or their
|
||||
# first value.
|
||||
target = fake_wren.Target
|
||||
assert fake_wren.targets[0] == [
|
||||
target("kitchen_light", "bool", True),
|
||||
target("living_room_heating", "float", 5.0, range=[5.0, 30.0], unit="°C"),
|
||||
target("kitchen_shutter", "str", "UP", values=["UP", "DOWN"]),
|
||||
target(
|
||||
"outdoor_temperature",
|
||||
"float",
|
||||
6.2,
|
||||
description="temperature outside",
|
||||
readonly=True,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def test_applied_calls_reach_their_targets_and_the_reply_says_so(fake_wren):
|
||||
node = _node(
|
||||
fake_wren,
|
||||
("kitchen_light", DType.BOOL),
|
||||
("living_room_heating", DType.FLOAT),
|
||||
saying=True,
|
||||
apply=True,
|
||||
targets={"living_room_heating": {"range": [5, 30], "unit": "°C"}},
|
||||
)
|
||||
Pipeline([node]).state["house.living_room_setpoint"] = 21.0
|
||||
fake_wren.answer = (
|
||||
[
|
||||
FakeCall("heating.set", {"id": "living room:heat", "temp": 23}),
|
||||
# The living room's light is filled in for the model, not wired.
|
||||
FakeCall("lights.on", {"id": "living room:light"}),
|
||||
# 0.95 clears the model's own threshold, 0.9 here.
|
||||
fake_wren.answer = [
|
||||
fake_wren.Call("kitchen_light", True, confidence=0.95),
|
||||
fake_wren.Call("living_room_heating", 21),
|
||||
]
|
||||
|
||||
out = node.execute({"house.command": "kitchen light on and the heating to 21"})
|
||||
|
||||
assert out == {
|
||||
"house.kitchen_light": True,
|
||||
"house.living_room_heating": 21.0,
|
||||
"house.reply": "Kitchen light on, living room heating to 21 °C.",
|
||||
"house.actions": [
|
||||
_record("kitchen_light", True, "set(kitchen_light, true)", confidence=0.95),
|
||||
_record("living_room_heating", 21.0, "set(living_room_heating, 21)"),
|
||||
],
|
||||
0.98,
|
||||
)
|
||||
|
||||
out = node.execute({"house.command": "set the heating to 23 degrees"})
|
||||
|
||||
assert out is not None
|
||||
assert out["house.living_room_setpoint"] == 23.0
|
||||
assert "house.living_room_boost" not in out
|
||||
assert [(a["message"], a["applied"]) for a in out["house.actions"]] == [
|
||||
("house.living_room_setpoint", True),
|
||||
("", False),
|
||||
]
|
||||
# Areas nothing is wired to are shown too, so a call can name them.
|
||||
assert set(fake_wren.houses[0].areas) == set(fake_wren.AREAS)
|
||||
living = fake_wren.houses[0].areas["living room"]
|
||||
assert [(d.kind, d.state) for d in living] == [
|
||||
("light", "off"),
|
||||
("media", "off"),
|
||||
("heat", "21C"),
|
||||
]
|
||||
}
|
||||
assert isinstance(out["house.living_room_heating"], float)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("params", "confidence"),
|
||||
[({}, 0.99), ({"apply": True}, 0.5), ({"apply": True, "min_confidence": 1}, 0.99)],
|
||||
[
|
||||
({}, 0.99),
|
||||
# Below the model's own threshold, 0.9 here.
|
||||
({"apply": True}, 0.85),
|
||||
({"apply": True, "min_confidence": 1}, 0.99),
|
||||
],
|
||||
)
|
||||
def test_nothing_is_written_unless_applied_and_sure(fake_wren, params, confidence):
|
||||
node = _node(("kitchen_light", DType.BOOL), **params)
|
||||
fake_wren.answer = ([FakeCall("lights.on", {"id": "kitchen:light"})], confidence)
|
||||
node = _node(fake_wren, ("kitchen_light", DType.BOOL), **params)
|
||||
fake_wren.answer = [fake_wren.Call("kitchen_light", True, confidence=confidence)]
|
||||
|
||||
out = node.execute({"house.command": "turn on the lights in the kitchen"})
|
||||
out = node.execute({"house.command": "turn on the kitchen light"})
|
||||
|
||||
assert out == {
|
||||
"house.actions": [
|
||||
{
|
||||
"message": "house.kitchen_light",
|
||||
"value": True,
|
||||
"applied": False,
|
||||
"delay_s": None,
|
||||
"call": "lights.on()",
|
||||
"confidence": confidence,
|
||||
}
|
||||
_record(
|
||||
"kitchen_light",
|
||||
True,
|
||||
"set(kitchen_light, true)",
|
||||
applied=False,
|
||||
confidence=confidence,
|
||||
)
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("params", "applied"),
|
||||
[
|
||||
({}, True),
|
||||
# One call short of its target's bar stops the whole command.
|
||||
({"targets": {"kitchen_light": {"min_confidence": 0.99}}}, False),
|
||||
({"targets": {"kitchen_light": {"pass_on": True}}}, False),
|
||||
({"min_confidence": 0.99}, False),
|
||||
# A target's own bar is taken over the node's.
|
||||
(
|
||||
{
|
||||
"min_confidence": 0.99,
|
||||
"targets": {
|
||||
"kitchen_light": {"min_confidence": 0.9},
|
||||
"garden_light": {"min_confidence": 0.9},
|
||||
},
|
||||
},
|
||||
True,
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_a_command_is_applied_whole_or_not_at_all(fake_wren, params, applied):
|
||||
node = _node(
|
||||
fake_wren,
|
||||
("kitchen_light", DType.BOOL),
|
||||
("garden_light", DType.BOOL),
|
||||
saying=True,
|
||||
apply=True,
|
||||
**params,
|
||||
)
|
||||
fake_wren.answer = [
|
||||
fake_wren.Call("garden_light", True, confidence=0.95),
|
||||
fake_wren.Call("kitchen_light", True, confidence=0.95),
|
||||
]
|
||||
command = "garden and kitchen lights on"
|
||||
|
||||
out = node.execute({"house.command": command})
|
||||
|
||||
assert out is not None
|
||||
assert [a["applied"] for a in out.pop("house.actions")] == [applied, applied]
|
||||
assert out == (
|
||||
{
|
||||
"house.garden_light": True,
|
||||
"house.kitchen_light": True,
|
||||
"house.reply": "Garden light on, kitchen light on.",
|
||||
}
|
||||
if applied
|
||||
else {"house.unhandled": command, "house.reply": CANT}
|
||||
)
|
||||
|
||||
|
||||
def test_a_target_the_model_was_not_trained_on_is_never_applied(fake_wren):
|
||||
trained = [
|
||||
# As the pack writes it: [5, 30] is the node's (5.0, 30.0), and the
|
||||
# unit is not the model's to know.
|
||||
{
|
||||
"name": "living_room_heating",
|
||||
"type": "float",
|
||||
"values": None,
|
||||
"range": [5, 30],
|
||||
"description": "",
|
||||
"unit": None,
|
||||
"readonly": False,
|
||||
},
|
||||
# Described otherwise since.
|
||||
{
|
||||
"name": "canopy",
|
||||
"type": "str",
|
||||
"values": ["UP", "DOWN", "STOP"],
|
||||
"range": None,
|
||||
"description": "awning",
|
||||
"unit": None,
|
||||
"readonly": False,
|
||||
},
|
||||
]
|
||||
health: list[tuple[str, str | None]] = []
|
||||
node = _node(
|
||||
fake_wren,
|
||||
("living_room_heating", DType.FLOAT),
|
||||
("canopy", DType.STR),
|
||||
("garden_light", DType.BOOL),
|
||||
saying=True,
|
||||
trained=trained,
|
||||
health=health,
|
||||
apply=True,
|
||||
targets={
|
||||
"living_room_heating": {"range": [5, 30], "unit": "°C"},
|
||||
"canopy": {
|
||||
"values": ["UP", "DOWN", "STOP"],
|
||||
"description": "awning over the patio",
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
((status, detail),) = health
|
||||
assert status == "degraded"
|
||||
assert detail is not None and "retraining for canopy, garden_light" in detail
|
||||
|
||||
fake_wren.answer = [fake_wren.Call("canopy", "DOWN")]
|
||||
out = node.execute({"house.command": "close the awning"})
|
||||
assert out is not None
|
||||
assert [a["applied"] for a in out["house.actions"]] == [False]
|
||||
assert out["house.unhandled"] == "close the awning"
|
||||
|
||||
|
||||
def test_a_read_is_answered_from_the_target_value(fake_wren):
|
||||
node = _node(
|
||||
fake_wren,
|
||||
inputs=[OUTDOOR],
|
||||
saying=True,
|
||||
# Not applied: a reading is answered anyway, as it writes nothing.
|
||||
targets={"outdoor_temperature": {"unit": "°C"}},
|
||||
)
|
||||
fake_wren.answer = [fake_wren.Call("outdoor_temperature")]
|
||||
command = "how warm is it outside"
|
||||
|
||||
out = node.execute({"house.command": command, "house.outdoor": 6.2})
|
||||
assert out == {
|
||||
"house.reply": "Outdoor temperature is 6.2 °C.",
|
||||
"house.actions": [
|
||||
_record("outdoor", 6.2, "read(outdoor_temperature)"),
|
||||
],
|
||||
}
|
||||
|
||||
# Nothing has written the reading yet: there is no answer to give.
|
||||
out = node.execute({"house.command": command})
|
||||
assert out is not None
|
||||
assert out["house.unhandled"] == command
|
||||
|
||||
|
||||
def test_a_delayed_call_is_held_by_the_work_queue(fake_wren, monkeypatch):
|
||||
node = _node(fake_wren, ("kitchen_light", DType.BOOL), saying=True, apply=True)
|
||||
deferred: list[tuple[dict[str, Any], float]] = []
|
||||
|
||||
def defer(self, node, outputs, seconds):
|
||||
deferred.append((outputs, seconds))
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(Pipeline, "defer", defer)
|
||||
Pipeline([node])
|
||||
fake_wren.answer = [
|
||||
fake_wren.Call("kitchen_light", False),
|
||||
fake_wren.Call("kitchen_light", True, delay_s=420),
|
||||
]
|
||||
|
||||
out = node.execute({"house.command": "kitchen light off for 7 minutes"})
|
||||
|
||||
assert deferred == [({"house.kitchen_light": True}, 420)]
|
||||
assert out is not None
|
||||
assert out["house.kitchen_light"] is False
|
||||
assert out["house.reply"] == "Kitchen light off, kitchen light on in 7 minutes."
|
||||
|
||||
|
||||
def test_a_trained_model_is_read_from_the_artifact_store(fake_wren, tmp_path):
|
||||
store = ArtifactStore(tmp_path)
|
||||
digest = store.put([b"WREN"])["digest"]
|
||||
app = SimpleNamespace(state=SimpleNamespace(artifact_store=store))
|
||||
health: list[tuple[str, str | None]] = []
|
||||
|
||||
_node(
|
||||
fake_wren, ("kitchen_light", DType.BOOL), app=app, health=health, model=digest
|
||||
)
|
||||
assert fake_wren.loaded == [store.path(digest)]
|
||||
assert health == [("ok", None)]
|
||||
|
||||
# One the store does not have: every command goes on.
|
||||
node = _node(
|
||||
fake_wren,
|
||||
("kitchen_light", DType.BOOL),
|
||||
saying=True,
|
||||
app=app,
|
||||
health=health,
|
||||
apply=True,
|
||||
model="sha256:" + "0" * 64,
|
||||
)
|
||||
assert health[-1][0] == "degraded"
|
||||
out = node.execute({"house.command": "kitchen light on"})
|
||||
assert out == {
|
||||
"house.actions": [],
|
||||
"house.unhandled": "kitchen light on",
|
||||
"house.reply": CANT,
|
||||
}
|
||||
assert fake_wren.commands == []
|
||||
|
||||
|
||||
def test_the_model_runs_on_one_blas_thread(fake_wren, monkeypatch):
|
||||
"""About twice as fast for a model this small; numpy is loaded by `mlp`."""
|
||||
from threadpoolctl import threadpool_info
|
||||
@@ -169,163 +387,44 @@ def test_the_model_runs_on_one_blas_thread(fake_wren, monkeypatch):
|
||||
threads: list[int] = []
|
||||
act = fake_wren.act
|
||||
|
||||
def counted(command: str, house: Any) -> tuple[list[Any], float]:
|
||||
def counted(command: str, targets: Any, *, model: Any = None) -> Any:
|
||||
threads.extend(
|
||||
pool["num_threads"]
|
||||
for pool in threadpool_info()
|
||||
if pool["user_api"] == "blas"
|
||||
)
|
||||
return act(command, house)
|
||||
return act(command, targets, model=model)
|
||||
|
||||
monkeypatch.setattr(fake_wren, "act", counted)
|
||||
_node(("kitchen_lamp", DType.BOOL)).execute({"house.command": "lights on"})
|
||||
node = _node(fake_wren, ("kitchen_light", DType.BOOL))
|
||||
node.execute({"house.command": "lights on"})
|
||||
|
||||
assert threads
|
||||
assert set(threads) == {1}
|
||||
|
||||
|
||||
def test_a_reply_says_what_was_applied_and_the_rest_is_unhandled(fake_wren):
|
||||
node = WrenNode(
|
||||
requires=[MessageSpec(name="command", port="prompt", dtype=DType.STR)],
|
||||
provides=[
|
||||
MessageSpec(name="kitchen_light", dtype=DType.BOOL),
|
||||
MessageSpec(name="reply", dtype=DType.STR),
|
||||
MessageSpec(name="unhandled", dtype=DType.STR),
|
||||
],
|
||||
params={"apply": True},
|
||||
def test_own_words_and_number_words_reach_the_model_as_its_own(fake_wren):
|
||||
node = _node(
|
||||
fake_wren,
|
||||
("garden_light", DType.BOOL),
|
||||
saying=True,
|
||||
apply=True,
|
||||
aliases={"Page Too": "garden"},
|
||||
)
|
||||
node.assign_flow("house", "control")
|
||||
fake_wren.answer = ([FakeCall("lights.on", {"id": "kitchen:light"})], 0.99)
|
||||
|
||||
out = node.execute({"house.command": "turn on the kitchen light"})
|
||||
|
||||
assert out == {"house.kitchen_light": True, "house.reply": "Kitchen light on."}
|
||||
|
||||
# Nothing wired for the call, or nothing to call at all. Nothing reads
|
||||
# what is left, so the node says so itself.
|
||||
asked = "what is the weather tomorrow"
|
||||
for calls in ([FakeCall("lights.on", {"id": "garage:light"})], []):
|
||||
fake_wren.answer = (calls, 0.99)
|
||||
out = node.execute({"house.command": asked})
|
||||
assert out == {"house.unhandled": asked, "house.reply": CANT}
|
||||
|
||||
# An assistant reading it answers instead.
|
||||
unhandled = MessageSpec(name="house.unhandled", port="unhandled", dtype=DType.STR)
|
||||
Pipeline([node, Node(lambda params, unhandled: None, requires=[unhandled])])
|
||||
assert node.execute({"house.command": asked}) == {"house.unhandled": asked}
|
||||
|
||||
|
||||
def test_a_call_on_an_area_the_command_does_not_name_is_not_applied(fake_wren):
|
||||
"""A word the model never learned lands on a neighbour it was shown."""
|
||||
node = _node(("bathroom_light", DType.BOOL), apply=True)
|
||||
fake_wren.answer = ([FakeCall("lights.on", {"id": "bathroom:light"})], 0.97)
|
||||
|
||||
out = node.execute({"house.command": "switch on the appliances"})
|
||||
|
||||
assert out is not None
|
||||
assert "house.bathroom_light" not in out
|
||||
assert [a["applied"] for a in out["house.actions"]] == [False]
|
||||
|
||||
|
||||
def _speaking(*targets: tuple[str, DType], inputs=(), **params: object) -> WrenNode:
|
||||
node = WrenNode(
|
||||
requires=[MessageSpec(name="command", port="prompt", dtype=DType.STR), *inputs],
|
||||
provides=[
|
||||
*(MessageSpec(name=port, dtype=dtype) for port, dtype in targets),
|
||||
MessageSpec(name="reply", dtype=DType.STR),
|
||||
MessageSpec(name="unhandled", dtype=DType.STR),
|
||||
],
|
||||
params={"apply": True, **params},
|
||||
)
|
||||
node.assign_flow("house", "control")
|
||||
return node
|
||||
|
||||
|
||||
def test_the_house_words_and_number_words_reach_the_model_as_its_own(fake_wren):
|
||||
node = _speaking(("patio_light", DType.BOOL), aliases={"Page Too": "patio"})
|
||||
fake_wren.answer = ([], 0.99)
|
||||
|
||||
out = node.execute({"house.command": "Turn on the page too light in five minutes."})
|
||||
|
||||
assert fake_wren.commands == ["turn on the patio light in 5 minutes"]
|
||||
# The assistant reads the house's words too, as they were said otherwise.
|
||||
assert fake_wren.commands == ["turn on the garden light in 5 minutes"]
|
||||
# The assistant reads the own words too, as they were said otherwise.
|
||||
passed = "Turn on the garden light in five minutes."
|
||||
assert out == {
|
||||
"house.unhandled": "Turn on the patio light in five minutes.",
|
||||
"house.actions": [],
|
||||
"house.unhandled": passed,
|
||||
"house.reply": CANT,
|
||||
}
|
||||
|
||||
|
||||
def test_a_command_for_all_of_a_kind_reaches_every_wired_one(fake_wren):
|
||||
lights = [(f"{area}_light", DType.BOOL) for area in ("kitchen", "office")]
|
||||
node = _speaking(*lights, ("office_shutter", DType.STR))
|
||||
|
||||
out = node.execute({"house.command": "Turn off all lights."})
|
||||
assert out == {
|
||||
"house.kitchen_light": False,
|
||||
"house.office_light": False,
|
||||
"house.reply": "All lights off.",
|
||||
}
|
||||
out = node.execute({"house.command": "turn everything off in the office"})
|
||||
assert out == {
|
||||
"house.office_light": False,
|
||||
"house.reply": "Lights off in the office.",
|
||||
}
|
||||
assert fake_wren.commands == [] # the node's, not the model's
|
||||
|
||||
# A question or an exception is the model's again.
|
||||
fake_wren.answer = ([], 0.99)
|
||||
node.execute({"house.command": "are all lights off"})
|
||||
node.execute({"house.command": "turn off all lights but the kitchen"})
|
||||
assert len(fake_wren.commands) == 2
|
||||
|
||||
|
||||
def test_a_question_for_a_temperature_is_answered_from_its_reading(fake_wren):
|
||||
node = _speaking(
|
||||
inputs=[
|
||||
MessageSpec(name="outside", port="outdoor", dtype=DType.FLOAT),
|
||||
MessageSpec(
|
||||
name="kitchen_temp", port="kitchen_temperature", dtype=DType.FLOAT
|
||||
),
|
||||
]
|
||||
)
|
||||
assert node.readings == {"kitchen": "kitchen_temperature"}
|
||||
readings = {"house.outside": 7.6, "house.kitchen_temp": 21.2}
|
||||
|
||||
for area, said in [("garden", "outside"), ("kitchen", "in the kitchen")]:
|
||||
fake_wren.answer = ([FakeCall("sensors.temperature", {"area": area})], 0.99)
|
||||
command = f"what is the temperature in the {area}"
|
||||
out = node.execute({"house.command": command, **readings})
|
||||
degrees = 8 if area == "garden" else 21
|
||||
assert out == {"house.reply": f"It's {degrees} degrees {said}."}
|
||||
|
||||
# No reading for the bedroom, or a model unsure of it: the assistant's.
|
||||
for area, confidence in [("bedroom", 0.99), ("kitchen", 0.5)]:
|
||||
fake_wren.answer = (
|
||||
[FakeCall("sensors.temperature", {"area": area})],
|
||||
confidence,
|
||||
)
|
||||
command = f"how warm is it in the {area}"
|
||||
out = node.execute({"house.command": command, **readings})
|
||||
assert out == {"house.unhandled": command, "house.reply": CANT}
|
||||
|
||||
|
||||
def test_a_brightness_call_sets_the_house_brightness(fake_wren):
|
||||
node = _speaking(("brightness", DType.INT))
|
||||
# Whatever area the call names: the brightness is the house's.
|
||||
for call, level in [
|
||||
(FakeCall("lights.set", {"id": "kitchen:light", "level": 30}), 30),
|
||||
(FakeCall("lights.dim", {"id": "kitchen:light"}), 40),
|
||||
(FakeCall("lights.brighten", {"id": "bedroom:light"}), 100),
|
||||
]:
|
||||
fake_wren.answer = ([call], 0.95)
|
||||
out = node.execute({"house.command": "dim the lights in the kitchen"})
|
||||
assert out == {
|
||||
"house.brightness": level,
|
||||
"house.reply": f"Brightness at {level} percent.",
|
||||
}
|
||||
|
||||
# Nothing wired for it: the assistant's.
|
||||
node = _speaking(("kitchen_light", DType.BOOL))
|
||||
fake_wren.answer = ([FakeCall("lights.dim", {"id": "kitchen:light"})], 0.95)
|
||||
out = node.execute({"house.command": "dim the lights"})
|
||||
assert out == {"house.unhandled": "dim the lights", "house.reply": CANT}
|
||||
# An assistant reading `unhandled` answers instead.
|
||||
unhandled = MessageSpec(name="house.unhandled", port="unhandled", dtype=DType.STR)
|
||||
Pipeline([node, Node(lambda params, unhandled: None, requires=[unhandled])])
|
||||
out = node.execute({"house.command": "Turn on the page too light in five minutes."})
|
||||
assert out == {"house.actions": [], "house.unhandled": passed}
|
||||
|
||||
Reference in New Issue
Block a user