Skip to content

Floyd11/OpenGradient-Developer-Cookbook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

OpenGradient Developer Cookbook 🍳

The ultimate collection of boilerplates, snippets, and templates for building trustless, verifiable AI applications using the OpenGradient SDK.

Python 3.10+ License: MIT Network: Base Sepolia Testnet OpenGradient Docs


πŸ—οΈ Architecture Overview

Your App
   β”‚
   β–Ό
og.LLM (x402 Gateway)
   β”‚  Permit2 $OPG payment on Base Sepolia
   β–Ό
TEE Node (Intel TDX Confidential Compute)
   β”‚  Hardware-attested code execution
   β–Ό
OpenAI / Anthropic / Google / xAI API
   β”‚
   β–Ό
Base Sepolia  ←── $OPG payment + TEE attestation proof
   β”‚
   β–Ό
OpenGradient Network  ←── Proof settlement (Merkle / Full / Private)

Every LLM call through OpenGradient returns a payment_hash β€” a cryptographic on-chain proof that a specific prompt was processed inside a Trusted Execution Environment. This makes AI decisions auditable, verifiable, and trustless.


πŸ“š Table of Contents

Snippets

# File What it demonstrates
01 01_llm_completion_basic.py Simple verifiable LLM text completion
02 02_llm_chat_with_tools.py Multi-turn chat with function/tool calling
03 03_llm_streaming.py Real-time streaming chat output
04 04_settlement_modes.py PRIVATE / INDIVIDUAL_FULL / BATCH_HASHED modes
05 05_model_hub_management.py Model Hub: create β†’ version β†’ upload β†’ list
06 06_check_opg_balance.py Wallet inspector: OPG + ETH balance on Base Sepolia
07 07_memsync_personalized_bot.py MemSync: store β†’ search β†’ profile β†’ chatbot
08 08_ml_inference_alpha.py On-chain ML inference (VANILLA / ZKML / TEE)
09 09_ml_workflow_deploy.py Deploy + read automated ML volatility workflow
10 10_permit2_approval.py OPG Permit2 token approval flow
11 11_langchain_agent.py LangChain agent with OG verifiable LLM backend

Boilerplates

Boilerplate Description
fastapi-verifiable-backend Production FastAPI server with verifiable AI inference endpoints
antigravity-ai-agent Autonomous agent loop with on-chain proof trail
defi-risk-analyzer DeFi smart contract auditor with TEE-verified analysis
memsync-chatbot Personalized chatbot with persistent MemSync memory
digital-twins-tracker Twin.fun share price tracker + AI commentary

πŸš€ Getting Started

Prerequisites

  • Python 3.10+
  • A testnet-only Ethereum wallet (never use mainnet funds)
  • Test $OPG tokens on Base Sepolia β€” get them at faucet.opengradient.ai
  • OpenGradient Model Hub account β€” register at hub.opengradient.ai
  • MemSync API key (optional, for snippets 07+) β€” register at app.memsync.ai

Installation

# 1. Clone the repo
git clone https://github.com/your-org/OpenGradient-Cookbook.git
cd OpenGradient-Cookbook

# 2. Create a virtual environment
python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate

# 3. Install dependencies
pip install -r requirements.txt

# 4. Configure environment variables
cp .env.example .env
# Edit .env with your testnet private key and credentials

Environment Setup

# .env β€” fill in your values
OG_PRIVATE_KEY=0xYOUR_TESTNET_PRIVATE_KEY
OG_EMAIL=your@email.com
OG_PASSWORD=your_model_hub_password
MEMSYNC_API_KEY=your_memsync_key

⚠️ SECURITY: The private key in .env must be a testnet-only wallet. Never put mainnet funds in a wallet used for development. Your .env file is in .gitignore and will never be committed.


πŸ“ Snippets

