Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve_typing #528

Merged
merged 8 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions ovos_core/intent_services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from collections import namedtuple
from typing import Tuple, Callable
from ovos_bus_client.message import Message
from ovos_bus_client.session import SessionManager
Expand All @@ -31,20 +30,11 @@
from ovos_utils.log import LOG, deprecated, log_deprecation
from ovos_utils.metrics import Stopwatch
from ovos_workshop.intents import open_intent_envelope

# Intent match response tuple containing
# intent_service: Name of the service that matched the intent
# intent_type: intent name (used to call intent handler over the message bus)
# intent_data: data provided by the intent match
# skill_id: the skill this handler belongs to
IntentMatch = namedtuple('IntentMatch',
['intent_service', 'intent_type',
'intent_data', 'skill_id', 'utterance']
)
from ovos_plugin_manager.templates.pipeline import IntentMatch


class IntentService:
"""Mycroft intent service. parses utterances using a variety of systems.
"""OVOS intent service. parses utterances using a variety of systems.

The intent service also provides the internal API for registering and
querying the intent service.
Expand Down
9 changes: 5 additions & 4 deletions ovos_core/intent_services/adapt_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
# limitations under the License.
#
"""An intent parsing service using the Adapt parser."""
from threading import Lock
from functools import lru_cache
from threading import Lock
from typing import List, Tuple, Optional

from adapt.engine import IntentDeterminationEngine
from ovos_config.config import Configuration
from ovos_bus_client.message import Message
import ovos_core.intent_services
from ovos_bus_client.session import IntentContextManager as ContextManager, \
SessionManager
from ovos_config.config import Configuration
from ovos_utils import flatten_list
from ovos_utils.log import LOG

from ovos_plugin_manager.templates.pipeline import IntentMatch


def _entity_skill_id(skill_id):
"""Helper converting a skill id to the format used in entities.
Expand Down Expand Up @@ -248,7 +249,7 @@ def take_best(intent, utt):
sess.context.update_context(ents)

skill_id = best_intent['intent_type'].split(":")[0]
ret = ovos_core.intent_services.IntentMatch(
ret = IntentMatch(
'Adapt', best_intent['intent_type'], best_intent, skill_id,
best_intent['utterance']
)
Expand Down
22 changes: 11 additions & 11 deletions ovos_core/intent_services/commonqa_service.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import time
from dataclasses import dataclass
from os.path import dirname
from threading import Event
from typing import Dict

from ovos_classifiers.opm.heuristics import BM25MultipleChoiceSolver
from typing import Dict, Optional

import ovos_core.intent_services
import time
from ovos_bus_client.message import Message
from ovos_bus_client.session import SessionManager
from ovos_classifiers.opm.heuristics import BM25MultipleChoiceSolver
from ovos_config.config import Configuration
from ovos_utils import flatten_list
from ovos_utils.log import LOG
from ovos_workshop.app import OVOSAbstractApplication

from ovos_plugin_manager.templates.pipeline import IntentMatch


@dataclass
class Query:
Expand Down Expand Up @@ -76,7 +76,7 @@ def is_question_like(self, utterance: str, lang: str):
# require a "question word"
return self.voc_match(utterance, "QuestionWord", lang)

def match(self, utterances: str, lang: str, message: Message):
def match(self, utterances: str, lang: str, message: Message) -> Optional[IntentMatch]:
"""
Send common query request and select best response

Expand Down Expand Up @@ -114,11 +114,11 @@ def match(self, utterances: str, lang: str, message: Message):
message.data["utterance"] = utterance
answered, skill_id = self.handle_question(message)
if answered:
match = ovos_core.intent_services.IntentMatch(intent_service='CommonQuery',
intent_type=True,
intent_data={},
skill_id=skill_id,
utterance=utterance)
match = IntentMatch(intent_service='CommonQuery',
intent_type=True,
intent_data={},
skill_id=skill_id,
utterance=utterance)
break
return match

Expand Down
20 changes: 11 additions & 9 deletions ovos_core/intent_services/converse_service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from threading import Event
from typing import Optional

import ovos_core.intent_services
import time
from ovos_bus_client.message import Message
from ovos_bus_client.session import SessionManager, UtteranceState
from ovos_bus_client.util import get_message_lang
Expand All @@ -11,6 +11,8 @@
from ovos_utils.log import LOG
from ovos_workshop.permissions import ConverseMode, ConverseActivationMode

from ovos_plugin_manager.templates.pipeline import IntentMatch


class ConverseService:
"""Intent Service handling conversational skills."""
Expand Down Expand Up @@ -311,7 +313,7 @@ def converse(self, utterances, skill_id, lang, message):
f'increasing "max_skill_runtime" in mycroft.conf might help alleviate this issue')
return False

def converse_with_skills(self, utterances, lang, message):
def converse_with_skills(self, utterances, lang, message) -> Optional[IntentMatch]:
"""Give active skills a chance at the utterance

Args:
Expand All @@ -335,12 +337,12 @@ def converse_with_skills(self, utterances, lang, message):
continue
if self.converse(utterances, skill_id, lang, message):
state = session.utterance_states.get(skill_id, UtteranceState.INTENT)
return ovos_core.intent_services.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 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 None

def handle_get_response_enable(self, message):
Expand Down
25 changes: 13 additions & 12 deletions ovos_core/intent_services/fallback_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
"""Intent service for Mycroft's fallback system."""
import operator
from collections import namedtuple
from typing import Optional

import time
from ovos_bus_client.session import SessionManager
from ovos_config import Configuration

import ovos_core.intent_services
from ovos_utils import flatten_list
from ovos_utils.log import LOG
from ovos_bus_client.session import SessionManager
from ovos_workshop.skills.fallback import FallbackMode

from ovos_plugin_manager.templates.pipeline import IntentMatch

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


Expand Down Expand Up @@ -157,7 +158,7 @@ def attempt_fallback(self, utterances, skill_id, lang, message):
f'increasing "max_skill_runtime" in mycroft.conf might help alleviate this issue')
return False

def _fallback_range(self, utterances, lang, message, fb_range):
def _fallback_range(self, utterances, lang, message, fb_range) -> Optional[IntentMatch]:
"""Send fallback request for a specified priority range.

Args:
Expand Down Expand Up @@ -187,24 +188,24 @@ def _fallback_range(self, utterances, lang, message, fb_range):
continue
result = self.attempt_fallback(utterances, skill_id, lang, message)
if result:
return ovos_core.intent_services.IntentMatch(intent_service='Fallback',
intent_type=None,
intent_data={},
skill_id=skill_id,
utterance=utterances[0])
return IntentMatch(intent_service='Fallback',
intent_type=None,
intent_data={},
skill_id=skill_id,
utterance=utterances[0])
return None

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

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

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