|
1 | 1 | # OpenGradient TypeScript SDK |
2 | 2 |
|
3 | | -A TypeScript/JavaScript SDK for performing on-chain inference using the OpenGradient network. Run machine learning models and LLMs directly on the blockchain with robust transaction handling and retry mechanisms. |
| 3 | +A TypeScript/JavaScript SDK for performing LLM chat and completion via OpenGradient's TEE (Trusted Execution Environment) with [x402](https://x402.org) payment protocol support. |
4 | 4 |
|
5 | 5 | ## Installation |
6 | 6 |
|
7 | 7 | ```bash |
8 | 8 | npm install opengradient-sdk |
9 | 9 | ``` |
10 | 10 |
|
| 11 | +## Requirements |
| 12 | + |
| 13 | +- Node.js 18+ (for global `fetch`) |
| 14 | +- A funded EVM wallet on Base (settlement happens in OPG on the Base network via [x402](https://x402.org)) |
| 15 | + |
11 | 16 | ## Quick Start |
12 | 17 |
|
13 | 18 | ```typescript |
14 | | -import { Client, InferenceMode, LLMInferenceMode } from 'opengradient-sdk'; |
| 19 | +import { Client, TEE_LLM } from "opengradient-sdk"; |
15 | 20 |
|
16 | | -// Initialize the client |
17 | 21 | const client = new Client({ |
18 | | - privateKey: 'your-private-key' |
| 22 | + privateKey: process.env.PRIVATE_KEY!, // EVM private key (with or without 0x prefix) |
| 23 | +}); |
| 24 | + |
| 25 | +// Non-streaming chat |
| 26 | +const result = await client.llm.chat({ |
| 27 | + model: TEE_LLM.CLAUDE_3_5_HAIKU, |
| 28 | + messages: [{ role: "user", content: "Hello!" }], |
| 29 | + maxTokens: 100, |
| 30 | +}); |
| 31 | +console.log(result.chatOutput?.content); |
| 32 | +console.log("payment hash:", result.paymentHash); |
| 33 | +``` |
| 34 | + |
| 35 | +### Streaming chat |
| 36 | + |
| 37 | +```typescript |
| 38 | +import { Client, TEE_LLM } from "opengradient-sdk"; |
| 39 | + |
| 40 | +const client = new Client({ privateKey: process.env.PRIVATE_KEY! }); |
| 41 | + |
| 42 | +const stream = client.llm.chat({ |
| 43 | + model: TEE_LLM.CLAUDE_3_5_HAIKU, |
| 44 | + messages: [{ role: "user", content: "Stream me a haiku." }], |
| 45 | + stream: true, |
| 46 | +}); |
| 47 | + |
| 48 | +for await (const chunk of stream) { |
| 49 | + process.stdout.write(chunk.choices[0]?.delta.content ?? ""); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Tool / function calling |
| 54 | + |
| 55 | +```typescript |
| 56 | +const result = await client.llm.chat({ |
| 57 | + model: TEE_LLM.GPT_4O, |
| 58 | + messages: [{ role: "user", content: "What's the weather in Paris?" }], |
| 59 | + tools: [ |
| 60 | + { |
| 61 | + type: "function", |
| 62 | + function: { |
| 63 | + name: "get_weather", |
| 64 | + description: "Get current weather for a city", |
| 65 | + parameters: { |
| 66 | + type: "object", |
| 67 | + properties: { city: { type: "string" } }, |
| 68 | + required: ["city"], |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + ], |
| 73 | +}); |
| 74 | +console.log(result.chatOutput?.tool_calls); |
| 75 | +``` |
| 76 | + |
| 77 | +### Completion |
| 78 | + |
| 79 | +```typescript |
| 80 | +const result = await client.llm.completion({ |
| 81 | + model: TEE_LLM.CLAUDE_3_5_HAIKU, |
| 82 | + prompt: "The capital of France is", |
| 83 | + maxTokens: 20, |
| 84 | +}); |
| 85 | +console.log(result.completionOutput); |
| 86 | +``` |
| 87 | + |
| 88 | +## x402 Settlement Modes |
| 89 | + |
| 90 | +```typescript |
| 91 | +import { X402SettlementMode } from "opengradient-sdk"; |
| 92 | + |
| 93 | +await client.llm.chat({ |
| 94 | + model: TEE_LLM.GPT_4O, |
| 95 | + messages: [{ role: "user", content: "Hi" }], |
| 96 | + x402SettlementMode: X402SettlementMode.SETTLE_BATCH, // default |
19 | 97 | }); |
| 98 | +``` |
| 99 | + |
| 100 | +- `SETTLE` — records input/output hashes only (most privacy-preserving). |
| 101 | +- `SETTLE_METADATA` — records full model info, complete input/output, and metadata. |
| 102 | +- `SETTLE_BATCH` — aggregates multiple inferences into a single on-chain settlement (most cost-efficient, default). |
20 | 103 |
|
21 | | -// Run LLM chat inference |
22 | | -const [txHash, finishReason, response] = await client.llmChat( |
23 | | - 'Qwen/Qwen2.5-72B-Instruct', |
24 | | - LLMInferenceMode.VANILLA, |
25 | | - [{ role: 'user', content: 'Hello!' }], |
26 | | - 100 // max tokens |
27 | | -); |
28 | | - |
29 | | -// Run general model inference |
30 | | -const modelInput = { |
31 | | - num_input1: [1.0, 2.0, 3.0], |
32 | | - num_input2: 10, |
33 | | - str_input1: ["hello", "ONNXY"], |
34 | | - str_input2: " world" |
35 | | -}; |
36 | | - |
37 | | -const [txHash, output] = await client.infer( |
38 | | - "QmbUqS93oc4JTLMHwpVxsE39mhNxy6hpf6Py3r9oANr8aZ", |
39 | | - InferenceMode.VANILLA, |
40 | | - modelInput |
41 | | -); |
| 104 | +## Development |
| 105 | + |
| 106 | +```bash |
| 107 | +npm install # install deps |
| 108 | +npm run lint # ESLint over src/ |
| 109 | +npm test # Jest unit tests |
| 110 | +npm run build # tsc → dist/ |
| 111 | +npm run format # prettier --write |
42 | 112 | ``` |
43 | 113 |
|
44 | | -## Features |
| 114 | +CI runs `lint`, `test`, and `build` on Node 18 and 20 — see `.github/workflows/ci.yml`. |
45 | 115 |
|
46 | | -- On-chain ML model inference |
47 | | -- LLM completion and chat interfaces |
48 | | -- Support for vanilla, ZKML and TEE (Trusted Execution Environment) inference modes |
49 | | -- Automatic transaction retry with configurable parameters |
50 | | -- Built-in gas estimation and management |
51 | | -- Tool calling support for LLM chat |
| 116 | +## Available models |
52 | 117 |
|
53 | | -## Contributing |
| 118 | +See `TEE_LLM` for the supported models, including: |
54 | 119 |
|
55 | | -We welcome contributions! Please check our contribution guidelines for more details. |
| 120 | +- `TEE_LLM.GPT_4O`, `TEE_LLM.GPT_4_1_2025_04_14`, `TEE_LLM.O4_MINI` |
| 121 | +- `TEE_LLM.CLAUDE_3_5_HAIKU`, `TEE_LLM.CLAUDE_3_7_SONNET`, `TEE_LLM.CLAUDE_4_0_SONNET` |
| 122 | +- `TEE_LLM.GEMINI_2_0_FLASH`, `TEE_LLM.GEMINI_2_5_FLASH`, `TEE_LLM.GEMINI_2_5_FLASH_LITE`, `TEE_LLM.GEMINI_2_5_PRO` |
| 123 | +- `TEE_LLM.GROK_2_1212`, `TEE_LLM.GROK_2_VISION_LATEST`, `TEE_LLM.GROK_3_BETA`, `TEE_LLM.GROK_3_MINI_BETA`, `TEE_LLM.GROK_4_1_FAST`, `TEE_LLM.GROK_4_1_FAST_NON_REASONING` |
0 commit comments