Automatically fill in slots based on LLM context (#118619)
* Automatically fill in slots from LLM context * Add tests * Apply suggestions from code review Co-authored-by: Allen Porter <allen@thebends.org> --------- Co-authored-by: Allen Porter <allen@thebends.org>
This commit is contained in:
parent
8a68529dd1
commit
bdcfd93129
2 changed files with 97 additions and 6 deletions
|
@ -181,14 +181,48 @@ class IntentTool(Tool):
|
|||
self.description = (
|
||||
intent_handler.description or f"Execute Home Assistant {self.name} intent"
|
||||
)
|
||||
if slot_schema := intent_handler.slot_schema:
|
||||
self.parameters = vol.Schema(slot_schema)
|
||||
self.extra_slots = None
|
||||
if not (slot_schema := intent_handler.slot_schema):
|
||||
return
|
||||
|
||||
slot_schema = {**slot_schema}
|
||||
extra_slots = set()
|
||||
|
||||
for field in ("preferred_area_id", "preferred_floor_id"):
|
||||
if field in slot_schema:
|
||||
extra_slots.add(field)
|
||||
del slot_schema[field]
|
||||
|
||||
self.parameters = vol.Schema(slot_schema)
|
||||
if extra_slots:
|
||||
self.extra_slots = extra_slots
|
||||
|
||||
async def async_call(
|
||||
self, hass: HomeAssistant, tool_input: ToolInput, llm_context: LLMContext
|
||||
) -> JsonObjectType:
|
||||
"""Handle the intent."""
|
||||
slots = {key: {"value": val} for key, val in tool_input.tool_args.items()}
|
||||
|
||||
if self.extra_slots and llm_context.device_id:
|
||||
device_reg = dr.async_get(hass)
|
||||
device = device_reg.async_get(llm_context.device_id)
|
||||
|
||||
area: ar.AreaEntry | None = None
|
||||
floor: fr.FloorEntry | None = None
|
||||
if device:
|
||||
area_reg = ar.async_get(hass)
|
||||
if device.area_id and (area := area_reg.async_get_area(device.area_id)):
|
||||
if area.floor_id:
|
||||
floor_reg = fr.async_get(hass)
|
||||
floor = floor_reg.async_get_floor(area.floor_id)
|
||||
|
||||
for slot_name, slot_value in (
|
||||
("preferred_area_id", area.id if area else None),
|
||||
("preferred_floor_id", floor.floor_id if floor else None),
|
||||
):
|
||||
if slot_value and slot_name in self.extra_slots:
|
||||
slots[slot_name] = {"value": slot_value}
|
||||
|
||||
intent_response = await intent.async_handle(
|
||||
hass=hass,
|
||||
platform=llm_context.platform,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue