Skip to content

Commit 69fac61

Browse files
authored
Refactor: update the doc to support compute network v0.1.1 (#114)
1 parent 97a8902 commit 69fac61

File tree

4 files changed

+12021
-7365
lines changed

4 files changed

+12021
-7365
lines changed

docs/build-with-0g/compute-network/provider.md

+4-33
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ Please visit the [releases page](https://github.com/0glabs/0g-serving-broker/rel
8585
- Copy the `config.example.yaml` file.
8686
- Modify `servingUrl` to point to your publicly exposed URL.
8787
- Set `privateKeys` to your wallet's private key for the 0G blockchain.
88+
- Set `servingUrl` to your service's public URL.
89+
- Set `targetUrl` to your internal URL corresponding to the prepared LLM service.
90+
- Set `model` to the model name of your LLM service.
8891
- Save the file as `config.local.yaml`.
8992
- Replace `#PORT#` in `docker-compose.yml` with the port you want to use. It should be the same as the port of `servingUrl` in `config.local.yaml`.
9093

@@ -94,36 +97,4 @@ Please visit the [releases page](https://github.com/0glabs/0g-serving-broker/rel
9497
docker compose -f docker-compose.yml up -d
9598
```
9699

97-
### Key Commands
98-
99-
1. **Register the Service**
100-
101-
The compute network currently supports `chatbot` services. Additional services are in the pipeline to be released soon.
102-
103-
```bash
104-
curl -X POST http://127.0.0.1:<PORT>/v1/service \
105-
-H "Content-Type: application/json" \
106-
-d '{
107-
     "URL": "<endpoint_of_the_prepared_service>",
108-
     "inputPrice": "10000000",
109-
     "outputPrice": "20000000",
110-
     "Type": "chatbot",
111-
     "Name": "llama8Bb",
112-
     "Model": "llama-3.1-8B-Instruct",
113-
"verifiability":"TeeML"
114-
}'
115-
```
116-
117-
- `inputPrice` and `outputPrice` vary by service type, for `chatbot`, they represent the cost per token. The unit is in neuron. 1 A0GI = 1e18 neuron.
118-
119-
2. **Settle the Fee**
120-
121-
```bash
122-
curl -X POST http://127.0.0.1:<PORT>/v1/settle
123-
```
124-
125-
- The provider broker has an automatic settlement engine that ensures you can collect fees promptly before your customer's account balance is insufficient, while also minimizing the frequency of charges to reduce gas consumption.
126-
127-
### Additional API Information
128-
129-
For more details, please refer to the <a href="/html/compute-network-provider-api.html" target="_blank" rel="noopener noreferrer">API Page</a>.
100+
The provider broker has an automatic settlement engine that ensures you can collect fees promptly before your customer's account balance is insufficient, while also minimizing the frequency of charges to reduce gas consumption.

docs/build-with-0g/compute-network/sdk.md

+50-28
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import TabItem from '@theme/TabItem';
99

1010
## Overview
1111

12-
The 0G Compute Network SDK enables developers to integrate AI inference services from the 0G Compute Network into their applications. Currently, the 0G Compute Network SDK supports Large Language Model (LLM) inference services, with fine-tuning and additional features planned for future releases.
12+
The 0G Compute Network SDK enables developers to integrate AI inference services from the 0G Compute Network into their applications. Currently, the 0G Compute Network SDK supports Large Language Model (LLM) inference services, with fine-tuning and additional features planned for future releases.
1313

1414
In just five minutes, you can initialize your broker to manage operations, set up and fund your account to pay for services, and learn how to send requests and handle responses.
1515

@@ -35,13 +35,15 @@ pnpm add @0glabs/0g-serving-broker @types/[email protected] [email protected]
3535
The broker is your main interface to the 0G Compute Network. It handles authentication, billing, and service communication.
3636

3737
Before initializing the broker, you need:
38+
3839
1. A wallet signer (implements either `JsonRpcSigner` or `Wallet` from ethers package)
3940
2. (Optional) The contract address for the 0G Serving contract
4041

4142
**Option 1: Using a private key**
43+
4244
```typescript
4345
const { ethers } = require("ethers");
44-
const { createZGServingNetworkBroker } = require("@0glabs/0g-serving-broker");
46+
const { createZGComputeNetworkBroker } = require("@0glabs/0g-serving-broker");
4547

4648
const provider = new ethers.JsonRpcProvider("https://evmrpc-testnet.0g.ai");
4749

@@ -50,25 +52,26 @@ const privateKey = "INPUT_PRIVATE_KEY_HERE";
5052
const wallet = new ethers.Wallet(privateKey, provider);
5153

5254
// Initialize broker
53-
const broker = await createZGServingNetworkBroker(wallet);
55+
const broker = await createZGComputeNetworkBroker(wallet);
5456
```
5557

5658
**Option 2: Using a browser wallet**
59+
5760
```typescript
5861
import { BrowserProvider } from "ethers";
59-
import { createZGServingNetworkBroker } from "@0glabs/0g-serving-broker";
62+
import { createZGComputeNetworkBroker } from "@0glabs/0g-serving-broker";
6063

6164
async function initializeBrokerWithWallet() {
6265
// Ensure wallet is installed
63-
if (typeof window.ethereum !== 'undefined') {
66+
if (typeof window.ethereum !== "undefined") {
6467
// Create a provider
6568
const provider = new BrowserProvider(window.ethereum);
66-
69+
6770
// Get the signer
6871
const signer = await provider.getSigner();
69-
72+
7073
// Initialize broker
71-
const broker = await createZGServingNetworkBroker(signer);
74+
const broker = await createZGComputeNetworkBroker(signer);
7275
}
7376
}
7477
```
@@ -80,57 +83,70 @@ async function initializeBrokerWithWallet() {
8083
The 0G Compute Network hosts multiple AI service providers. The service discovery process helps you find and select the appropriate services for your needs.
8184

8285
```typescript
83-
const services = await broker.listService();
86+
const services = await broker.inference.listService();
8487
```
8588

8689
Each service contains the following information:
90+
8791
```typescript
8892
type ServiceStructOutput = {
89-
provider: string; // Provider's wallet address (unique identifier)
90-
name: string; // Service name
93+
provider: string; // Provider's wallet address (unique identifier)
9194
serviceType: string; // Type of service
92-
url: string; // Service URL
95+
url: string; // Service URL
9396
inputPrice: bigint; // Price for input processing
9497
outputPrice: bigint; // Price for output generation
95-
updatedAt: bigint; // Last update timestamp
96-
model: string; // Model identifier
97-
verifiability: string // Indicates how the service's outputs can be verified. 'TeeML' means it runs with verification in a Trusted Execution Environment. An empty value means no verification.
98+
updatedAt: bigint; // Last update timestamp
99+
model: string; // Model identifier
100+
verifiability: string; // Indicates how the service's outputs can be verified. 'TeeML' means it runs with verification in a Trusted Execution Environment. An empty value means no verification.
98101
};
99102
```
100103

104+
Currently, we provide two official services:
105+
106+
1. **llama-3.3-70b-instruct**
107+
108+
- Provider address: `0xf07240Efa67755B5311bc75784a061eDB47165Dd`
109+
110+
2. **deepseek-r1-70b**
111+
112+
- Provider address: `0x3feE5a4dd5FDb8a32dDA97Bed899830605dBD9D3`
113+
101114
### Account Management
102115

103116
The 0G Compute Network uses a prepaid account system for each provider. Before using any services, you need to set up and fund an account for each provider you want to use.
104117

105118
#### Create an Account
119+
106120
```typescript
107-
await broker.addAccount(providerAddress, initialBalance);
121+
await broker.ledger.addLedger(initialBalance);
108122
```
123+
109124
The `initialBalance` needs to be specified in A0GI units.
110125

111126
#### Add Funds
127+
112128
```typescript
113-
await broker.depositFund(providerAddress, amount);
129+
await broker.ledger.depositFund(amount);
114130
```
131+
115132
The `amount` needs to be specified in A0GI units.
116133

117134
### Making Service Requests
118135

119136
Service usage in the 0G Network involves two key steps:
137+
120138
1. Retrieving service metadata
121139
2. Generating authenticated request headers
122140

123141
```typescript
124142
// Get service metadata
125-
const { endpoint, model } = await broker.getServiceMetadata(
126-
providerAddress,
127-
serviceName
143+
const { endpoint, model } = await broker.inference.getServiceMetadata(
144+
providerAddress
128145
);
129146

130147
// Generate request headers
131-
const headers = await broker.getRequestHeaders(
148+
const headers = await broker.inference.getRequestHeaders(
132149
providerAddress,
133-
serviceName,
134150
content
135151
);
136152
```
@@ -142,6 +158,7 @@ const headers = await broker.getRequestHeaders(
142158
You can make requests using either the native fetch API or the OpenAI SDK.
143159

144160
**Using fetch:**
161+
145162
```typescript
146163
const response = await fetch(`${endpoint}/chat/completions`, {
147164
method: "POST",
@@ -157,6 +174,7 @@ const response = await fetch(`${endpoint}/chat/completions`, {
157174
```
158175

159176
**Using OpenAI SDK:**
177+
160178
```typescript
161179
const openai = new OpenAI({
162180
baseURL: endpoint,
@@ -180,10 +198,11 @@ const completion = await openai.chat.completions.create(
180198

181199
This function is used to verify the response and settle the fee. If it is a verifiable service, it will return whether the response is valid.
182200

201+
For streaming services, `content` should be the total content received from the service. And chatID can be any chat ID of the chunk.
202+
183203
```typescript
184-
const valid = await broker.processResponse(
204+
const valid = await broker.inference.processResponse(
185205
providerAddress,
186-
serviceName,
187206
content,
188207
chatID // Optional: Only for verifiable services
189208
);
@@ -194,24 +213,27 @@ const valid = await broker.processResponse(
194213
If `processResponse` fails, you can settle fees manually by calling `settleFee` function.
195214

196215
```typescript
197-
await broker.settleFee(providerAddress, serviceName, fee);
216+
await broker.inference.settleFee(providerAddress, fee);
198217
```
199218

200219
### Helper Functions
201220

202221
#### Get Account Details
222+
203223
This function is used to get account details (like balance) on the given provider address.
224+
204225
```typescript
205-
const accountDetails = await broker.getAccount(providerAddress);
226+
const accountDetails = await broker.ledger.getLedger();
206227
```
207228

208229
#### Request Refund
230+
209231
This function is used to request a refund from your account.
232+
210233
```typescript
211-
await broker.requestRefund(user, providerAddress, amount);
234+
await broker.ledger.retrieveFund("inference", amount);
212235
```
213236

214-
215237
</TabItem>
216238

217239
<TabItem value="other" label="Other SDKs">

0 commit comments

Comments
 (0)