rhasspy-hermes-app icon indicating copy to clipboard operation
rhasspy-hermes-app copied to clipboard

Add easier way to access slot values

Open koenvervloesem opened this issue 4 years ago • 0 comments

See the forum. Quoting @DanielWe2:

One idea would be: add a new parameter “supported_slots” to the decorator.

Could look like this:

@app.on_intent("StartTimer", supported_slots=["minutes", "seconds"]) def handle_start_timer(intent, minutes, seconds): or def handle_start_timer(intent, slot_minutes, slot_seconds):

If the Intent has slot values for minutes or seconds handle_start_timer will get called with that otherwise it will provide “None”. One variant of this would be to also put the expected type into the decorator and let the decorator do the validation.

Another option: Just provide a helper function like mine in your module to get the slot values out of an intent. As you can see in my source (maybe I am doing it wrong) it is not that simple.

Example code from https://github.com/DanielWe2/rhasspy-hermes-app/blob/examples/timer_app.py with a modification from https://community.rhasspy.org/t/mqtt-error-coroutine-get-test-was-never-awaited/1436/21?u=koan):

def extract_slot_value(intent: NluIntent, slot_name: str, default=None):
    """extracts the value of a slot"""

    slot = next(filter(lambda slot: slot.slot_name == slot_name, intent.slots), None)
    if slot:
        return slot.value.get("value", default)
    return default

Usage in the intent handler function:

    minutes = extract_slot_value(intent, "minutes", 0)
    seconds = extract_slot_value(intent, "seconds", 0)

koenvervloesem avatar Jun 16 '20 07:06 koenvervloesem