File size: 5,945 Bytes
6dc9d46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Examples Directory

Integration examples for RagBot in different environments.

## Contents

### `test_website.html`
HTML example for integrating RagBot biomarker analysis into a web application.

**Features:**
- Form-based biomarker input
- JavaScript/fetch POST requests to RagBot API
- Real-time result display
- Responsive design

**Usage:**
```bash
1. Start API: python -m uvicorn api.app.main:app
2. Open: examples/test_website.html in browser
3. Enter biomarkers and submit
```

**Integration Points:**
- POST to `http://localhost:8000/api/v1/analyze`
- Handles JSON responses
- Displays analysis results

---

### `website_integration.js`
JavaScript utility library for integrating RagBot into web applications.

**Features:**
- Biomarker validation
- API request handling
- Response parsing
- Error handling

**Usage:**
```html
<script src="examples/website_integration.js"></script>

<script>
  const ragbot = new RagBotClient('http://localhost:8000');
  
  ragbot.analyze({
    biomarkers: {
      'Glucose': 140,
      'HbA1c': 10.0
    }
  }).then(result => {
    console.log('Analysis:', result);
  });
</script>
```

---

## Creating Your Own Integration

### For Web Applications

```javascript
// 1. Initialize client
const client = new RagBotClient('http://localhost:8000');

// 2. Get biomarkers from user form
const biomarkers = {
  'Glucose': parseFloat(document.getElementById('glucose').value),
  'HbA1c': parseFloat(document.getElementById('hba1c').value)
};

// 3. Call analysis endpoint
client.analyze({ biomarkers })
  .then(result => {
    // Display prediction
    console.log(`Disease: ${result.prediction.disease}`);
    console.log(`Confidence: ${result.prediction.confidence}`);
    
    // Show recommendations
    result.recommendations.immediate_actions.forEach(action => {
      console.log(`Action: ${action}`);
    });
  })
  .catch(error => console.error('Analysis failed:', error));
```

### For Mobile Apps (React Native)

```javascript
import fetch from 'react-native-fetch';

const analyzeBiomarkers = async (biomarkers) => {
  const response = await fetch(
    'http://ragbot-api.yourserver.com/api/v1/analyze',
    {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ biomarkers })
    }
  );
  return response.json();
};

// Usage in component
const [result, setResult] = useState(null);
analyzeBiomarkers(userBiomarkers).then(setResult);
```

### For Python Applications

```python
import requests

API_URL = 'http://localhost:8000/api/v1'

biomarkers = {
    'Glucose': 140,
    'HbA1c': 10.0,
    'LDL Cholesterol': 150
}

response = requests.post(
    f'{API_URL}/analyze',
    json={'biomarkers': biomarkers}
)

result = response.json()
print(f"Disease: {result['prediction']['disease']}")
print(f"Confidence: {result['prediction']['confidence']}")
```

### For Server-Side (Node.js)

```javascript
const axios = require('axios');

async function analyzePatient(biomarkers) {
  try {
    const response = await axios.post(
      'http://localhost:8000/api/v1/analyze',
      { biomarkers }
    );
    return response.data;
  } catch (error) {
    console.error('API Error:', error.response.data);
  }
}

// Usage
const result = await analyzePatient({
  'Glucose': 140,
  'HbA1c': 10.0
});
```

---

## Deployment Scenarios

### Scenario 1: Web Dashboard

```
Healthcare Portal (React/Vue)
         ↓
   RagBot API (FastAPI)
         ↓
   Multi-Agent Workflow
         ↓
   FAISS Vector Store + Groq LLM
```

### Scenario 2: Mobile App

```
Mobile App (React Native/Flutter)
         ↓
   RagBot API (Cloud Deployment)
         ↓
   Multi-Agent Workflow
         ↓
   FAISS Vector Store + Groq LLM
```

### Scenario 3: EHR Integration

```
Electronic Health Record System
         ↓
   RagBot Embedded Library
         ↓
   Multi-Agent Workflow (in-process)
         ↓
   FAISS Vector Store + Groq/OpenAI LLM
```

---

## Configuration for Production

### CORS Setup

```python
# api/app/main.py
from fastapi.middleware.cors import CORSMiddleware

app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://yourdomain.com"],
    allow_methods=["POST", "GET"],
    allow_headers=["Content-Type"],
)
```

### Authentication

```javascript
// Add API key to requests
const headers = {
  'Content-Type': 'application/json',
  'Authorization': `Bearer ${API_KEY}`
};

fetch('http://api.ragbot.com/api/v1/analyze', {
  method: 'POST',
  headers,
  body: JSON.stringify({ biomarkers })
});
```

### Rate Limiting

Configure in `api/app/main.py`:

```python
from slowapi import Limiter
from slowapi.util import get_remote_address

limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter

@app.post("/api/v1/analyze")
@limiter.limit("100/minute")
async def analyze(request: Request, ...):
    ...
```

---

## Testing Your Integration

### Basic Test

```bash
# 1. Start API
python -m uvicorn api.app.main:app

# 2. In another terminal, test endpoint
curl -X POST http://localhost:8000/api/v1/analyze \
  -H "Content-Type: application/json" \
  -d '{
    "biomarkers": {
      "Glucose": 140,
      "HbA1c": 10.0
    }
  }'
```

### Load Testing

```bash
# Install Apache Bench
ab -n 100 -c 10 -p data.json \
   http://localhost:8000/api/v1/analyze
```

---

## Troubleshooting Integration Issues

### CORS Errors

**Problem:** "No 'Access-Control-Allow-Origin' header"

**Solution:** Configure CORS in API settings

### Connection Timeouts

**Problem:** Request hangs after 30 seconds

**Solution:** 
- Increase timeout
- Check API server logs
- Verify network connectivity

### Invalid Biomarker Names

**Problem:** "Invalid biomarker" error

**Solution:**
- Check `config/biomarker_references.json`
- Normalize names properly (case-sensitive)

---

For more information:
- [API Documentation](../docs/API.md)
- [Architecture](../docs/ARCHITECTURE.md)
- [Development Guide](../docs/DEVELOPMENT.md)