Skip to content

Commit

Permalink
fix:matching and dialogs (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored Nov 13, 2024
1 parent 97beb98 commit ecd38a5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ print(response)

## Events

- **persona:answer**: Sent when a persona provides an answer to a query.
- **persona:query**: Sent to submit a query to a persona.
- **persona:summon**: Sent when a persona is summoned.
- **persona:release**: Sent when a persona is released.

Expand Down
66 changes: 35 additions & 31 deletions ovos_persona/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from padacioso import IntentContainer

from ovos_persona.solvers import QuestionSolversService

try:
from ovos_plugin_manager.solvers import find_chat_solver_plugins
except ImportError:
Expand Down Expand Up @@ -66,7 +67,7 @@ def __init__(self, bus: Optional[Union[MessageBusClient, FakeBus]] = None,
self.blacklist = self.config.get("persona_blacklist") or []
self.load_personas(self.config.get("personas_path"))
self.active_persona = None
self.add_event('persona:answer', self.handle_persona_answer)
self.add_event('persona:query', self.handle_persona_query)
self.add_event('persona:summon', self.handle_persona_summon)
self.add_event('persona:release', self.handle_persona_release)
self.add_event("speak", self.handle_speak)
Expand Down Expand Up @@ -216,29 +217,18 @@ def match_high(self, utterances: List[str], lang: Optional[str] = None,
utterance=utterances[0])
elif match["name"] == "ask":
utterance = match["entities"].pop("query")
ans = self.chatbox_ask(utterance,
lang=lang,
persona=persona)
if ans:
return IntentHandlerMatch(match_type='persona:answer',
match_data={"answer": ans,
"persona": persona},
skill_id="persona.openvoiceos",
utterance=utterances[0])

# override regular intent parsing, handle utterance until persona release
if self.active_persona:
LOG.debug(f"Persona is active: {self.active_persona}")
ans = self.chatbox_ask(utterances[0],
lang=lang,
persona=self.active_persona)
if ans:
return IntentHandlerMatch(match_type='persona:answer',
match_data={"answer": ans,
"persona": self.active_persona},
return IntentHandlerMatch(match_type='persona:query',
match_data={"utterance": utterance,
"lang": lang,
"persona": persona},
skill_id="persona.openvoiceos",
utterance=utterances[0])

# override regular intent parsing, handle utterance until persona is released
if self.active_persona:
LOG.debug(f"Persona is active: {self.active_persona}")
return self.match_low(utterances, lang, message)

def match_medium(self, utterances: List[str], lang: str, message: Message) -> None:
return self.match_high(utterances, lang, message)

Expand All @@ -255,12 +245,13 @@ def match_low(self, utterances: List[str], lang: Optional[str] = None,
Returns:
IntentMatch if handled otherwise None.
"""
ans = self.chatbox_ask(utterances[0], lang=lang)
if ans:
return IntentHandlerMatch(match_type='persona:answer',
match_data={"answer": ans},
skill_id="persona.openvoiceos",
utterance=utterances[0])
# always matches! use as last resort in pipeline
return IntentHandlerMatch(match_type='persona:query',
match_data={"utterance": utterances[0],
"lang": lang,
"persona": self.active_persona},
skill_id="persona.openvoiceos",
utterance=utterances[0])

# bus events
def handle_utterance(self, message):
Expand All @@ -276,20 +267,33 @@ def handle_speak(self, message):
if sess.session_id in self.sessions:
self.sessions[sess.session_id].append(("ai", utt))

def handle_persona_answer(self, message):
utt = message.data["answer"]
self.speak(utt)
def handle_persona_query(self, message):
utt = message.data["utterance"]
lang = message.data["lang"]
persona = message.data["persona"]

if persona not in self.personas:
self.speak_dialog("unknown_persona", {"persona": persona})
return

# TODO - streaming support
ans = self.chatbox_ask(utt, lang=lang, persona=persona)
if not ans:
self.speak_dialog("persona_error")
else:
self.speak(ans)

def handle_persona_summon(self, message):
persona = message.data["persona"]
if persona not in self.personas:
self.speak_dialog("unknown_persona")
self.speak_dialog("unknown_persona", {"persona": persona})
else:
self.active_persona = persona
LOG.info(f"Summoned Persona: {self.active_persona}")

def handle_persona_release(self, message):
LOG.info(f"Releasing Persona: {self.active_persona}")
self.speak_dialog("release_persona", {"persona": self.active_persona})
self.active_persona = None


Expand Down

0 comments on commit ecd38a5

Please sign in to comment.