/* ============================================================================ RagBot API Integration - Ready to Copy & Paste ============================================================================ Add this to your website to integrate RagBot medical analysis Prerequisites: 1. RagBot API server running on http://localhost:8000 (or your server URL) 2. CORS is already enabled - no configuration needed! ============================================================================ */ // Configuration const RAGBOT_API_URL = 'http://localhost:8000'; // Change to your server URL // ============================================================================ // 1. SIMPLE EXAMPLE - Get Pre-run Diabetes Analysis // ============================================================================ async function getExampleAnalysis() { try { const response = await fetch(`${RAGBOT_API_URL}/api/v1/example`); const data = await response.json(); console.log('Predicted Disease:', data.analysis.prediction.predicted_disease); console.log('Confidence:', data.analysis.prediction.confidence); console.log('Full Response:', data); return data; } catch (error) { console.error('Error:', error); throw error; } } // Usage: // getExampleAnalysis().then(data => displayResults(data)); // ============================================================================ // 2. CUSTOM ANALYSIS - Submit Patient Biomarkers // ============================================================================ async function analyzePatientBiomarkers(biomarkers, patientContext = {}) { try { const response = await fetch(`${RAGBOT_API_URL}/api/v1/analyze/structured`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ biomarkers: biomarkers, patient_context: patientContext }) }); if (!response.ok) { throw new Error(`API Error: ${response.status}`); } const data = await response.json(); return data; } catch (error) { console.error('Error analyzing biomarkers:', error); throw error; } } // Usage Example: /* const biomarkers = { glucose: 180, // mg/dL hba1c: 8.2, // % ldl: 145, // mg/dL hdl: 35, // mg/dL triglycerides: 220 // mg/dL }; const patientContext = { age: 55, gender: 'male', bmi: 28.5 }; analyzePatientBiomarkers(biomarkers, patientContext) .then(data => { console.log('Disease:', data.analysis.prediction.predicted_disease); console.log('Confidence:', data.analysis.prediction.confidence); console.log('Biomarker Flags:', data.analysis.biomarker_flags); console.log('Safety Alerts:', data.analysis.safety_alerts); console.log('Recommendations:', data.analysis.recommendations); }) .catch(error => console.error('Failed:', error)); */ // ============================================================================ // 3. NATURAL LANGUAGE INPUT (Requires Ollama) // ============================================================================ async function analyzeNaturalLanguage(text, patientContext = {}) { try { const response = await fetch(`${RAGBOT_API_URL}/api/v1/analyze/natural`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ text: text, patient_context: patientContext }) }); if (!response.ok) { throw new Error(`API Error: ${response.status}`); } const data = await response.json(); return data; } catch (error) { console.error('Error analyzing text:', error); throw error; } } // Usage Example: /* const patientDescription = "The patient's glucose level is 180 and HbA1c is 8.2. LDL cholesterol is 145."; analyzeNaturalLanguage(patientDescription, { age: 55, gender: 'male' }) .then(data => console.log('Analysis:', data)) .catch(error => console.error('Failed:', error)); */ // ============================================================================ // 4. GET AVAILABLE BIOMARKERS // ============================================================================ async function getAvailableBiomarkers() { try { const response = await fetch(`${RAGBOT_API_URL}/api/v1/biomarkers`); const data = await response.json(); console.log('Total Biomarkers:', data.total); console.log('Biomarkers:', data.biomarkers); return data.biomarkers; } catch (error) { console.error('Error fetching biomarkers:', error); throw error; } } // Usage: // getAvailableBiomarkers().then(biomarkers => populateDropdown(biomarkers)); // ============================================================================ // 5. HEALTH CHECK // ============================================================================ async function checkAPIHealth() { try { const response = await fetch(`${RAGBOT_API_URL}/api/v1/health`); const data = await response.json(); return { isOnline: data.status === 'healthy', ragbotReady: data.ragbot_initialized, details: data }; } catch (error) { console.error('API is offline:', error); return { isOnline: false, ragbotReady: false, error: error.message }; } } // Usage: // checkAPIHealth().then(health => { // if (health.isOnline) { // console.log('API is ready!'); // } else { // console.log('API is offline'); // } // }); // ============================================================================ // 6. COMPLETE EXAMPLE - HTML Form Integration // ============================================================================ /*
*/ // ============================================================================ // 7. REACT INTEGRATION EXAMPLE // ============================================================================ /* import React, { useState } from 'react'; const RAGBOT_API_URL = 'http://localhost:8000'; function BiomarkerAnalysis() { const [biomarkers, setBiomarkers] = useState({ glucose: '', hba1c: '', ldl: '', hdl: '' }); const [patientContext, setPatientContext] = useState({ age: '', gender: 'male' }); const [results, setResults] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleAnalyze = async (e) => { e.preventDefault(); setLoading(true); setError(null); try { // Filter out empty values const cleanBiomarkers = Object.entries(biomarkers) .filter(([_, value]) => value !== '') .reduce((acc, [key, value]) => ({ ...acc, [key]: parseFloat(value) }), {}); const cleanContext = { age: patientContext.age ? parseInt(patientContext.age) : undefined, gender: patientContext.gender }; const response = await fetch(`${RAGBOT_API_URL}/api/v1/analyze/structured`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ biomarkers: cleanBiomarkers, patient_context: cleanContext }) }); if (!response.ok) throw new Error('Analysis failed'); const data = await response.json(); setResults(data); } catch (err) { setError(err.message); } finally { setLoading(false); } }; return (Disease: {results.analysis.prediction.predicted_disease}
Confidence: {(results.analysis.prediction.confidence * 100).toFixed(1)}%
{/* Display more results... *\/}