File size: 8,657 Bytes
99cde10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Lung Cancer AI Advisor API Documentation


## Base URL
```
https://moazx-lung-cancer-ai-advisor.hf.space/  # Production
http://127.0.0.1:7860/                         # Development
```

---

## 1. POST `/ask`
**Full Endpoint:** `https://moazx-lung-cancer-ai-advisor.hf.space/ask` (Production)
**Development:** `http://127.0.0.1:7860/ask`
**Purpose:** Get a complete AI response for a lung cancer-related medical query.

### Request
- **Method:** `POST`
- **Content-Type:** `application/json`
- **Authentication:** Required (via `Authorization` header)

**Request Body (JSON):**
```json
{
  "query": "What are the early symptoms of lung cancer?",
  "session_id": "user_123_session_1699612345"
}
```

**Parameters:**
- `query` (required, string): The medical question about lung cancer
- `session_id` (required, string): Unique session identifier for conversation continuity. Format: `user_{user_id}_session_{timestamp}`

### Response (200 OK)
```json
{
  "response": "Early symptoms of lung cancer may include...\n\n**Common Early Signs:**\n- Persistent cough...",
  "session_id": "user_123_session_1699612345"
}
```

### Error Responses
- **400 Bad Request**: Missing or invalid parameters
- **401 Unauthorized**: Missing or invalid authentication
- **500 Internal Server Error**: Error processing the query
- **503 Service Unavailable**: Service is initializing, try again later

---

## 2. POST `/ask/stream` (Recommended)
**Full Endpoint:** `https://moazx-lung-cancer-ai-advisor.hf.space/ask/stream` (Production)
**Development:** `http://127.0.0.1:7860/ask/stream`
**Purpose:** Stream AI response in real-time for better UX.

### Request
- **Method:** `POST`
- **Content-Type:** `application/json`
- **Authentication:** Required (via `Authorization` header)

**Request Body (JSON):**
```json
{
  "query": "What are the early symptoms of lung cancer?",
  "session_id": "user_123_session_1699612345"
}
```

**Parameters:**
- `query` (required, string): The medical question about lung cancer
- `session_id` (required, string): Unique session identifier for conversation continuity. Format: `user_{user_id}_session_{timestamp}`

### Response
- Streams the AI response in chunks (text/event-stream or similar).
- Each chunk contains a part of the response.

### Error Responses
- **400 Bad Request**: Missing or invalid parameters
- **401 Unauthorized**: Missing or invalid authentication
- **500 Internal Server Error**: Error processing the query
- **503 Service Unavailable**: Service is initializing, try again later

---

## 3. GET `/health`
**Full Endpoint:** `https://moazx-lung-cancer-ai-advisor.hf.space/health` (Production)
**Development:** `http://127.0.0.1:7860/health`

**Related Endpoints:**
- `GET /health/ping` - Basic connectivity check
- `GET /health/initialization` - Check initialization status
- `GET /health/version` - Get API version information
**Purpose:** Comprehensive health check of all system components.

### Response (200 OK)
```json
{
  "status": "healthy",
  "version": "1.0.0",
  "timestamp": "2024-01-01T12:00:00Z",
  "components": {
    "agent": "healthy",
    "vector_store": "healthy",
    "data_loaders": "healthy",
    "tools": "healthy"
  },
  "initialization": {
    "is_complete": true,
    "status_message": "Initialization complete",
    "is_successful": true,
    "error": null
  }
}
```

**Component Status Values:**
- `healthy`: Component is functioning normally
- `unhealthy`: Component is not functioning
- `initializing`: Component is still initializing

**Initialization Status:**
- `is_complete`: Whether initialization has completed
- `is_successful`: Whether initialization was successful
- `status_message`: Current status message
- `error`: Any error that occurred during initialization

---

## 4. Export Endpoints

### 4.1 GET `/export/{format}`
**Full Endpoint:** `https://moazx-lung-cancer-ai-advisor.hf.space/export/{format}` (Production)
**Development:** `http://127.0.0.1:7860/export/{format}`
**Purpose:** Export conversation history in the specified format.

**Path Parameters:**
- `format` (string, required): 
  - `docx`: Microsoft Word document (.docx)
  - `pdf`: Portable Document Format (.pdf)

**Query Parameters:**
- `session_id` (string, required): The session ID to export (e.g., "user_123_session_1699612345")

