Skip to content

Efficient Communication Between Swarm Agents via RabbitMQ

Summary: Code doing agent handoff between RabbitMQ inhabitants. Agent in haiku_send.py sends a message, agent in haiku_rcv.py receives it and runs in thru ollama llm model. The gist is simple: send message to queue whose name is target agent’s name. Consumer catches message according to queue name and callback runs target agent with message sent from source agent. It is very simple. Only thing to take care of is assuring target agent name should be the same in publisher and consumer.

Details

In the world of agent-based systems, seamless communication between agents is crucial for their collective intelligence and efficiency. This blog post delves into how we can leverage RabbitMQ as a message broker for facilitating communication between Swarm agents using Python. We’ll explore the publisher-consumer model through example scripts: haiku_send.py and haiku_rcv.py, and we’ll also see how RabbitMQ can be easily set up using Docker Compose.

Look in my github repo for work in progress.

https://github.com/sq5rix/swarmq

Why RabbitMQ?

RabbitMQ is a high-performance message broker that facilitates messaging between distributed systems. It is widely used due to its robustness, scalability, and support for multiple messaging protocols. It fits perfectly into the architecture of swarm systems where agents need to communicate consistently and efficiently.

Setting Up RabbitMQ with Docker Compose

Docker Compose simplifies the process of setting up and managing RabbitMQ. Here is a snippet from our docker-compose.yml file:

services:
  rabbitmq:
    image: rabbitmq:3-management
    hostname: rabbitmq
    restart: always
    ports:
      - "5672:5672" # AMQP protocol port
      - "15672:15672" # Management UI port
    environment:
      - RABBITMQ_DEFAULT_USER=${RABBITMQ_USER:-guest}
      - RABBITMQ_DEFAULT_PASS=${RABBITMQ_PASSWORD:-guest}
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    healthcheck:
      test: ["CMD", "rabbitmqctl", "status"]
      interval: 30s
      timeout: 10s
      retries: 5
volumes:
  rabbitmq_data:
    driver: local

This configuration pulls the RabbitMQ image with management tools, sets up necessary ports, and configures health checks. Once you run docker-compose up, RabbitMQ is ready to handle messages.

The Publisher: haiku_send.py

The publisher script is responsible for sending messages to the message broker. Here’s a snippet showing how we do it:

import json
from swarm import Agent
from rabbit import publish
MODEL = "llama3.2:latest"
client = Swarm()
english_agent_name = "English_Agent"
spanish_agent_name = "Spanish_Agent"
def transfer_to_spanish_agent():
    """Transfer Spanish speaking users immediately."""
    return publish(spanish_agent_name, json.dumps(messages))
english_agent = Agent(
    name=english_agent_name,
    model=MODEL,
    instructions="You only speak English.",
)
english_agent.functions.append(transfer_to_spanish_agent)
messages = [{"role": "user", "content": "Hola. ¿Como estás?"}]
response = client.run(agent=english_agent, messages=messages)
print(english_agent)

Here, we define an English_Agent which only speaks English. However, when it encounters a Spanish message, it uses the transfer_to_spanish_agent function to publish the message to RabbitMQ, targeting the Spanish agent.

The Consumer: haiku_rcv.py

The consumer script listens to messages from the message broker and processes them accordingly:

import json
from swarm import Agent, Swarm
from rabbit import consume
MODEL = "llama3.2:latest"
client = Swarm()
spanish_agent_name = "Spanish_Agent"
spanish_agent = Agent(
    name=spanish_agent_name,
    model=MODEL,
    instructions="You only speak Spanish.",
)
def run_agent(body):
    print(body)
    response = client.run(
        agent=spanish_agent, messages=json.loads(body.decode("utf-8"))
    )
    print(response.messages[-1]["content"])
consume(spanish_agent_name, run_agent)

This script defines a Spanish_Agent that processes messages from the spanish_agent_name queue. It decodes the message, runs the agent, and prints the response.

Conclusion

By using RabbitMQ as a message broker, we can effectively manage communication between Swarm agents. This setup allows for efficient message passing, which is crucial for the real-time operation of agent-based systems. The use of Docker Compose further simplifies the deployment process, making it accessible even for those new to RabbitMQ.

This approach not only enhances communication but also allows for scaling, monitoring, and maintaining the system with ease.

RabbitMQ, Docker, Docker Compose, Swarm Agents, Agent-Based Systems, Python, Message Broker, Publisher-Consumer Model, Distributed Systems

With this setup, you are well on your way to building a robust, scalable agent-based system. Happy coding! ■

Pretty girl thinking about RabbitMQ and Agent swarm integration

Leave a Reply

Your email address will not be published. Required fields are marked *