01 β€” LLM Completion Basic {#01}

Simple verifiable text completion. The "Hello World" of OpenGradient.

python snippets/01_llm_completion_basic.py

02 β€” LLM Chat with Tools {#02}

Multi-turn chat with function/tool calling. Build agents that call external APIs.

python snippets/02_llm_chat_with_tools.py

03 β€” LLM Streaming {#03}

Real-time streaming output for terminal apps and web UIs.

python snippets/03_llm_streaming.py

04 β€” Settlement Modes {#04}

Compare PRIVATE / INDIVIDUAL_FULL / BATCH_HASHED β€” choose the right privacy/cost tradeoff.

python snippets/04_settlement_modes.py

05 β€” Model Hub Management {#05}

Create a model repo, version it, upload an ONNX file, and list files.

python snippets/05_model_hub_management.py

06 β€” Check OPG Balance {#06}

Inspect your wallet's ETH and $OPG balance before running inference.

python snippets/06_check_opg_balance.py

07 β€” MemSync Personalized Bot {#07}

Store conversations β†’ search memories β†’ retrieve user profile using MemSync.

python snippets/07_memsync_personalized_bot.py

08 β€” ML Inference Alpha {#08}

Run on-chain ML model inference in VANILLA, ZKML, and TEE modes (alpha testnet).

python snippets/08_ml_inference_alpha.py

09 β€” ML Workflow Deploy {#09}

Deploy an automated ETH volatility prediction workflow with oracle data feeds.

python snippets/09_ml_workflow_deploy.py

10 β€” Permit2 Approval {#10}

Approve $OPG tokens via Permit2 β€” required once before any LLM inference.

python snippets/10_permit2_approval.py

11 β€” LangChain Agent {#11}

Use OpenGradient's verifiable LLM as a drop-in LangChain backend.

python snippets/11_langchain_agent.py

πŸ—οΈ Boilerplates

A production-ready REST API server with /infer, /chat, and /stream endpoints. Returns verifiable payment_hash with every AI response.

Fully autonomous agent that breaks tasks into steps, executes them with verifiable LLM inference, and logs a cryptographic proof trail to agent_log.jsonl.

Analyzes Solidity smart contracts and loan applications using TEE-verified LLM. Every analysis includes a payment_hash for regulatory audit compliance.

A personalized chatbot that remembers users across sessions using MemSync's semantic memory layer combined with OpenGradient's verifiable LLM.

Track Twin.fun share prices, check your holdings, and get AI-generated market commentary β€” all with verifiable on-chain proof of the AI analysis.


🎯 Use Cases

Domain Use Case Key Feature
DeFi Smart contract audits, trading agent decisions INDIVIDUAL_FULL settlement β€” proof of every prompt
Finance Loan risk assessment, fraud detection TEE verification β€” hardware-attested privacy
Healthcare Patient data analysis, clinical decision support TEE confidential compute β€” data never exposed
Enterprise AI Content moderation, compliance logging Verifiable audit trails for regulatory requirements
AI Agents Autonomous agents with provable actions payment_hash per step β€” full reasoning transparency
Consumer Apps Personalized assistants, tutors, CRM MemSync β€” persistent memory across sessions

🀝 Contributing

Contributions are welcome! Here's how:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/my-snippet
  3. Follow the coding standards:
    • Python 3.10+ with full type hints
    • Google-style docstrings on all modules and functions
    • logging for operational messages, print() for user-facing output
    • All secrets via os.getenv() β€” never hardcoded
  4. Test your snippet: python snippets/your_snippet.py
  5. Open a Pull Request with a clear description

Adding a New Snippet

  • Place it in snippets/ with the next number prefix (e.g. 12_my_feature.py)
  • Add it to the Table of Contents in this README
  • Use utils/client.py for SDK initialization

Adding a New Boilerplate

  • Create a new folder in boilerplates/
  • Include main.py (or equivalent), requirements.txt, and README.md
  • The README must include: what it does, how to run, example output

πŸ“– Resources


πŸ“„ License

MIT β€” see LICENSE for details.

About

OpenGradient Developer Cookbook - A collection of boilerplates, snippets, and utilities for building decentralized AI applications on OpenGradient.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages