Spaces:
Sleeping
Sleeping
File size: 5,988 Bytes
bf10662 |
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 |
# RAG Pipeline API Usage Guide
This API provides a REST interface to the RAG Pipeline system, allowing you to use it from the terminal, build custom UIs, or integrate it into other applications.
## Starting the API Server
```bash
# Using uvicorn directly
uvicorn api:app --reload --host 0.0.0.0 --port 8000
# Or using Python
python api.py
```
The API will be available at `http://localhost:8000`
## API Documentation
Once the server is running, visit:
- **Swagger UI**: http://localhost:8000/docs
- **ReDoc**: http://localhost:8000/redoc
## Endpoints
### 1. Get API Information
```bash
curl http://localhost:8000/
```
### 2. Check System Status
```bash
curl http://localhost:8000/status
```
### 3. Upload and Process PDF Documents
```bash
curl -X POST "http://localhost:8000/upload" \
-F "files=@/path/to/document1.pdf" \
-F "files=@/path/to/document2.pdf" \
-F "chunk_size=800" \
-F "chunk_overlap=200"
```
**Parameters:**
- `files`: PDF files to upload (can upload multiple)
- `chunk_size`: Size of text chunks (default: 800)
- `chunk_overlap`: Overlap between chunks (default: 200)
- `collection_name`: Optional custom collection name
- `persist_directory`: Optional custom persist directory
### 4. Query Documents
```bash
curl -X POST "http://localhost:8000/query" \
-H "Content-Type: application/json" \
-d '{
"query": "What is attention mechanism?",
"top_k": 5,
"use_memory": true
}'
```
**With session ID (for conversation memory):**
```bash
curl -X POST "http://localhost:8000/query" \
-H "Content-Type: application/json" \
-d '{
"query": "Who are the authors?",
"session_id": "my-session-123",
"top_k": 5,
"use_memory": true
}'
```
**With metadata filters:**
```bash
curl -X POST "http://localhost:8000/query" \
-H "Content-Type: application/json" \
-d '{
"query": "What is attention?",
"top_k": 5,
"metadata_filters": {
"source": ["../data/pdf/NIPS-2017-attention-is-all-you-need-Paper.pdf"],
"page": 1
}
}'
```
**Response:**
```json
{
"answer": "The answer from the RAG system...",
"sources": [
{
"score": 0.85,
"preview": "Document preview...",
"metadata": {...},
"id": "doc-id"
}
],
"session_id": "auto-generated-or-provided",
"message": "Query processed successfully"
}
```
### 5. Get Chat History
```bash
curl http://localhost:8000/chat-history/{session_id}
```
### 6. Clear Chat History
```bash
curl -X DELETE http://localhost:8000/chat-history/{session_id}
```
### 7. List All Sessions
```bash
curl http://localhost:8000/sessions
```
### 8. Reset System
```bash
curl -X POST http://localhost:8000/reset
```
## Python Client Example
```python
import requests
# Base URL
BASE_URL = "http://localhost:8000"
# 1. Upload documents
with open("document.pdf", "rb") as f:
files = {"files": f}
data = {"chunk_size": 800, "chunk_overlap": 200}
response = requests.post(f"{BASE_URL}/upload", files=files, data=data)
print(response.json())
# 2. Query documents
query_data = {
"query": "What is attention mechanism?",
"session_id": "my-session",
"top_k": 5,
"use_memory": True
}
response = requests.post(f"{BASE_URL}/query", json=query_data)
result = response.json()
print(f"Answer: {result['answer']}")
print(f"Sources: {result['sources']}")
# 3. Continue conversation
query_data = {
"query": "Tell me more about it",
"session_id": "my-session", # Same session ID
"top_k": 5,
"use_memory": True
}
response = requests.post(f"{BASE_URL}/query", json=query_data)
print(response.json()["answer"])
# 4. Get chat history
response = requests.get(f"{BASE_URL}/chat-history/my-session")
print(response.json())
```
## JavaScript/TypeScript Example
```javascript
// Upload documents
const formData = new FormData();
formData.append('files', fileInput.files[0]);
formData.append('chunk_size', '800');
formData.append('chunk_overlap', '200');
const uploadResponse = await fetch('http://localhost:8000/upload', {
method: 'POST',
body: formData
});
const uploadResult = await uploadResponse.json();
console.log(uploadResult);
// Query documents
const queryResponse = await fetch('http://localhost:8000/query', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: 'What is attention mechanism?',
session_id: 'my-session',
top_k: 5,
use_memory: true
})
});
const queryResult = await queryResponse.json();
console.log(queryResult.answer);
```
## Building a Custom Streamlit App
You can use the API from your own Streamlit app:
```python
import streamlit as st
import requests
API_URL = "http://localhost:8000"
# Query function
def query_rag(query, session_id=None):
response = requests.post(
f"{API_URL}/query",
json={
"query": query,
"session_id": session_id,
"top_k": 5,
"use_memory": True
}
)
return response.json()
# Use in your Streamlit app
st.title("My Custom RAG App")
query = st.text_input("Ask a question")
if query:
result = query_rag(query, session_id="my-session")
st.write(result["answer"])
```
## Features
✅ **Document Upload & Processing**: Upload PDFs and process them into chunks
✅ **RAG Querying**: Query documents with retrieval-augmented generation
✅ **Conversation Memory**: Maintain conversation history per session
✅ **Metadata Filtering**: Filter documents by source, page, or custom metadata
✅ **Concise Memory**: Automatically summarizes answers for efficient memory storage
✅ **Session Management**: Multiple concurrent chat sessions
✅ **RESTful API**: Standard REST endpoints for easy integration
## Error Handling
All endpoints return appropriate HTTP status codes:
- `200`: Success
- `400`: Bad Request (invalid input)
- `404`: Not Found (session/resource not found)
- `500`: Internal Server Error
Error responses include a `detail` field with the error message.
|