Utilizing the Agentverse Mailroom feature
Introduction
The Agentverse assists you in setting up mailboxes for local and Hosted Agents , allowing them to have a two-way communication with each other without the need to be constantly online and without requiring your constant presence to operate.
Prerequisites
Make sure you have read the following resources before going on with this guide:
- Quick Start Guide for uAgents Framework
- Creating your first agent
- Agents address
- Almanac contract
- Register in Almanac
- Communicating with other agents
- Agents protocols
- Exchange protocol
- Agent Mailboxes
Local Agent setup
Let's now start by creating a local agent named alice
with a handle_message()
function using an @agent.on_message()
decorator to handle messages received by other agents and matching the Message
class:
local_agent_setup.pyfrom uagents import Agent, Context, Model class Message(Model): message: str # First generate a secure seed phrase (e.g. https://pypi.org/project/mnemonic/) SEED_PHRASE = "put_your_seed_phrase_here" # Now your agent is ready to join the agentverse! agent = Agent( name="alice", seed=SEED_PHRASE, mailbox=True ) # Copy the address shown below print(f"Your agent's address is: {agent.address}") @agent.on_message(model=Message, replies={Message}) async def handle_message(ctx: Context, sender: str, msg: Message): ctx.logger.info(f"Received message from {sender}: {msg.message}") if __name__ == "__main__": agent.run()
To create a Mailbox for your local Agent, you first need to run it and click on the local Agent Inspector URL provided within the terminal output. You will be redirected towards the Inspector UI. Here, you will be able to see multiple details about your local Agent.
To connect the local Agent with a Mailbox, click the Connect button.
Then, select Mailbox and move on:
At this point, you do not need to do anything. Just click on Finish:
Great! You successfully connected the local Agent via a Mailbox to the Agentverse!
You can then restart your local Agent. You will see a similar terminal output on your end:
INFO: [Alice]: Starting agent with address: agent1qw8jn3nfl2fyyhe7v4x8pfmsge4hs9zqrqw9eq7h7hluzmd0da8z7j0uacx INFO: [Alice]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8002&address=agent1qw8jn3nfl2fyyhe7v4x8pfmsge4hs9zqrqw9eq7h7hluzmd0da8z7j0uacx INFO: [Alice]: Starting server on http://0.0.0.0:8002 (Press CTRL+C to quit) INFO: [Alice]: Starting mailbox client for https://agentverse.ai INFO: [Alice]: Mailbox access token acquired INFO: [Alice]: Registration on Almanac API successful INFO: [Alice]: Registering on almanac contract... INFO: [Alice]: Registering on almanac contract...complete INFO: [mailbox]: Successfully registered as mailbox agent in Agentverse
You can then restart your Agent; now your Agent doesn't need to run all the time as their messages will be waiting for them for when they come back online.
Agentverse Agent setup
Now create an Agentverse Hosted Agent, bob
, by selecting + New Agent in the My Agents tab in the Agentverse (opens in a new tab). Then, select a blank script and add the following code into it:
agentverse_agent_setup.pyfrom uagents import Agent, Context, Model class Message(Model): message: str # Now let's create the Agentverse agent agent = Agent( name="bob", seed=SEED_PHRASE, endpoint="http://127.0.0.1:8001/submit", ) @agent.on_interval(period=2.0) async def send_message(ctx: Context): ctx.logger.info("Sending message to alice") await ctx.send(ALICE_ADDRESS, Message(message="hello there alice")) @agent.on_message(model=Message, replies=set()) async def on_message(ctx: Context, sender: str, msg: Message): ctx.logger.info(f"Received message from {sender}: {msg.message}") if __name__ == "__main__": agent.run()
Next, run bob on the Agentverse.
Expected output
You will see something like the following depending on the agent considered:
-
Agentverse Agent output:
2024-12-18 16:41:52 Debug System Starting agent... 2024-12-18 16:41:53 Info Agent Creating wrapper for preloaded 'agent' instance (Ignoring kwargs={'name': 'bob', 'seed': 'put_your_seed_phrase_here', 'endpoint': 'http://127.0.0.1:8001/submit'}) 2024-12-18 16:41:54 Debug System Registered to Almanac api fast track 2024-12-18 16:41:55 Debug System Successfully started agent 2024-12-18 16:41:56 Info System Interval 0 period set to 5 2024-12-18 16:41:57 Debug System Registered to Almanac api fast track 2024-12-18 16:41:58 Debug System Successfully registered agent on Almanac contract 2024-12-18 16:41:59 Debug System Envelope sent to agent1qfa53drat8rzau90u4494gx5mhj3v87tm4t5cuzkd7gkegxcm5vx5pku7kf 2024-12-18 16:42:00 Info Agent Sending message to alice 2024-12-18 16:42:01 Debug System Envelope sent to agent1qfa53drat8rzau90u4494gx5mhj3v87tm4t5cuzkd7gkegxcm5vx5pku7kf 2024-12-18 16:42:02 Info Agent Sending message to alice 2024-12-18 16:42:03 Debug System Envelope sent to agent1qfa53drat8rzau90u4494gx5mhj3v87tm4t5cuzkd7gkegxcm5vx5pku7kf
-
Local Agent output:
Your agent's address is: agent1qfa53drat8rzau90u4494gx5mhj3v87tm4t5cuzkd7gkegxcm5vx5pku7kf INFO: [alice]: Starting agent with address: agent1qfa53drat8rzau90u4494gx5mhj3v87tm4t5cuzkd7gkegxcm5vx5pku7kf INFO: [alice]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8000&address=agent1qfa53drat8rzau90u4494gx5mhj3v87tm4t5cuzkd7gkegxcm5vx5pku7kf INFO: [alice]: Starting server on http://0.0.0.0:8000 (Press CTRL+C to quit) INFO: [alice]: Starting mailbox client for https://agentverse.ai INFO: [alice]: Registration on Almanac API successful INFO: [alice]: Registering on almanac contract... INFO: [alice]: Registering on almanac contract...complete INFO: [alice]: Mailbox access token acquired INFO: [alice]: Received message from agent1qvzc3svtjv6dt9pql8ec7sfeuwfjmdg6ptafs9zc55ppvdwl5fe82j5m70v: hello there alice INFO: [alice]: Received message from agent1qvzc3svtjv6dt9pql8ec7sfeuwfjmdg6ptafs9zc55ppvdwl5fe82j5m70v: hello there alice INFO: [alice]: Received message from agent1qvzc3svtjv6dt9pql8ec7sfeuwfjmdg6ptafs9zc55ppvdwl5fe82j5m70v: hello there alice INFO: [alice]: Received message from agent1qvzc3svtjv6dt9pql8ec7sfeuwfjmdg6ptafs9zc55ppvdwl5fe82j5m70v: hello there alice