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 7 |
+
http://127.0.0.1:7860/ # Development
|
| 8 |
+
```
|
| 9 |
+
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
## 1. POST `/ask`
|
| 13 |
+
**Full Endpoint:** `https://moazx-lung-cancer-ai-advisor.hf.space/ask` (Production)
|
| 14 |
+
**Development:** `http://127.0.0.1:7860/ask`
|
| 15 |
+
**Purpose:** Get a complete AI response for a lung cancer-related medical query.
|
| 16 |
+
|
| 17 |
+
### Request
|
| 18 |
+
- **Method:** `POST`
|
| 19 |
+
- **Content-Type:** `application/json`
|
| 20 |
+
- **Authentication:** Required (via `Authorization` header)
|
| 21 |
+
|
| 22 |
+
**Request Body (JSON):**
|
| 23 |
+
```json
|
| 24 |
+
{
|
| 25 |
+
"query": "What are the early symptoms of lung cancer?",
|
| 26 |
+
"session_id": "user_123_session_1699612345"
|
| 27 |
+
}
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
**Parameters:**
|
| 31 |
+
- `query` (required, string): The medical question about lung cancer
|
| 32 |
+
- `session_id` (required, string): Unique session identifier for conversation continuity. Format: `user_{user_id}_session_{timestamp}`
|
| 33 |
+
|
| 34 |
+
### Response (200 OK)
|
| 35 |
+
```json
|
| 36 |
+
{
|
| 37 |
+
"response": "Early symptoms of lung cancer may include...\n\n**Common Early Signs:**\n- Persistent cough...",
|
| 38 |
+
"session_id": "user_123_session_1699612345"
|
| 39 |
+
}
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
### Error Responses
|
| 43 |
+
- **400 Bad Request**: Missing or invalid parameters
|
| 44 |
+
- **401 Unauthorized**: Missing or invalid authentication
|
| 45 |
+
- **500 Internal Server Error**: Error processing the query
|
| 46 |
+
- **503 Service Unavailable**: Service is initializing, try again later
|
| 47 |
+
|
| 48 |
+
---
|
| 49 |
+
|
| 50 |
+
## 2. POST `/ask/stream` (Recommended)
|
| 51 |
+
**Full Endpoint:** `https://moazx-lung-cancer-ai-advisor.hf.space/ask/stream` (Production)
|
| 52 |
+
**Development:** `http://127.0.0.1:7860/ask/stream`
|
| 53 |
+
**Purpose:** Stream AI response in real-time for better UX.
|
| 54 |
+
|
| 55 |
+
### Request
|
| 56 |
+
- **Method:** `POST`
|
| 57 |
+
- **Content-Type:** `application/json`
|
| 58 |
+
- **Authentication:** Required (via `Authorization` header)
|
| 59 |
+
|
| 60 |
+
**Request Body (JSON):**
|
| 61 |
+
```json
|
| 62 |
+
{
|
| 63 |
+
"query": "What are the early symptoms of lung cancer?",
|
| 64 |
+
"session_id": "user_123_session_1699612345"
|
| 65 |
+
}
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
**Parameters:**
|
| 69 |
+
- `query` (required, string): The medical question about lung cancer
|
| 70 |
+
- `session_id` (required, string): Unique session identifier for conversation continuity. Format: `user_{user_id}_session_{timestamp}`
|
| 71 |
+
|
| 72 |
+
### Response
|
| 73 |
+
- Streams the AI response in chunks (text/event-stream or similar).
|
| 74 |
+
- Each chunk contains a part of the response.
|
| 75 |
+
|
| 76 |
+
### Error Responses
|
| 77 |
+
- **400 Bad Request**: Missing or invalid parameters
|
| 78 |
+
- **401 Unauthorized**: Missing or invalid authentication
|
| 79 |
+
- **500 Internal Server Error**: Error processing the query
|
| 80 |
+
- **503 Service Unavailable**: Service is initializing, try again later
|
| 81 |
+
|
| 82 |
+
---
|
| 83 |
+
|
| 84 |
+
## 3. GET `/health`
|
| 85 |
+
**Full Endpoint:** `https://moazx-lung-cancer-ai-advisor.hf.space/health` (Production)
|
| 86 |
+
**Development:** `http://127.0.0.1:7860/health`
|
| 87 |
+
|
| 88 |
+
**Related Endpoints:**
|
| 89 |
+
- `GET /health/ping` - Basic connectivity check
|
| 90 |
+
- `GET /health/initialization` - Check initialization status
|
| 91 |
+
- `GET /health/version` - Get API version information
|
| 92 |
+
**Purpose:** Comprehensive health check of all system components.
|
| 93 |
+
|
| 94 |
+
### Response (200 OK)
|
| 95 |
+
```json
|
| 96 |
+
{
|
| 97 |
+
"status": "healthy",
|
| 98 |
+
"version": "1.0.0",
|
| 99 |
+
"timestamp": "2024-01-01T12:00:00Z",
|
| 100 |
+
"components": {
|
| 101 |
+
"agent": "healthy",
|
| 102 |
+
"vector_store": "healthy",
|
| 103 |
+
"data_loaders": "healthy",
|
| 104 |
+
"tools": "healthy"
|
| 105 |
+
},
|
| 106 |
+
"initialization": {
|
| 107 |
+
"is_complete": true,
|
| 108 |
+
"status_message": "Initialization complete",
|
| 109 |
+
"is_successful": true,
|
| 110 |
+
"error": null
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
**Component Status Values:**
|
| 116 |
+
- `healthy`: Component is functioning normally
|
| 117 |
+
- `unhealthy`: Component is not functioning
|
| 118 |
+
- `initializing`: Component is still initializing
|
| 119 |
+
|
| 120 |
+
**Initialization Status:**
|
| 121 |
+
- `is_complete`: Whether initialization has completed
|
| 122 |
+
- `is_successful`: Whether initialization was successful
|
| 123 |
+
- `status_message`: Current status message
|
| 124 |
+
- `error`: Any error that occurred during initialization
|
| 125 |
+
|
| 126 |
+
---
|
| 127 |
+
|
| 128 |
+
## 4. Export Endpoints
|
| 129 |
+
|
| 130 |
+
### 4.1 GET `/export/{format}`
|
| 131 |
+
**Full Endpoint:** `https://moazx-lung-cancer-ai-advisor.hf.space/export/{format}` (Production)
|
| 132 |
+
**Development:** `http://127.0.0.1:7860/export/{format}`
|
| 133 |
+
**Purpose:** Export conversation history in the specified format.
|
| 134 |
+
|
| 135 |
+
**Path Parameters:**
|
| 136 |
+
- `format` (string, required):
|
| 137 |
+
- `docx`: Microsoft Word document (.docx)
|
| 138 |
+
- `pdf`: Portable Document Format (.pdf)
|
| 139 |
+
|
| 140 |
+
**Query Parameters:**
|
| 141 |
+
- `session_id` (string, required): The session ID to export (e.g., "user_123_session_1699612345")
|
| 142 |
+
|
| 143 |
+
**Example Request:**
|
| 144 |
+
```
|
| 145 |
+
GET /export/pdf?session_id=user_123_session_1699612345
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
**Responses:**
|
| 149 |
+
- **200 OK**:
|
| 150 |
+
- Returns the exported file with appropriate content-type header
|
| 151 |
+
- Headers:
|
| 152 |
+
- `Content-Type`: `application/pdf` or `application/vnd.openxmlformats-officedocument.wordprocessingml.document`
|
| 153 |
+
- `Content-Disposition`: `attachment; filename=conversation_{session_id}_{timestamp}.{format}`
|
| 154 |
+
- **400 Bad Request**:
|
| 155 |
+
- Invalid format (not 'docx' or 'pdf')
|
| 156 |
+
- Missing required parameters
|
| 157 |
+
- **401 Unauthorized**: Missing or invalid authentication
|
| 158 |
+
- **404 Not Found**: No conversation history found for the specified session
|
| 159 |
+
- **500 Internal Server Error**: Error generating export file
|
| 160 |
+
- **503 Service Unavailable**: Service is initializing, try again later
|
| 161 |
+
|
| 162 |
+
**Response Headers:**
|
| 163 |
+
- `Content-Type`: `application/pdf` or `application/vnd.openxmlformats-officedocument.wordprocessingml.document`
|
| 164 |
+
- `Content-Disposition`: `attachment; filename=conversation_{session_id}_{timestamp}.{format}`
|
| 165 |
+
|
| 166 |
+
**Frontend Integration Tips:**
|
| 167 |
+
- Trigger file download using the browser's built-in behavior
|
| 168 |
+
- Show loading state during export generation
|
| 169 |
+
- Handle errors gracefully with user-friendly messages
|
| 170 |
+
- For PDF exports, consider showing a preview using PDF.js
|
| 171 |
+
|
| 172 |
+
---
|
| 173 |
+
|
| 174 |
+
## 5. Session Management
|
| 175 |
+
|
| 176 |
+
### 5.1 GET `/sessions`
|
| 177 |
+
**Purpose:** List all active conversation sessions.
|
| 178 |
+
|
| 179 |
+
**Response (200 OK):**
|
| 180 |
+
```json
|
| 181 |
+
{
|
| 182 |
+
"active_sessions": ["session1", "session2"],
|
| 183 |
+
"count": 2,
|
| 184 |
+
"timestamp": "2024-01-01T12:00:00Z"
|
| 185 |
+
}
|
| 186 |
+
```
|
| 187 |
+
|
| 188 |
+
### 5.2 DELETE `/sessions/{session_id}`
|
| 189 |
+
**Purpose:** Clear conversation history for a specific session.
|
| 190 |
+
|
| 191 |
+
**Path Parameters:**
|
| 192 |
+
- `session_id`: The session ID to clear
|
| 193 |
+
|
| 194 |
+
**Response (200 OK):**
|
| 195 |
+
```json
|
| 196 |
+
{
|
| 197 |
+
"message": "Session 'session_id' cleared successfully",
|
| 198 |
+
"session_id": "session_id",
|
| 199 |
+
"timestamp": "2024-01-01T12:00:00Z"
|
| 200 |
+
}
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
+
### 5.3 DELETE `/sessions`
|
| 204 |
+
**Purpose:** Clear all conversation sessions.
|
| 205 |
+
|
| 206 |
+
**Response (200 OK):**
|
| 207 |
+
```json
|
| 208 |
+
{
|
| 209 |
+
"message": "All sessions cleared successfully",
|
| 210 |
+
"timestamp": "2024-01-01T12:00:00Z"
|
| 211 |
+
}
|
| 212 |
+
```
|
| 213 |
+
|
| 214 |
+
---
|
| 215 |
+
|
| 216 |
+
## 6. Error Handling
|
| 217 |
+
|
| 218 |
+
All error responses follow this format:
|
| 219 |
+
```json
|
| 220 |
+
{
|
| 221 |
+
"error": "error_type",
|
| 222 |
+
"message": "Human-readable error message",
|
| 223 |
+
"details": {
|
| 224 |
+
"field": "Additional error details"
|
| 225 |
+
},
|
| 226 |
+
"timestamp": "2024-01-01T12:00:00Z"
|
| 227 |
+
}
|
| 228 |
+
```
|
| 229 |
+
|
| 230 |
+
**Common Error Types:**
|
| 231 |
+
- `validation_error`: Request validation failed
|
| 232 |
+
- `authentication_error`: Invalid or missing authentication
|
| 233 |
+
- `not_found`: Requested resource not found
|
| 234 |
+
- `rate_limit_exceeded`: Too many requests
|
| 235 |
+
- `server_error`: Internal server error
|
| 236 |
+
|
| 237 |
+
---
|
| 238 |
+
|
| 239 |
+
## 7. Rate Limiting
|
| 240 |
+
|
| 241 |
+
- **Rate Limit:** 100 requests per minute per IP address
|
| 242 |
+
- **Headers:**
|
| 243 |
+
- `X-RateLimit-Limit`: Request limit per time window
|
| 244 |
+
- `X-RateLimit-Remaining`: Remaining requests in current window
|
| 245 |
+
- `X-RateLimit-Reset`: Time when the rate limit resets (UTC timestamp)
|
| 246 |
+
|
| 247 |
+
---
|
| 248 |
+
|
| 249 |
+
## 8. Authentication
|
| 250 |
+
|
| 251 |
+
All endpoints (except `/health` and `/health/ping`) require authentication.
|
| 252 |
+
|
| 253 |
+
**Authentication Header:**
|
| 254 |
+
```
|
| 255 |
+
Authorization: Bearer YOUR_API_KEY
|
| 256 |
+
```
|
| 257 |
+
|
| 258 |
+
**Getting an API Key:**
|
| 259 |
+
Contact the administrator to obtain an API key with appropriate permissions.
|
| 260 |
+
|
| 261 |
+
---
|
| 262 |
+
|
| 263 |
+
## 9. Best Practices
|
| 264 |
+
|
| 265 |
+
1. **Session Management**
|
| 266 |
+
- Always use unique session IDs for different users
|
| 267 |
+
- Reuse the same session ID for follow-up questions
|
| 268 |
+
- Clean up old sessions when no longer needed
|
| 269 |
+
|
| 270 |
+
2. **Error Handling**
|
| 271 |
+
- Implement retry logic for transient errors (5xx)
|
| 272 |
+
- Cache successful responses when appropriate
|
| 273 |
+
- Log errors for debugging purposes
|
| 274 |
+
|
| 275 |
+
3. **Performance**
|
| 276 |
+
- Use the streaming endpoint (`/ask/stream`) for better UX
|
| 277 |
+
- Implement client-side caching of responses
|
| 278 |
+
- Batch requests when possible
|
| 279 |
+
|
| 280 |
+
4. **Security**
|
| 281 |
+
- Never expose API keys in client-side code
|
| 282 |
+
- Use HTTPS for all requests
|
| 283 |
+
- Validate all user input
|
| 284 |
+
|
| 285 |
+
---
|
| 286 |
+
|
| 287 |
+
## 10. Support
|
| 288 |
+
|
| 289 |
+
For questions or issues, please contact:
|
| 290 |
+
- **Email:** support@lungcanceradvisor.com
|
| 291 |
+
- **Slack:** #api-support channel
|
| 292 |
+
- **Documentation:** https://docs.lungcanceradvisor.com/api
|
| 293 |
+
|
| 294 |
+
---
|
| 295 |
+
|
| 296 |
+
## Changelog
|
| 297 |
+
|
| 298 |
+
### v1.0.0 (2024-01-01)
|
| 299 |
+
- Initial release of the Lung Cancer AI Advisor API
|
| 300 |
+
- Support for streaming and non-streaming queries
|
| 301 |
+
- Session-based conversation history
|
| 302 |
+
- Export functionality (PDF/DOCX)
|
| 303 |
+
- Comprehensive health monitoring
|
| 304 |
+
|
| 305 |
+
---
|
| 306 |
+
|
| 307 |
+
*Documentation last updated: November 10, 2024*
|
| 308 |
+
|
| 309 |
+
For questions or further integration help, contact the backend team.
|