wren leaves unnamed areas to the assistant; assistant targets take a closed set of values
wren drops a call on an area the command does not name (other than the
user's or, with none named, the living room): "switch on the appliances"
turned on the bathroom light at 0.97. The assistant's `values` setting gives
a str target its only values, explains them in the tool and confirms in the
word set for each ("Canopy open.").
This commit is contained in:
@@ -74,9 +74,11 @@ few words, such as "Sorry, I can't do that."
|
||||
_END = re.compile(r"(?<=[.!?])\s+")
|
||||
|
||||
|
||||
def phrase(port: str, value: Any) -> str:
|
||||
"""A value set, as the reply says it: "kitchen light off"."""
|
||||
def phrase(port: str, value: Any, word: str | None = None) -> str:
|
||||
"""A value set, as the reply says it: "kitchen light off", "canopy open"."""
|
||||
label = port.replace("_", " ")
|
||||
if word:
|
||||
return f"{label} {word}"
|
||||
if isinstance(value, bool):
|
||||
return f"{label} {'on' if value else 'off'}"
|
||||
return (
|
||||
@@ -120,6 +122,13 @@ class AssistantNode(Node):
|
||||
"loaded for 30 minutes on Ollama; drop what a provider refuses."
|
||||
),
|
||||
)
|
||||
values: dict[str, dict[str, str]] = Field(
|
||||
default_factory=dict,
|
||||
description=(
|
||||
"For a str target, by port: the only values it takes, each with "
|
||||
'what the reply says for it, as {"UP": "open", "DOWN": "closed"}.'
|
||||
),
|
||||
)
|
||||
|
||||
__slots__ = ("cfg", "_listening", "_lock", "_loop")
|
||||
|
||||
@@ -183,22 +192,31 @@ class AssistantNode(Node):
|
||||
if spec.name and spec.port not in (REPLY, ACTIONS)
|
||||
]
|
||||
|
||||
def _tools(self) -> list[dict[str, Any]]:
|
||||
return [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": spec.port,
|
||||
"description": f"Set the {spec.port.replace('_', ' ')}.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"value": _SCHEMAS[spec.dtype]},
|
||||
"required": ["value"],
|
||||
},
|
||||
def _tool(self, spec: MessageSpec) -> dict[str, Any]:
|
||||
label = spec.port.replace("_", " ")
|
||||
schema: dict[str, Any] = _SCHEMAS[spec.dtype]
|
||||
description = f"Set the {label}."
|
||||
words = self.cfg.values.get(spec.port)
|
||||
if words:
|
||||
# A code such as UP means nothing to the model until it is told.
|
||||
schema = {**schema, "enum": list(words)}
|
||||
meant = ", ".join(f'"{v}" means {label} {w}' for v, w in words.items())
|
||||
description = f"Set the {label}: {meant}."
|
||||
return {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": spec.port,
|
||||
"description": description,
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"value": schema},
|
||||
"required": ["value"],
|
||||
},
|
||||
}
|
||||
for spec in self._targets()
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
def _tools(self) -> list[dict[str, Any]]:
|
||||
return [self._tool(spec) for spec in self._targets()]
|
||||
|
||||
def _system(self, context: dict[str, Any]) -> str:
|
||||
"""The instructions, then what the inputs read and the targets hold."""
|
||||
@@ -328,7 +346,11 @@ class AssistantNode(Node):
|
||||
def _confirm(self, actions: list[dict[str, Any]]) -> str:
|
||||
"""The values set, as one sentence: "Kitchen light off, setpoint to 22."."""
|
||||
ports = {spec.name: spec.port for spec in self._targets()}
|
||||
text = ", ".join(phrase(ports[a["message"]], a["value"]) for a in actions)
|
||||
said = []
|
||||
for action in actions:
|
||||
port, value = ports[action["message"]], action["value"]
|
||||
said.append(phrase(port, value, self.cfg.values.get(port, {}).get(value)))
|
||||
text = ", ".join(said)
|
||||
return text[:1].upper() + text[1:] + "." if text else ""
|
||||
|
||||
def _caller(self, actions: list[dict[str, Any]]) -> agent.ToolCaller:
|
||||
@@ -346,6 +368,11 @@ class AssistantNode(Node):
|
||||
spec.check(value)
|
||||
except TypeError as exc:
|
||||
return json.dumps({"error": str(exc)})
|
||||
words = self.cfg.values.get(name)
|
||||
if words and value not in words:
|
||||
# A device takes its own codes only: a motor would run "OPEN"
|
||||
# as a direction it has never heard of.
|
||||
return json.dumps({"error": f"{name} is one of {', '.join(words)}."})
|
||||
await asyncio.to_thread(self.emit, {name: value})
|
||||
actions.append(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user