import React, { useState, useEffect } from 'react'; import { AIRouterProvider, useAIRouter, useAIRouterContext, createWebLLMConfig, isWebLLMSupported, generateViralPollRoute, z } from '../src'; /** * FREE LLM SELECTION COMPONENT * * This example shows how users can choose from various free LLMs that adhere to OpenAI standards: * * 1. LOCAL MODELS (WebLLM) - Completely free, runs in browser * 2. FREE OPENAI-COMPATIBLE APIs - Various providers offering free tiers * 3. OPENROUTER FREE MODELS - Free models via OpenRouter API */ // ============ FREE LLM OPTIONS ============ interface FreeLLMOption { id: string name: string provider: 'webllm' | 'openai-compatible' | 'openrouter-free' description: string pros: string[] cons: string[] setup: { requiresApi: boolean steps: string[] } performance: { speed: 'fast' | 'medium' | 'slow' quality: 'high' | 'medium' | 'basic' privacy: 'complete' | 'good' | 'basic' } } const FREE_LLM_OPTIONS: FreeLLMOption[] = [ // LOCAL WEBLLM MODELS (Completely Free) { id: 'llama-3.2-3b', name: 'Llama 3.2 3B (Local)', provider: 'webllm', description: 'Meta\'s Llama 3.2 3B model running locally in your browser', pros: [ 'Completely free forever', 'Complete privacy - nothing leaves your device', 'No API keys needed', 'Works offline once loaded', 'Good reasoning capabilities' ], cons: [ 'Requires modern GPU (WebGPU support)', 'Initial download ~2GB', 'Slower than cloud models', 'Limited context window' ], setup: { requiresApi: false, steps: [ 'Enable WebGPU in your browser', 'Click "Use Local Model"', 'Wait for initial download (~2GB)', 'Model ready to use!' ] }, performance: { speed: 'medium', quality: 'medium', privacy: 'complete' } }, { id: 'llama-3.2-1b', name: 'Llama 3.2 1B (Local)', provider: 'webllm', description: 'Smaller, faster version of Llama 3.2', pros: [ 'Completely free', 'Smaller download (~800MB)', 'Faster inference', 'Good for simple tasks' ], cons: [ 'Lower quality than 3B model', 'Limited reasoning', 'Still requires WebGPU' ], setup: { requiresApi: false, steps: [ 'Enable WebGPU in browser', 'Select Llama 3.2 1B', 'Wait for download', 'Ready to use' ] }, performance: { speed: 'fast', quality: 'basic', privacy: 'complete' } }, { id: 'phi-3.5-mini', name: 'Phi-3.5 Mini (Local)', provider: 'webllm', description: 'Microsoft\'s efficient small language model', pros: [ 'Excellent for coding tasks', 'Very fast inference', 'Good reasoning despite size' ], cons: [ 'Specialized for certain tasks', 'Limited general knowledge' ], setup: { requiresApi: false, steps: [ 'Check WebGPU compatibility', 'Select Phi-3.5 Mini', 'Download and initialize' ] }, performance: { speed: 'fast', quality: 'medium', privacy: 'complete' } }, // FREE OPENAI-COMPATIBLE APIS { id: 'groq-llama', name: 'Groq Llama 3.1 70B (Free Tier)', provider: 'openai-compatible', description: 'Ultra-fast inference via Groq\'s LPU chips', pros: [ 'Extremely fast (500+ tokens/sec)', 'High quality responses', 'Generous free tier', 'OpenAI-compatible API' ], cons: [ 'Requires free Groq account', 'Rate limits on free tier', 'API calls go to Groq servers' ], setup: { requiresApi: true, steps: [ 'Sign up at console.groq.com', 'Get free API key', 'Use base URL: https://api.groq.com/openai/v1', 'Model: llama-3.1-70b-versatile' ] }, performance: { speed: 'fast', quality: 'high', privacy: 'good' } }, { id: 'together-llama', name: 'Together AI Llama 3.1 (Free)', provider: 'openai-compatible', description: 'Free access to Llama models via Together AI', pros: [ 'Multiple model options', 'Good free tier limits', 'OpenAI API compatibility', 'Fast inference' ], cons: [ 'Requires account signup', 'Monthly free limits', 'Data sent to Together AI' ], setup: { requiresApi: true, steps: [ 'Create account at together.ai', 'Generate API key', 'Base URL: https://api.together.xyz/v1', 'Choose from various Llama models' ] }, performance: { speed: 'fast', quality: 'high', privacy: 'good' } }, // OPENROUTER FREE MODELS { id: 'openrouter-free-models', name: 'OpenRouter Free Models', provider: 'openrouter-free', description: 'Various free models available through OpenRouter', pros: [ 'Access to multiple free models', 'Easy switching between models', 'Some models completely free', 'Good model variety' ], cons: [ 'Some models have usage limits', 'Quality varies by model', 'Requires OpenRouter account' ], setup: { requiresApi: true, steps: [ 'Sign up at openrouter.ai', 'Get API key (free tier available)', 'Use models with $0.00 pricing', 'Examples: google/gemma-2-9b-it:free' ] }, performance: { speed: 'medium', quality: 'medium', privacy: 'basic' } } ] // ============ MAIN COMPONENT ============ export default function FreeLLMSelectionDemo() { const [selectedLLM, setSelectedLLM] = useState(null) const [webLLMSupported, setWebLLMSupported] = useState(null) const [customApiConfig, setCustomApiConfig] = useState({ baseUrl: '', apiKey: '', model: '' }) useEffect(() => { isWebLLMSupported().then(setWebLLMSupported) }, []) return (

