Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ result = client.llm.chat(
)
```

### Payment Network Selection

By default, payments settle on the OG EVM network. To pay via Base Sepolia instead, set the `payment_network` parameter at client initialization. The Base Sepolia payment network uses the OG token at address `0x240b09731D96979f50B2C649C9CE10FcF9C7987F`.
```python
client = og.Client(
private_key=os.environ.get("OG_PRIVATE_KEY"),
payment_network=og.PaymentNetwork.BASE_SEPOLIA,
)
```

## Examples

Additional code examples are available in the [examples](./examples) directory.
Expand Down
3 changes: 2 additions & 1 deletion docs/opengradient/client/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ blockchain private key and optional Model Hub credentials.
#### Constructor

```python
def __init__(private_key: str, email: Optional[str] = None, password: Optional[str] = None, rpc_url: str = 'https://ogevmdevnet.opengradient.ai', api_url: str = 'https://sdk-devnet.opengradient.ai', contract_address: str = '0x8383C9bD7462F12Eb996DD02F78234C0421A6FaE', og_llm_server_url: Optional[str] = 'https://llmogevm.opengradient.ai', og_llm_streaming_server_url: Optional[str] = 'https://llmogevm.opengradient.ai')
def __init__(private_key: str, email: Optional[str] = None, password: Optional[str] = None, rpc_url: str = 'https://ogevmdevnet.opengradient.ai', api_url: str = 'https://sdk-devnet.opengradient.ai', contract_address: str = '0x8383C9bD7462F12Eb996DD02F78234C0421A6FaE', og_llm_server_url: Optional[str] = 'https://llmogevm.opengradient.ai', og_llm_streaming_server_url: Optional[str] = 'https://llmogevm.opengradient.ai', payment_network: `PaymentNetwork` = PaymentNetwork.OG_EVM)
```

**Arguments**
Expand All @@ -34,6 +34,7 @@ def __init__(private_key: str, email: Optional[str] = None, password: Optio
* **`contract_address`**: Inference contract address.
* **`og_llm_server_url`**: OpenGradient LLM server URL.
* **`og_llm_streaming_server_url`**: OpenGradient LLM streaming server URL.
* **`payment_network`**: Payment network for x402 transactions. Defaults to OG_EVM.

#### Variables

Expand Down
3 changes: 2 additions & 1 deletion docs/opengradient/client/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ blockchain private key and optional Model Hub credentials.
#### Constructor

```python
def __init__(private_key: str, email: Optional[str] = None, password: Optional[str] = None, rpc_url: str = 'https://ogevmdevnet.opengradient.ai', api_url: str = 'https://sdk-devnet.opengradient.ai', contract_address: str = '0x8383C9bD7462F12Eb996DD02F78234C0421A6FaE', og_llm_server_url: Optional[str] = 'https://llmogevm.opengradient.ai', og_llm_streaming_server_url: Optional[str] = 'https://llmogevm.opengradient.ai')
def __init__(private_key: str, email: Optional[str] = None, password: Optional[str] = None, rpc_url: str = 'https://ogevmdevnet.opengradient.ai', api_url: str = 'https://sdk-devnet.opengradient.ai', contract_address: str = '0x8383C9bD7462F12Eb996DD02F78234C0421A6FaE', og_llm_server_url: Optional[str] = 'https://llmogevm.opengradient.ai', og_llm_streaming_server_url: Optional[str] = 'https://llmogevm.opengradient.ai', payment_network: `PaymentNetwork` = PaymentNetwork.OG_EVM)
```

**Arguments**
Expand All @@ -79,6 +79,7 @@ def __init__(private_key: str, email: Optional[str] = None, password: Optio
* **`contract_address`**: Inference contract address.
* **`og_llm_server_url`**: OpenGradient LLM server URL.
* **`og_llm_streaming_server_url`**: OpenGradient LLM streaming server URL.
* **`payment_network`**: Payment network for x402 transactions. Defaults to OG_EVM.

#### Variables

Expand Down
2 changes: 1 addition & 1 deletion docs/opengradient/client/llm.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Supports both streaming and non-streaming responses.
#### Constructor

```python
def __init__(wallet_account: `LocalAccount`, og_llm_server_url: str, og_llm_streaming_server_url: str)
def __init__(wallet_account: `LocalAccount`, og_llm_server_url: str, og_llm_streaming_server_url: str, network_filter: str = 'og-evm')
```

#### Methods
Expand Down
20 changes: 19 additions & 1 deletion docs/opengradient/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ blockchain private key and optional Model Hub credentials.
#### Constructor

```python
def __init__(private_key: str, email: Optional[str] = None, password: Optional[str] = None, rpc_url: str = 'https://ogevmdevnet.opengradient.ai', api_url: str = 'https://sdk-devnet.opengradient.ai', contract_address: str = '0x8383C9bD7462F12Eb996DD02F78234C0421A6FaE', og_llm_server_url: Optional[str] = 'https://llmogevm.opengradient.ai', og_llm_streaming_server_url: Optional[str] = 'https://llmogevm.opengradient.ai')
def __init__(private_key: str, email: Optional[str] = None, password: Optional[str] = None, rpc_url: str = 'https://ogevmdevnet.opengradient.ai', api_url: str = 'https://sdk-devnet.opengradient.ai', contract_address: str = '0x8383C9bD7462F12Eb996DD02F78234C0421A6FaE', og_llm_server_url: Optional[str] = 'https://llmogevm.opengradient.ai', og_llm_streaming_server_url: Optional[str] = 'https://llmogevm.opengradient.ai', payment_network: `PaymentNetwork` = PaymentNetwork.OG_EVM)
```

**Arguments**
Expand All @@ -139,6 +139,7 @@ def __init__(private_key: str, email: Optional[str] = None, password: Optio
* **`contract_address`**: Inference contract address.
* **`og_llm_server_url`**: OpenGradient LLM server URL.
* **`og_llm_streaming_server_url`**: OpenGradient LLM streaming server URL.
* **`payment_network`**: Payment network for x402 transactions. Defaults to OG_EVM.

#### Variables

Expand All @@ -156,6 +157,23 @@ Enum for the different inference modes available for inference (VANILLA, ZKML, T
* static `VANILLA`
* static `ZKML`

### `PaymentNetwork`

Payment network for x402 payment protocol transactions.

Controls which blockchain network is used for paying for LLM inference.

**Attributes**

* **`OG_EVM`**: Pay on the OpenGradient EVM network (default).
* **`BASE_SEPOLIA`**: Pay on the Base Sepolia testnet using the OG token
at address ``0x240b09731D96979f50B2C649C9CE10FcF9C7987F``.

#### Variables

* static `BASE_SEPOLIA`
* static `OG_EVM`

### `TEE_LLM`

Enum for LLM models available for TEE (Trusted Execution Environment) execution.
Expand Down
17 changes: 17 additions & 0 deletions docs/opengradient/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ def __init__(name: str, values: List[Tuple[int, int]])
* static `name` : str
* static `values` : List[Tuple[int, int]]

### `PaymentNetwork`

Payment network for x402 payment protocol transactions.

Controls which blockchain network is used for paying for LLM inference.

**Attributes**

* **`OG_EVM`**: Pay on the OpenGradient EVM network (default).
* **`BASE_SEPOLIA`**: Pay on the Base Sepolia testnet using the OG token
at address ``0x240b09731D96979f50B2C649C9CE10FcF9C7987F``.

#### Variables

* static `BASE_SEPOLIA`
* static `OG_EVM`

### `SchedulerParams`

#### Constructor
Expand Down
2 changes: 2 additions & 0 deletions examples/run_x402_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

client = og.Client(
private_key=os.environ.get("OG_PRIVATE_KEY"),
# To pay via Base Sepolia instead of OG EVM:
# payment_network=og.PaymentNetwork.BASE_SEPOLIA,
)

messages = [
Expand Down
2 changes: 2 additions & 0 deletions src/opengradient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
InferenceResult,
ModelOutput,
ModelRepository,
PaymentNetwork,
SchedulerParams,
TextGenerationOutput,
TextGenerationStream,
Expand Down Expand Up @@ -132,6 +133,7 @@ def init(
"init",
"TEE_LLM",
"InferenceMode",
"PaymentNetwork",
"HistoricalInputQuery",
"SchedulerParams",
"CandleType",
Expand Down
4 changes: 1 addition & 3 deletions src/opengradient/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,7 @@ def infer(ctx, model_cid: str, inference_mode: str, input_data, input_file: Path
model_input = json.load(file)

click.echo(f'Running {inference_mode} inference for model "{model_cid}"')
inference_result = client.alpha.infer(
model_cid=model_cid, inference_mode=InferenceModes[inference_mode], model_input=model_input
)
inference_result = client.alpha.infer(model_cid=model_cid, inference_mode=InferenceModes[inference_mode], model_input=model_input)

click.echo() # Add a newline for better spacing
click.secho("✅ Transaction successful", fg="green", bold=True)
Expand Down
5 changes: 4 additions & 1 deletion src/opengradient/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
DEFAULT_OPENGRADIENT_LLM_STREAMING_SERVER_URL,
DEFAULT_RPC_URL,
)
from ..types import PaymentNetwork
from .alpha import Alpha
from .llm import LLM
from .model_hub import ModelHub
Expand Down Expand Up @@ -50,6 +51,7 @@ def __init__(
contract_address: str = DEFAULT_INFERENCE_CONTRACT_ADDRESS,
og_llm_server_url: Optional[str] = DEFAULT_OPENGRADIENT_LLM_SERVER_URL,
og_llm_streaming_server_url: Optional[str] = DEFAULT_OPENGRADIENT_LLM_STREAMING_SERVER_URL,
payment_network: PaymentNetwork = PaymentNetwork.OG_EVM,
):
"""
Initialize the OpenGradient client.
Expand All @@ -63,6 +65,7 @@ def __init__(
contract_address: Inference contract address.
og_llm_server_url: OpenGradient LLM server URL.
og_llm_streaming_server_url: OpenGradient LLM streaming server URL.
payment_network: Payment network for x402 transactions. Defaults to OG_EVM.
"""
blockchain = Web3(Web3.HTTPProvider(rpc_url))
wallet_account = blockchain.eth.account.from_key(private_key)
Expand All @@ -78,6 +81,7 @@ def __init__(
wallet_account=wallet_account,
og_llm_server_url=og_llm_server_url,
og_llm_streaming_server_url=og_llm_streaming_server_url,
network_filter=payment_network.value,
)

self.alpha = Alpha(
Expand All @@ -86,4 +90,3 @@ def __init__(
inference_hub_contract_address=contract_address,
api_url=api_url,
)

15 changes: 11 additions & 4 deletions src/opengradient/client/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,23 @@ class LLM:
result = client.llm.completion(model=TEE_LLM.CLAUDE_3_5_HAIKU, prompt="Hello")
"""

def __init__(self, wallet_account: LocalAccount, og_llm_server_url: str, og_llm_streaming_server_url: str):
def __init__(
self,
wallet_account: LocalAccount,
og_llm_server_url: str,
og_llm_streaming_server_url: str,
network_filter: str = DEFAULT_NETWORK_FILTER,
):
self._wallet_account = wallet_account
self._og_llm_server_url = og_llm_server_url
self._og_llm_streaming_server_url = og_llm_streaming_server_url
self._network_filter = network_filter

def _og_payment_selector(self, accepts, network_filter=DEFAULT_NETWORK_FILTER, scheme_filter=None, max_value=None):
def _og_payment_selector(self, accepts, network_filter=None, scheme_filter=None, max_value=None):
"""Custom payment selector for OpenGradient network."""
return x402Client.default_payment_requirements_selector(
accepts,
network_filter=network_filter,
network_filter=network_filter or self._network_filter,
scheme_filter=scheme_filter,
max_value=max_value,
)
Expand Down Expand Up @@ -418,7 +425,7 @@ async def _tee_llm_chat_stream_async(
limits=LIMITS,
http2=False,
follow_redirects=False,
auth=X402Auth(account=self._wallet_account, network_filter=DEFAULT_NETWORK_FILTER), # type: ignore
auth=X402Auth(account=self._wallet_account, network_filter=self._network_filter), # type: ignore
verify=True,
) as client:
headers = {
Expand Down
1 change: 1 addition & 0 deletions src/opengradient/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
DEFAULT_OPENGRADIENT_LLM_SERVER_URL = "https://llmogevm.opengradient.ai"
DEFAULT_OPENGRADIENT_LLM_STREAMING_SERVER_URL = "https://llmogevm.opengradient.ai"
DEFAULT_NETWORK_FILTER = "og-evm"
DEFAULT_BASE_SEPOLIA_NETWORK_FILTER = "base-sepolia"
22 changes: 22 additions & 0 deletions src/opengradient/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@
import numpy as np


class PaymentNetwork(str, Enum):
"""
Payment network for x402 payment protocol transactions.

Controls which blockchain network is used for paying for LLM inference.

Attributes:
OG_EVM: Pay on the OpenGradient EVM network (default).
BASE_SEPOLIA: Pay on the Base Sepolia testnet using the OG token
at address ``0x240b09731D96979f50B2C649C9CE10FcF9C7987F``.

Examples:
>>> client = og.Client(
... private_key="0x...",
... payment_network=og.PaymentNetwork.BASE_SEPOLIA,
... )
"""

OG_EVM = "og-evm"
BASE_SEPOLIA = "base-sepolia"


class x402SettlementMode(str, Enum):
"""
Settlement modes for x402 payment protocol transactions.
Expand Down
Loading