-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
utils.py
326 lines (251 loc) 路 9.84 KB
/
utils.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import json
import logging
import os
from decimal import Decimal
from pathlib import Path
from typing import (
Any,
Dict,
Optional,
Set,
Text,
Tuple,
Union,
)
import numpy as np
import rasa.shared.utils.io
from rasa.constants import DEFAULT_SANIC_WORKERS, ENV_SANIC_WORKERS
from rasa.shared.constants import DEFAULT_ENDPOINTS_PATH
from rasa.core.lock_store import LockStore, RedisLockStore, InMemoryLockStore
from rasa.utils.endpoints import EndpointConfig, read_endpoint_config
from sanic import Sanic
from sanic.views import CompositionView
import rasa.cli.utils as cli_utils
logger = logging.getLogger(__name__)
def configure_file_logging(
logger_obj: logging.Logger, log_file: Optional[Text]
) -> None:
"""Configure logging to a file.
Args:
logger_obj: Logger object to configure.
log_file: Path of log file to write to.
"""
if not log_file:
return
formatter = logging.Formatter("%(asctime)s [%(levelname)-5.5s] %(message)s")
file_handler = logging.FileHandler(
log_file, encoding=rasa.shared.utils.io.DEFAULT_ENCODING
)
file_handler.setLevel(logger_obj.level)
file_handler.setFormatter(formatter)
logger_obj.addHandler(file_handler)
def one_hot(hot_idx: int, length: int, dtype: Optional[Text] = None) -> np.ndarray:
"""Create a one-hot array.
Args:
hot_idx: Index of the hot element.
length: Length of the array.
dtype: ``numpy.dtype`` of the array.
Returns:
One-hot array.
"""
if hot_idx >= length:
raise ValueError(
"Can't create one hot. Index '{}' is out "
"of range (length '{}')".format(hot_idx, length)
)
r = np.zeros(length, dtype)
r[hot_idx] = 1
return r
def dump_obj_as_yaml_to_file(
filename: Union[Text, Path], obj: Any, should_preserve_key_order: bool = False
) -> None:
"""Writes `obj` to the filename in YAML repr.
Args:
filename: Target filename.
obj: Object to dump.
should_preserve_key_order: Whether to preserve key order in `obj`.
"""
rasa.shared.utils.io.write_yaml(
obj, filename, should_preserve_key_order=should_preserve_key_order
)
def list_routes(app: Sanic) -> Dict[Text, Text]:
"""List all the routes of a sanic application. Mainly used for debugging."""
from urllib.parse import unquote
output = {}
def find_route(suffix: Text, path: Text) -> Optional[Text]:
for name, (uri, _) in app.router.routes_names.items():
if name.split(".")[-1] == suffix and uri == path:
return name
return None
for endpoint, route in app.router.routes_all.items():
if endpoint[:-1] in app.router.routes_all and endpoint[-1] == "/":
continue
options = {}
for arg in route.parameters:
options[arg] = f"[{arg}]"
if not isinstance(route.handler, CompositionView):
handlers = [(list(route.methods)[0], route.name)]
else:
handlers = [
(method, find_route(v.__name__, endpoint) or v.__name__)
for method, v in route.handler.handlers.items()
]
for method, name in handlers:
line = unquote(f"{endpoint:50s} {method:30s} {name}")
output[name] = line
url_table = "\n".join(output[url] for url in sorted(output))
logger.debug(f"Available web server routes: \n{url_table}")
return output
def extract_args(
kwargs: Dict[Text, Any], keys_to_extract: Set[Text]
) -> Tuple[Dict[Text, Any], Dict[Text, Any]]:
"""Go through the kwargs and filter out the specified keys.
Return both, the filtered kwargs as well as the remaining kwargs."""
remaining = {}
extracted = {}
for k, v in kwargs.items():
if k in keys_to_extract:
extracted[k] = v
else:
remaining[k] = v
return extracted, remaining
def is_limit_reached(num_messages: int, limit: Optional[int]) -> bool:
"""Determine whether the number of messages has reached a limit.
Args:
num_messages: The number of messages to check.
limit: Limit on the number of messages.
Returns:
`True` if the limit has been reached, otherwise `False`.
"""
return limit is not None and num_messages >= limit
def file_as_bytes(path: Text) -> bytes:
"""Read in a file as a byte array."""
with open(path, "rb") as f:
return f.read()
class AvailableEndpoints:
"""Collection of configured endpoints."""
@classmethod
def read_endpoints(cls, endpoint_file: Text) -> "AvailableEndpoints":
nlg = read_endpoint_config(endpoint_file, endpoint_type="nlg")
nlu = read_endpoint_config(endpoint_file, endpoint_type="nlu")
action = read_endpoint_config(endpoint_file, endpoint_type="action_endpoint")
model = read_endpoint_config(endpoint_file, endpoint_type="models")
tracker_store = read_endpoint_config(
endpoint_file, endpoint_type="tracker_store"
)
lock_store = read_endpoint_config(endpoint_file, endpoint_type="lock_store")
event_broker = read_endpoint_config(endpoint_file, endpoint_type="event_broker")
return cls(nlg, nlu, action, model, tracker_store, lock_store, event_broker)
def __init__(
self,
nlg: Optional[EndpointConfig] = None,
nlu: Optional[EndpointConfig] = None,
action: Optional[EndpointConfig] = None,
model: Optional[EndpointConfig] = None,
tracker_store: Optional[EndpointConfig] = None,
lock_store: Optional[EndpointConfig] = None,
event_broker: Optional[EndpointConfig] = None,
) -> None:
self.model = model
self.action = action
self.nlu = nlu
self.nlg = nlg
self.tracker_store = tracker_store
self.lock_store = lock_store
self.event_broker = event_broker
def read_endpoints_from_path(
endpoints_path: Union[Path, Text, None] = None
) -> AvailableEndpoints:
"""Get `AvailableEndpoints` object from specified path.
Args:
endpoints_path: Path of the endpoints file to be read. If `None` the
default path for that file is used (`endpoints.yml`).
Returns:
`AvailableEndpoints` object read from endpoints file.
"""
endpoints_config_path = cli_utils.get_validated_path(
endpoints_path, "endpoints", DEFAULT_ENDPOINTS_PATH, True
)
return AvailableEndpoints.read_endpoints(endpoints_config_path)
def replace_floats_with_decimals(obj: Any, round_digits: int = 9) -> Any:
"""Convert all instances in `obj` of `float` to `Decimal`.
Args:
obj: Input object.
round_digits: Rounding precision of `Decimal` values.
Returns:
Input `obj` with all `float` types replaced by `Decimal`s rounded to
`round_digits` decimal places.
"""
def _float_to_rounded_decimal(s: Text) -> Decimal:
return Decimal(s).quantize(Decimal(10) ** -round_digits)
return json.loads(json.dumps(obj), parse_float=_float_to_rounded_decimal)
class DecimalEncoder(json.JSONEncoder):
"""`json.JSONEncoder` that dumps `Decimal`s as `float`s."""
def default(self, obj: Any) -> Any:
"""Get serializable object for `o`.
Args:
obj: Object to serialize.
Returns:
`obj` converted to `float` if `o` is a `Decimals`, else the base class
`default()` method.
"""
if isinstance(obj, Decimal):
return float(obj)
return super().default(obj)
def replace_decimals_with_floats(obj: Any) -> Any:
"""Convert all instances in `obj` of `Decimal` to `float`.
Args:
obj: A `List` or `Dict` object.
Returns:
Input `obj` with all `Decimal` types replaced by `float`s.
"""
return json.loads(json.dumps(obj, cls=DecimalEncoder))
def _lock_store_is_multi_worker_compatible(
lock_store: Union[EndpointConfig, LockStore, None]
) -> bool:
if isinstance(lock_store, InMemoryLockStore):
return False
if isinstance(lock_store, RedisLockStore):
return True
# `lock_store` is `None` or `EndpointConfig`
return (
lock_store is not None
and isinstance(lock_store, EndpointConfig)
and lock_store.type != "in_memory"
)
def number_of_sanic_workers(lock_store: Union[EndpointConfig, LockStore, None]) -> int:
"""Get the number of Sanic workers to use in `app.run()`.
If the environment variable constants.ENV_SANIC_WORKERS is set and is not equal to
1, that value will only be permitted if the used lock store is not the
`InMemoryLockStore`.
"""
def _log_and_get_default_number_of_workers() -> int:
logger.debug(
f"Using the default number of Sanic workers ({DEFAULT_SANIC_WORKERS})."
)
return DEFAULT_SANIC_WORKERS
try:
env_value = int(os.environ.get(ENV_SANIC_WORKERS, DEFAULT_SANIC_WORKERS))
except ValueError:
logger.error(
f"Cannot convert environment variable `{ENV_SANIC_WORKERS}` "
f"to int ('{os.environ[ENV_SANIC_WORKERS]}')."
)
return _log_and_get_default_number_of_workers()
if env_value == DEFAULT_SANIC_WORKERS:
return _log_and_get_default_number_of_workers()
if env_value < 1:
logger.debug(
f"Cannot set number of Sanic workers to the desired value "
f"({env_value}). The number of workers must be at least 1."
)
return _log_and_get_default_number_of_workers()
if _lock_store_is_multi_worker_compatible(lock_store):
logger.debug(f"Using {env_value} Sanic workers.")
return env_value
logger.debug(
f"Unable to assign desired number of Sanic workers ({env_value}) as "
f"no `RedisLockStore` or custom `LockStore` endpoint "
f"configuration has been found."
)
return _log_and_get_default_number_of_workers()