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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# Guia da API REST - RAG Template

API REST completa para o RAG Template usando FastAPI.

---

## Visao Geral

A API REST permite integracao programatica com o sistema RAG, oferecendo endpoints para:
- Ingestao de documentos (texto ou upload de arquivos)
- Queries RAG
- Gerenciamento de documentos
- Estatisticas do sistema
- Health checks

**Base URL**: `http://localhost:8000/api/v1`

**Documentacao Interativa**: `http://localhost:8000/api/docs`

---

## Autenticacao

Todos os endpoints (exceto `/health`) requerem autenticacao via API key.

### Configurar API Keys

No arquivo `.env`:

```bash
API_KEYS=key1,key2,key3
```

### Usar API Key

Inclua header em todas as requisicoes:

```
X-API-Key: sua_api_key_aqui
```

---

## Iniciar Servidor

### Modo Desenvolvimento

```bash
python api_server.py
```

### Modo Producao

```bash
uvicorn src.api:app --host 0.0.0.0 --port 8000 --workers 4
```

### Com Docker

```bash
docker run -p 8000:8000 -e DATABASE_URL=... -e API_KEYS=... rag-template
```

---

## Endpoints

### GET /api/v1/health

Health check do sistema.

**Autenticacao**: Nao requerida

**Response**:
```json
{
  "status": "healthy",
  "timestamp": "2026-01-23T10:30:00",
  "database": "healthy",
  "embeddings": "healthy",
  "version": "1.6.0"
}
```

### POST /api/v1/ingest

Ingere texto no sistema.

**Request Body**:
```json
{
  "text": "Conteudo do documento...",
  "title": "Titulo do Documento",
  "chunk_size": 1000,
  "chunk_overlap": 200,
  "strategy": "recursive",
  "metadata": {
    "document_type": "TXT",
    "tags": ["tech", "ai"],
    "security_level": "public"
  }
}
```

**Response**:
```json
{
  "document_id": 123,
  "num_chunks": 15,
  "message": "Document ingested successfully",
  "metadata": {...}
}
```

### POST /api/v1/upload

Upload e ingere arquivo (PDF ou TXT).

**Request**: `multipart/form-data`
- `file`: Arquivo a fazer upload
- `chunk_size`: (opcional) Tamanho dos chunks
- `chunk_overlap`: (opcional) Overlap entre chunks
- `strategy`: (opcional) Estrategia de chunking

**Response**: Similar ao `/ingest`

### POST /api/v1/query

Executa query RAG.

**Request Body**:
```json
{
  "query": "O que e RAG?",
  "top_k": 5,
  "temperature": 0.3,
  "max_tokens": 512,
  "model": "huggingface",
  "filters": {
    "document_type": "PDF",
    "tags": ["tech"]
  }
}
```

**Response**:
```json
{
  "query": "O que e RAG?",
  "response": "RAG e Retrieval-Augmented Generation...",
  "contexts": [
    {
      "content": "Contexto relevante...",
      "similarity": 0.92,
      "document_id": 123
    }
  ],
  "metadata": {
    "num_contexts": 5,
    "model": "huggingface",
    "temperature": 0.3,
    "max_tokens": 512
  }
}
```

### GET /api/v1/documents

Lista documentos no sistema.

**Query Parameters**:
- `limit`: (opcional) Numero maximo de documentos (default: 100)
- `offset`: (opcional) Offset para paginacao (default: 0)
- `session_id`: (opcional) Filtrar por session_id

**Response**:
```json
[
  {
    "id": 123,
    "title": "Documento 1",
    "content": "Conteudo...",
    "chunk_count": 15,
    "created_at": "2026-01-23T10:30:00",
    "metadata": {...}
  }
]
```

### DELETE /api/v1/documents/{document_id}

Deleta documento do sistema.

**Path Parameters**:
- `document_id`: ID do documento

**Response**:
```json
{
  "message": "Document deleted successfully",
  "document_id": 123
}
```

### GET /api/v1/stats

Retorna estatisticas do sistema.

**Response**:
```json
{
  "database": {
    "total_documents": 150,
    "total_chunks": 2500,
    "avg_chunks_per_doc": 16.67
  },
  "metadata": {
    "total": 150,
    "by_type": {"PDF": 100, "TXT": 50},
    "by_security": {"public": 120, "internal": 30}
  },
  "timestamp": "2026-01-23T10:30:00"
}
```

---

## Usando Python SDK

### Instalacao

```bash
pip install -e .  # Instalar localmente
```

### Uso Basico

```python
from sdk import RAGClient

# Criar cliente
client = RAGClient(
    base_url="http://localhost:8000",
    api_key="sua_api_key"
)

# Health check
health = client.health_check()
print(health)

# Ingerir texto
result = client.ingest_text(
    text="Conteudo do documento...",
    title="Meu Documento",
    metadata={"tags": ["tech", "ai"]}
)
print(f"Document ID: {result['document_id']}")

# Upload arquivo
result = client.upload_file("documento.pdf")
print(f"Chunks: {result['num_chunks']}")

# Query
response = client.query(
    query="O que e RAG?",
    top_k=5,
    filters={"tags": ["tech"]}
)
print(response['response'])

# Listar documentos
docs = client.list_documents(limit=10)
for doc in docs:
    print(f"{doc['id']}: {doc['title']}")

# Deletar documento
client.delete_document(123)

# Estatisticas
stats = client.get_stats()
print(stats)
```

---

## Exemplos de Uso

### Exemplo 1: Pipeline de Ingestao

```python
from sdk import RAGClient
from pathlib import Path

client = RAGClient(api_key="my_key")

# Ingerir multiplos arquivos
docs_dir = Path("./documents")
for file in docs_dir.glob("*.pdf"):
    result = client.upload_file(str(file))
    print(f"Ingested {file.name}: {result['num_chunks']} chunks")
```

### Exemplo 2: Chatbot Simples

```python
from sdk import RAGClient

client = RAGClient(api_key="my_key")

while True:
    query = input("Voce: ")
    if query.lower() in ["sair", "exit"]:
        break

    response = client.query(query, top_k=5)
    print(f"Bot: {response['response']}\n")
```

### Exemplo 3: Busca Filtrada

```python
from sdk import RAGClient

client = RAGClient(api_key="my_key")

# Buscar apenas em documentos publicos de tech
response = client.query(
    query="Como funciona embedding?",
    filters={
        "security_level": "public",
        "tags": ["tech", "ai"]
    }
)

print(response['response'])
print(f"Contextos usados: {response['metadata']['num_contexts']}")
```

---

## Usando cURL

### Health Check

```bash
curl http://localhost:8000/api/v1/health
```

### Ingerir Texto

```bash
curl -X POST http://localhost:8000/api/v1/ingest \
  -H "Content-Type: application/json" \
  -H "X-API-Key: sua_key" \
  -d '{
    "text": "Conteudo do documento",
    "title": "Titulo"
  }'
```

### Query

```bash
curl -X POST http://localhost:8000/api/v1/query \
  -H "Content-Type: application/json" \
  -H "X-API-Key: sua_key" \
  -d '{
    "query": "O que e RAG?",
    "top_k": 5
  }'
```

### Listar Documentos

```bash
curl http://localhost:8000/api/v1/documents?limit=10 \
  -H "X-API-Key: sua_key"
```

---

## Rate Limiting

A API nao implementa rate limiting por padrao. Para producao, considere usar:

- **Nginx**: Com `limit_req_zone`
- **Traefik**: Com middleware de rate limiting
- **CloudFlare**: Rate limiting no CDN

---

## Erros

### Codigos de Status

- `200`: Sucesso
- `400`: Bad Request (parametros invalidos)
- `401`: Unauthorized (API key invalida ou ausente)
- `404`: Not Found (recurso nao encontrado)
- `500`: Internal Server Error

### Formato de Erro

```json
{
  "detail": "Error message here"
}
```

---

## Performance

### Benchmarks

Testes em maquina local (M1 Pro, 16GB RAM):

| Endpoint | Tempo Medio | Notas |
|----------|-------------|-------|
| /health | <10ms | Muito rapido |
| /ingest | 500-2000ms | Depende do tamanho do documento |
| /query | 200-1000ms | Depende do LLM escolhido |
| /documents | <100ms | Paginado |

### Otimizacoes

1. **Cache de Embeddings**: Ativado automaticamente
2. **Connection Pooling**: Usar pgBouncer ou Supabase
3. **Workers**: Multiplos workers Uvicorn para producao
4. **Async**: Endpoints sao async por padrao

---

## Deploy em Producao

### Docker Compose

```yaml
version: '3.8'
services:
  api:
    build: .
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgresql://...
      - HF_TOKEN=...
      - API_KEYS=key1,key2
    command: uvicorn src.api:app --host 0.0.0.0 --port 8000 --workers 4
```

### Variavies de Ambiente

```bash
# API Config
API_HOST=0.0.0.0
API_PORT=8000
API_WORKERS=4
API_RELOAD=false
API_KEYS=key1,key2,key3

# Database
DATABASE_URL=postgresql://...

# LLM
HF_TOKEN=...
```

---

## Seguranca

### Best Practices

1. **HTTPS**: Sempre use HTTPS em producao
2. **API Keys**: Gere keys fortes e rotacione regularmente
3. **Rate Limiting**: Implemente rate limiting
4. **CORS**: Configure CORS apropriadamente
5. **Input Validation**: Validacao automatica via Pydantic
6. **Logs**: Monitore logs de acesso

---

## Troubleshooting

### API nao inicia

Verificar:
- PostgreSQL esta rodando
- `DATABASE_URL` esta correto
- Porta 8000 esta disponivel

### Erros de autenticacao

Verificar:
- API key esta configurada no `.env`
- Header `X-API-Key` esta presente
- Key esta correta

### Queries lentas

Verificar:
- Indices do banco estao criados
- Cache de embeddings esta ativo
- Modelo LLM nao esta muito grande

---

## Proximos Passos

1. Implementar rate limiting
2. Adicionar autenticacao OAuth2
3. Criar dashboard de monitoramento
4. Publicar SDK no PyPI
5. Adicionar webhooks para eventos

---

## Recursos

- [Documentacao FastAPI](https://fastapi.tiangolo.com/)
- [Documentacao Uvicorn](https://www.uvicorn.org/)
- [OpenAPI/Swagger](http://localhost:8000/api/docs)
- [ReDoc](http://localhost:8000/api/redoc)