Skip to content

Commit

Permalink
fox:update requirements
Browse files Browse the repository at this point in the history
update imports and requirements for latest pipeline plugins signature
  • Loading branch information
JarbasAl committed Oct 18, 2024
1 parent 4c7cbaa commit b994db3
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 199 deletions.
16 changes: 8 additions & 8 deletions mycroft/skills/intent_services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from ovos_core.intent_services import AdaptService,\
ConverseService, \
CommonQAService, \
FallbackService, \
PadaciosoService
from ovos_core.intent_services import IntentMatch
from mycroft.skills.intent_services.adapt_service import AdaptIntent, IntentBuilder, Intent
from ovos_core.intent_services.fallback_service import FallbackService
from ovos_core.intent_services.converse_service import ConverseService
from ovos_adapt.opm import AdaptPipeline as AdaptService
from padacioso.opm import PadaciosoPipeline as PadaciosoService
from ovos_commonqa.opm import CommonQAService
from ovos_plugin_manager.templates.pipeline import IntentMatch
from ovos_workshop.intents import Intent as AdaptIntent, IntentBuilder, Intent

try:
from ovos_core.intent_services.padatious_service import PadatiousService, PadatiousMatcher
from ovos_padatious.opm import PadatiousPipeline as PadatiousService, PadatiousMatcher
except ImportError:
from ovos_utils.log import LOG
LOG.warning("padatious not installed")
Expand Down
5 changes: 3 additions & 2 deletions mycroft/skills/intent_services/adapt_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
# limitations under the License.
#
"""An intent parsing service using the Adapt parser."""
from ovos_adapt.context import ContextManagerFrame
from ovos_adapt.engine import IntentDeterminationEngine
from ovos_workshop.intents import IntentBuilder, Intent
from ovos_adapt.opm import ContextManager, AdaptPipeline as AdaptService
from ovos_adapt.opm import AdaptPipeline as AdaptService
from ovos_bus_client.session import IntentContextManagerFrame as ContextManagerFrame, \
IntentContextManager as ContextManager


class AdaptIntent(IntentBuilder):
Expand Down
61 changes: 32 additions & 29 deletions ovos_core/intent_services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import Tuple, Callable
from typing import Tuple, Callable, Union

from ocp_pipeline.opm import OCPPipelineMatcher
from ovos_adapt.opm import AdaptPipeline as AdaptService
from ovos_bus_client.message import Message
from ovos_bus_client.session import SessionManager
from ovos_bus_client.util import get_message_lang
from ovos_workshop.intents import open_intent_envelope

from ocp_pipeline.opm import OCPPipelineMatcher
from ovos_adapt.opm import AdaptPipeline as AdaptService
from ovos_commonqa.opm import CommonQAService
from ovos_config.config import Configuration
from ovos_config.locale import setup_locale, get_valid_languages, get_full_lang_code
from ovos_config.locale import setup_locale, get_valid_languages
from ovos_core.intent_services.converse_service import ConverseService
from ovos_core.intent_services.fallback_service import FallbackService
from ovos_core.intent_services.stop_service import StopService
from ovos_core.transformers import MetadataTransformersService, UtteranceTransformersService
from ovos_plugin_manager.templates.pipeline import IntentMatch
from ovos_plugin_manager.templates.pipeline import PipelineMatch, IntentHandlerMatch
from ovos_utils.lang import standardize_lang_tag
from ovos_utils.log import LOG, deprecated, log_deprecation
from ovos_utils.metrics import Stopwatch
from ovos_workshop.intents import open_intent_envelope
from padacioso.opm import PadaciosoPipeline as PadaciosoService


