This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: uncouple agent and network protocol from hivemind protocol (…
…#1) * refactor!: uncouple agent and network protocol from hivemind protocol companion to JarbasHiveMind/HiveMind-core#24 * refactor!: uncouple agent and network protocol from hivemind protocol companion to JarbasHiveMind/HiveMind-core#24 * .
- Loading branch information
Showing
3 changed files
with
68 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,71 @@ | ||
import dataclasses | ||
import json | ||
import os | ||
from typing import Dict, Any, List | ||
|
||
from hivemind_bus_client.message import HiveMessage, HiveMessageType | ||
from hivemind_core.protocol import HiveMindListenerProtocol | ||
from hivemind_core.protocol import AgentProtocol | ||
from ovos_bus_client import MessageBusClient | ||
from ovos_bus_client.message import Message | ||
from ovos_bus_client.session import SessionManager | ||
from ovos_persona import Persona | ||
from ovos_utils.messagebus import Message | ||
from ovos_utils.fakebus import FakeBus | ||
from ovos_utils.log import LOG | ||
|
||
|
||
class FakeCroftPersonaProtocol(HiveMindListenerProtocol): | ||
"""""" | ||
persona = None | ||
@dataclasses.dataclass() | ||
class PersonaProtocol(AgentProtocol): | ||
bus: MessageBusClient = dataclasses.field(default_factory=FakeBus) | ||
config: Dict[str, Any] = dataclasses.field(default_factory=dict) | ||
sessions: Dict[str, List[Dict[str, str]]] = dataclasses.field(default_factory=dict) | ||
persona: Persona = None | ||
|
||
@classmethod | ||
def set_persona(cls, persona_json: str): | ||
with open(persona_json) as f: | ||
persona = json.load(f) | ||
name = persona.get("name") or os.path.basename(persona_json) | ||
cls.persona = Persona(name=name, config=persona) | ||
def __post_init__(self): | ||
if not self.persona: | ||
persona_json = self.config.get("persona") | ||
if not persona_json: | ||
persona = { | ||
"name": "ChatGPT", | ||
"solvers": [ | ||
"ovos-solver-openai-persona-plugin" | ||
], | ||
"ovos-solver-openai-persona-plugin": { | ||
"api_url": "https://llama.smartgic.io/v1", | ||
"key": "sk-xxxx", | ||
"persona": "helpful, creative, clever, and very friendly." | ||
} | ||
} | ||
name = persona.get("name") | ||
else: | ||
with open(persona_json) as f: | ||
persona = json.load(f) | ||
name = persona.get("name") or os.path.basename(persona_json) | ||
self.persona = Persona(name=name, config=persona) | ||
|
||
def handle_inject_mycroft_msg(self, message: Message, client): | ||
LOG.debug("registering internal OVOS bus handlers") | ||
self.bus.on("recognizer_loop:utterance", self.handle_utterance) # catch all | ||
|
||
def handle_utterance(self, message: Message): | ||
""" | ||
message (Message): mycroft bus message object | ||
""" | ||
if message.msg_type == "recognizer_loop:utterance": | ||
utt = message.data["utterances"][0] | ||
answer = response = self.persona.complete(utt) | ||
payload = HiveMessage(HiveMessageType.BUS, | ||
message.reply("speak", {"utterance": answer})) | ||
client.send(payload) | ||
utt = message.data["utterances"][0] | ||
|
||
sess = SessionManager.get(message) | ||
|
||
# track msg history | ||
if sess.session_id not in self.sessions: | ||
self.sessions[sess.session_id] = [] | ||
self.sessions[sess.session_id].append({"role": "user", "content": utt}) | ||
|
||
answer = self.persona.chat(self.sessions[sess.session_id], lang=sess.lang).strip() | ||
peer = message.context["source"] | ||
client = self.clients[peer] | ||
|
||
msg = HiveMessage( | ||
HiveMessageType.BUS, | ||
source_peer=self.hm_protocol.peer, | ||
target_peers=[peer], | ||
payload=message.reply("speak", {"utterance": answer}), | ||
) | ||
client.send(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
jarbas_hive_mind | ||
ovos-persona @ git+https://github.com/OpenVoiceOS/ovos-persona | ||
hivemind-core>=1.0.0,<2.0.0 | ||
ovos-persona |