File size: 4,134 Bytes
535ca47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# API Documentation

This document provides a quick reference for the RAG Chat Application REST API endpoints.

## Base URL
```
http://localhost:8000
```

## Authentication
Most endpoints require a GROQ API key to be configured:

```bash
POST /set-api-key
Content-Type: application/json

{
  "api_key": "your_groq_api_key_here"
}
```

## Core Endpoints

### Document Processing

#### Upload Files
```bash
POST /upload-files
Content-Type: multipart/form-data

# Form data with file uploads
files: [file1.pdf, file2.txt, ...]
```

**Response:**
```json
{
  "total_files": 5,
  "total_documents": 12,
  "total_chunks": 87,
  "file_types": ["pdf", "txt", "py"],
  "type_counts": {"pdf": 3, "txt": 1, "py": 1}
}
```

#### Process Directory
```bash
POST /process-directory
Content-Type: application/x-www-form-urlencoded

directory_path=/path/to/documents
```

### Chat Interface

#### Send Chat Message
```bash
POST /chat
Content-Type: application/json

{
  "message": "What is the main topic of the documents?"
}
```

**Response:**
```json
{
  "response": "Based on the documents, the main topics include...",
  "citations": [
    {
      "content": "relevant excerpt from document",
      "citation": "/path/to/source/file.pdf",
      "type": "pdf",
      "score": 0.85
    }
  ],
  "themes": {
    "key_themes": ["AI", "Machine Learning", "RAG"],
    "analysis": "The documents focus on AI and ML concepts..."
  },
  "timestamp": "2025-06-11T10:30:00.123456"
}
```

### Data Management

#### Get Statistics
```bash
GET /stats
```

**Response:**
```json
{
  "total_files": 10,
  "total_documents": 25,
  "total_chunks": 150,
  "file_types": ["pdf", "txt", "py", "md"],
  "type_counts": {"pdf": 5, "txt": 3, "py": 1, "md": 1},
  "processed_at": "2025-06-11 10:30:00"
}
```

#### Get Chat History
```bash
GET /chat-history
```

**Response:**
```json
[
  {
    "user_message": "What is RAG?",
    "assistant_response": "RAG stands for Retrieval-Augmented Generation...",
    "timestamp": "2025-06-11T10:30:00.123456",
    "citations": [...]
  }
]
```

#### Clear Chat History
```bash
DELETE /clear-chat
```

### Vector Store Management

#### Save Vector Store
```bash
POST /save-vector-store
```

**Response:**
```json
{
  "message": "Vector store saved successfully"
}
```

#### Load Vector Store
```bash
POST /load-vector-store
```

**Response:**
```json
{
  "message": "Vector store loaded successfully",
  "stats": {
    "total_files": 10,
    "total_documents": 25,
    "total_chunks": 150
  }
}
```

## Frontend Serving

#### Main Application
```bash
GET /
```
Returns the HTML frontend application.

## Error Responses

All endpoints return errors in this format:
```json
{
  "detail": "Error description message"
}
```

Common HTTP status codes:
- `200` - Success
- `400` - Bad Request (invalid input)
- `422` - Validation Error
- `500` - Internal Server Error

## Interactive Documentation

When the server is running, visit:
- **Swagger UI**: http://localhost:8000/docs
- **ReDoc**: http://localhost:8000/redoc

## Examples

### Complete Workflow
```bash
# 1. Set API key
curl -X POST "http://localhost:8000/set-api-key" \
  -H "Content-Type: application/json" \
  -d '{"api_key": "your_groq_key"}'

# 2. Upload files
curl -X POST "http://localhost:8000/upload-files" \
  -F "files=@document1.pdf" \
  -F "files=@document2.txt"

# 3. Chat with documents
curl -X POST "http://localhost:8000/chat" \
  -H "Content-Type: application/json" \
  -d '{"message": "Summarize the key points"}'

# 4. Get statistics
curl -X GET "http://localhost:8000/stats"

# 5. Save vector store
curl -X POST "http://localhost:8000/save-vector-store"
```

### Python Client Example
```python
import requests

base_url = "http://localhost:8000"

# Set API key
response = requests.post(f"{base_url}/set-api-key", 
                        json={"api_key": "your_groq_key"})

# Upload files
files = {'files': open('document.pdf', 'rb')}
response = requests.post(f"{base_url}/upload-files", files=files)

# Chat
response = requests.post(f"{base_url}/chat", 
                        json={"message": "What is this document about?"})
print(response.json())
```