"use client"; import * as React from "react"; import { useState } from "react"; import { useAccount } from "wagmi"; import { useRouter } from "next/navigation"; import { use0GBroker } from "../../../hooks/use0GBroker"; import { useOptimizedDataFetching } from "../../../hooks/useOptimizedDataFetching"; import { useNavigation } from "../../../components/OptimizedNavigation"; import ReactMarkdown from "react-markdown"; // Convert neuron to A0GI (1 A0GI = 10^18 neuron) const neuronToA0gi = (value: bigint): number => { const divisor = BigInt(10 ** 18); const integerPart = value / divisor; const remainder = value % divisor; const decimalPart = Number(remainder) / Number(divisor); return Number(integerPart) + decimalPart; }; interface Provider { address: string; model: string; name: string; verifiability: string; url?: string; endpoint?: string; inputPrice?: number; outputPrice?: number; inputPriceNeuron?: bigint; outputPriceNeuron?: bigint; } // Official providers based on the documentation const OFFICIAL_PROVIDERS: Provider[] = [ { address: "0xf07240Efa67755B5311bc75784a061eDB47165Dd", model: "llama-3.3-70b-instruct", name: "Llama 3.3 70B Instruct", verifiability: "TEE (TeeML)", }, { address: "0x3feE5a4dd5FDb8a32dDA97Bed899830605dBD9D3", model: "deepseek-r1-70b", name: "DeepSeek R1 70B", verifiability: "TEE (TeeML)", }, ]; export function OptimizedInferencePage() { const { isConnected } = useAccount(); const { broker, isInitializing } = use0GBroker(); const router = useRouter(); const { setIsNavigating, setTargetRoute, setTargetPageType } = useNavigation(); const [isDrawerOpen, setIsDrawerOpen] = useState(false); const [isDrawerVisible, setIsDrawerVisible] = useState(false); const [selectedProviderForBuild, setSelectedProviderForBuild] = useState(null); const [selectedTab, setSelectedTab] = useState<'curl' | 'javascript' | 'python' | 'node'>('curl'); type TabType = 'curl' | 'javascript' | 'python' | 'node'; // Optimized providers data fetching const { data: providers, loading: providersLoading, error: providersError } = useOptimizedDataFetching({ fetchFn: async () => { if (!broker) throw new Error('Broker not available'); try { const services = await broker.inference.listService(); // Transform services to Provider format const transformedProviders: Provider[] = services.map( (service: unknown) => { const serviceObj = service as { provider?: string; model?: string; name?: string; verifiability?: string; url?: string; inputPrice?: bigint; outputPrice?: bigint; }; const providerAddress = serviceObj.provider || ""; const rawModel = serviceObj.model || "Unknown Model"; const modelName = rawModel.includes('/') ? rawModel.split('/').slice(1).join('/') : rawModel; const rawProviderName = serviceObj.name || serviceObj.model || "Unknown Provider"; const providerName = rawProviderName.includes('/') ? rawProviderName.split('/').slice(1).join('/') : rawProviderName; const verifiability = serviceObj.verifiability || "TEE"; const serviceUrl = serviceObj.url || ""; // Convert prices from neuron to A0GI per million tokens const inputPrice = serviceObj.inputPrice ? neuronToA0gi(serviceObj.inputPrice * BigInt(1000000)) : undefined; const outputPrice = serviceObj.outputPrice ? neuronToA0gi(serviceObj.outputPrice * BigInt(1000000)) : undefined; return { address: providerAddress, model: modelName, name: providerName, verifiability: verifiability, url: serviceUrl, inputPrice, outputPrice, inputPriceNeuron: serviceObj.inputPrice, outputPriceNeuron: serviceObj.outputPrice, }; } ); return transformedProviders; } catch (err) { return OFFICIAL_PROVIDERS; } }, cacheKey: 'inference-providers', cacheTTL: 2 * 60 * 1000, // 2 minutes cache dependencies: [broker], skip: !broker, }); const handleChatWithProvider = (provider: Provider) => { setIsNavigating(true); setTargetRoute('Chat'); setTargetPageType('chat'); router.push(`/inference/chat?provider=${encodeURIComponent(provider.address)}`); }; const handleBuildWithProvider = (provider: Provider) => { setSelectedProviderForBuild(provider); setIsDrawerVisible(true); setTimeout(() => setIsDrawerOpen(true), 10); }; const handleCloseDrawer = () => { setIsDrawerOpen(false); setTimeout(() => { setIsDrawerVisible(false); setSelectedProviderForBuild(null); }, 300); }; const copyToClipboard = (text: string) => { navigator.clipboard.writeText(text); }; const formatCodeAsMarkdown = (code: string, language: string) => { // Format code as markdown with language identifier for proper highlighting return `\`\`\`${language}\n${code}\n\`\`\``; }; const getCodeExample = (tab: string) => { const examples = { curl: `curl http://127.0.0.1:3000/v1/chat/completions \\ -H "Content-Type: application/json" \\ -d '{ "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello!" } ] }'`, javascript: `const response = await fetch('http://127.0.0.1:3000/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ messages: [ { role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: 'Hello!' } ] }) }); const data = await response.json(); console.log(data);`, python: `import requests response = requests.post('http://127.0.0.1:3000/v1/chat/completions', headers={'Content-Type': 'application/json'}, json={ "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello!" } ] } ) print(response.json())`, node: `const OpenAI = require('openai'); const client = new OpenAI({ baseURL: 'http://127.0.0.1:3000/v1', apiKey: '' }); async function main() { const completion = await client.chat.completions.create({ messages: [ { role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: 'Hello!' } ], }); console.log(completion.choices[0].message); } main();` }; return examples[tab as keyof typeof examples] || examples.curl; }; if (!isConnected) { return (

