0

如何在对话期间提取插槽/实体的值FormAction.slot_mappings

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

template_name = "example"我不想使用硬编码,而是将插槽值用于template_name.

这看起来应该很容易解决,但我做不到!

通过查看 我认为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
4

2 回答 2

3

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

 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 []
于 2021-01-13T17:27:55.417 回答
0

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

    return {
        "new_entity": [
            self.from_entity(entity="template_name")
        ]
    }
于 2020-02-11T17:52:34.900 回答