简体   繁体   English

Rasa - 获取用于自定义操作的槽值

[英]Rasa - getting slot value for use in custom action

How can I extract the value of a slot/entity during a conversation in FormAction.slot_mappings ?如何在FormAction.slot_mappings中的对话期间提取槽/实体的值?

I have the value set in tracker.slots['template_name'] , but don't have access to that value from this function.我在tracker.slots['template_name']设置了值,但无法从此函数访问该值。 I am able to access tracker in required_slots but not slot_mappings我可以访问required_slots tracker ,但不能访问slot_mappings

Rather than hard coding template_name = "example" I'd like to use the slot value for template_name .而不是硬编码template_name = "example"我想使用template_name的槽值。

This seems like it should be simple to work out, but I can't!这看起来应该很容易解决,但我做不到!

from looking at the documentation I thought that self.from_entity(entity='reqd_form') would give me the information, but it returns a blank.从查看文档我认为self.from_entity(entity='reqd_form')会给我信息,但它返回一个空白。

from typing import Dict, Text, Any, List, Union, Optional
from rasa_sdk import Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormAction

class ExampleForm(FormAction):
    """Example of a custom form action"""

    def name(self) -> Text:
        """Unique identifier of the form"""
        return "example_form"

    @staticmethod
    def required_slots(tracker: Tracker) -> List[Text]:
        """A list of required slots that the form has to fill"""
        #get a list of the slots required for this particular user
        slots_to_return = util.get_slots(tracker.slots, tracker.sender_id)
        return slots_to_return

    def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
        """A dictionary to map required slots to
            - an extracted
            - intent: value pairs
            - a whole message
            or a list of them, where a first match will be picked"""
        template_name = "example"
        test_return = get_slot_mappings(template_name, self)
        return test_return 

    def submit(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict]:
        """Define what the form has to do
            after all required slots are filled"""
        return []

    def get_slot_mappings(form_name, form):
        #get a list of the slots required for the form
        slot_list = util.get_form_slots(form_name)
        slot_mappings = {}
        for entity_item in slot_list:
            if (entity_item != "ice"):
                temp_list = []
                temp_list.append(form.from_entity(entity=entity_item))
                temp_list.append(form.from_intent(intent="affirm", value=True))
                temp_list.append(form.from_intent(intent="deny", value=False))
                slot_mappings[entity_item] = temp_list
                return slot_mappings

To get the slot values in custom action here is what you can do:要在自定义操作中获取槽值,您可以执行以下操作:

 from rasa_sdk import Action, Tracker

 class ActionHelloWorld(Action):

 def name(self) -> Text:
     return "action_hello_world"

 def run(self, dispatcher: CollectingDispatcher,
         tracker: Tracker,
         domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

     slot_value = tracker.get_slot('slot_name')

     dispatcher.utter_message(text="Hello World!")

     return []

You can access the slot value like this.您可以像这样访问插槽值。 Did you want to do something more with the extracted value?您想对提取的值做更多的事情吗?

    return {
        "new_entity": [
            self.from_entity(entity="template_name")
        ]
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM