networkkit


Namenetworkkit JSON
Version 0.0.2 PyPI version JSON
download
home_pagehttps://github.com/japanvik/networkkit
SummaryA simple communication framework for agents using pubsub and http
upload_time2025-01-04 20:38:29
maintainerNone
docs_urlNone
authorVikram Kumar
requires_python>=3.8
licenseApache-2.0
keywords network agents pubsub framework communication
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # NetworkKit

NetworkKit is a simple yet powerful communication framework designed to streamline the interaction between distributed agents. Originally built for AI agent swarms, NetworkKit leverages both publish/subscribe (pub/sub) and HTTP protocols to enable seamless and scalable message exchange across diverse systems. Whether you are building a network of IoT devices, a microservices architecture, or orchestrating interactions within AI agent swarms, NetworkKit provides the necessary tools and abstractions to ensure reliable and efficient communication.

## Features

- **Message Handling**: Define and manage various message types and content, including support for multiple message categories such as chat, system updates, and sensor data.
- **ZeroMQ Integration**: Efficiently publish and subscribe to messages using ZeroMQ, enabling high-performance asynchronous communication.
- **HTTP Integration**: Send and receive messages via HTTP using FastAPI, providing flexibility for web-based communication.
- **Pydantic Models**: Leverage Pydantic for robust data validation and serialization, ensuring data integrity and ease of use.
- **Asynchronous Communication**: Support for asynchronous message handling, allowing non-blocking operations and improved performance in distributed systems.
- **Extensible Architecture**: Easily extend the framework to support additional protocols or custom message handling logic.

## Installation

To install NetworkKit, use pip to install the package:

```bash
pip install networkkit
```

## Usage

### Message Data Structures

The `messages.py` module defines the message data structures used for communication within the NetworkKit framework.

#### Message

The `Message` class is a Pydantic model representing a message object exchanged through the data bus. It provides a structured way to define and validate the content of messages, ensuring consistency and reliability in communication.

Attributes:
- `source` (str): The source of the message (e.g., agent name, sensor name).
- `to` (str): The intended recipient of the message (e.g., agent name, or 'ALL' for broadcast).
- `content` (str): The actual message content in string format.
- `created_at` (str, optional): The timestamp of when the message was created. If not provided, it will be automatically set to the current time by the databus.
- `message_type` (MessageType): The type of message as defined by the `MessageType` enumeration.

Example:
```python
from networkkit.messages import Message, MessageType

message = Message(
    source="Agent1",
    to="Agent2",
    content="Hello, Agent2!",
    message_type=MessageType.CHAT
)
```

#### MessageType

An enumeration class representing the different message types used in NetworkKit:

- `HELO`: Indicates a login request or checking if the agent “to” is available.
- `ACK`: Response to a HELO request, indicating the agent is available.
- `CHAT`: Text message intended for conversation.
- `SYSTEM`: System message coming from the data hub.
- `SENSOR`: Messages for data coming from sensors.
- `ERROR`: Error messages.
- `INFO`: A communication for agents on any non conversational or sensor data.

### Network Module

The `network.py` module provides interfaces and implementations for message sending and receiving.

#### Subscriber Protocol

Defines the interface for subscribers to the bus that can recieve and handle Messages. Subscribers must implement the following methods:

- `handle_message(self, message: Message) -> Any`: Asynchronous method for handling received messages.
- `is_intended_for_me(self, message: Message) -> bool`: Method to determine if a message is intended for this subscriber.

#### MessageSender Protocol

Defines the interface for senders that send messages over the network. Implementations must provide the following method:

- `send_message(self, message: Message) -> Any`: Method to send a message over the network.

#### ZMQMessageReceiver

Class to receive messages using ZeroMQ and distribute them to registered subscribers. Implementors of the Suscriber protocol can register to subscribe to this receiver. It establishes a ZeroMQ subscriber socket, listens for messages, and distributes them to registered subscribers based on their `is_intended_for_me` method.

#### HTTPMessageSender

Class to send messages over HTTP using the `requests` library. It sends messages as JSON payloads to a specified HTTP endpoint.

### Data Bus

The `databus.py` module provides a data bus service for publishing messages using ZeroMQ and a FastAPI interface (HTTP) for receiving messages. Incoming Messages via the HTTP endpoint will be published via the ZeroMQ for subscribers to pick up.

#### Running the Data Bus

1. Execute the script from the console:

```bash
python -m networkkit.databus
```

This will start the FastAPI server and the ZeroMQ publisher.


## Example Code

