Skip to main content

No project description provided

Project description

python-0g

A Python client for the 0G.ai on-chain inference network. It lets you obtain an OpenAI-compatible client for any 0G provider (by ENS), sign requests locally with your wallet key, and query network/service metadata over the 0G smart contracts.

Highlights:

  • OpenAI-compatible sync and async clients (drop-in for openai SDK)
  • Provider discovery via on-chain contracts
  • Local signing using your EVM private key
  • Utility helpers for balance/ledger and quoting costs

Requirements

  • Python >= 3.10, < 4.0
  • Node.js (required by the embedded JS bindings via the javascript package)
  • An EVM private key funded on the 0G network
  • RPC endpoint for the 0G testnet or mainnet (HTTP)

Installation

Install from PyPI:

pip install python-0g

Quick start

Set your credentials using environment variables:

  • A0G_PRIVATE_KEY — your EVM private key (hex string without leading 0x)
  • A0G_RPC_URL — RPC endpoint URL for the 0G network (optional; defaults to https://evmrpc-testnet.0g.ai)

Example (macOS/Linux):

export A0G_PRIVATE_KEY=YOUR_HEX_PRIVATE_KEY
export A0G_RPC_URL=https://evmrpc-testnet.0g.ai

Then use the SDK to get an OpenAI-compatible client for a provider and make a chat completion:

from a0g.base import A0G

# Replace with a valid provider ENS name registered on 0G (example placeholder)

a0g_client = A0G()
service = a0g_client.get_all_services()[1]
client = a0g_client.get_openai_client(service.provider)

resp = client.chat.completions.create(
    model=service.model,  # model used by the provider
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Say hello from 0G!"},
    ],
)

print(resp.choices[0].message.content)

Async usage

import asyncio
from a0g.base import A0G

PROVIDER = "vllm-testnet.0gai.eth"  # replace with a valid provider

async def main():
    a0g_client = A0G()
    service = a0g_client.get_all_services()[1]
    
    aclient = a0g_client.get_openai_async_client(service.provider)
    resp = await aclient.chat.completions.create(
        model="meta-llama/Meta-Llama-3.1-8B-Instruct",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Ping?"},
        ],
    )
    print(resp.choices[0].message.content)

asyncio.run(main())

Working directly with the model type:

from a0g.base import A0G
from a0g.types.model import ServiceStructOutput

a0g_client = A0G()
svc = a0g_client.get_all_services()[1]
# If you already have a ServiceStructOutput instance `svc` from on-chain calls
print(svc.get_service_metadata())  # { 'endpoint': '.../v1/proxy', 'model': '...' }
quote = svc.get_quote()            # GET {url}/v1/quote, bigint-safe JSON
print(quote)

# Helpers to approximate byte-based accounting used by some providers:
inputs = ServiceStructOutput.get_input_count("Hello")
outputs = ServiceStructOutput.get_output_count("World")
print(inputs, outputs)

On-chain helpers

A0G exposes basic helpers around the 0G contracts using web3.py:

from a0g.base import A0G
from web3.types import ENS

PROVIDER = "0x...."
sdk = A0G()
print("Balance (ETH):", sdk.get_balance())
print("Ledger inference address:", sdk.get_ledger_inference_address())
print("Ledger owner address:", sdk.get_ledger_owner_address())

acct = sdk.get_account(PROVIDER)
print("Provider account struct:", acct)

svc = sdk.get_service(PROVIDER)
print("Service struct:", svc)

Configuration

  • A0G_PRIVATE_KEY: hex-encoded EVM private key used to sign provider requests.
  • A0G_RPC_URL: 0G RPC URL. Defaults to https://evmrpc-testnet.0g.ai if not provided.

The constructor also accepts private_key and rpc_url keyword arguments to override env vars:

from a0g.base import A0G
sdk = A0G(private_key="...", rpc_url="https://evmrpc-testnet.0g.ai")

Development

  • Formatting/linting: ruff, isort, mypy are configured in pyproject for dev use.
  • JS bindings: bundled under a0g/jsbindings and loaded via the javascript package; ensure Node.js is installed and accessible.

Run tests:

pytest -q

Troubleshooting

  • Module "javascript" or Node.js errors: Ensure Node.js is installed and available on PATH.
  • RPC connection error: Verify A0G_RPC_URL is reachable; the SDK checks connectivity on init.
  • Provider unavailable: get_openai_client raises if get_service_metadata returns unsuccessful; try another provider or network.
  • HTTP 4xx/5xx: Check your wallet funding and provider’s whitelist or availability windows.

License

This project is licensed under the terms of the LICENSE file included in the repository.

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

python_0g-0.4.4.2.tar.gz (2.4 MB view details)

Uploaded Source

Built Distribution

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

python_0g-0.4.4.2-py3-none-any.whl (2.4 MB view details)

Uploaded Python 3

File details

Details for the file python_0g-0.4.4.2.tar.gz.

File metadata

  • Download URL: python_0g-0.4.4.2.tar.gz
  • Upload date:
  • Size: 2.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for python_0g-0.4.4.2.tar.gz
Algorithm Hash digest
SHA256 ce76e4c9456fc4fc78a0e6d922c1064bb7c14ebdfb57d9eed0f1fcf0b509218d
MD5 f052aa1067151315a93dfdc2e07182d6
BLAKE2b-256 162b031fe700dfa75beddfc79b86a95c848da0e0f481729c10176d6b36912298

See more details on using hashes here.

File details

Details for the file python_0g-0.4.4.2-py3-none-any.whl.

File metadata

  • Download URL: python_0g-0.4.4.2-py3-none-any.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for python_0g-0.4.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f02238529c9b66cbbf9050a878d6bb05c1e68abdc912d72e12e933533e18ed42
MD5 2ed5d1ed99f1a87601e9231a00b97183
BLAKE2b-256 a77d50143cfdda16a595edd1a3e49aaa4abcce5e5e5f50a9b831147d497e119c

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