Skip to main content

Message bus integration for HAI

Project description

H Message Bus

A message bus integration for HAI applications based on NATS.io

Overview

H Message Bus provides a robust, asynchronous messaging infrastructure built on NATS.io for HAI applications. It enables seamless communication between components through a publish-subscribe pattern, supporting both fire-and-forget messaging and request-response patterns.

Features

  • Asynchronous Communication: Built for modern, non-blocking I/O operations
  • Flexible Message Routing: Publish and subscribe to specific topics
  • High Reliability: Automatic reconnection handling and configurable timeouts
  • Simple API: Focus on core messaging functionality with minimal dependencies

Installation

pip install h_message_bus

Requirements

  • Python 3.10+
  • NATS.io server (can be run via Docker)

Topics

H Message Bus includes predefined topics following the convention: hai.[source].[destination].[action]

Available topics:

Topic Constant Topic String Description
Topic.AI_SEND_TG_CHAT_MESSAGE hai.ai.tg.chat.send AI sending message to Telegram chat
Topic.AI_VECTORS_SAVE hai.ai.vectors.save AI saving data to vector database
Topic.AI_VECTORS_QUERY hai.ai.vectors.query AI querying vector database
Topic.TG_SEND_AI_CHAT_MESSAGE hai.tg.ai.chat.send Telegram sending message to AI

You can use these predefined topics or create your own topic strings.

Quick Start

Start a NATS Server

The easiest way to get started is with Docker:

docker-compose up -d

Create a Publisher

import asyncio
import uuid
from h_message_bus import NatsConfig, NatsPublisherAdapter, HaiMessage, Topic

async def main():
    # Configure NATS connection
    config = NatsConfig(server="nats://localhost:4222")
    
    # Create publisher adapter
    publisher = NatsPublisherAdapter(config)
    
    # Connect to NATS
    await publisher.connect()
    
    # Create and publish a message using a predefined topic
    message = HaiMessage(
        message_id=str(uuid.uuid4()),
        sender="service-a",
        topic=Topic.TG_SEND_AI_CHAT_MESSAGE,
        payload={"text": "Hello AI, this is a message from Telegram", "chat_id": 12345}
    )
    
    # Publish message
    await publisher.publish(message)
    
    # Clean up
    await publisher.close()

if __name__ == "__main__":
    asyncio.run(main())

Create a Subscriber

import asyncio
from h_message_bus import NatsConfig, NatsSubscriberAdapter, HaiMessage, Topic

async def message_handler(message: HaiMessage):
    print(f"Received message: {message.message_id}")
    print(f"From: {message.sender}")
    print(f"Topic: {message.topic}")
    print(f"Payload: {message.payload}")

async def main():
    # Configure NATS connection
    config = NatsConfig(server="nats://localhost:4222")
    
    # Create subscriber
    subscriber = NatsSubscriberAdapter(config)
    
    # Connect to NATS
    await subscriber.connect()
    
    # Subscribe to a topic
    await subscriber.subscribe(Topic.TG_SEND_AI_CHAT_MESSAGE, message_handler)
    
    # Keep the application running
    try:
        print("Subscriber running. Press Ctrl+C to exit.")
        while True:
            await asyncio.sleep(1)
    except KeyboardInterrupt:
        # Clean up
        await subscriber.close()

if __name__ == "__main__":
    asyncio.run(main())

Advanced Usage

Request-Response Pattern

import asyncio
import uuid
from h_message_bus import NatsConfig, NatsPublisherAdapter, HaiMessage, Topic

async def main():
    config = NatsConfig(server="nats://localhost:4222")
    publisher = NatsPublisherAdapter(config)
    
    # Connect to NATS
    await publisher.connect()
    
    request_message = HaiMessage(
        message_id=str(uuid.uuid4()),
        sender="client-service",
        topic=Topic.AI_VECTORS_QUERY,
        payload={"query": "find similar documents", "limit": 10}
    )
    
    # Send request and wait for response (with timeout)
    response = await publisher.request(request_message, timeout=5.0)
    
    if response:
        print(f"Received response: {response.payload}")
    else:
        print("Request timed out")
    
    await publisher.close()