**Example Request:**
```
GET /export/pdf?session_id=user_123_session_1699612345
```

**Responses:**
- **200 OK**: 
  - Returns the exported file with appropriate content-type header
  - Headers:
    - `Content-Type`: `application/pdf` or `application/vnd.openxmlformats-officedocument.wordprocessingml.document`
    - `Content-Disposition`: `attachment; filename=conversation_{session_id}_{timestamp}.{format}`
- **400 Bad Request**: 
  - Invalid format (not 'docx' or 'pdf')
  - Missing required parameters
- **401 Unauthorized**: Missing or invalid authentication
- **404 Not Found**: No conversation history found for the specified session
- **500 Internal Server Error**: Error generating export file
- **503 Service Unavailable**: Service is initializing, try again later

**Response Headers:**
- `Content-Type`: `application/pdf` or `application/vnd.openxmlformats-officedocument.wordprocessingml.document`
- `Content-Disposition`: `attachment; filename=conversation_{session_id}_{timestamp}.{format}`

**Frontend Integration Tips:**
- Trigger file download using the browser's built-in behavior
- Show loading state during export generation
- Handle errors gracefully with user-friendly messages
- For PDF exports, consider showing a preview using PDF.js

---

## 5. Session Management

### 5.1 GET `/sessions`
**Purpose:** List all active conversation sessions.

**Response (200 OK):**
```json
{
  "active_sessions": ["session1", "session2"],
  "count": 2,
  "timestamp": "2024-01-01T12:00:00Z"
}
```

### 5.2 DELETE `/sessions/{session_id}`
**Purpose:** Clear conversation history for a specific session.

**Path Parameters:**
- `session_id`: The session ID to clear

**Response (200 OK):**
```json
{
  "message": "Session 'session_id' cleared successfully",
  "session_id": "session_id",
  "timestamp": "2024-01-01T12:00:00Z"
}
```

### 5.3 DELETE `/sessions`
**Purpose:** Clear all conversation sessions.

**Response (200 OK):**
```json
{
  "message": "All sessions cleared successfully",
  "timestamp": "2024-01-01T12:00:00Z"
}
```

---

## 6. Error Handling

All error responses follow this format:
```json
{
  "error": "error_type",
  "message": "Human-readable error message",
  "details": {
    "field": "Additional error details"
  },
  "timestamp": "2024-01-01T12:00:00Z"
}
```

**Common Error Types:**
- `validation_error`: Request validation failed
- `authentication_error`: Invalid or missing authentication
- `not_found`: Requested resource not found
- `rate_limit_exceeded`: Too many requests
- `server_error`: Internal server error

---

## 7. Rate Limiting

- **Rate Limit:** 100 requests per minute per IP address
- **Headers:**
  - `X-RateLimit-Limit`: Request limit per time window
  - `X-RateLimit-Remaining`: Remaining requests in current window
  - `X-RateLimit-Reset`: Time when the rate limit resets (UTC timestamp)

---

## 8. Authentication

All endpoints (except `/health` and `/health/ping`) require authentication.

**Authentication Header:**
```
Authorization: Bearer YOUR_API_KEY
```

**Getting an API Key:**
Contact the administrator to obtain an API key with appropriate permissions.

---

## 9. Best Practices

1. **Session Management**
   - Always use unique session IDs for different users
   - Reuse the same session ID for follow-up questions
   - Clean up old sessions when no longer needed

2. **Error Handling**
   - Implement retry logic for transient errors (5xx)
   - Cache successful responses when appropriate
   - Log errors for debugging purposes

3. **Performance**
   - Use the streaming endpoint (`/ask/stream`) for better UX
   - Implement client-side caching of responses
   - Batch requests when possible

4. **Security**
   - Never expose API keys in client-side code
   - Use HTTPS for all requests
   - Validate all user input

---

## 10. Support

For questions or issues, please contact:
- **Email:** support@lungcanceradvisor.com
- **Slack:** #api-support channel
- **Documentation:** https://docs.lungcanceradvisor.com/api

---

## Changelog

### v1.0.0 (2024-01-01)
- Initial release of the Lung Cancer AI Advisor API
- Support for streaming and non-streaming queries
- Session-based conversation history
- Export functionality (PDF/DOCX)
- Comprehensive health monitoring

---

*Documentation last updated: November 10, 2024*

For questions or further integration help, contact the backend team.