Complete handling of intent with @intent_manager on top of method (woohoo)

This commit is contained in:
Mathieu B 2021-07-28 19:31:47 +02:00
parent 3372594987
commit dc42b827fb
5 changed files with 57 additions and 14 deletions

View File

@ -36,6 +36,9 @@ def process_request():
if __name__ == '__main__': if __name__ == '__main__':
# Tests # Tests
WikipediaSkill().register() WikipediaSkill().register()
intent_manager.process_handlers()
intent_manager.recognise("cherche sur wikipedia Elon Musk") intent_manager.recognise("cherche sur wikipedia Elon Musk")
# start the flask server # start the flask server

View File

@ -1,5 +1,6 @@
import glob import glob
import os import os
import types
from jarvis import get_path_file from jarvis import get_path_file
from jarvis.skills import intent_manager from jarvis.skills import intent_manager
@ -39,5 +40,14 @@ class Skill:
for line in infile.readlines(): for line in infile.readlines():
intent_manager.register_regex(line.replace('\n', ''), self.name) intent_manager.register_regex(line.replace('\n', ''), self.name)
def register_intent(self, intent):
intent_manager.register_intent(intent.build(), domain=self.name) class SkillRegistering(type):
def __init__(cls, name, bases, attrs):
for key, val in attrs.items():
if type(val) is types.FunctionType and not str(val).__contains__("__"):
properties = getattr(val, "_register", None)
intent = properties[0]
intent_name = intent.name
if properties is not None:
intent_manager.intents_handlers[f"{intent_name}"] = [getattr(cls, key), name]

View File

@ -0,0 +1,11 @@
def intent_handler(*args):
"""
Creates an attribute on the method, so it can
be discovered by the metaclass
"""
def decorator(f):
f._register = args
return f
return decorator

View File

@ -1,16 +1,20 @@
import json
from adapt.engine import DomainIntentDeterminationEngine from adapt.engine import DomainIntentDeterminationEngine
engine = DomainIntentDeterminationEngine() engine = DomainIntentDeterminationEngine()
intents_handlers = dict()
def register_entity(entity_value, entity_type, domain): def register_entity(entity_value, entity_type, domain):
engine.register_entity(entity_value=entity_value, entity_type=entity_type, domain=domain) engine.register_entity(entity_value=entity_value, entity_type=entity_type, domain=domain)
print("[Adapt]: Added entity with type " + entity_type + " for " + domain) # print("[Adapt]: Added entity with type " + entity_type + " for " + domain)
def register_regex(regex, domain): def register_regex(regex, domain):
engine.register_regex_entity(regex, domain) engine.register_regex_entity(regex, domain)
print("[Adapt]: Added new regex for " + domain) # print("[Adapt]: Added new regex for " + domain)
def register_intent(intent, domain): def register_intent(intent, domain):
@ -18,6 +22,23 @@ def register_intent(intent, domain):
print("[Adapt]: Registered new intent for skill " + domain) print("[Adapt]: Registered new intent for skill " + domain)
def process_handlers():
for handler in intents_handlers:
function_handler = intents_handlers.get(handler)
intent_builder = getattr(function_handler[0], "_register", [])[0]
skill_name = function_handler[1]
register_intent(intent_builder.build(), domain=skill_name)
def handle(intent_name):
if intent_name in intents_handlers:
method = intents_handlers.get(intent_name)[0]
method(None, [])
def recognise(sentence): def recognise(sentence):
for intents in engine.determine_intent(sentence): for intents in engine.determine_intent(sentence):
print(intents) json_response = json.loads(json.dumps(intents))
handle(json_response['intent_type'])

View File

@ -1,16 +1,14 @@
from adapt.intent import IntentBuilder from adapt.intent import IntentBuilder
from jarvis.skills import Skill from jarvis.skills import Skill, SkillRegistering
from jarvis.skills.decorators import intent_handler
class WikipediaSkill(Skill): class WikipediaSkill(Skill, metaclass=SkillRegistering):
def __init__(self): def __init__(self):
# TODO: shorten that
super().__init__("WikipediaSkill", "research", "wikipedia") super().__init__("WikipediaSkill", "research", "wikipedia")
def register(self): @intent_handler(IntentBuilder("WikipediaQueryIntent").require("Wikipedia").require("ArticleTitle"))
super().register() def handle_wikipedia_query_intent(self, data):
print("Handle Wikipedia Query Intent Method")
wikipedia_query_intent = IntentBuilder("WikipediaQueryIntent")\
.require("Wikipedia")\
.require("ArticleTitle")
super().register_intent(wikipedia_query_intent)