uAgent ↔ uAgent Communication
In the uAgents Framework, agents can be triggered in multiple ways using different types of handlers. These handlers act as decorators, enabling agents to execute specific functions based on predefined conditions. In this section, we will explore two handlers. Below are the available handlers which can be used with uAgents:
- on_event()
- on_message()
- on_rest_get()
- on_rest_post()
on_event
Agents can respond to events such as initialization and termination. The startup and shutdown handlers are used by the uAgents library to manage these events, ensuring that specific actions are executed when an agent starts or stops.
on_event("startup")
@agent.on_event("startup")
async def introduce_agent(ctx: Context):
ctx.logger.info(f"Hello, I'm agent {agent.name} and my address is {agent.address}.")
...
on_event("shutdown")
@agent.on_event("shutdown")
async def introduce_agent(ctx: Context):
ctx.logger.info(f"Hello, I'm agent {agent.name} and I am shutting down")
...
on_message()
In this section, we will explore the on_message() decorator, which allows us to send messages between microservice agents in a structured way. We will create two microservice agents and enable them to communicate with each other.
Let's create agent1 who will send a message to agent2 on startup.
uAgent1 Script
Please remember to have the uagents package installed in the terminal in order to create and run uAgents.
from uagents import Agent, Context, Model
# Data model (envolope) which you want to send from one agent to another
class Message(Model):
message : str
field : int
my_first_agent = Agent(
name = 'My First Agent',
port = 5050,
endpoint = ['http://localhost:5050/submit']
)
second_agent = 'your_second_agent_address'
@my_first_agent.on_event('startup')
async def startup_handler(ctx : Context):
ctx.logger.info(f'My name is {ctx.agent.name} and my address is {ctx.agent.address}')
await ctx.send(second_agent, Message(message = 'Hi Second Agent, this is the first agent.'))
if __name__ == "__main__":
my_first_agent.run()
uAgent2 Script
from uagents import Agent, Context, Model
class Message(Model):
message : str
my_second_agent = Agent(
name = 'My Second Agent',
port = 5051,
endpoint = ['http://localhost:5051/submit']
)
@my_second_agent.on_event('startup')
async def startup_handler(ctx : Context):
ctx.logger.info(f'My name is {ctx.agent.name} and my address is {ctx.agent.address}')
@my_second_agent.on_message(model = Message)
async def message_handler(ctx: Context, sender : str, msg: Message):
ctx.logger.info(f'I have received a message from {sender}.')
ctx.logger.info(f'I have received a message {msg.message}.')
if __name__ == "__main__":
my_second_agent.run()
We need to define the data Model which is class Message
in both the sender and receiver agents. The receiving agent must include an on_message
handler that correctly implements this data model. It is crucial to ensure that both agents use the exact same data model for seamless communication.
Running agents
Now that we have created both agents, let's run them in separate terminals. Agent 2 is on the receiver's end so we have to start agent2 first. Whenever agent1 is started it will send message to agent 2 and it will receive and handle the message.
Terminal 1
python3 agent2.py
Terminal 2
python3 agent1.py