Skip to main content

No project description provided

Project description

Neurion Ganglion - Ion Framework

Overview

Neurion Ganglion provides a framework for defining, deploying, and managing Ions – decentralized computational units that operate within the Neurion ecosystem. This repository offers a streamlined way to create and register Ions, either as self-hosted services or pre-existing services ready for registration.

Features

  • Define input and output schemas using Pydantic.
  • Register Ions with Neurion automatically or manually.
  • Health-check endpoints for ensuring service availability.
  • Auto-recovery mechanism for self-hosted Ions.
  • Easy-to-use decorators for defining execution logic.
  • Integrated Ganglion Server for managing pathways and processing Ion calls.
  • Support for localnet, alphanet, betanet, and mainnet environments.

Installation

pip install neurion-ganglion

Environment Variables

Neurion Ganglion relies on several environment variables for configuration:

Variable Description
NEURION_PRIVATE_KEY The private key used for authentication and transaction signing.
NEURION_MNEMONIC Mnemonic phrase for wallet recovery and authentication.
NEURION_NETWORK Network selection: localnet, alphanet, betanet, or mainnet.
POSTGRES_DATABASE_URL PostgreSQL database URL for storing Ion and Pathway usage records.
CLICKHOUSE_DATABASE_URL ClickHouse database URL for storing Ion and Pathway usage records. Due to its performance, it is the default embedded solution
ENABLE_CALLS_COUNT Boolean flag (true/false) to enable or disable call metering.
FREE_ION_CALLS Number of free calls allowed for an Ion before requiring payment.

You can configure these by setting them in your .env file or export them as environment variables.


Network Configuration

Neurion supports multiple networks. To configure the appropriate network:

import os
from functools import lru_cache
from neurionpy.synapse.config import NetworkConfig

@lru_cache()
def get_network() -> NetworkConfig:
    network = os.getenv("NEURION_NETWORK", "alphanet")
    if network.lower() == "mainnet":
        return NetworkConfig.neurion_mainnet()
    elif network.lower() == "alphanet":
        return NetworkConfig.neurion_alpha_testnet()
    elif network.lower() == "betanet":
        return NetworkConfig.neurion_beta_testnet()
    elif network.lower() == "localnet":
        return NetworkConfig.neurion_localnet()
    return NetworkConfig.neurion_alpha_testnet()

Authentication Flow

To ensure security, every request to the Ganglion server must be authenticated using the following headers:

  • X-Neurion-Timestamp: The UNIX timestamp of the request.
  • X-Neurion-Sender: The sender's wallet address.
  • X-Neurion-Signature: The signature of the sender over the timestamp.

How Authentication Works:

  1. The sender signs the current timestamp using their private key.
  2. The request is sent with the signed timestamp and wallet address in headers.
  3. The server verifies:
    • The timestamp is within the allowed expiry window.
    • The signature is valid using the sender's public key.
    • The sender has enough balance if ENABLE_CALLS_COUNT is enabled.
  4. If validation passes, the request is processed.

Example Signature Verification in Middleware:

from neurionpy.crypto.keypairs import PublicKey

def verify_signature(timestamp: str, sender: str, signature: str) -> bool:
    try:
        recovered_pubkey = PublicKey.from_signature_hex(timestamp, signature)
        return recovered_pubkey.to_address() == sender
    except Exception as e:
        print(f"Signature verification failed: {e}")
        return False

Creating an Ion

You can create an Ion in two different ways:

1. Self-Hosting Ion (Auto-Registering)

from pydantic import BaseModel
from neurion_ganglion.ion.ion import Ion, ion_handler
from neurion_ganglion.custom_types.capacity import Capacity

description = "My Ion Server"
stake = 20000000
fee_per_thousand_calls = 1
capacities = [Capacity.SCRAPER, Capacity.AI_AGENT]

# Define Input/Output Schemas
class MyInputSchema(BaseModel):
    task_id: str
    parameters: int

class MyOutputSchema(BaseModel):
    message: str
    result: float

@ion_handler(MyInputSchema, MyOutputSchema)
def my_ion_handler(data: MyInputSchema) -> MyOutputSchema:
    return MyOutputSchema(message="Success", result=12)

if __name__ == "__main__":
    Ion.create_self_hosting_ion(description, stake, fee_per_thousand_calls, capacities, my_ion_handler).start()

2. Pure Ion Server & Manual Registration

Step 1: Start the Pure Ion Server

Ion.start_pure_ion_server(my_ion_handler)

Step 2: Register the Running Ion Server

endpoints = ["http://<public-ip>:8000"]
Ion.create_server_ready_ion(description, stake, fee_per_thousand_calls, capacities, MyInputSchema, MyOutputSchema, endpoints).register_ion()

Using Pathways

A Pathway defines a structured flow between multiple Ions.

from neurion_ganglion.ion.pathway import Pathway

pathway = Pathway.of(1)
response = pathway.call({"task_id": "1234", "parameters": 100})
print(response)

Ganglion Server

The Ganglion Server routes requests to the appropriate Ion or Pathway.

from neurion_ganglion.server.server import GanglionServer
GanglionServer.start()

Health Check

All Ions expose a /health endpoint for availability checking.

curl http://localhost:8000/health

License

This project is licensed under the MIT License.

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

neurion_ganglion-0.13.0.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

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

neurion_ganglion-0.13.0-py3-none-any.whl (29.1 kB view details)

Uploaded Python 3

File details

Details for the file neurion_ganglion-0.13.0.tar.gz.

File metadata

  • Download URL: neurion_ganglion-0.13.0.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.9.21 Linux/6.8.0-1021-azure

File hashes

Hashes for neurion_ganglion-0.13.0.tar.gz
Algorithm Hash digest
SHA256 9b0780992827c15cbd88fa3c86e4957ad6afd2208b795bfb5f0c11b113ab23d4
MD5 c81ff48bc126b0899afb621ce14b6fc5
BLAKE2b-256 04d7c716ec6506f4f5d7fccdd421ecdf1806fa3c056f7128ab693facfc1cac8c

See more details on using hashes here.

File details

Details for the file neurion_ganglion-0.13.0-py3-none-any.whl.

File metadata

  • Download URL: neurion_ganglion-0.13.0-py3-none-any.whl
  • Upload date:
  • Size: 29.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.9.21 Linux/6.8.0-1021-azure

File hashes

Hashes for neurion_ganglion-0.13.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19227b95d1d103c47671d34c7bb7b4062b365a8a48445b6432c0de8955b9ed5b
MD5 8d6ca48f166df4b9d175ade9afb15627
BLAKE2b-256 528b18fa071b7fde26b78ae6a00ba2ae670acb8b7f586b61f2cedb650834db92

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