if __name__ == "__main__":
    asyncio.run(main())

Creating a Service with Request Handler

import asyncio
import uuid
from h_message_bus import NatsConfig, NatsSubscriberAdapter, NatsPublisherAdapter, HaiMessage, Topic

async def request_handler(request: HaiMessage):
    print(f"Received request: {request.message_id}")
    print(f"Payload: {request.payload}")
    
    # Process the request
    result = {"status": "success", "data": {"result": 42}}
    
    # Create a response message
    return HaiMessage(
        message_id=str(uuid.uuid4()),
        sender="service-b",
        topic=f"{request.topic}.response",
        payload=result,
        correlation_id=request.message_id
    )

async def main():
    # Configure NATS connection
    config = NatsConfig(server="nats://localhost:4222")
    
    # Create subscriber for handling requests
    subscriber = NatsSubscriberAdapter(config)
    publisher = NatsPublisherAdapter(config)
    
    # Connect to NATS
    await subscriber.connect()
    await publisher.connect()
    
    # Register request handler for vector database queries
    await subscriber.subscribe_with_response(Topic.AI_VECTORS_QUERY, request_handler, publisher)
    
    # Keep the application running
    try:
        print("Service running. Press Ctrl+C to exit.")
        while True:
            await asyncio.sleep(1)
    except KeyboardInterrupt:
        # Clean up
        await subscriber.close()
        await publisher.close()

if __name__ == "__main__":
    asyncio.run(main())

Configuration Options

The NatsConfig class allows you to customize your NATS connection:

Parameter Description Default
server NATS server address Required
max_reconnect_attempts Maximum reconnection attempts 10
reconnect_time_wait Time between reconnection attempts (seconds) 2
connection_timeout Connection timeout (seconds) 2
ping_interval Interval for ping frames (seconds) 20
max_outstanding_pings Maximum unanswered pings before disconnect 5
max_payload Maximum size of the payload in bytes 1048576 (1MB)

API Reference

Exported Classes

The following classes are exported directly from the package:

  • NatsConfig - Configuration for the NATS connection
  • HaiMessage - Message structure for HAI communication
  • NatsPublisherAdapter - Adapter for publishing messages
  • NatsSubscriberAdapter - Adapter for subscribing to messages
  • MessageProcessor - Processing incoming messages
  • NatsClientRepository - Low-level NATS client operations
  • Topic - Enumeration of predefined topic strings

HaiMessage Structure

The HaiMessage class is the core data structure used for all messaging:

class HaiMessage:
    message_id: str       # Unique identifier for the message
    sender: str           # Identifier of the sender
    topic: str            # The topic or channel for the message
    payload: dict         # Actual message data
    correlation_id: str = None  # Optional reference to a related message
    timestamp: float = None     # Optional message creation timestamp

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

h_message_bus-0.0.5.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

h_message_bus-0.0.5-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file h_message_bus-0.0.5.tar.gz.

File metadata

  • Download URL: h_message_bus-0.0.5.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for h_message_bus-0.0.5.tar.gz
Algorithm Hash digest
SHA256 3c5ecf5f231f6ad8f655ce8710653f2a53d04864993e12acf07a02cfdd0b8c1c
MD5 9e6e40de75e06c54d14f4bf0272309c2
BLAKE2b-256 ff673f38ae5f306aa0de510caab83d2cd6cbe04587e083b06e0c050aaed4674b

See more details on using hashes here.

File details

Details for the file h_message_bus-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: h_message_bus-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for h_message_bus-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8ef18ac624abe29dd6f819ca9ed5be7b5daa5cd01e447382f8a49be8e3a33bc4
MD5 f4ffc2d1e05b362ad1512ba25f963791
BLAKE2b-256 2b3b5e6c449a34c45dd32b01ef71f12627f1bc800347af0078cd33ee239dabed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page