File size: 4,630 Bytes
f866820
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# API Reference

Base URL: `https://vn6295337-rag-document-assistant.hf.space/api`

---

## Zero-Storage Endpoints

### POST /embed-chunks

Generate embeddings for text chunks and store in Pinecone. **Text is discarded immediately after embedding.**

**Request Body:**
```json
{
  "chunks": [
    {
      "text": "The actual text content...",
      "metadata": {
        "filename": "document.pdf",
        "filePath": "/Documents/document.pdf",
        "fileId": "dropbox_file_id",
        "chunkIndex": 0,
        "startChar": 0,
        "endChar": 1000
      }
    }
  ]
}
```

**Response:**
```json
{
  "status": "success",
  "vectors_upserted": 5,
  "error": null
}
```

**Privacy Note:** Text is used only for embedding generation, then immediately deleted. Only embeddings and metadata (file paths, positions) are stored.

---

### POST /query-secure

Execute a zero-storage query. Re-fetches text from user's Dropbox at query time.

**Request Body:**
```json
{
  "query": "What are the payment terms?",
  "access_token": "dropbox_access_token",
  "top_k": 3
}
```

**Response:**
```json
{
  "answer": "According to the contract, payment is due within 30 days...",
  "citations": [
    {
      "id": "file_id::0",
      "score": 0.85,
      "snippet": "Payment terms: Net 30 days from invoice date..."
    }
  ],
  "error": null
}
```

**Flow:**
1. Generate query embedding
2. Search Pinecone for similar chunks
3. Re-fetch files from user's Dropbox using provided token
4. Extract chunk text using stored positions
5. Send to LLM for answer generation
6. Return answer (text never stored)

---

### DELETE /clear-index

Clear all vectors from Pinecone index.

**Response:**
```json
{
  "status": "success",
  "message": "Index cleared"
}
```

---

## Dropbox Integration Endpoints

### POST /dropbox/token

Exchange Dropbox authorization code for access token. Client secret is kept server-side.

**Request Body:**
```json
{
  "code": "authorization_code_from_oauth",
  "redirect_uri": "https://your-app.com/callback"
}
```

**Response:**
```json
{
  "access_token": "sl.xxxxx",
  "token_type": "bearer",
  "expires_in": 14400
}
```

---

### POST /dropbox/folder

List contents of a Dropbox folder (proxy to avoid CORS).

**Request Body:**
```json
{
  "path": "",
  "access_token": "dropbox_access_token"
}
```

**Response:**
```json
{
  "entries": [
    {
      ".tag": "file",
      "name": "document.pdf",
      "id": "id:xxxxx",
      "path_lower": "/documents/document.pdf",
      "size": 12345
    }
  ],
  "has_more": false
}
```

---

### POST /dropbox/file

Download file content from Dropbox. Supports text files and PDFs (with text extraction).

**Request Body:**
```json
{
  "path": "/documents/document.pdf",
  "access_token": "dropbox_access_token"
}
```

**Response:**
```json
{
  "content": "Extracted text content from the file..."
}
```

**Supported File Types:**
- `.txt` - Plain text
- `.md` - Markdown
- `.pdf` - PDF (text extraction via PyPDF2)

---

## Utility Endpoints

### GET /health

Health check endpoint.

**Response:**
```json
{
  "status": "ok"
}
```

---

### GET /status

Get current index status.

**Query Parameters:**
- `chunks_path` (optional): Path to chunks file (default: `data/chunks.jsonl`)

**Response:**
```json
{
  "exists": true,
  "chunks": 44,
  "documents": 5,
  "path": "data/chunks.jsonl",
  "error": null
}
```

---

## Legacy Endpoints

### POST /query

Standard RAG query (uses local chunks file, not zero-storage).

**Request Body:**
```json
{
  "query": "What is GDPR?",
  "top_k": 3,
  "use_hybrid": false,
  "use_reranking": false
}
```

---

### POST /ingest

Ingest documents from a directory (server-side processing).

**Request Body:**
```json
{
  "docs_dir": "sample_docs",
  "output_path": "data/chunks.jsonl",
  "provider": "sentence-transformers"
}
```

---

## Error Responses

All endpoints return errors in this format:

```json
{
  "status": "error",
  "error": "Description of the error"
}
```

Common error codes:
- Missing required parameters
- Invalid access token
- Dropbox API errors
- Pinecone connection errors
- LLM provider failures

---

## Rate Limits

- **Embedding**: No explicit limit (Pinecone free tier: 100K operations/month)
- **Queries**: Subject to LLM provider limits:
  - Gemini: 15 RPM
  - Groq: 30 RPM
  - OpenRouter: Varies by model

---

## Environment Variables

Required on backend:
- `PINECONE_API_KEY` - Pinecone vector database
- `DROPBOX_APP_KEY` - Dropbox OAuth app key
- `DROPBOX_APP_SECRET` - Dropbox OAuth app secret
- `GEMINI_API_KEY` - Google Gemini API (primary LLM)
- `GROQ_API_KEY` - Groq API (fallback LLM)