-
Notifications
You must be signed in to change notification settings - Fork 39
/
actions.py
36 lines (28 loc) · 1.28 KB
/
actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import json
from pathlib import Path
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.knowledge_base.storage import InMemoryKnowledgeBase
from rasa_sdk.knowledge_base.actions import ActionQueryKnowledgeBase
class ActionCheckExistence(Action):
knowledge = Path("data/pokenames.txt").read_text().split("\n")
def name(self) -> Text:
return "action_check_existence"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
for blob in tracker.latest_message['entities']:
print(tracker.latest_message)
if blob['entity'] == 'pokemon_name':
name = blob['value']
if name in self.knowledge:
dispatcher.utter_message(text=f"Yes, {name} is a pokemon.")
else:
dispatcher.utter_message(
text=f"I do not recognize {name}, are you sure it is correctly spelled?")
return []
class MyKnowledgeBaseAction(ActionQueryKnowledgeBase):
def __init__(self):
knowledge_base = InMemoryKnowledgeBase("data/pokemondb.json")
super().__init__(knowledge_base)