Expand Down Expand Up @@ -284,13 +283,14 @@ def disambiguate_lang(message):
"""
default_lang = get_message_lang(message)
valid_langs = get_valid_languages()
valid_langs = [standardize_lang_tag(l) for l in valid_langs]
lang_keys = ["stt_lang",
"request_lang",
"detected_lang"]
for k in lang_keys:
if k in message.context:
v = get_full_lang_code(message.context[k])
if v in valid_langs:
v = standardize_lang_tag(message.context[k])
if v in valid_langs: # TODO - use lang distance instead to choose best dialect
if v != default_lang:
LOG.info(f"replaced {default_lang} with {k}: {v}")
return v
Expand All @@ -314,8 +314,7 @@ def get_pipeline(self, skips=None, session=None) -> Tuple[str, Callable]:
"intent matching will be extremely slow in comparison")
padatious_matcher = self._padacioso_service
else:
from ovos_core.intent_services.padatious_service import PadatiousMatcher
padatious_matcher = PadatiousMatcher(self._padatious_service)
padatious_matcher = self._padatious_service

matchers = {
"converse": self._converse.converse_with_skills,
Expand Down Expand Up @@ -351,7 +350,8 @@ def get_pipeline(self, skips=None, session=None) -> Tuple[str, Callable]:
LOG.debug(f"Session pipeline: {pipeline}")
return [(k, matchers[k]) for k in pipeline]

def _validate_session(self, message, lang):
@staticmethod
def _validate_session(message, lang):
# get session
lang = standardize_lang_tag(lang)
sess = SessionManager.get(message)
Expand All @@ -373,12 +373,12 @@ def _validate_session(self, message, lang):
sess.touch()
return sess

def _emit_match_message(self, match: IntentMatch, message: Message):
def _emit_match_message(self, match: Union[IntentHandlerMatch, PipelineMatch], message: Message):
"""Update the message data with the matched utterance information and
activate the corresponding skill if available.
Args:
match (IntentMatch): The matched utterance object.
match (IntentHandlerMatch): The matched utterance object.
message (Message): The messagebus data.
"""
message.data["utterance"] = match.utterance
Expand All @@ -387,19 +387,19 @@ def _emit_match_message(self, match: IntentMatch, message: Message):
# ensure skill_id is present in message.context
message.context["skill_id"] = match.skill_id

if match.intent_type is True:
if isinstance(match, PipelineMatch) and match.handled:
# utterance fully handled
reply = message.reply("ovos.utterance.handled",
{"skill_id": match.skill_id})
self.bus.emit(reply)
# Launch skill if not handled by the match function
elif match.intent_type:
elif isinstance(match, IntentHandlerMatch) and match.match_type:
# keep all original message.data and update with intent match
data = dict(message.data)
data.update(match.intent_data)
data.update(match.match_data)

# NOTE: message.reply to ensure correct message destination
reply = message.reply(match.intent_type, data)
reply = message.reply(match.match_type, data)

# let's activate the skill BEFORE the intent is triggered
# to ensure an accurate Session
Expand Down Expand Up @@ -483,9 +483,9 @@ def handle_utterance(self, message: Message):
LOG.debug(
f"ignoring match, skill_id '{match.skill_id}' blacklisted by Session '{sess.session_id}'")
continue
if match.intent_type and match.intent_type in sess.blacklisted_intents:
if isinstance(match, IntentHandlerMatch) and match.match_type in sess.blacklisted_intents:
LOG.debug(
f"ignoring match, intent '{match.intent_type}' blacklisted by Session '{sess.session_id}'")
f"ignoring match, intent '{match.match_type}' blacklisted by Session '{sess.session_id}'")
continue
try:
self._emit_match_message(match, message)
Expand Down Expand Up @@ -529,7 +529,7 @@ def handle_register_vocab(self, message):
alias_of = message.data.get('alias_of')
lang = get_message_lang(message)
self._adapt_service.register_vocabulary(entity_value, entity_type,
alias_of, regex_str, lang)
alias_of, regex_str, lang)
self.registered_vocab.append(message.data)

def handle_register_intent(self, message):
Expand Down Expand Up @@ -559,7 +559,8 @@ def handle_detach_skill(self, message):
skill_id = message.data.get('skill_id')
self._adapt_service.detach_skill(skill_id)

def handle_add_context(self, message):
@staticmethod
def handle_add_context(message: Message):
"""Add context
Args:
Expand All @@ -581,7 +582,8 @@ def handle_add_context(self, message):
sess = SessionManager.get(message)
sess.context.inject_context(entity)

