|
| 1 | +""" |
| 2 | +OpenGradient Python SDK for decentralized AI inference with end-to-end verification. |
1 | 3 |
|
| 4 | +## Overview |
| 5 | +
|
| 6 | +The OpenGradient SDK provides programmatic access to decentralized AI infrastructure. |
| 7 | +All LLM inference runs inside Trusted Execution Environments (TEEs) and settles |
| 8 | +on-chain via the x402 payment protocol, giving you cryptographic proof that |
| 9 | +inference was performed correctly. |
| 10 | +
|
| 11 | +The SDK operates across two chains with separate private keys: |
| 12 | +
|
| 13 | +- **`opengradient.client.llm`** (``og.LLM``) -- LLM chat and completion with TEE-verified execution. Pays via x402 on **Base Sepolia** (requires OPG tokens). |
| 14 | +- **`opengradient.client.alpha`** (``og.Alpha``) -- On-chain ONNX model inference with VANILLA, TEE, or ZKML verification. Pays gas on the **OpenGradient alpha testnet**. |
| 15 | +- **`opengradient.client.model_hub`** (``og.ModelHub``) -- Model repository management: create, version, and upload ML models. Requires email/password auth. |
| 16 | +- **`opengradient.client.twins`** (``og.Twins``) -- Digital twins chat via verifiable inference. Requires a twins API key. |
| 17 | +
|
| 18 | +See **`opengradient.types`** for shared data types (``TEE_LLM``, ``InferenceMode``, ``TextGenerationOutput``, ``x402SettlementMode``, etc.). |
| 19 | +
|
| 20 | +## LLM Chat |
| 21 | +
|
| 22 | +```python |
| 23 | +import asyncio |
| 24 | +import opengradient as og |
| 25 | +
|
| 26 | +llm = og.LLM(private_key="0x...") |
| 27 | +
|
| 28 | +# One-time OPG token approval (idempotent -- skips if allowance is sufficient) |
| 29 | +llm.ensure_opg_approval(opg_amount=5) |
| 30 | +
|
| 31 | +# Chat with an LLM (TEE-verified) |
| 32 | +response = asyncio.run(llm.chat( |
| 33 | + model=og.TEE_LLM.CLAUDE_SONNET_4_6, |
| 34 | + messages=[{"role": "user", "content": "Hello!"}], |
| 35 | + max_tokens=200, |
| 36 | +)) |
| 37 | +print(response.chat_output) |
| 38 | +``` |
| 39 | +
|
| 40 | +## Streaming |
| 41 | +
|
| 42 | +```python |
| 43 | +async def stream_example(): |
| 44 | + llm = og.LLM(private_key="0x...") |
| 45 | + stream = await llm.chat( |
| 46 | + model=og.TEE_LLM.GPT_5, |
| 47 | + messages=[{"role": "user", "content": "Explain TEE in one paragraph."}], |
| 48 | + max_tokens=300, |
| 49 | + stream=True, |
| 50 | + ) |
| 51 | + async for chunk in stream: |
| 52 | + if chunk.choices[0].delta.content: |
| 53 | + print(chunk.choices[0].delta.content, end="") |
| 54 | +
|
| 55 | +asyncio.run(stream_example()) |
| 56 | +``` |
| 57 | +
|
| 58 | +## On-chain Model Inference |
| 59 | +
|
| 60 | +```python |
| 61 | +alpha = og.Alpha(private_key="0x...") |
| 62 | +result = alpha.infer( |
| 63 | + model_cid="your_model_cid", |
| 64 | + inference_mode=og.InferenceMode.VANILLA, |
| 65 | + model_input={"input": [1.0, 2.0, 3.0]}, |
| 66 | +) |
| 67 | +print(result.model_output) |
| 68 | +``` |
| 69 | +
|
| 70 | +## Model Hub |
| 71 | +
|
| 72 | +```python |
| 73 | +hub = og.ModelHub(email="you@example.com", password="...") |
| 74 | +repo = hub.create_model("my-model", "A price prediction model") |
| 75 | +hub.upload("model.onnx", repo.name, repo.initialVersion) |
| 76 | +``` |
| 77 | +""" |
| 78 | + |
| 79 | +from . import agents, alphasense |
| 80 | +from .client import LLM, Alpha, ModelHub, Twins |
| 81 | +from .types import ( |
| 82 | + TEE_LLM, |
| 83 | + CandleOrder, |
| 84 | + CandleType, |
| 85 | + FileUploadResult, |
| 86 | + HistoricalInputQuery, |
| 87 | + InferenceMode, |
| 88 | + InferenceResult, |
| 89 | + ModelOutput, |
| 90 | + ModelRepository, |
| 91 | + SchedulerParams, |
| 92 | + TextGenerationOutput, |
| 93 | + TextGenerationStream, |
| 94 | + x402SettlementMode, |
| 95 | +) |
| 96 | + |
| 97 | +__all__ = [ |
| 98 | + "LLM", |
| 99 | + "Alpha", |
| 100 | + "ModelHub", |
| 101 | + "Twins", |
| 102 | + "TEE_LLM", |
| 103 | + "InferenceMode", |
| 104 | + "HistoricalInputQuery", |
| 105 | + "SchedulerParams", |
| 106 | + "CandleType", |
| 107 | + "CandleOrder", |
| 108 | + "TextGenerationOutput", |
| 109 | + "TextGenerationStream", |
| 110 | + "x402SettlementMode", |
| 111 | + "agents", |
| 112 | + "alphasense", |
| 113 | + "InferenceResult", |
| 114 | + "ModelOutput", |
| 115 | + "ModelRepository", |
| 116 | + "FileUploadResult", |
| 117 | +] |
| 118 | + |
| 119 | +__pdoc__ = { |
| 120 | + "account": False, |
| 121 | + "cli": False, |
| 122 | + "client": True, |
| 123 | + "defaults": False, |
| 124 | + "agents": True, |
| 125 | + "alphasense": True, |
| 126 | + "types": True, |
| 127 | + # Hide re-exported classes from the top-level page -- they are documented on their own submodule pages |
| 128 | + "LLM": False, |
| 129 | + "Alpha": False, |
| 130 | + "ModelHub": False, |
| 131 | + "Twins": False, |
| 132 | + "TEE_LLM": False, |
| 133 | + "InferenceMode": False, |
| 134 | + "TextGenerationOutput": False, |
| 135 | + "TextGenerationStream": False, |
| 136 | + "x402SettlementMode": False, |
| 137 | + "InferenceResult": False, |
| 138 | + "ModelOutput": False, |
| 139 | + "FileUploadResult": False, |
| 140 | + "ModelRepository": False, |
| 141 | + "CandleOrder": False, |
| 142 | + "CandleType": False, |
| 143 | + "HistoricalInputQuery": False, |
| 144 | + "SchedulerParams": False, |
| 145 | +} |
0 commit comments