Skip to Content

Communicating with other agents

Introduction

Communication is an essential feature within any agents network. Communication allows agents to work together, exchange information, and forms an organic marketplace.

Let’s start with local communication. This is the first step you would need to undertake to familiarize yourself with the code syntax we will be using in the remote communication section.

Imports needed

Agents: Local Communication

Walk-through

The first step to better understand how agents communicate is to introduce how 2 agents perform a local communication. Let’s consider a basic example in which two agents say hello to each other.

First of all, let’s create a Python script for the agent1 for this task:

windows
echo. > agent_sigmar.py

Let’s create a Python script for the agent2 for this task:

windows
echo. > agent_slaanesh.py

Agent 1: Sigmar

1. Define the Message Model

This defines a simple message structure using the Model class from uAgents. The message field is a string that will be exchanged between agents. This ensures that both agents can communicate using a predefined format.

agent_sigmar.py
from uagents import Agent, Context, Model class Message(Model): message: str

2. Create Sigmar Agent

Here, the Sigmar agent is initialized with a unique name and seed (recovery phrase) to establish its identity. It runs on port 8000 and listens for messages at the specified endpoint. This setup enables the agent to send and receive messages in a local environment.

agent_sigmar.py
sigmar = Agent(name="sigmar", seed="sigmar recovery phrase", port=8000, endpoint=["http://localhost:8000/submit"])

3. Sigmar Sends a Message Every 3 Seconds

A periodic function is defined, which executes every 3 seconds. It sends a message, “hello there sigmar”, to the Slaanesh agent using its unique address. This ensures that communication is continuously initiated from Sigmar without requiring any manual input.

agent_sigmar.py
SLAANESH_ADDRESS = < SLAANESH ADDRESS > @sigmar.on_interval(period=3.0) async def send_message(ctx: Context): await ctx.send(SLAANESH_ADDRESS, Message(message="hello there sigmar"))

4. Handle Incoming Messages

This function triggers whenever Slaanesh receives a message matching the Message model. It logs the sender’s address and message content, then replies with “hello there slaanesh” to the sender. This ensures a two-way communication flow between the agents.

agent_sigmar.py
@sigmar.on_message(model=Message) async def sigmar_message_handler(ctx: Context, sender: str, msg: Message): ctx.logger.info(f"Received message from {sender}: {msg.message}")

5. Start the Agent

The agent is started using sigmar.run(), making it continuously run and process messages. This ensures that the agent remains active and responsive to incoming communications.

agent_sigmar.py
if __name__ == "__main__": sigmar.run()

Agent 2: Slaanesh

1. Define the Message Model

The same message structure is defined for Slaanesh, ensuring compatibility with Sigmar. This consistency allows both agents to understand the messages they exchange.

agent_slaanesh.py
from uagents import Agent, Context, Model class Message(Model): message: str

2. Create Slaanesh Agent

The Slaanesh agent is initialized with its own name and seed for identity. It runs on port 8001 and listens for messages at its specified endpoint. This setup enables Slaanesh to receive messages from Sigmar and respond appropriately.

agent_slaanesh.py
slaanesh = Agent(name="slaanesh", seed="slaanesh recovery phrase", port=8001, endpoint=["http://localhost:8001/submit"])

3. Handle Incoming Messages & Reply

This function triggers whenever Slaanesh receives a message matching the Message model. It logs the sender’s address and message content, then replies with “hello there slaanesh” to the sender. This ensures a two-way communication flow between the agents.

agent_slaanesh.py
@slaanesh.on_message(model=Message) async def slaanesh_message_handler(ctx: Context, sender: str, msg: Message): ctx.logger.info(f"Received message from {sender}: {msg.message}") await ctx.send(sender, Message(message="hello there slaanesh"))

4. Start the Agent

The agent is started using slaanesh.run(), making it continuously run and process messages. This keeps the agent active and ready to communicate with Sigmar whenever it receives a message.

agent_slaanesh.py
if __name__ == "__main__": slaanesh.run()

Complete Script for Both Agents

These two scripts together create an autonomous request-response loop between two local agents, continuously exchanging messages. To ensure smooth communication between Sigmar and Slaanesh, you must start the Slaanesh agent first before running Sigmar.

agent_sigmar.py
from uagents import Agent, Context, Model class Message(Model): message: str sigmar = Agent(name="sigmar", seed="sigmar recovery phrase", port=8000, endpoint=["http://localhost:8000/submit"]) SLAANESH_ADDRESS = < SLAANESH ADDRESS > @sigmar.on_interval(period=3.0) async def send_message(ctx: Context): await ctx.send(SLAANESH_ADDRESS, Message(message="hello there slaanesh")) @sigmar.on_message(model=Message) async def sigmar_message_handler(ctx: Context, sender: str, msg: Message): ctx.logger.info(f"Received message from {sender}: {msg.message}") if __name__ == "__main__": sigmar.run()
agent_slaanesh.py
from uagents import Agent, Context, Model class Message(Model): message: str slaanesh = Agent(name="slaanesh", seed="slaanesh recovery phrase", port=8001, endpoint=["http://localhost:8001/submit"]) @slaanesh.on_message(model=Message) async def slaanesh_message_handler(ctx: Context, sender: str, msg: Message): ctx.logger.info(f"Received message from {sender}: {msg.message}") await ctx.send(sender, Message(message="hello there sigmar")) if __name__ == "__main__": slaanesh.run()

Steps to Run the Agents in Order:

  1. Start Slaanesh first in one terminal window:
python agent_slaanesh.py
  1. Then start Sigmar in another terminal window:
python agent_sigmar.py

The output would be:

for agent agent_slaanesh.py

INFO: [slaanesh]: Starting agent with address: agent1qddw8cfn685e3p082lcn9dxe63yrqf03s77puv4d0as8a4j7c84s572juzj INFO: [slaanesh]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8001&address=agent1qddw8cfn685e3p082lcn9dxe63yrqf03s77puv4d0as8a4j7c84s572juzj INFO: [slaanesh]: Starting server on http://0.0.0.0:8001 (Press CTRL+C to quit) INFO: [uagents.registration]: Registration on Almanac API successful INFO: [uagents.registration]: Almanac contract registration is up to date! INFO: [slaanesh]: Received message from agent1qdtmapxwfljj2xwz8yqpljd75tmnxjjdmrta86q68aqf5r9d7nw7kqyt40p: hello there sigmar INFO: [slaanesh]: Received message from agent1qdtmapxwfljj2xwz8yqpljd75tmnxjjdmrta86q68aqf5r9d7nw7kqyt40p: hello there sigmar

for agent agent_sigmar.py

INFO: [sigmar]: Starting agent with address: agent1qdtmapxwfljj2xwz8yqpljd75tmnxjjdmrta86q68aqf5r9d7nw7kqyt40p INFO: [sigmar]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8000&address=agent1qdtmapxwfljj2xwz8yqpljd75tmnxjjdmrta86q68aqf5r9d7nw7kqyt40p INFO: [sigmar]: Starting server on http://0.0.0.0:8000 (Press CTRL+C to quit) INFO: [uagents.registration]: Registration on Almanac API successful INFO: [uagents.registration]: Almanac contract registration is up to date! INFO: [sigmar]: Received message from agent1qddw8cfn685e3p082lcn9dxe63yrqf03s77puv4d0as8a4j7c84s572juzj: hello there slaanesh INFO: [sigmar]: Received message from agent1qddw8cfn685e3p082lcn9dxe63yrqf03s77puv4d0as8a4j7c84s572juzj: hello there slaanesh

However, these agents can only communicate with each other on their local network.

Last updated on