Package: mango-agents
mangoPyPi|Read the Docs|Github|mailNote:This project is still in an early development stage.
We appreciate constructive feedback and suggestions for improvement.mango (modular pythonagent framework) is a python library for multi-agent systems (MAS).
It is written on top of asyncio and is released under the MIT license.mango allows the user to create simple agents with little effort and in the same time offers options
to structure agents with complex behaviour.The main features of mango are:Container mechanism to speedup local message exchangeMessage definition based on the FIPA ACL standardStructuring complex agents with loose coupling and agent rolesBuilt-in codecs: JSON and protobufSupports communication between agents directly via TCP or via an external MQTT brokerA detailed documentation for this project can be found atmango-agents.readthedocs.ioInstallationmangorequires Python >= 3.8 and runs on Linux, OSX and Windows.For installation of mango you should usevirtualenvwhich can create isolated Python environments for different projects.It is also recommended to installvirtualenvwrapperwhich makes it easier to manage different virtual environments.Once you have created a virtual environment you can just runpipto install it:$ pip install mango-agentsGetting started####Creating an agentIn our first example, we'll create a very simple agent that simply prints the content of
all messages it receives:frommangoimportAgentclassRepeatingAgent(Agent):def__init__(self,container):# We must pass a ref of the container to "mango.Agent":super().__init__(container)print(f"Hello world! My id is{self.aid}.")defhandle_message(self,content,meta):# This method defines what the agent will do with incoming messages.print(f"Received a message with the following content:{content}")Agents must be a subclass ofAgent. This base class needs
a reference to the container the agents live in, so you must forward
acontainerargument to it if you override__init__().####Creating a containerAgents live in a container, so we need to know how to create a mango container.
The container is responsible for message exchange between agents.# Containers need to be started via a factory function.# This method is a coroutine so it needs to be called using the# await statementfirst_container=awaitcreate_container(addr=('localhost',5555))This is how a container is created. Since the methodcreate_container()is acoroutinewe need to await its result.Running your first agent within a containerTo put it all together we will wrap the creation of a container and the agent into a coroutine
and execute it usingasyncio.run().
The following script will create a RepeatingAgent
and let it run within a container for three seconds and
then shutdown the container:importasynciofrommangoimportAgent,create_containerclassRepeatingAgent(Agent):def__init__(self,container):# We must pass a ref. to the container to "mango.Agent":super().__init__(container)print(f"Hello world! My id is{self.aid}.")defhandle_message(self,content,meta):# This method defines what the agent will do with incoming messages.print(f"Received a message with the following content:{content}")asyncdefrun_container_and_agent(addr,duration):first_container=awaitcreate_container(addr=addr)first_agent=RepeatingAgent(first_container)awaitasyncio.sleep(duration)awaitfirst_agent.shutdown()awaitfirst_container.shutdown()asyncio.run(run_container_and_agent(addr=('localhost',5555),duration=3))The only output you should see isHello world! My id is agent0., because
the agent does yet not receive any messages.Creating a proactive AgentLet's implement another agent that is able to send a hello world message
to another agent:frommangoimportAgentfrommango.util.schedulingimportInstantScheduledTaskclassHelloWorldAgent(Agent):def__init__(self,container,other_addr,other_id):super().__init__(container)self.schedule_instant_acl_message(receiver_addr=other_addr,receiver_id=other_id,content="Hello world!"))defhandle_message(self,content,meta):print(f"Received a message with the following content:{content}")Connecting two agentsWe can now connect an instance of a HelloWorldAgent with an instance of a RepeatingAgent and run them.importasynciofrommangoimportAgent,create_containerfrommango.util.schedulingimportInstantScheduledTaskclassRepeatingAgent(Agent):def__init__(self,container):# We must pass a ref. to the container to "mango.Agent":super().__init__(container)print(f"Hello world! My id is{self.aid}.")defhandle_message(self,content,meta):# This method defines what the agent will do with incoming messages.print(f"Received a message with the following content:{content}")classHelloWorldAgent(Agent):def__init__(self,container,other_addr,other_id):super().__init__(container)self.schedule_instant_acl_message(receiver_addr=other_addr,receiver_id=other_id,content="Hello world!"))defhandle_message(self,content,meta):print(f"Received a message with the following content:{content}")asyncdefrun_container_and_two_agents(first_addr,second_addr):first_container=awaitcreate_container(addr=first_addr)second_container=awaitcreate_container(addr=second_addr)first_agent=RepeatingAgent(first_container)second_agent=HelloWorldAgent(second_container,first_container.addr,first_agent.aid)awaitasyncio.sleep(1)awaitfirst_agent.shutdown()awaitsecond_agent.shutdown()awaitfirst_container.shutdown()awaitsecond_container.shutdown()deftest_second_example():asyncio.run(run_container_and_two_agents(first_addr=('localhost',5555),second_addr=('localhost',5556)))You should now see the following output:Hello world! My id is agent0.Received a message with the following content: Hello world!You have now successfully created two agents and connected them.SupportDocumentation:mango-agents.readthedocs.ioE-mail:mango@offis.deLicenseDistributed under the MIT license.