Rasa 3.1 机器学习二构建槽值对话

1、控制请求(domain.yml)

version: "3.1"
intents:
  - hello
entities:
  - medicinname
  - wherehome

actions:
  - action_medicin_form_submit

slots:
  medicinname:
    type: text
    influence_conversation: false
    mappings:
      - type: from_entity
        entity: medicinname
        intent: haveb
        conditions:
          - active_loop: medicin_form
            requested_slot: medicinname
          - active_loop: null
  wherehome:
    type: text
    influence_conversation: false
    mappings:
      - type: from_entity
        entity: wherehome
        intent: howdo
        conditions:
          - active_loop: medicin_form
            requested_slot: wherehome
          - active_loop: null

session_config:
  session_expiration_time: 60
  carry_over_slots_to_new_session: true

2、规则制定(rules.yml)

version: "3.1"
rules:
  - rule: haveb
    steps:
      - intent: haveb
      - action: action_medicin_form_submit

3、故事编写(stories.yml)

version: "3.1"
stories:
  - story: haveb
    steps:
      - intent: haveb
      - action: action_medicin_form_submit

4、训练对话数据模板(nlu.yml)

version: "3.1"
nlu:
  - intent: hello
    examples: |
      - 早
      - 你好
      - hi
      - hello
  - intent: haveb
    examples: |
      - [人参](medicinname)主治什么
      - [人参](medicinname)
      - [人参](medicinname)有什么功效
      - [枸杞](medicinname)主治什么
      - [枸杞](medicinname)
      - [枸杞](medicinname)有什么功效
  - intent: howdo
    examples: |
      - [湖北](wherehome)
      - [湖南](wherehome)
      - [河南](wherehome)
      - [河北](wherehome)

5、action响应(action.py)

5-1、form表单校验

class ValidateRestaurantForm(FormValidationAction):
    def name(self) -> Text:
        return "validate_medicin_form"

    async def extract_medicinname(
            self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
    ) -> Dict[Text, Any]:
        text_of_last_user_message = tracker.latest_message.get("text")
        return {"medicinname":tracker.get_slot('medicinname')}


    def validate_medicinname(
            self,
            value: Text,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> Dict[Text, Any]:
        return {"medicinname":value}


    async def extract_wherehome(
            self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
    ) -> Dict[Text, Any]:
        text_of_last_user_message = tracker.latest_message.get("text")
        return {"wherehome":tracker.get_slot('wherehome')}


    def validate_wherehome(
            self,
            value: Text,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> Dict[Text, Any]:
        return {"wherehome":value}

    async def required_slots(
            self,
            slots_mapped_in_domain: List[Text],
            dispatcher: "CollectingDispatcher",
            tracker: "Tracker",
            domain: "DomainDict",
    ) -> Optional[List[Text]]:
        additional_slots = ["wherehome"]
        if tracker.slots.get("wherehome") is True:
            additional_slots.append("shade_or_sun")

        return additional_slots + slots_mapped_in_domain

5-2、Action控制

class AskForSlotAction(Action):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}

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

    @staticmethod
    def fetch_slots(tracker: Tracker) -> List[EventType]:
        slots = []
        value = tracker.get_slot("medicinname")
        for key in ("medicinname"):
            print(key)
            value = tracker.get_slot(key)
            if value is not None:
                slots.append(SlotSet(key=key, value=value))
        return slots

    def run(self, dispatcher, tracker, domain):
        medicinname =tracker.get_slot("medicinname")
        wherehome =tracker.get_slot("wherehome")
        print('medicinname=%s,wherehome=%s'%(medicinname,wherehome))

        events = [SessionStarted()]
        events.append(ActionExecuted("action_listen"))
        if medicinname is not None:
            if wherehome is  None:
                text= medicinname
                requesturl="http://xxxxx/medicine/answer?name="+text
                data =requests.get(requesturl,headers=self.headers)
                content = data.content.decode()
                result = json.loads(content).get('result')
                dispatcher.utter_message(text=result)
                return []

        text= wherehome+medicinname
        requesturl="http://xxxxx/medicine/answer?name="+text
        data =requests.get(requesturl,headers=self.headers)
        content = data.content.decode()
        result = json.loads(content).get('result')
        dispatcher.utter_message(text=result)
        return []

你可能感兴趣的:(机器学习,深度学习)