We demonstrate four different ways of implementing a multi-agent chat system using APPL. The four implementations are:
Resume(Recommended): Uses the resume feature of the APPL function to store the state of the conversation. The context is stored in the instance of the class with a private variable.
History: Uses a variable to store the history of the conversation. The history is stored in the beginning and updated at the end of each turn.
Generator: Utilizes Python's yield and generator capabilities, enabling functions to produce outputs in stages, temporarily hand over control, and later resume where they left off by calling the send method, which also allows for new inputs to be passed to the yield statement.
Same Context: Creates a context in the init function and uses the same context throughout the conversation.
fromtypingimportOptionalimportapplfromapplimportAIRole,PromptContext,SystemMessage,gen,ppl,recordsappl.init()classAgent(object):def__init__(self,name:Optional[str]=None):self._name=nameself._setup()@ppldef_setup(self):ifself._name:SystemMessage(f"Your name is {self._name}.")self.chat(None)# setup the context@ppl(ctx="resume")# The context is resumed from the last call to chatdefchat(self,msg:Optional[str]):ifmsgisNone:# first call to setup the contextreturnmsg# add to the promptwithAIRole():(reply:=gen(max_tokens=50))# generate reply and add to the promptreturnreplyalice=Agent("Alice")bob=Agent("Bob")msg="Hello"foriinrange(2):msg=str(alice.chat(msg))print("Alice:",msg)msg=str(bob.chat(msg))print("Bob:",msg)
fromtypingimportOptionalimportapplfromapplimportAIRole,PromptContext,SystemMessage,gen,ppl,recordsappl.init()classAgent(object):def__init__(self,name:Optional[str]=None):self._name=nameself._history=self._setup()# initialize history@ppldef_setup(self):ifself._name:SystemMessage(f"Your name is {self._name}.")returnrecords()@ppldefchat(self,msg):self._history# retrieve historymsg# add to the promptwithAIRole():(reply:=gen(max_tokens=50))# generate reply and add to the promptself._history=records()# update historyreturnreplyalice=Agent("Alice")bob=Agent("Bob")msg="Hello!"foriinrange(2):msg=str(alice.chat(msg))print("Alice:",msg)msg=str(bob.chat(msg))print("Bob:",msg)
fromtypingimportOptionalimportapplfromapplimportAIRole,PromptContext,SystemMessage,gen,ppl,recordsappl.init()classAgent(object):def__init__(self,name:Optional[str]=None):self._name=nameself.chat=self._chat_generator().send@ppldef_setup(self):ifself._name:SystemMessage(f"Your name is {self._name}.")returnrecords()@ppl(auto_prime=True)# auto prime the generatordef_chat_generator(self):self._setup()reply=NonewhileTrue:yieldreply# yield and receive messageswithAIRole():(reply:=gen(max_tokens=50))alice=Agent("Alice")bob=Agent("Bob")msg="Hello!"foriinrange(2):msg=str(alice.chat(msg))print("Alice:",msg)msg=str(bob.chat(msg))print("Bob:",msg)
fromabcimportABC,abstractmethodfromtypingimportOptionalimportapplfromapplimportAIRole,PromptContext,SystemMessage,gen,ppl,recordsfromappl.funcimportwrapsappl.init()classAgentBase(ABC):def__init__(self):self._ctx=PromptContext()# the context for the agentself._setup(_ctx=self._ctx)# manually provide the shared context@abstractmethod@ppl(ctx="same")def_setup(self):raiseNotImplementedError@abstractmethod@ppl(ctx="same")def_chat(self,msg:str):raiseNotImplementedError@wraps(_chat)defchat(self,*args,**kwargs):# manually provide the shared context self._ctx stored in the instancereturnself._chat(*args,_ctx=self._ctx,**kwargs)setattr(chat,"__isabstractmethod__",False)# set to False since _chat is abstractmethodclassAgent(AgentBase):def__init__(self,name:Optional[str]=None):self._name=namesuper().__init__()@ppl(ctx="same")def_setup(self):# modify the shared contextifself._name:SystemMessage(f"Your name is {self._name}.")@ppl(ctx="same")def_chat(self,msg:str):msgwithAIRole():(reply:=gen(max_tokens=50))returnreplyalice=Agent("Alice")bob=Agent("Bob")msg="Hello!"foriinrange(2):msg=str(alice.chat(msg))print("Alice:",msg)msg=str(bob.chat(msg))print("Bob:",msg)
Hi there! I'm just here to chat and answer any questions you might have. How's your day going?
Alice
Thank you for asking! My day is going well. How about you?
Bob
I'm just a virtual assistant, so I don't have feelings, but I'm here to help you with anything you need. Is there anything specific you'd like to talk about or ask me?
The conversation seen by Alice:
Role
Message
System
Your name is Alice.
User
Hello!
Assistant
Hello! How can I assist you today?
User
Hi there! I'm just here to chat and answer any questions you might have. How's your day going?
Assistant
Thank you for asking! My day is going well. How about you?
The conversation seen by Bob:
Role
Message
System
Your name is Bob.
User
Hello! How can I assist you today?
Assistant
Hi there! I'm just here to chat and answer any questions you might have. How's your day going?
User
Thank you for asking! My day is going well. How about you?
Assistant
I'm just a virtual assistant, so I don't have feelings, but I'm here to help you with anything you need. Is there anything specific you'd like to talk about or ask me?