Skip to content

Commit 8ac8d89

Browse files
feat: intent transformers (#316)
* feat: intent transformers add base class for intent transformer plugins * feat: intent transformers add base class for intent transformer plugins * 📝 Add docstrings to `feat/intent_transformers` (#317) Docstrings generation was requested by @JarbasAl. * #316 (comment) The following files were modified: * `ovos_plugin_manager/templates/transformers.py` Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent ffc4bf9 commit 8ac8d89

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

ovos_plugin_manager/templates/transformers.py

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import abc
2-
from typing import List, Tuple, Optional
2+
from typing import List, Tuple, Optional, Union
33

44
from ovos_bus_client.util import get_mycroft_bus
55
from ovos_config.config import Configuration
66
from ovos_config.locale import get_default_lang
7+
from ovos_plugin_manager.templates.pipeline import IntentHandlerMatch, PipelineMatch
78
from ovos_utils.log import LOG
89

910
from ovos_plugin_manager.utils import ReadWriteStream
@@ -75,10 +76,72 @@ def transform(self, utterances: List[str],
7576
return utterances, {}
7677

7778
def default_shutdown(self):
78-
""" perform any shutdown actions """
79+
"""
80+
Performs any necessary shutdown or cleanup actions.
81+
82+
Intended to be overridden by subclasses to implement custom shutdown logic.
83+
"""
7984
pass
8085

8186

87+
class IntentTransformer:
88+
""" runs before selected intent is triggered, can be used to inject message.data"""
89+
90+
def __init__(self, name, priority=50, config=None):
91+
"""
92+
Initializes the IntentTransformer with a name, priority, and optional configuration.
93+
94+
If no configuration is provided, attempts to load it from the global configuration under "intent_transformers" using the given name.
95+
"""
96+
self.name = name
97+
self.bus = None
98+
self.priority = priority
99+
if not config:
100+
config_core = dict(Configuration())
101+
config = config_core.get("intent_transformers", {}).get(self.name)
102+
self.config = config or {}
103+
104+
def bind(self, bus=None):
105+
"""
106+
Attach a message bus instance to the transformer.
107+
108+
If no bus is provided, the default Mycroft message bus is used.
109+
"""
110+
self.bus = bus or get_mycroft_bus()
111+
112+
def initialize(self):
113+
"""
114+
Performs any necessary initialization actions for the transformer.
115+
116+
Intended to be overridden by subclasses to implement custom setup logic.
117+
"""
118+
pass
119+
120+
@abc.abstractmethod
121+
def transform(self, intent: Union[IntentHandlerMatch, PipelineMatch]) -> Union[IntentHandlerMatch, PipelineMatch]:
122+
"""
123+
Transforms the intent match object before the intent handler is triggered.
124+
125+
This method can be used to modify or inject data into the intent, such as performing named entity recognition (NER) or altering match data. By default, it returns the intent unchanged.
126+
127+
Args:
128+
intent: The intent match object to be transformed.
129+
130+
Returns:
131+
The transformed intent match object.
132+
"""
133+
return intent
134+
135+
def default_shutdown(self):
136+
"""
137+
Performs any necessary shutdown actions for the transformer.
138+
139+
Intended to be overridden by subclasses to implement cleanup procedures.
140+
"""
141+
pass
142+
143+
144+
82145
class AudioTransformer:
83146
"""process audio data and optionally transform it before STT stage"""
84147

ovos_plugin_manager/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class PluginTypes(str, Enum):
4747
AUDIO_TRANSFORMER = "neon.plugin.audio" # TODO rename "opm.transformer.audio"
4848
DIALOG_TRANSFORMER = "opm.transformer.dialog"
4949
TTS_TRANSFORMER = "opm.transformer.tts"
50+
INTENT_TRANSFORMER = "opm.transformer.intent"
5051
QUESTION_SOLVER = "neon.plugin.solver" # TODO rename "opm.solver.question"
5152
CHAT_SOLVER = "opm.solver.chat"
5253
TLDR_SOLVER = "opm.solver.summarization"
@@ -91,6 +92,7 @@ class PluginConfigTypes(str, Enum):
9192
AUDIO_TRANSFORMER = "neon.plugin.audio.config"
9293
DIALOG_TRANSFORMER = "opm.transformer.dialog.config"
9394
TTS_TRANSFORMER = "opm.transformer.tts.config"
95+
INTENT_TRANSFORMER = "opm.transformer.intent.config"
9496
QUESTION_SOLVER = "neon.plugin.solver.config"
9597
CHAT_SOLVER = "opm.solver.chat.config"
9698
TLDR_SOLVER = "opm.solver.summarization.config"

0 commit comments

Comments
 (0)