Wallet Not Connected

Please connect your wallet to access AI inference features.

); } // Show loading state only for critical loading (broker initialization) const isLoading = isInitializing; const displayProviders = providers || OFFICIAL_PROVIDERS; const hasError = providersError && !providers; return (

Inference

Choose from decentralized AI providers to start chatting or integrate the service into your own application

{hasError && (

Notice

Failed to fetch live provider data. Showing fallback providers.

)} {isLoading ? (

Initializing...

) : ( <> {/* Show providers immediately with inline loading indicators for data being fetched */}
{displayProviders.map((provider) => { const isOfficial = OFFICIAL_PROVIDERS.some( (op) => op.address === provider.address ); return (
{/* Show loading indicator for individual provider data if still loading */} {providersLoading && (
)} {/* Header with name, badges and address */}

{provider.name}

{isOfficial && ( 0G )} {provider.verifiability}
{/* Pricing and address with copy button */}
{/* Pricing section first */} {(provider.inputPrice !== undefined || provider.outputPrice !== undefined) && (
{provider.inputPrice !== undefined && (
In: {provider.inputPrice.toFixed(4)}
)} {provider.outputPrice !== undefined && (
Out: {provider.outputPrice.toFixed(4)}
)} A0GI
)}
{provider.address.slice(0, 8)}...{provider.address.slice(-6)} {/* Tooltip showing full address */}
{provider.address}
{/* Copy tooltip */}
Copy address
{/* Action buttons */}
); })}
)} {!isLoading && displayProviders.length === 0 && (

No Providers Available

There are currently no AI inference providers available. Please try again later.

)} {/* Build Guide Drawer - keeping the same as original */} {isDrawerVisible && (

Build with {selectedProviderForBuild ? (selectedProviderForBuild.model.includes('/') ? selectedProviderForBuild.model.split('/').slice(1).join('/') : selectedProviderForBuild.model ) : 'Provider' }

Quick Start a Service

1. Install the 0G Compute CLI

pnpm install @0glabs/0g-serving-broker -g

2. Set environment variables

export ZG_PRIVATE_KEY=<YOUR_PRIVATE_KEY>

3. Start the server

{selectedProviderForBuild ? `0g-compute-cli serve --provider ${selectedProviderForBuild.address}` : '0g-compute-cli serve --provider ' }

4. Use OpenAI API format to make a request

{/* Tab Navigation */}
{[ { key: 'curl', label: 'cURL' }, { key: 'javascript', label: 'JavaScript' }, { key: 'python', label: 'Python' }, { key: 'node', label: 'Node.js SDK' } ].map(tab => ( ))}
{/* Code Display */}
{ const isInline = !className; if (isInline) { return ( {children} ); } return ( {children} ); }, pre: ({ children }) => (
                                    {children}
                                  
), }} > {formatCodeAsMarkdown(getCodeExample(selectedTab), selectedTab)}

Integrate into your App

SDK Documentation

Comprehensive guides for integrating 0G Compute Network into your applications.

View Documentation

Starter Kit

Ready-to-use TypeScript starter kit with examples and best practices.

View on GitHub

Not satisfied with existing providers?

Become a Provider

Learn how to add your own inference provider to the 0G Compute Network through our comprehensive documentation.

View Provider Documentation
)}
); } export default OptimizedInferencePage;