Skip to content

Commit

Permalink
Refactor context and chat_id wise-agents#363
Browse files Browse the repository at this point in the history
  • Loading branch information
maeste committed Oct 1, 2024
1 parent 99e167a commit e0e81a3
Show file tree
Hide file tree
Showing 23 changed files with 513 additions and 639 deletions.
9 changes: 7 additions & 2 deletions examples/perceive_and_act/custom_agents.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
import threading
import time
import uuid
from openai.types.chat import ChatCompletionToolParam, ChatCompletionMessageParam
from typing import List, Optional
from wiseagents import WiseAgent, WiseAgentEvent, WiseAgentMessage, WiseAgentMetaData, WiseAgentTransport
from wiseagents.core import WiseAgentRegistry
from wiseagents.yaml import WiseAgentsLoader

class PerceivingAgent(WiseAgent):
Expand All @@ -22,10 +24,13 @@ def start_agent(self):
super().start_agent()
self.stop_event.clear()
self.perceive(self._file_path, self.on_file_change, self._check_interval)
self.context_name = self.name + str(uuid.uuid4())
WiseAgentRegistry.create_context(context_name=self.context_name)

def stop_agent(self):
self.stop_event.set()
super().stop_agent()
WiseAgentRegistry.remove_context(context_name=self.context_name)

def process_request(self, request: WiseAgentMessage,
conversation_history: List[ChatCompletionMessageParam]) -> Optional[str]:
Expand Down Expand Up @@ -80,7 +85,7 @@ def watch():

def on_file_change(self, content):
print(f"sending message: {content}, {self.name}, {self._destination_agent_name}")
self.send_request(WiseAgentMessage(content, self.name), self._destination_agent_name)
self.send_request(WiseAgentMessage(message = content, sender=self.name, context_name=self.context_name), self._destination_agent_name)

class ActionAgent(WiseAgent):
yaml_tag = u'!custom_agents.ActionAgent'
Expand All @@ -96,7 +101,7 @@ def start_agent(self):
def process_request(self, request: WiseAgentMessage, conversation_history: List[ChatCompletionMessageParam]) -> str | None:
with open(self._destination_file_path, 'w') as f:
f.write(request.message)
self.send_response(WiseAgentMessage("File updated", self.name), request.sender)
self.send_response(WiseAgentMessage(message="File updated", sender=self.name, context_name=request.context_name), request.sender)


def process_response(self, response: WiseAgentMessage):
Expand Down
11 changes: 8 additions & 3 deletions examples/perceive_ask_and_act/custom_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import signal
import threading
import time
import uuid
from openai.types.chat import ChatCompletionToolParam, ChatCompletionMessageParam
from typing import List, Optional
from wiseagents import WiseAgent, WiseAgentEvent, WiseAgentMessage, WiseAgentMetaData, WiseAgentTransport
from wiseagents.core import WiseAgentRegistry
from wiseagents.transports.stomp import StompWiseAgentTransport
from wiseagents.yaml import WiseAgentsLoader

Expand All @@ -25,10 +27,13 @@ def start_agent(self):
super().start_agent()
self.stop_event.clear()
self.perceive(self._file_path, self.on_file_change, self._check_interval)
self.context_name = self.name + str(uuid.uuid4())
WiseAgentRegistry.create_context(context_name=self.context_name)

def stop_agent(self):
self.stop_event.set()
super().stop_agent()
WiseAgentRegistry.remove_context(context_name=self.context_name)

def process_request(self, request: WiseAgentMessage,
conversation_history: List[ChatCompletionMessageParam]) -> Optional[str]:
Expand Down Expand Up @@ -83,7 +88,7 @@ def watch():

def on_file_change(self, content):
print(f"sending message: {content}, {self.name}, {self._destination_agent_name}")
self.send_request(WiseAgentMessage(content, self.name), self._destination_agent_name)
self.send_request(WiseAgentMessage(message = content, sender=self.name, context_name=self.context_name), self._destination_agent_name)

class ActionAgent(WiseAgent):
yaml_tag = u'!custom_agents.ActionAgent'
Expand All @@ -99,7 +104,7 @@ def start_agent(self):
def process_request(self, request: WiseAgentMessage, conversation_history: List[ChatCompletionMessageParam]) -> str | None:
with open(self._destination_file_path, 'w') as f:
f.write(request.message)
self.send_response(WiseAgentMessage("File updated", self.name), request.sender)
self.send_response(WiseAgentMessage(message="File updated", sender=self.name, context_name=request.context_name), request.sender)


def process_response(self, response: WiseAgentMessage):
Expand All @@ -109,7 +114,7 @@ def process_event(self, event: WiseAgentEvent):
pass

def process_error(self, error: WiseAgentMessage):
pass
pass
class UserQuestionAgent(WiseAgent):
yaml_tag = u'!custom_agents.UserQuestionAgent'
yaml_loader = WiseAgentsLoader
Expand Down
15 changes: 9 additions & 6 deletions src/wiseagents/agents/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AssistantAgent(WiseAgent):
_response_delivery = None
_cond = threading.Condition()
_response : WiseAgentMessage = None
_chat_id = None
_ctx = None

def __new__(cls, *args, **kwargs):
"""Create a new instance of the class, setting default values for the optional instance variables."""
Expand Down Expand Up @@ -52,14 +52,17 @@ def __repr__(self):

def start_agent(self):
super().start_agent()
self._chat_id = str(uuid.uuid4())
WiseAgentRegistry.get_or_create_context("default").set_collaboration_type(self._chat_id,
WiseAgentCollaborationType.CHAT)
self._ctx = f'{self.name}.{str(uuid.uuid4())}'
WiseAgentRegistry.create_context(self._ctx).set_collaboration_type(WiseAgentCollaborationType.CHAT)
gradio.ChatInterface(self.slow_echo).launch(prevent_thread_lock=True)

def stop_agent(self):
super().stop_agent()
WiseAgentRegistry.remove_context(self._ctx)

def slow_echo(self, message, history):
with self._cond:
self.handle_request(WiseAgentMessage(message=message, sender=self.name, chat_id=self._chat_id))
self.handle_request(WiseAgentMessage(message=message, sender=self.name, context_name=self._ctx))
self._cond.wait()
return self._response.message

Expand All @@ -79,7 +82,7 @@ def process_request(self, request: WiseAgentMessage,
no string response yet
"""
print(f"AssistantAgent: process_request: {request}")
WiseAgentRegistry.get_or_create_context("default").append_chat_completion(self._chat_id, {"role": "user", "content": request.message})
WiseAgentRegistry.get_context(request.context_name).append_chat_completion({"role": "user", "content": request.message})
self.send_request(request, self.destination_agent_name)
return None

Expand Down
Loading

0 comments on commit e0e81a3

Please sign in to comment.