def handle_remove_context(self, message):
@staticmethod
def handle_remove_context(message: Message):
"""Remove specific context
Args:
Expand All @@ -592,7 +594,8 @@ def handle_remove_context(self, message):
sess = SessionManager.get(message)
sess.context.remove_context(context)

def handle_clear_context(self, message):
@staticmethod
def handle_clear_context(message: Message):
"""Clears all keywords from context """
sess = SessionManager.get(message)
sess.context.clear_context()
Expand All @@ -615,10 +618,10 @@ def handle_get_intent(self, message):
session=sess):
match = match_func([utterance], lang, message)
if match:
if match.intent_type:
intent_data = match.intent_data
intent_data["intent_name"] = match.intent_type
intent_data["intent_service"] = match.intent_service
if match.match_type:
intent_data = match.match_data
intent_data["intent_name"] = match.match_type
intent_data["intent_service"] = pipeline
intent_data["skill_id"] = match.skill_id
intent_data["handler"] = match_func.__name__
self.bus.emit(message.reply("intent.service.intent.reply",
Expand Down Expand Up @@ -657,7 +660,7 @@ def handle_get_adapt(self, message: Message):
utterance = message.data["utterance"]
lang = get_message_lang(message)
intent = self._adapt_service.match_intent((utterance,), lang, message.serialize())
intent_data = intent.intent_data if intent else None
intent_data = intent.match_data if intent else None
self.bus.emit(message.reply("intent.service.adapt.reply",
{"intent": intent_data}))

Expand Down
18 changes: 8 additions & 10 deletions ovos_core/intent_services/converse_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
from ovos_bus_client.message import Message
from ovos_bus_client.session import SessionManager, UtteranceState, Session
from ovos_bus_client.util import get_message_lang
from ovos_workshop.permissions import ConverseMode, ConverseActivationMode

from ovos_config.config import Configuration
from ovos_config.locale import setup_locale
from ovos_plugin_manager.templates.pipeline import IntentMatch, PipelinePlugin
from ovos_plugin_manager.templates.pipeline import PipelineMatch, PipelinePlugin
from ovos_utils import flatten_list
from ovos_utils.lang import standardize_lang_tag
from ovos_utils.log import LOG
from ovos_workshop.permissions import ConverseMode, ConverseActivationMode


class ConverseService(PipelinePlugin):
Expand Down Expand Up @@ -313,7 +312,7 @@ def converse(self, utterances: List[str], skill_id: str, lang: str, message: Mes
f'increasing "max_skill_runtime" in mycroft.conf might help alleviate this issue')
return False

def converse_with_skills(self, utterances: List[str], lang: str, message: Message) -> Optional[IntentMatch]:
def converse_with_skills(self, utterances: List[str], lang: str, message: Message) -> Optional[PipelineMatch]:
"""Give active skills a chance at the utterance
Args:
Expand All @@ -338,12 +337,11 @@ def converse_with_skills(self, utterances: List[str], lang: str, message: Messag
continue
if self.converse(utterances, skill_id, lang, message):
state = session.utterance_states.get(skill_id, UtteranceState.INTENT)
return IntentMatch(intent_service='Converse',
intent_type=state != UtteranceState.RESPONSE,
# intent_type == True -> emit "ovos.utterance.handled"
intent_data={},
skill_id=skill_id,
utterance=utterances[0])
return PipelineMatch(handled=state != UtteranceState.RESPONSE,
# handled == True -> emit "ovos.utterance.handled"
match_data={},
skill_id=skill_id,
utterance=utterances[0])
return None

@staticmethod
Expand Down
24 changes: 11 additions & 13 deletions ovos_core/intent_services/fallback_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@

from ovos_bus_client.message import Message
from ovos_bus_client.session import SessionManager
from ovos_workshop.permissions import FallbackMode

from ovos_config import Configuration
from ovos_plugin_manager.templates.pipeline import IntentMatch, PipelinePlugin
from ovos_plugin_manager.templates.pipeline import PipelineMatch, PipelinePlugin
from ovos_utils import flatten_list
from ovos_utils.lang import standardize_lang_tag
from ovos_utils.log import LOG
from ovos_workshop.permissions import FallbackMode

FallbackRange = namedtuple('FallbackRange', ['start', 'stop'])

Expand Down Expand Up @@ -166,7 +165,7 @@ def attempt_fallback(self, utterances: List[str], skill_id: str, lang: str, mess
return False

def _fallback_range(self, utterances: List[str], lang: str,
message: Message, fb_range: FallbackRange) -> Optional[IntentMatch]:
message: Message, fb_range: FallbackRange) -> Optional[PipelineMatch]:
"""Send fallback request for a specified priority range.
Args:
Expand All @@ -177,7 +176,7 @@ def _fallback_range(self, utterances: List[str], lang: str,
fb_range (FallbackRange): fallback order start and stop.
Returns:
IntentMatch or None
PipelineMatch or None
"""
lang = standardize_lang_tag(lang)
# we call flatten in case someone is sending the old style list of tuples
Expand All @@ -197,24 +196,23 @@ def _fallback_range(self, utterances: List[str], lang: str,
continue
result = self.attempt_fallback(utterances, skill_id, lang, message)
if result:
return IntentMatch(intent_service='Fallback',
intent_type=None,
intent_data={},
skill_id=skill_id,
utterance=utterances[0])
return PipelineMatch(handled=True,
match_data={},
skill_id=skill_id,
utterance=utterances[0])
return None

def high_prio(self, utterances: List[str], lang: str, message: Message) -> Optional[IntentMatch]:
def high_prio(self, utterances: List[str], lang: str, message: Message) -> Optional[PipelineMatch]:
"""Pre-padatious fallbacks."""
return self._fallback_range(utterances, lang, message,
FallbackRange(0, 5))

def medium_prio(self, utterances: List[str], lang: str, message: Message) -> Optional[IntentMatch]:
def medium_prio(self, utterances: List[str], lang: str, message: Message) -> Optional[PipelineMatch]:
"""General fallbacks."""
return self._fallback_range(utterances, lang, message,
FallbackRange(5, 90))

def low_prio(self, utterances: List[str], lang: str, message: Message) -> Optional[IntentMatch]:
def low_prio(self, utterances: List[str], lang: str, message: Message) -> Optional[PipelineMatch]:
"""Low prio fallbacks with general matching such as chat-bot."""
return self._fallback_range(utterances, lang, message,
FallbackRange(90, 101))
Expand Down
Loading

0 comments on commit b994db3

Please sign in to comment.