快速入门
十几行代码部署 Llama 3.1 8B。
SDK 会处理任务创建、EIP-2612 permit 和 createJob 交易,然后等待提供方启动您的端点。
npm install @mzgrid/sdk viem— 安装 SDK 和 viemBase 上的 MZG— 持有足够支付“价格 × 最长时长”的 MZGBase 上的 ETH— 少量 ETH,用于支付注资交易的 Gas
deploy.ts
import { CURRENT_TERMS_VERSION, MZGridClient } from "@mzgrid/sdk";
import { createWalletClient, custom } from "viem";
import { base } from "viem/chains";
const wallet = createWalletClient({ chain: base, transport: custom(window.ethereum!) });
// Wallet sign-in requires the Terms of Service version the user has explicitly accepted
const mzg = new MZGridClient({ apiUrl: "https://api.mzgrid.io", walletClient: wallet, termsVersion: CURRENT_TERMS_VERSION });
// 1. Let the scheduler pick the best RTX 4090 by price, reliability and proximity
const created = await mzg.jobs.create({
sku: "rtx4090",
autoSelect: true,
template: "llama-3.1-8b",
maxDurationHours: 4,
});
// 2. Deposit into escrow (EIP-2612 permit + createJob) — deposit = price × 4 h
await mzg.jobs.fund(created, wallet);
// 3. Wait for the OpenAI-compatible endpoint
const { endpointUrl } = await mzg.jobs.waitForEndpoint(created.job.id);
console.log("Ready:", endpointUrl);shell
# Sign in once with your wallet, then create an API key in the dashboard
export MZG_API_KEY="mzg_live_..."
# Browse live offers: cheapest H100 SXM with reliability >= 95
curl -s "https://api.mzgrid.io/v1/market/offers?sku=h100-sxm&minReliability=95&sort=price&limit=5"
# Create a job — returns the escrow parameters to fund on Base
curl -s -X POST "https://api.mzgrid.io/v1/jobs" \
-H "Authorization: Bearer $MZG_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"sku": "h100-sxm",
"autoSelect": true,
"template": "llama-3.3-70b",
"maxDurationHours": 6,
"config": {}
}'
# After funding, report the transaction so the job is dispatched
curl -s -X POST "https://api.mzgrid.io/v1/jobs/$JOB_ID/funded" \
-H "Authorization: Bearer $MZG_API_KEY" \
-H "Content-Type: application/json" \
-d '{"txHash": "0x..."}'poll_job.py
import os, time, requests
API = "https://api.mzgrid.io/v1"
H = {"Authorization": f"Bearer {os.environ['MZG_API_KEY']}"}
# Live GPU catalogue with availability and prices in MZG
for g in requests.get(f"{API}/catalog/gpus").json():
print(f"{g['name']:<24} {g['availableGpus']:>5} free from {g['lowestPrice']} MZG/h")
# Poll a job until its endpoint is ready
job_id = os.environ["JOB_ID"]
while True:
job = requests.get(f"{API}/jobs/{job_id}", headers=H).json()
if job["status"] == "running":
print("Endpoint:", job["endpointUrl"])
break
time.sleep(3)兼容 OpenAI
将现有客户端指向您的端点即可。
推理模板运行 vLLM,并提供 /v1/chat/completions、/v1/completions 和 /v1/embeddings。只需替换 base URL——LangChain、LlamaIndex 和 OpenAI SDK 无需任何改动即可使用。
chat.py
from openai import OpenAI
# endpointUrl from GET /v1/jobs/:id once status == "running"
client = OpenAI(base_url=f"{endpoint_url}/v1", api_key=os.environ["VLLM_API_KEY"])
resp = client.chat.completions.create(
model="meta-llama/Llama-3.1-8B-Instruct",
messages=[{"role": "user", "content": "Summarise how MZGrid escrow works in two sentences."}],
max_tokens=200,
)
print(resp.choices[0].message.content)stream.ts
import OpenAI from "openai";
const client = new OpenAI({ baseURL: `${endpointUrl}/v1`, apiKey: process.env.VLLM_API_KEY });
const stream = await client.chat.completions.create({
model: "meta-llama/Llama-3.1-8B-Instruct",
messages: [{ role: "user", content: "Write a haiku about idle GPUs." }],
stream: true,
});
for await (const chunk of stream) process.stdout.write(chunk.choices[0]?.delta?.content ?? "");API 参考
端点一览。
Base URL 为 https://api.mzgrid.io/v1。使用 Authorization: Bearer <token | key> 进行认证。
公共
- GET
/v1/health服务状态、链 ID 和已索引区块 - GET
/v1/catalog/gpusGPU 目录,含实时可用性和价格 - GET
/v1/catalog/templates工作负载模板及推荐 GPU - GET
/v1/market/offers实时报价——可按 sku、地区、显存、可靠性筛选 - GET
/v1/market/offers/:id报价详情,含价格历史和近期任务 - GET
/v1/network/stats网络总量和 24 小时数据 - GET
/v1/network/timeseriesgpuHours、jobs、providers、mzgSettled 等的每日序列… - GET
/v1/network/regions · /sku-mix · /leaderboard · /activity地理分布、GPU 构成、顶级提供方、实时动态
认证与密钥
- POST
/v1/auth/nonce为钱包地址获取登录消息 - POST
/v1/auth/verify用 EIP-191 签名换取会话令牌 - POST
/v1/keys创建 API 密钥(mzg_live_…) - GET · DELETE
/v1/keys · /v1/keys/:id列出或吊销 API 密钥
任务与账户
- POST
/v1/jobs基于报价或使用 autoSelect 创建任务;返回托管参数 - POST
/v1/jobs/:id/funded提交注资交易哈希;协调器验证 JobFunded - GET
/v1/jobs · /v1/jobs/:id列出任务,或获取单个任务的状态、端点和交易 - GET
/v1/jobs/:id/logs指定时间戳以来的容器日志 - POST
/v1/jobs/:id/stop提前停止——结算已用秒数,退还剩余部分 - GET
/v1/accountMZG 余额、托管金额、30 天支出
Base 上的智能合约
MZGToken、MZGridEscrow 和 ProviderRegistry 均已在 Basescan 上验证。在合约参考中查看每个函数、事件和常量。