-
Notifications
You must be signed in to change notification settings - Fork 136
/
actions.py
268 lines (214 loc) · 9.75 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from rasa_core_sdk import Tracker
from rasa_core_sdk.executor import CollectingDispatcher
from typing import Dict, Text, Any, List
import requests
from rasa_core_sdk import Action
from rasa_core_sdk.events import SlotSet, FollowupAction
from rasa_core_sdk.forms import FormAction
# We use the medicare.gov database to find information about 3 different
# healthcare facility types, given a city name, zip code or facility ID
# the identifiers for each facility type is given by the medicare database
# rbry-mqwu is for hospitals
# b27b-2uc7 is for nursing homes
# 9wzi-peqs is for home health agencies
ENDPOINTS = {
"base": "https://data.medicare.gov/resource/{}.json",
"rbry-mqwu": {
"city_query": "?city={}",
"zip_code_query": "?zip_code={}",
"id_query": "?provider_id={}"
},
"b27b-2uc7": {
"city_query": "?provider_city={}",
"zip_code_query": "?provider_zip_code={}",
"id_query": "?federal_provider_number={}"
},
"9wzi-peqs": {
"city_query": "?city={}",
"zip_code_query": "?zip={}",
"id_query": "?provider_number={}"
}
}
FACILITY_TYPES = {
"hospital":
{
"name": "hospital",
"resource": "rbry-mqwu"
},
"nursing_home":
{
"name": "nursing home",
"resource": "b27b-2uc7"
},
"home_health":
{
"name": "home health agency",
"resource": "9wzi-peqs"
}
}
def _create_path(base: Text, resource: Text,
query: Text, values: Text) -> Text:
"""Creates a path to find provider using the endpoints."""
if isinstance(values, list):
return (base + query).format(
resource, ', '.join('"{0}"'.format(w) for w in values))
else:
return (base + query).format(resource, values)
def _find_facilities(location: Text, resource: Text) -> List[Dict]:
"""Returns json of facilities matching the search criteria."""
if str.isdigit(location):
full_path = _create_path(ENDPOINTS["base"], resource,
ENDPOINTS[resource]["zip_code_query"],
location)
else:
full_path = _create_path(ENDPOINTS["base"], resource,
ENDPOINTS[resource]["city_query"],
location.upper())
results = requests.get(full_path).json()
return results
def _resolve_name(facility_types, resource) ->Text:
for key, value in facility_types.items():
if value.get("resource") == resource:
return value.get("name")
return ""
class FindFacilityTypes(Action):
"""This action class allows to display buttons for each facility type
for the user to chose from to fill the facility_type entity slot."""
def name(self) -> Text:
"""Unique identifier of the action"""
return "find_facility_types"
def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List:
buttons = []
for t in FACILITY_TYPES:
facility_type = FACILITY_TYPES[t]
payload = "/inform{\"facility_type\": \"" + facility_type.get(
"resource") + "\"}"
buttons.append(
{"title": "{}".format(facility_type.get("name").title()),
"payload": payload})
# TODO: update rasa core version for configurable `button_type`
dispatcher.utter_button_template("utter_greet", buttons, tracker)
return []
class FindHealthCareAddress(Action):
"""This action class retrieves the address of the user's
healthcare facility choice to display it to the user."""
def name(self) -> Text:
"""Unique identifier of the action"""
return "find_healthcare_address"
def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict]:
facility_type = tracker.get_slot("facility_type")
healthcare_id = tracker.get_slot("facility_id")
full_path = _create_path(ENDPOINTS["base"], facility_type,
ENDPOINTS[facility_type]["id_query"],
healthcare_id)
results = requests.get(full_path).json()
if results:
selected = results[0]
if facility_type == FACILITY_TYPES["hospital"]["resource"]:
address = "{}, {}, {} {}".format(selected["address"].title(),
selected["city"].title(),
selected["state"].upper(),
selected["zip_code"].title())
elif facility_type == FACILITY_TYPES["nursing_home"]["resource"]:
address = "{}, {}, {} {}".format(selected["provider_address"].title(),
selected["provider_city"].title(),
selected["provider_state"].upper(),
selected["provider_zip_code"].title())
else:
address = "{}, {}, {} {}".format(selected["address"].title(),
selected["city"].title(),
selected["state"].upper(),
selected["zip"].title())
return [SlotSet("facility_address", address)]
else:
print("No address found. Most likely this action was executed "
"before the user choose a healthcare facility from the "
"provided list. "
"If this is a common problem in your dialogue flow,"
"using a form instead for this action might be appropriate.")
return [SlotSet("facility_address", "not found")]
class FacilityForm(FormAction):
"""Custom form action to fill all slots required to find specific type
of healthcare facilities in a certain city or zip code."""
def name(self) -> Text:
"""Unique identifier of the form"""
return "facility_form"
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
"""A list of required slots that the form has to fill"""
return ["facility_type", "location"]
def slot_mappings(self) -> Dict[Text, Any]:
return {"facility_type": self.from_entity(entity="facility_type",
intent=["inform",
"search_provider"]),
"location": self.from_entity(entity="location",
intent=["inform",
"search_provider"])}
def submit(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]
) -> List[Dict]:
"""Once required slots are filled, print buttons for found facilities"""
location = tracker.get_slot('location')
facility_type = tracker.get_slot('facility_type')
results = _find_facilities(location, facility_type)
button_name = _resolve_name(FACILITY_TYPES, facility_type)
if len(results) == 0:
dispatcher.utter_message(
"Sorry, we could not find a {} in {}.".format(button_name,
location.title()))
return []
buttons = []
# limit number of results to 3 for clear presentation purposes
for r in results[:3]:
if facility_type == FACILITY_TYPES["hospital"]["resource"]:
facility_id = r.get("provider_id")
name = r["hospital_name"]
elif facility_type == FACILITY_TYPES["nursing_home"]["resource"]:
facility_id = r["federal_provider_number"]
name = r["provider_name"]
else:
facility_id = r["provider_number"]
name = r["provider_name"]
payload = "/inform{\"facility_id\":\"" + facility_id + "\"}"
buttons.append(
{"title": "{}".format(name.title()), "payload": payload})
if len(buttons) == 1:
message = "Here is a {} near you:".format(button_name)
else:
if button_name == "home health agency":
button_name = "home health agencie"
message = "Here are {} {}s near you:".format(len(buttons),
button_name)
# TODO: update rasa core version for configurable `button_type`
dispatcher.utter_button_message(message, buttons)
return []
class ActionChitchat(Action):
"""Returns the chitchat utterance dependent on the intent"""
def name(self) -> Text:
"""Unique identifier of the action"""
return "action_chitchat"
def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List:
intent = tracker.latest_message['intent'].get('name')
# retrieve the correct chitchat utterance dependent on the intent
if intent in ['ask_builder', 'ask_weather', 'ask_howdoing',
'ask_howold', 'ask_languagesbot', 'ask_restaurant',
'ask_time', 'ask_wherefrom', 'ask_whoami',
'handleinsult', 'telljoke', 'ask_whatismyname']:
dispatcher.utter_template('utter_' + intent, tracker)
return []