Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add easier way to access slot values #12

Open
koenvervloesem opened this issue Jun 16, 2020 · 0 comments
Open

Add easier way to access slot values #12

koenvervloesem opened this issue Jun 16, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@koenvervloesem
Copy link
Member

koenvervloesem commented Jun 16, 2020

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant