File size: 7,855 Bytes
0c8aadf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# RAG Platform Operator Guide

## Overview

This guide explains how to operate the hardened RAG SaaS platform after implementing the 7 critical fixes.

## For Tenants

### 1. Creating a Tenant Account

```bash

curl -X POST http://localhost:7860/v1/auth/tenants \

  -H "Content-Type: application/json" \

  -d '{

    "name": "Acme Corporation",

    "admin_email": "admin@acme.com",

    "admin_password": "secure_password"

  }'

```

**Response:**
```json

{

  "tenant_id": "abc123",

  "tenant_name": "Acme Corporation",

  "message": "Tenant created successfully. Use tenant_id 'abc123' for all future logins."

}

```

**IMPORTANT:** Save your `tenant_id` - you'll need it for all future logins.

---

### 2. Logging In

```bash

curl -X POST http://localhost:7860/v1/auth/login \

  -H "Content-Type: application/json" \

  -d '{

    "tenant_id": "abc123",

    "username": "admin@acme.com",

    "password": "secure_password"

  }'

```

**Response:**
```json

{

  "access_token": "abc123:admin@acme.com:token...",

  "token_type": "bearer",

  "tenant_id": "abc123",

  "tenant_name": "Acme Corporation"

}

```

---

### 3. Adding Sources

#### Document Upload
```bash

curl -X POST http://localhost:7860/v1/sources/ \

  -H "X-Tenant-ID: abc123" \

  -H "Content-Type: application/json" \

  -d '{

    "id": "doc1",

    "tenant_id": "abc123",

    "type": "file",

    "name": "Company Handbook",

    "uri": "/uploads/handbook.pdf",

    "metadata": {}

  }'

```

#### Web URL
```bash

curl -X POST http://localhost:7860/v1/sources/ \

  -H "X-Tenant-ID: abc123" \

  -H "Content-Type: application/json" \

  -d '{

    "id": "url1",

    "tenant_id": "abc123",

    "type": "web",

    "name": "Company Blog",

    "uri": "https://acme.com/blog",

    "metadata": {}

  }'

```

#### YouTube Video (with transcript upload)
```bash

curl -X POST http://localhost:7860/v1/sources/youtube \

  -H "X-Tenant-ID: abc123" \

  -F "youtube_url=https://youtube.com/watch?v=abc123" \

  -F "name=Product Demo" \

  -F "owner_attestation=true" \

  -F "transcript_file=@transcript.vtt"

```

**Note:** If you don't have a transcript file, the system will attempt to use YouTube captions.

---

### 4. Managing Sources

#### List All Sources
```bash

curl -X GET http://localhost:7860/v1/sources/ \

  -H "X-Tenant-ID: abc123"

```

#### List Active Sources Only
```bash

curl -X GET http://localhost:7860/v1/sources/active \

  -H "X-Tenant-ID: abc123"

```

#### Enable/Disable a Source
```bash

# Disable a source

curl -X PUT "http://localhost:7860/v1/sources/doc1/toggle?enabled=false" \

  -H "X-Tenant-ID: abc123"



# Re-enable a source

curl -X PUT "http://localhost:7860/v1/sources/doc1/toggle?enabled=true" \

  -H "X-Tenant-ID: abc123"

```

**Important:** Toggling sources is **additive**, not exclusive. Disabling one source does NOT affect others.

---

### 5. Asking Questions

```bash

curl -X POST http://localhost:7860/v1/ask/ \

  -H "X-Tenant-ID: abc123" \

  -H "Content-Type: application/json" \

  -d '{

    "query": "What is our return policy?",

    "top_k": 10

  }'

```

**Response:**
```json

{

  "answer": "According to our company handbook...",

  "citations": [

    {

      "source_type": "file",

      "document_name": "Company Handbook",

      "page": 15

    },

    {

      "source_type": "web",

      "url": "https://acme.com/blog/returns"

    }

  ],

  "confidence": 0.92,

  "sources_used": ["file", "web"],

  "retrieval_time_ms": 45.2,

  "generation_time_ms": 320.5

}

```

**Note:** The system automatically retrieves from ALL active sources (documents + URLs + YouTube).

---

### 6. Viewing Your Metrics

```bash

curl -X GET "http://localhost:7860/v1/tenants/abc123/metrics?hours=24" \

  -H "X-Tenant-ID: abc123"

```

**Response:**
```json

{

  "tenant_id": "abc123",

  "counters": {

    "retrieval_total": 150,

    "generation_total": 150,

    "ingestion_total": 25,

    "sources_created": 10

  },

  "time_series": {

    "retrieval_latency_ms": [

      {"timestamp": "2026-02-10T10:00:00", "value": 45.2, "labels": {}},

      ...

    ]

  }

}

```

**Security:** You can only view your own tenant's metrics.

---

## For Administrators

### 1. Viewing Global Metrics

```bash

curl -X GET http://localhost:7860/v1/admin/metrics

```

**Response:**
```json

{

  "global": {

    "counters": {

      "retrieval_total": 5000,

      "generation_total": 5000,

      "ingestion_total": 500

    },

    "total_tenants": 25

  },

  "by_tenant": [

    {

      "tenant_id": "abc123",

      "metrics": {

        "retrieval_total": 150,

        "generation_total": 150

      }

    },

    ...

  ]

}

```

---

### 2. Viewing Model Cache Statistics

```bash

curl -X GET http://localhost:7860/v1/metrics/cache-stats

```

**Response:**
```json

{

  "cache_size": 450,

  "cache_max_size": 1000,

  "cache_hits": 2500,

  "cache_misses": 800,

  "hit_rate": 0.757,

  "model_load_time_seconds": 12.5

}

```

---

## System Architecture

### Multi-Source Retrieval Flow

```

User Query

    ↓

Query Rewrite (optional)

    ↓

Parallel Retrieval:

    β”œβ”€ Documents Search

    β”œβ”€ URLs Search

    └─ YouTube Search

    ↓

Score Normalization

    ↓

RRF Fusion (Reciprocal Rank Fusion)

    ↓

Diversity Filter

    ↓

Top K Results

    ↓

Answer Generation with Citations

```

### Model Lifecycle

- **Startup:** Models load once during server startup
- **Runtime:** All requests reuse the same model instances
- **Caching:** Embeddings are cached with LRU eviction
- **Performance:** Typical latency reduction: 70-90%

---

## Troubleshooting

### "Tenant not found" error
- Verify you're using the correct `tenant_id` from account creation
- Check that you're including the `X-Tenant-ID` header

### No results from YouTube sources
- Verify the video has captions available, or upload a transcript file
- Check that the source is enabled: `GET /v1/sources/active`

### Slow first request
- This is normal - models load on first use
- Subsequent requests will be much faster due to caching

### Missing source types in results
- Verify all sources are enabled: `GET /v1/sources/active`
- Check that content has been ingested for each source

---

## Best Practices

1. **Source Management:**
   - Keep sources enabled unless you have a specific reason to disable them
   - Use descriptive names for sources to aid in citation tracking

2. **Query Optimization:**
   - Use `top_k=10-20` for best balance of quality and speed
   - More sources = slightly higher latency but better coverage

3. **Metrics Monitoring:**
   - Check metrics regularly to understand usage patterns
   - Monitor `retrieval_latency_ms` for performance issues

4. **YouTube Ingestion:**
   - Always provide transcript files when available for best quality
   - Set `owner_attestation=true` to confirm you have rights to the content

---

## API Endpoint Reference

| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | `/v1/auth/tenants` | Create tenant account |
| POST | `/v1/auth/login` | Login with tenant_id |

| POST | `/v1/sources/` | Create source |

| GET | `/v1/sources/` | List all sources |

| GET | `/v1/sources/active` | List active sources |

| PUT | `/v1/sources/{id}/toggle` | Enable/disable source |

| POST | `/v1/sources/youtube` | Create YouTube source |

| POST | `/v1/ask/` | Ask question |

| GET | `/v1/tenants/{id}/metrics` | Get tenant metrics |

| GET | `/v1/admin/metrics` | Get global metrics (admin) |

| GET | `/v1/metrics/cache-stats` | Get cache statistics |



---



## Support



For issues or questions, contact your system administrator.