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.

License

[Specify your license here]



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.0.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.0-py3-none-any.whl (36.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: genai_protocol_lite-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 54603eee7dadb1cc6442903da5288b680ffc04cc13bde5e171ad9a9451d42e51
MD5 41f484b8d60090d1a17062d684693cf8
BLAKE2b-256 acb03388d618a23fb64aca8278d1779908ecca05be2ab2b99b060e1f732f42d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for genai_protocol_lite-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5fec39af64166afd70ec924d4b4161f76f535fb545aeb6bcc975952c055f7d25
MD5 36bf5f9addcc728713b698ce1780c8bf
BLAKE2b-256 9b6a5cfc65c31b5533db3104f5f7f5510ebfcf2959da98deaa0b9035a8242ee5

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