Here is an example of how to use NetworkKit to send and receive messages:
```python
from networkkit.messages import Message, MessageType
from networkkit.network import ZMQMessageReceiver, HTTPMessageSender

# Example message
message = Message(
    source="Agent1",
    to="Agent2",
    content="Hello, Agent2!",
    message_type=MessageType.CHAT
)

# Sending a message over HTTP
sender = HTTPMessageSender(publish_address="http://127.0.0.1:8000")
response = sender.send_message(message)
print(response.status_code)

# Receiving messages with ZMQ
receiver = ZMQMessageReceiver(subscribe_address="tcp://127.0.0.1:5555")

class MySubscriber:
    name = "Agent2"

    async def handle_message(self, message: Message):
        print(f"Received message: {message.content}")

    def is_intended_for_me(self, message: Message) -> bool:
        return message.to == self.name or message.to == "ALL"

receiver.register_subscriber(MySubscriber())
asyncio.run(receiver.start())
```

## Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

## License

This project is licensed under the terms of the MIT license. See the LICENSE file for details.

## Authors
	•	Vikram Kumar - vik@japanvik.net


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/japanvik/networkkit",
    "name": "networkkit",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "network agents, pubsub, framework, communication",
    "author": "Vikram Kumar",
    "author_email": "vik@japanvik.net",
    "download_url": "https://files.pythonhosted.org/packages/ad/62/b97e45a913d3dd9893f17035088d8069bc0e42dc4125976244e009a8f611/networkkit-0.0.2.tar.gz",
    "platform": null,
    "description": "# NetworkKit\n\nNetworkKit is a simple yet powerful communication framework designed to streamline the interaction between distributed agents. Originally built for AI agent swarms, NetworkKit leverages both publish/subscribe (pub/sub) and HTTP protocols to enable seamless and scalable message exchange across diverse systems. Whether you are building a network of IoT devices, a microservices architecture, or orchestrating interactions within AI agent swarms, NetworkKit provides the necessary tools and abstractions to ensure reliable and efficient communication.\n\n## Features\n\n- **Message Handling**: Define and manage various message types and content, including support for multiple message categories such as chat, system updates, and sensor data.\n- **ZeroMQ Integration**: Efficiently publish and subscribe to messages using ZeroMQ, enabling high-performance asynchronous communication.\n- **HTTP Integration**: Send and receive messages via HTTP using FastAPI, providing flexibility for web-based communication.\n- **Pydantic Models**: Leverage Pydantic for robust data validation and serialization, ensuring data integrity and ease of use.\n- **Asynchronous Communication**: Support for asynchronous message handling, allowing non-blocking operations and improved performance in distributed systems.\n- **Extensible Architecture**: Easily extend the framework to support additional protocols or custom message handling logic.\n\n## Installation\n\nTo install NetworkKit, use pip to install the package:\n\n```bash\npip install networkkit\n```\n\n## Usage\n\n### Message Data Structures\n\nThe `messages.py` module defines the message data structures used for communication within the NetworkKit framework.\n\n#### Message\n\nThe `Message` class is a Pydantic model representing a message object exchanged through the data bus. It provides a structured way to define and validate the content of messages, ensuring consistency and reliability in communication.\n\nAttributes:\n- `source` (str): The source of the message (e.g., agent name, sensor name).\n- `to` (str): The intended recipient of the message (e.g., agent name, or 'ALL' for broadcast).\n- `content` (str): The actual message content in string format.\n- `created_at` (str, optional): The timestamp of when the message was created. If not provided, it will be automatically set to the current time by the databus.\n- `message_type` (MessageType): The type of message as defined by the `MessageType` enumeration.\n\nExample:\n```python\nfrom networkkit.messages import Message, MessageType\n\nmessage = Message(\n    source=\"Agent1\",\n    to=\"Agent2\",\n    content=\"Hello, Agent2!\",\n    message_type=MessageType.CHAT\n)\n```\n\n#### MessageType\n\nAn enumeration class representing the different message types used in NetworkKit:\n\n- `HELO`: Indicates a login request or checking if the agent \u201cto\u201d is available.\n- `ACK`: Response to a HELO request, indicating the agent is available.\n- `CHAT`: Text message intended for conversation.\n- `SYSTEM`: System message coming from the data hub.\n- `SENSOR`: Messages for data coming from sensors.\n- `ERROR`: Error messages.\n- `INFO`: A communication for agents on any non conversational or sensor data.\n\n### Network Module\n\nThe `network.py` module provides interfaces and implementations for message sending and receiving.\n\n#### Subscriber Protocol\n\nDefines the interface for subscribers to the bus that can recieve and handle Messages. Subscribers must implement the following methods:\n\n- `handle_message(self, message: Message) -> Any`: Asynchronous method for handling received messages.\n- `is_intended_for_me(self, message: Message) -> bool`: Method to determine if a message is intended for this subscriber.\n\n#### MessageSender Protocol\n\nDefines the interface for senders that send messages over the network. Implementations must provide the following method:\n\n- `send_message(self, message: Message) -> Any`: Method to send a message over the network.\n\n#### ZMQMessageReceiver\n\nClass to receive messages using ZeroMQ and distribute them to registered subscribers. Implementors of the Suscriber protocol can register to subscribe to this receiver. It establishes a ZeroMQ subscriber socket, listens for messages, and distributes them to registered subscribers based on their `is_intended_for_me` method.\n\n#### HTTPMessageSender\n\nClass to send messages over HTTP using the `requests` library. It sends messages as JSON payloads to a specified HTTP endpoint.\n\n### Data Bus\n\nThe `databus.py` module provides a data bus service for publishing messages using ZeroMQ and a FastAPI interface (HTTP) for receiving messages. Incoming Messages via the HTTP endpoint will be published via the ZeroMQ for subscribers to pick up.\n\n#### Running the Data Bus\n\n1. Execute the script from the console:\n\n```bash\npython -m networkkit.databus\n```\n\nThis will start the FastAPI server and the ZeroMQ publisher.\n\n\n## Example Code\n\nHere is an example of how to use NetworkKit to send and receive messages:\n```python\nfrom networkkit.messages import Message, MessageType\nfrom networkkit.network import ZMQMessageReceiver, HTTPMessageSender\n\n# Example message\nmessage = Message(\n    source=\"Agent1\",\n    to=\"Agent2\",\n    content=\"Hello, Agent2!\",\n    message_type=MessageType.CHAT\n)\n\n# Sending a message over HTTP\nsender = HTTPMessageSender(publish_address=\"http://127.0.0.1:8000\")\nresponse = sender.send_message(message)\nprint(response.status_code)\n\n# Receiving messages with ZMQ\nreceiver = ZMQMessageReceiver(subscribe_address=\"tcp://127.0.0.1:5555\")\n\nclass MySubscriber:\n    name = \"Agent2\"\n\n    async def handle_message(self, message: Message):\n        print(f\"Received message: {message.content}\")\n\n    def is_intended_for_me(self, message: Message) -> bool:\n        return message.to == self.name or message.to == \"ALL\"\n\nreceiver.register_subscriber(MySubscriber())\nasyncio.run(receiver.start())\n```\n\n## Contributing\n\nContributions are welcome! Please fork the repository and submit a pull request.\n\n## License\n\nThis project is licensed under the terms of the MIT license. See the LICENSE file for details.\n\n## Authors\n\t\u2022\tVikram Kumar - vik@japanvik.net\n\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "A simple communication framework for agents using pubsub and http",
    "version": "0.0.2",
    "project_urls": {
        "Homepage": "https://github.com/japanvik/networkkit"
    },
    "split_keywords": [
        "network agents",
        " pubsub",
        " framework",
        " communication"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "eb048ccecaf7d7f02ab97bae46e4a0b56c00a91b817ccb11503f9e628a37be94",
                "md5": "d0dbd25b8e86f2e739dae44acc2bd1e5",
                "sha256": "5d4458f65ccb5ff888ecbc1d2d89a0a890ce7de2bc32dc4afc6e405dd1e56436"
            },
            "downloads": -1,
            "filename": "networkkit-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d0dbd25b8e86f2e739dae44acc2bd1e5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 12972,
            "upload_time": "2025-01-04T20:38:27",
            "upload_time_iso_8601": "2025-01-04T20:38:27.138089Z",
            "url": "https://files.pythonhosted.org/packages/eb/04/8ccecaf7d7f02ab97bae46e4a0b56c00a91b817ccb11503f9e628a37be94/networkkit-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ad62b97e45a913d3dd9893f17035088d8069bc0e42dc4125976244e009a8f611",
                "md5": "dd772e685ebaf49409e32be25e91a025",
                "sha256": "add7267a726962bbf74f6029f06d635fcf27cb038bb54554ec8761fa29151fbc"
            },
            "downloads": -1,
            "filename": "networkkit-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "dd772e685ebaf49409e32be25e91a025",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 10903,
            "upload_time": "2025-01-04T20:38:29",
            "upload_time_iso_8601": "2025-01-04T20:38:29.374767Z",
            "url": "https://files.pythonhosted.org/packages/ad/62/b97e45a913d3dd9893f17035088d8069bc0e42dc4125976244e009a8f611/networkkit-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-04 20:38:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "japanvik",
    "github_project": "networkkit",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "networkkit"
}
        
Elapsed time: 1.85472s