๐Ÿ†“ Choose Your Free LLM

Select from completely free, open-source models that follow OpenAI API standards

{FREE_LLM_OPTIONS.map((llm) => ( setSelectedLLM(llm)} /> ))}
{selectedLLM && (
)}

๐Ÿ“Š Quick Comparison

) } // ============ LLM OPTION CARD ============ interface LLMOptionCardProps { option: FreeLLMOption isSelected: boolean isSupported: boolean | null onSelect: () => void } function LLMOptionCard({ option, isSelected, isSupported, onSelect }: LLMOptionCardProps) { const getSupportText = () => { if (option.provider === 'webllm') { if (isSupported === null) return '๐Ÿ”„ Checking...' if (isSupported === false) return 'โŒ WebGPU not supported' return 'โœ… Supported' } return 'โœ… Available' } const getProviderIcon = () => { switch (option.provider) { case 'webllm': return '๐Ÿ’ป' case 'openai-compatible': return '๐Ÿš€' case 'openrouter-free': return '๐Ÿ”€' default: return '๐Ÿค–' } } return (
{getProviderIcon()}

{option.name}

{getSupportText()}

{option.description}

Speed: {option.performance.speed}
Quality: {option.performance.quality}
Privacy: {option.performance.privacy}
โœ… Pros:
    {option.pros.slice(0, 2).map((pro, idx) => (
  • {pro}
  • ))}
) } // ============ SETUP GUIDE ============ interface LLMSetupGuideProps { option: FreeLLMOption customConfig: any onConfigChange: (config: any) => void } function LLMSetupGuide({ option, customConfig, onConfigChange }: LLMSetupGuideProps) { return (

๐Ÿ› ๏ธ Setup Guide: {option.name}

Setup Steps:

    {option.setup.steps.map((step, idx) => (
  1. {step}
  2. ))}
{option.setup.requiresApi && (

API Configuration:

onConfigChange({ ...customConfig, baseUrl: e.target.value })} />
onConfigChange({ ...customConfig, apiKey: e.target.value })} />
onConfigChange({ ...customConfig, model: e.target.value })} />
)}

โœ… Advantages:

    {option.pros.map((pro, idx) => (
  • {pro}
  • ))}

โš ๏ธ Considerations:

    {option.cons.map((con, idx) => (
  • {con}
  • ))}
) } // ============ LLM TESTER ============ interface LLMTesterProps { selectedLLM: FreeLLMOption customConfig: any } function LLMTester({ selectedLLM, customConfig }: LLMTesterProps) { // Create dynamic configuration based on selected LLM const createProviderConfig = () => { if (selectedLLM.provider === 'webllm') { return createWebLLMConfig(selectedLLM.id.includes('llama-3.2-3b') ? 'Llama-3.2-3B-Instruct-q4f32_1' : selectedLLM.id.includes('llama-3.2-1b') ? 'Llama-3.2-1B-Instruct-q4f32_1' : 'Phi-3.5-mini-instruct-q4f16_1' ) } else { // For API-based models, create OpenAI-compatible config return { provider: 'openai' as const, apiKey: customConfig.apiKey || 'demo-key', baseUrl: customConfig.baseUrl || 'https://api.groq.com/openai/v1', model: customConfig.model || 'llama-3.1-70b-versatile', maxTokens: 500, temperature: 0.7 } } } const config = { defaultProvider: createProviderConfig(), fallbackProviders: [], enableRetries: true, maxRetries: 1, timeout: 30000 } return (

๐Ÿงช Test {selectedLLM.name}

) } function TestInterface({ selectedLLM }: { selectedLLM: FreeLLMOption }) { const [testPrompt, setTestPrompt] = useState('Create a prediction market question about renewable energy adoption.') const { execute, isLoading, error, response } = useAIRouter(generateViralPollRoute) const handleTest = async () => { try { await execute({ article_data: { url: 'https://example.com/test', title: 'Renewable Energy Test Article', summary: testPrompt, news_site: 'test.com', tags: ['energy', 'test'] }, perspective: 'balanced' as const, payment_token: 'FLOW' as const }) } catch (err) { console.error('Test failed:', err) } } return (