Rasa-获取用于自定义操作的广告位值

时间:2019-12-05 16:22:00

标签: python rasa

如何在FormAction.slot_mappings中进行对话期间提取广告位/实体的值?

我在tracker.slots['template_name']中设置了值,但是无法从该函数访问该值。我可以访问tracker中的required_slots,但不能访问slot_mappings

我要对template_name = "example"使用槽值,而不是硬编码template_name

这看起来应该很简单,但是我做不到!

from looking at the documentation我以为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

2 个答案:

答案 0 :(得分:2)

要在自定义操作中获取槽值,您可以执行以下操作:

 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 []

答案 1 :(得分:0)

您可以像这样访问插槽值。您是否想对提取的值做更多的事情?

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