Skip to main content

GenAI Python project for direct agents connection either locally via websockets or remote via Azure Web PubSub

Project description

########################################

# Multi-Agent Communication Library

This library provides a unified, session-based API for building distributed multi-agent communication systems. It
supports two connection modes:

- **Local Mode:** Uses UDP/WebSocket for local communication.
- **Remote Mode:** Uses Azure Pub/Sub for cloud-based messaging.

The `Session` class is the main entry point, allowing agents to start a session, send messages to peers, and process
incoming messages asynchronously.

## Features

- **Dual Connection Modes:**
  - **Local Mode:** Communication using UDP/WebSocket.
  - **Remote Mode:** Communication via Azure Pub/Sub.
- **Session Management:**
  Persistent client identification with an optional display name.
- **Asynchronous Messaging:**
  Send requests to other clients and wait for their responses.
- **Custom Workload Processing:**
  Easily bind your own asynchronous message handler to process incoming requests.
- **Flexible Configuration:**
  Advanced settings available via the `config` parameter.

## Installation

```bash
  python pip install multi-agent-communication-library

Usage Examples

Agent Example (Providing Services)

This example demonstrates an agent that provides a service. The agent binds a custom asynchronous message handler to process incoming requests with an abstract workload.

import asyncio
from library.session import Session

async def process(message: str) -> str:
    # Simulate an abstract workload (e.g., data processing or computation)
    print(f"Processing workload for message: {message}")
    await asyncio.sleep(2)  # Simulate time-consuming work
    return f"Processed result for: {message}"

async def main():
    # Create a session in local mode (use "remote" for Azure Pub/Sub mode)
    session = Session(
        connection_type="local",
        client_name="worker_agent"
    )
    # Start the session with the message handler bound to process incoming requests
    await session.start(message_handler=process)
    # Keep the agent running indefinitely to process incoming tasks
    await asyncio.Future()

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

Client Example (Using Services)

This example shows a client that sends a task request to a service provider identified by its client_name.

import asyncio

from library.session import Session

async def main():
    session = Session(
        connection_type="local",
        client_name="client"
    )
    await session.start()
    request = "Ipsum tempor ut adipisicing do magna ut excepteur deserunt non irure veniam dolore."
    result = await session.send("scrapper", request)
    print("Result:", result)
if __name__ == '__main__':
    asyncio.run(main())

API Reference

Session Class

Constructor

  • connection_type:
    • 'local' – Uses UDP/WebSocket for local communication.
    • 'remote' – Uses Azure Pub/Sub for remote communication.
  • client_name: Optional display name for the client.
  • client_id: Optional unique identifier; if not provided, one will be generated.
  • config: Optional configuration dictionary for advanced settings.
  • log_level: Logging level (default is 'info').

Methods

  • async start(message_handler: Optional[Callable[[str], Awaitable[str]]] = None) -> None Starts the session. If a message_handler is provided (for local mode), it will be bound to process incoming messages.
  • async send(target_client_name: str, text: str, timeout: Optional[float] = None) -> str Sends a message to a peer identified by target_client_name and waits for a response

Configuration Parameters

Parameter Type Default Value Description Required In
webserver_port Optional[int] None Port number for the webserver. If not set, an available port from the autodiscovery range is used. local
azure_endpoint_url Optional[str] None URL endpoint for Azure connectivity. remote
azure_access_key Optional[str] None Access key for Azure services. remote
advertisement_port int 9999 Port used for network advertisement. local
advertisement_ip str "224.1.1.1" IP address for network advertisement. local
advertisement_interval float 15.0 Interval (in seconds) for sending advertisements. local, remote
heartbeat_delay float 5.0 Delay (in seconds) between heartbeat messages. local, remote
webserver_port_min int 5000 Minimum port number for autodiscovery. local
webserver_port_max int 65000 Maximum port number for autodiscovery. local
peer_discovery_timeout float 30.0 Timeout (in seconds) for peer discovery. local, remote
peer_ping_interval float 5.0 Interval (in seconds) for pinging peers. local, remote
azure_api_version str "1.0" API version for Azure communications. remote
connection_type str "local" Type of connection, either"local" or "remote". local, remote

Notes:

  • For remote connections, azure_endpoint_url and azure_access_key are required.
  • If webserver_port is not set, it will be selected from the range [webserver_port_min, webserver_port_max].
  • connection_type must be either "local" or "remote".

Customization

  • Message Handler: Provide your own asynchronous function to process incoming messages. This function should accept a string message and return an awaitable string result.
  • Configuration Options: Use the config parameter in the Session constructor for advanced settings.

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

genai_protocol_lite-1.0.1.tar.gz (38.0 kB view details)

Uploaded Source

Built Distribution

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

genai_protocol_lite-1.0.1-py3-none-any.whl (36.1 kB view details)

Uploaded Python 3

File details

Details for the file genai_protocol_lite-1.0.1.tar.gz.

File metadata

  • Download URL: genai_protocol_lite-1.0.1.tar.gz
  • Upload date:
  • Size: 38.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for genai_protocol_lite-1.0.1.tar.gz
Algorithm Hash digest
SHA256 049ac10d346eece782c98292199c8634b3ae9d2f02014e8221a3244742ec8b97
MD5 05c4c83d9fb9b77bb04f1bbb1027d019
BLAKE2b-256 68c238b05fcbd933d7d0f92ed8748bac0af3710c2779c749b8f368a2a9287b3c

See more details on using hashes here.

File details

Details for the file genai_protocol_lite-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for genai_protocol_lite-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 294e5cd47daecc1ca708eee8e8919aa211059d41fe8967b728fdb510d1345b24
MD5 7f32d2d9323de03ad5da2a3814be1fcc
BLAKE2b-256 3bb5eef274d930f8113fb40857ddce856488bcdba9d6bd24f7c6e9572eb08a2a

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