Skip to main content
Version: 1.0.2

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_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.

uagent1.py
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

uagent2.py
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

Expected Output

abhi@Fetchs-MacBook-Pro testing % python3 second_agent.py 
INFO: [My Second Agent]: Starting agent with address: agent1qtgc4vqn4ehh88hct0umnnqeg36m5722hc4e63lwy573kjtqee7qg5afmap
INFO: [My Second Agent]: My name is My Second Agent and my address is agent1qtgc4vqn4ehh88hct0umnnqeg36m5722hc4e63lwy573kjtqee7qg5afmap
INFO: [My Second Agent]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A5051&address=agent1qtgc4vqn4ehh88hct0umnnqeg36m5722hc4e63lwy573kjtqee7qg5afmap
INFO: [My Second Agent]: Starting server on http://0.0.0.0:5051 (Press CTRL+C to quit)
INFO: [My Second Agent]: Registration on Almanac API successful
INFO: [My Second Agent]: Almanac contract registration is up to date!
INFO: [My Second Agent]: I have received a message from agent1q0kpqwd5q7akt9utnw540h293zhw063ua90m66rkx5lg98wq4zrjkhmgwd7.
INFO: [My Second Agent]: I have received a message Hi Second Agent, this is the first agent..
abhi@Fetchs-MacBook-Pro testing % python3 first_agent.py
INFO: [My First Agent]: Starting agent with address: agent1q0kpqwd5q7akt9utnw540h293zhw063ua90m66rkx5lg98wq4zrjkhmgwd7
INFO: [My First Agent]: My name is My First Agent and my address is agent1q0kpqwd5q7akt9utnw540h293zhw063ua90m66rkx5lg98wq4zrjkhmgwd7
INFO: [My First Agent]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A5050&address=agent1q0kpqwd5q7akt9utnw540h293zhw063ua90m66rkx5lg98wq4zrjkhmgwd7
INFO: [My First Agent]: Starting server on http://0.0.0.0:5050 (Press CTRL+C to quit)
INFO: [My First Agent]: Registration on Almanac API successful
INFO: [My First Agent]: Almanac contract registration is up to date!

In this way we can establish a communicate between two uAgents.