API Reference: ogpu.client¶
Essential API reference for the ogpu.client module - the foundation for task publishing and response management on the OpenGPU network.
Task and Source Management
This module is designed for interacting with the OpenGPU network. Use ogpu.client to publish tasks, manage sources, retrieve responses, and configure blockchain connections.
Source Operations¶
ogpu.client.source.publish_source
¶
Publish a source to the Nexus contract.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_info
|
SourceInfo
|
SourceInfo object containing source configuration |
required |
private_key
|
Optional[str]
|
Private key for signing the transaction. If None, will use CLIENT_PRIVATE_KEY environment variable. |
None
|
nonce
|
Optional[int]
|
Optional manual nonce override. If None, will be fetched automatically. |
None
|
auto_fix_nonce
|
bool
|
If True, automatically retry on nonce errors (default: True) |
True
|
max_retries
|
int
|
Maximum number of retry attempts on recoverable errors (default: 3) |
3
|
Returns:
| Type | Description |
|---|---|
str
|
Address of the created source contract |
Raises:
| Type | Description |
|---|---|
Exception
|
If transaction fails after all retries |
Example
from ogpu.client import publish_source, SourceInfo
Normal usage with auto-retry¶
source_address = publish_source(source_info)
Manual nonce override (for advanced use)¶
source_address = publish_source(source_info, nonce=42)
Source code in ogpu/client/source.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
Purpose: Publish AI services as sources on the OpenGPU network.
Example:
from web3 import Web3
from ogpu.client import publish_source, SourceInfo, ImageEnvironments, DeliveryMethod
source_info = SourceInfo(
name="sentiment-service",
description="AI sentiment analysis service",
logoUrl="https://example.com/logo.png",
imageEnvs=ImageEnvironments(
cpu="https://raw.githubusercontent.com/user/repo/main/docker-compose.yml",
nvidia="https://raw.githubusercontent.com/user/repo/main/docker-compose-gpu.yml"
),
minPayment=Web3.to_wei(0.001, "ether"),
minAvailableLockup=Web3.to_wei(0.01, "ether"),
maxExpiryDuration=86400,
deliveryMethod=DeliveryMethod.MANUAL_CONFIRMATION
)
source_address = publish_source(source_info)
print(f"Source published at: {source_address}")
Task Operations¶
ogpu.client.task.publish_task
¶
Publish a task to the Controller contract.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_info
|
TaskInfo
|
TaskInfo object containing task configuration |
required |
private_key
|
Optional[str]
|
Private key for signing the transaction. If None, will use CLIENT_PRIVATE_KEY environment variable. |
None
|
nonce
|
Optional[int]
|
Optional manual nonce override. If None, will be fetched automatically. |
None
|
auto_fix_nonce
|
bool
|
If True, automatically retry on nonce errors (default: True) |
True
|
max_retries
|
int
|
Maximum number of retry attempts on recoverable errors (default: 3) |
3
|
Returns:
| Type | Description |
|---|---|
str
|
Address of the created task contract |
Raises:
| Type | Description |
|---|---|
Exception
|
If transaction fails after all retries |
Example
from ogpu.client import publish_task, TaskInfo
Normal usage with auto-retry¶
task_address = publish_task(task_info)
Manual nonce override (for advanced use)¶
task_address = publish_task(task_info, nonce=42)
Disable auto-fix¶
task_address = publish_task(task_info, auto_fix_nonce=False)
Source code in ogpu/client/task.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
Purpose: Publish AI tasks to the OpenGPU network for distributed processing.
Example:
import time
from web3 import Web3
from ogpu.client import publish_task, TaskInfo, TaskInput
# Create task configuration
task_config = TaskInput(
function_name="analyze_sentiment",
data={"text": "I love this product!"}
)
task_info = TaskInfo(
source="0x1234567890123456789012345678901234567890",
config=task_config,
expiryTime=int(time.time()) + 3600, # 1 hour from now
payment=Web3.to_wei(0.01, "ether")
)
task_address = publish_task(task_info)
print(f"Task published at: {task_address}")
Response Operations¶
ogpu.client.responses.get_task_responses
¶
Get all responses for a specific task address.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_address
|
str
|
The task contract address |
required |
lower
|
int
|
Lower bound for response pagination (default: 0) |
0
|
upper
|
Optional[int]
|
Upper bound for response pagination (default: None, gets all) |
None
|
Returns:
| Type | Description |
|---|---|
List[Response]
|
List of TaskResponse objects containing response data |
Raises:
| Type | Description |
|---|---|
Exception
|
If the contract call fails or task doesn't exist |
Source code in ogpu/client/responses.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
Purpose: Retrieve all responses for a published task.
Example:
responses = get_task_responses(task_address)
for response in responses:
print(f"Provider: {response.provider}")
print(f"Status: {response.status}")
print(f"Data: {response.data}")
ogpu.client.responses.get_confirmed_response
¶
Get confirmed response data for a specific task address by calling the API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_address
|
str
|
The task contract address |
required |
Returns:
| Type | Description |
|---|---|
ConfirmedResponse
|
ConfirmedResponse object containing the confirmed response data |
Raises:
| Type | Description |
|---|---|
Exception
|
If the API call fails or no confirmed response is found |
Source code in ogpu/client/responses.py
Purpose: Retrieve the confirmed response for a completed task.
Example:
ogpu.client.responses.confirm_response
¶
Confirm a response using the Controller contract.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response_address
|
str
|
The response contract address to confirm |
required |
private_key
|
Optional[str]
|
Private key for signing the transaction. If None, will use CLIENT_PRIVATE_KEY environment variable. |
None
|
nonce
|
Optional[int]
|
Optional manual nonce override. If None, will be fetched automatically. |
None
|
auto_fix_nonce
|
bool
|
If True, automatically retry on nonce errors (default: True) |
True
|
max_retries
|
int
|
Maximum number of retry attempts on recoverable errors (default: 3) |
3
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Transaction hash of the confirmation |
Raises:
| Type | Description |
|---|---|
Exception
|
If the confirmation fails after all retries |
Example
from ogpu.client import confirm_response
Normal usage with auto-retry¶
tx_hash = confirm_response(response_address)
Manual nonce override¶
tx_hash = confirm_response(response_address, nonce=42)
Source code in ogpu/client/responses.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |
Purpose: Confirm a response to complete the task and release payment.
Example:
Network Configuration¶
ogpu.client.chain_config.ChainId
¶
Enum for supported blockchain networks.
Attributes:
| Name | Type | Description |
|---|---|---|
OGPU_MAINNET |
Main OpenGPU network (Chain ID: 1071) |
|
OGPU_TESTNET |
Test OpenGPU network (Chain ID: 200820172034) |
Purpose: Enum defining supported blockchain networks.
ogpu.client.chain_config.ChainConfig.set_chain
classmethod
¶
Set the current active chain
Purpose: Set the current active blockchain network.
Example:
from ogpu.client import ChainConfig, ChainId
# Set to testnet
ChainConfig.set_chain(ChainId.OGPU_TESTNET)
# Set to mainnet
ChainConfig.set_chain(ChainId.OGPU_MAINNET)
ogpu.client.chain_config.ChainConfig.get_current_chain
classmethod
¶
Purpose: Get the currently active blockchain network.
Example:
current_chain = ChainConfig.get_current_chain()
print(f"Current chain: {current_chain.name}")
print(f"Chain ID: {current_chain.value}")
ogpu.client.chain_config.ChainConfig.get_contract_address
classmethod
¶
Get contract address for the current chain
Source code in ogpu/client/chain_config.py
Purpose: Get contract address for a specific contract on the current chain.
Example:
# Get contract addresses for current chain
nexus_address = ChainConfig.get_contract_address("NEXUS")
controller_address = ChainConfig.get_contract_address("CONTROLLER")
terminal_address = ChainConfig.get_contract_address("TERMINAL")
print(f"NEXUS: {nexus_address}")
print(f"CONTROLLER: {controller_address}")
print(f"TERMINAL: {terminal_address}")
ogpu.client.chain_config.ChainConfig.get_all_supported_chains
classmethod
¶
Purpose: Get list of all supported blockchain networks.
Example:
supported_chains = ChainConfig.get_all_supported_chains()
for chain in supported_chains:
print(f"Chain: {chain.name} (ID: {chain.value})")
Nonce Management (v0.2.0.14+)¶
New in v0.2.0.14
Automatic nonce management with error detection and recovery.
ogpu.client.nonce_utils.fix_nonce
¶
Fix stuck nonce issues by canceling pending transactions.
This function will: 1. Detect pending transactions (transactions stuck in mempool) 2. Cancel them by sending 0 ETH self-transfers with higher gas price 3. Clear SDK's internal nonce cache 4. Return the next available nonce
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
Optional[str]
|
Ethereum address to fix (optional if private_key provided) |
None
|
private_key
|
Optional[str]
|
Private key for signing cancellation transactions If None, will use CLIENT_PRIVATE_KEY environment variable |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Next available nonce after fixing |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither address nor private_key is provided |
Example
from ogpu.client import fix_nonce
Fix nonce for current account¶
next_nonce = fix_nonce() print(f"Ready to send transaction with nonce: {next_nonce}")
Source code in ogpu/client/nonce_utils.py
Purpose: Fix stuck nonce issues by cancelling pending transactions.
Example:
from ogpu.client import fix_nonce
# Fix all stuck transactions
next_nonce = fix_nonce()
print(f"Ready to send with nonce: {next_nonce}")
ogpu.client.nonce_utils.get_nonce_info
¶
Get detailed nonce information for an address.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
Optional[str]
|
Ethereum address (optional if private_key provided) |
None
|
private_key
|
Optional[str]
|
Private key to derive address from If None, will use CLIENT_PRIVATE_KEY environment variable |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing: |
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
Example
from ogpu.client import get_nonce_info info = get_nonce_info() print(f"Pending transactions: {info['pending_nonce'] - info['mined_nonce']}")
Source code in ogpu/client/nonce_utils.py
Purpose: Get detailed nonce information for an address.
Example:
from ogpu.client import get_nonce_info
info = get_nonce_info()
if info['has_pending']:
print(f"Warning: {info['pending_count']} pending transactions")
ogpu.client.nonce_utils.reset_nonce_cache
¶
Reset the SDK's internal nonce cache without canceling transactions.
This is useful when you want to force the SDK to fetch a fresh nonce from the blockchain without canceling any pending transactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
Optional[str]
|
Ethereum address to reset (optional if private_key provided) |
None
|
private_key
|
Optional[str]
|
Private key to derive address from If None, will use CLIENT_PRIVATE_KEY environment variable |
None
|
Example
from ogpu.client import reset_nonce_cache reset_nonce_cache() print("Nonce cache cleared")
Source code in ogpu/client/nonce_utils.py
Purpose: Clear SDK nonce cache.
Example:
from ogpu.client import reset_nonce_cache
reset_nonce_cache()
# Next transaction fetches fresh nonce
ogpu.client.nonce_utils.clear_all_nonce_caches
¶
Clear all nonce caches for all addresses.
This is useful for testing or when you want to completely reset the SDK's nonce state.
Example
from ogpu.client import clear_all_nonce_caches clear_all_nonce_caches() print("All nonce caches cleared")
Source code in ogpu/client/nonce_utils.py
Purpose: Clear all nonce caches for all addresses.
Example:
Learn More
See the Nonce Management Guide for comprehensive documentation and best practices.
Data Types¶
ogpu.client.types.ImageEnvironments
dataclass
¶
Docker compose file paths for different environments.
Attributes:
| Name | Type | Description |
|---|---|---|
cpu |
str
|
URL to CPU-only docker-compose.yml file |
nvidia |
str
|
URL to NVIDIA GPU docker-compose.yml file |
amd |
str
|
URL to AMD GPU docker-compose.yml file |
Purpose: Docker compose file URLs for different hardware environments.
Example:
image_envs = ImageEnvironments(
cpu="https://raw.githubusercontent.com/user/repo/main/docker-compose.yml",
nvidia="https://raw.githubusercontent.com/user/repo/main/docker-compose-gpu.yml",
amd="https://raw.githubusercontent.com/user/repo/main/docker-compose-amd.yml"
)
ogpu.client.types.DeliveryMethod
¶
Enum for delivery method options.
Attributes:
| Name | Type | Description |
|---|---|---|
MANUAL_CONFIRMATION |
Client manually confirms the response |
|
FIRST_RESPONSE |
First provider to submit a response wins |
Purpose: Enum defining how task responses are delivered and confirmed.
ogpu.client.types.SourceInfo
dataclass
¶
User-friendly source information structure.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Human-readable name for the source |
description |
str
|
Description of the AI service |
logoUrl |
str
|
URL to the source logo image |
imageEnvs |
ImageEnvironments
|
Docker environment configurations |
minPayment |
int
|
Minimum payment required in wei |
minAvailableLockup |
int
|
Minimum lockup amount in wei |
maxExpiryDuration |
int
|
Maximum task duration in seconds |
deliveryMethod |
DeliveryMethod
|
How responses are delivered |
Purpose: Configuration data for publishing AI services as sources.
Example:
source_info = SourceInfo(
name="sentiment-service",
description="AI sentiment analysis service",
logoUrl="https://example.com/logo.png",
imageEnvs=ImageEnvironments(
cpu="https://raw.githubusercontent.com/user/repo/main/docker-compose.yml",
nvidia="https://raw.githubusercontent.com/user/repo/main/docker-compose-gpu.yml"
),
minPayment=Web3.to_wei(0.001, "ether"),
minAvailableLockup=Web3.to_wei(0.01, "ether"),
maxExpiryDuration=86400,
deliveryMethod=DeliveryMethod.MANUAL_CONFIRMATION
)
ogpu.client.types.TaskInput
dataclass
¶
Configuration structure for tasks.
Attributes:
| Name | Type | Description |
|---|---|---|
function_name |
str
|
Name of the function to call on the source |
data |
BaseModel | dict[str, Any]
|
Input data for the function (Pydantic model or dictionary) |
Purpose: Configuration for task input data and function specification.
Example:
# Using a dictionary
task_input = TaskInput(
function_name="inference",
data={"inputs": "Translate to French: Hello"}
)
# Using a Pydantic model
from pydantic import BaseModel
class InferenceData(BaseModel):
inputs: str
parameters: dict = {}
task_input = TaskInput(
function_name="inference",
data=InferenceData(inputs="Hello world")
)
ogpu.client.types.TaskInfo
dataclass
¶
User-friendly task information structure.
Attributes:
| Name | Type | Description |
|---|---|---|
source |
str
|
Address of the source to run the task on |
config |
TaskInput
|
Task input configuration and function call |
expiryTime |
int
|
Unix timestamp when task expires |
payment |
int
|
Payment amount for the task in wei |
Purpose: Configuration data for publishing tasks to the network.
Example:
task_info = TaskInfo(
source="0x1234...",
config=TaskInput(
function_name="inference",
data={"inputs": "What is AI?"}
),
expiryTime=1640995200,
payment=1000000000000000000 # 1 OGPU in wei
)
ogpu.client.types.Response
dataclass
¶
Response data structure for task responses.
Attributes:
| Name | Type | Description |
|---|---|---|
address |
str
|
Blockchain address of the response |
task |
str
|
Address of the task this responds to |
provider |
str
|
Address of the provider who submitted the response |
data |
str
|
Response data from the AI service |
payment |
int
|
Payment amount in wei |
status |
int
|
Response status code |
timestamp |
int
|
Unix timestamp when response was submitted |
confirmed |
bool
|
Whether the response has been confirmed |
Purpose: Response data structure from completed tasks.
Example:
# Response object from get_task_responses()
response = Response(
address="0xabcd...",
task="0x1234...",
provider="0x5678...",
data='{"result": "positive sentiment"}',
payment=1000000000000000000,
status=1,
timestamp=1640995200,
confirmed=False
)
ogpu.client.types.ConfirmedResponse
dataclass
¶
Simplified confirmed response data structure.
Attributes:
| Name | Type | Description |
|---|---|---|
address |
str
|
Blockchain address of the confirmed response |
data |
str
|
The confirmed response data |
Purpose: Simplified confirmed response data structure.
Example: