-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
609 additions
and
440 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Allow creation of natural language interpreter and generator by classname reference | ||
in the ``endpoints.yml``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import logging | ||
import warnings | ||
from typing import Any, Dict, Text, Optional, Union | ||
|
||
from rasa.utils import common | ||
from rasa.utils.endpoints import EndpointConfig | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class EventBroker: | ||
"""Base class for any event broker implementation.""" | ||
|
||
@staticmethod | ||
def create( | ||
obj: Union["EventBroker", EndpointConfig, None], | ||
) -> Optional["EventBroker"]: | ||
"""Factory to create an event broker.""" | ||
|
||
if isinstance(obj, EventBroker): | ||
return obj | ||
else: | ||
return _create_from_endpoint_config(obj) | ||
|
||
@classmethod | ||
def from_endpoint_config(cls, broker_config: EndpointConfig) -> "EventBroker": | ||
raise NotImplementedError( | ||
"Event broker must implement the `from_endpoint_config` method." | ||
) | ||
|
||
def publish(self, event: Dict[Text, Any]) -> None: | ||
"""Publishes a json-formatted Rasa Core event into an event queue.""" | ||
|
||
raise NotImplementedError("Event broker must implement the `publish` method.") | ||
|
||
|
||
def _create_from_endpoint_config( | ||
endpoint_config: Optional[EndpointConfig], | ||
) -> Optional["EventBroker"]: | ||
"""Instantiate an event broker based on its configuration.""" | ||
|
||
if endpoint_config is None: | ||
broker = None | ||
elif endpoint_config.type is None or endpoint_config.type.lower() == "pika": | ||
from rasa.core.brokers.pika import PikaEventBroker | ||
|
||
# default broker if no type is set | ||
broker = PikaEventBroker.from_endpoint_config(endpoint_config) | ||
elif endpoint_config.type.lower() == "sql": | ||
from rasa.core.brokers.sql import SQLEventBroker | ||
|
||
broker = SQLEventBroker.from_endpoint_config(endpoint_config) | ||
elif endpoint_config.type.lower() == "file": | ||
from rasa.core.brokers.file import FileEventBroker | ||
|
||
broker = FileEventBroker.from_endpoint_config(endpoint_config) | ||
elif endpoint_config.type.lower() == "kafka": | ||
from rasa.core.brokers.kafka import KafkaEventBroker | ||
|
||
broker = KafkaEventBroker.from_endpoint_config(endpoint_config) | ||
else: | ||
broker = _load_from_module_string(endpoint_config) | ||
|
||
logger.debug(f"Instantiated event broker to '{broker.__class__.__name__}'.") | ||
return broker | ||
|
||
|
||
def _load_from_module_string(broker_config: EndpointConfig,) -> Optional["EventBroker"]: | ||
"""Instantiate an event broker based on its class name.""" | ||
|
||
try: | ||
event_broker_class = common.class_from_module_path(broker_config.type) | ||
return event_broker_class.from_endpoint_config(broker_config) | ||
except (AttributeError, ImportError) as e: | ||
logger.warning( | ||
f"The `EventBroker` type '{broker_config.type}' could not be found. " | ||
f"Not using any event broker. Error: {e}" | ||
) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,13 @@ | ||
import logging | ||
from typing import Any, Dict, Text, Optional | ||
import warnings | ||
|
||
from rasa.utils.endpoints import EndpointConfig | ||
from rasa.core.brokers.broker import EventBroker | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class EventChannel: | ||
@classmethod | ||
def from_endpoint_config(cls, broker_config: EndpointConfig) -> "EventChannel": | ||
raise NotImplementedError( | ||
"Event broker must implement the `from_endpoint_config` method." | ||
) | ||
|
||
def publish(self, event: Dict[Text, Any]) -> None: | ||
"""Publishes a json-formatted Rasa Core event into an event queue.""" | ||
|
||
raise NotImplementedError("Event broker must implement the `publish` method.") | ||
# noinspection PyAbstractClass | ||
class EventChannel(EventBroker): | ||
warnings.warn( | ||
"Deprecated, inherit from `EventBroker` instead of `EventChannel`. " | ||
"The `EventChannel` class will be removed.", | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import json | ||
import logging | ||
import typing | ||
import warnings | ||
from typing import Optional, Text, Dict | ||
|
||
from rasa.core.brokers.broker import EventBroker | ||
|
||
if typing.TYPE_CHECKING: | ||
from rasa.utils.endpoints import EndpointConfig | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class FileEventBroker(EventBroker): | ||
"""Log events to a file in json format. | ||
There will be one event per line and each event is stored as json.""" | ||
|
||
DEFAULT_LOG_FILE_NAME = "rasa_event.log" | ||
|
||
def __init__(self, path: Optional[Text] = None) -> None: | ||
self.path = path or self.DEFAULT_LOG_FILE_NAME | ||
self.event_logger = self._event_logger() | ||
|
||
@classmethod | ||
def from_endpoint_config( | ||
cls, broker_config: Optional["EndpointConfig"] | ||
) -> Optional["FileEventBroker"]: | ||
if broker_config is None: | ||
return None | ||
|
||
# noinspection PyArgumentList | ||
return cls(**broker_config.kwargs) | ||
|
||
def _event_logger(self): | ||
"""Instantiate the file logger.""" | ||
|
||
logger_file = self.path | ||
# noinspection PyTypeChecker | ||
query_logger = logging.getLogger("event-logger") | ||
query_logger.setLevel(logging.INFO) | ||
handler = logging.FileHandler(logger_file) | ||
handler.setFormatter(logging.Formatter("%(message)s")) | ||
query_logger.propagate = False | ||
query_logger.addHandler(handler) | ||
|
||
logger.info(f"Logging events to '{logger_file}'.") | ||
|
||
return query_logger | ||
|
||
def publish(self, event: Dict) -> None: | ||
"""Write event to file.""" | ||
|
||
self.event_logger.info(json.dumps(event)) | ||
self.event_logger.handlers[0].flush() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,12 @@ | ||
import json | ||
import logging | ||
import typing | ||
from typing import Optional, Text, Dict | ||
import warnings | ||
|
||
from rasa.core.brokers.event_channel import EventChannel | ||
from rasa.core.brokers.file import FileEventBroker | ||
|
||
if typing.TYPE_CHECKING: | ||
from rasa.utils.endpoints import EndpointConfig | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class FileProducer(EventChannel): | ||
"""Log events to a file in json format. | ||
There will be one event per line and each event is stored as json.""" | ||
|
||
DEFAULT_LOG_FILE_NAME = "rasa_event.log" | ||
|
||
def __init__(self, path: Optional[Text] = None) -> None: | ||
self.path = path or self.DEFAULT_LOG_FILE_NAME | ||
self.event_logger = self._event_logger() | ||
|
||
@classmethod | ||
def from_endpoint_config( | ||
cls, broker_config: Optional["EndpointConfig"] | ||
) -> Optional["FileProducer"]: | ||
if broker_config is None: | ||
return None | ||
|
||
# noinspection PyArgumentList | ||
return cls(**broker_config.kwargs) | ||
|
||
def _event_logger(self): | ||
"""Instantiate the file logger.""" | ||
|
||
logger_file = self.path | ||
# noinspection PyTypeChecker | ||
query_logger = logging.getLogger("event-logger") | ||
query_logger.setLevel(logging.INFO) | ||
handler = logging.FileHandler(logger_file) | ||
handler.setFormatter(logging.Formatter("%(message)s")) | ||
query_logger.propagate = False | ||
query_logger.addHandler(handler) | ||
|
||
logger.info(f"Logging events to '{logger_file}'.") | ||
|
||
return query_logger | ||
|
||
def publish(self, event: Dict) -> None: | ||
"""Write event to file.""" | ||
|
||
self.event_logger.info(json.dumps(event)) | ||
self.event_logger.handlers[0].flush() | ||
class FileProducer(FileEventBroker): | ||
warnings.warn( | ||
"Deprecated, the class `FileProducer` has been renamed to `FileEventBroker`. " | ||
"The `FileProducer` class will be removed.", | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.