Skip to content
This repository has been archived by the owner on Dec 29, 2024. It is now read-only.

Commit

Permalink
refactor!: uncouple agent and network protocol from hivemind protocol (
Browse files Browse the repository at this point in the history
…#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
JarbasAl authored Dec 28, 2024
1 parent 13f5007 commit 218ac90
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
77 changes: 59 additions & 18 deletions hivemind_persona/__init__.py
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)
13 changes: 7 additions & 6 deletions hivemind_persona/__main__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import argparse

from hivemind_core.service import HiveMindService
from ovos_utils.messagebus import FakeBus
from hivemind_persona import PersonaProtocol

from hivemind_persona import FakeCroftPersonaProtocol
from hivemind_core.server import HiveMindWebsocketProtocol
from hivemind_core.service import HiveMindService


def run():
parser = argparse.ArgumentParser()
parser.add_argument("--persona", help="path to persona .json file", required=True)
args = parser.parse_args()
FakeCroftPersonaProtocol.set_persona(args.persona)
service = HiveMindService(protocol=FakeCroftPersonaProtocol,
bus=FakeBus())

service = HiveMindService(agent_protocol=PersonaProtocol,
agent_config={"persona": args.persona},
network_protocol=HiveMindWebsocketProtocol)
service.run()


Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
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

0 comments on commit 218ac90

Please sign in to comment.