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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Research AI Assistant - API Documentation
|
| 2 |
+
|
| 3 |
+
**Version:** 1.0.0
|
| 4 |
+
**Base URL:** `https://huggingface.co/spaces/JatinAutonomousLabs/Research_AI_Assistant`
|
| 5 |
+
**API Type:** Gradio Client API
|
| 6 |
+
**Last Updated:** December 2024
|
| 7 |
+
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
## Table of Contents
|
| 11 |
+
|
| 12 |
+
1. [Overview](#overview)
|
| 13 |
+
2. [Getting Started](#getting-started)
|
| 14 |
+
3. [Authentication](#authentication)
|
| 15 |
+
4. [API Endpoints](#api-endpoints)
|
| 16 |
+
5. [Error Handling](#error-handling)
|
| 17 |
+
6. [Code Examples](#code-examples)
|
| 18 |
+
7. [Integration Guide](#integration-guide)
|
| 19 |
+
8. [Testing](#testing)
|
| 20 |
+
9. [Rate Limits & Best Practices](#rate-limits--best-practices)
|
| 21 |
+
10. [Support & Troubleshooting](#support--troubleshooting)
|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
## Overview
|
| 26 |
+
|
| 27 |
+
The Research AI Assistant provides a RESTful-like API interface through Gradio's client library. All endpoints are accessible via the `gradio_client` Python package or HTTP requests.
|
| 28 |
+
|
| 29 |
+
### Key Features
|
| 30 |
+
|
| 31 |
+
- **Chat Interface**: Interactive conversation with AI assistant
|
| 32 |
+
- **Session Management**: Create and manage conversation sessions
|
| 33 |
+
- **Context Control**: Toggle between fresh and relevant context modes
|
| 34 |
+
- **Preferences**: Save and manage user preferences
|
| 35 |
+
- **Settings Control**: Toggle UI settings panel
|
| 36 |
+
|
| 37 |
+
### Response Format
|
| 38 |
+
|
| 39 |
+
All endpoints return data in standardized formats:
|
| 40 |
+
- **Chat endpoints**: Return tuples matching Gradio component outputs
|
| 41 |
+
- **Other endpoints**: Return strings or dictionaries
|
| 42 |
+
- **Errors**: Return standardized error messages with metadata
|
| 43 |
+
|
| 44 |
+
---
|
| 45 |
+
|
| 46 |
+
## Getting Started
|
| 47 |
+
|
| 48 |
+
### Installation
|
| 49 |
+
|
| 50 |
+
```bash
|
| 51 |
+
pip install gradio_client
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
### Basic Setup
|
| 55 |
+
|
| 56 |
+
```python
|
| 57 |
+
from gradio_client import Client
|
| 58 |
+
|
| 59 |
+
# Initialize client
|
| 60 |
+
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 61 |
+
|
| 62 |
+
# Or for private spaces (requires HF token)
|
| 63 |
+
client = Client(
|
| 64 |
+
"JatinAutonomousLabs/Research_AI_Assistant",
|
| 65 |
+
hf_token="your_hf_token_here"
|
| 66 |
+
)
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
---
|
| 70 |
+
|
| 71 |
+
## Authentication
|
| 72 |
+
|
| 73 |
+
### Public Space Access
|
| 74 |
+
|
| 75 |
+
Public spaces require no authentication:
|
| 76 |
+
|
| 77 |
+
```python
|
| 78 |
+
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
### Private Space Access
|
| 82 |
+
|
| 83 |
+
For private spaces, pass your Hugging Face token:
|
| 84 |
+
|
| 85 |
+
```python
|
| 86 |
+
client = Client(
|
| 87 |
+
"JatinAutonomousLabs/Research_AI_Assistant",
|
| 88 |
+
hf_token=os.getenv("HF_TOKEN")
|
| 89 |
+
)
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
### Token Management
|
| 93 |
+
|
| 94 |
+
1. Get token from: https://huggingface.co/settings/tokens
|
| 95 |
+
2. Store securely (environment variables recommended)
|
| 96 |
+
3. Never commit tokens to version control
|
| 97 |
+
|
| 98 |
+
---
|
| 99 |
+
|
| 100 |
+
## API Endpoints
|
| 101 |
+
|
| 102 |
+
### 1. Chat Handler - `/safe_gpu_chat_handler`
|
| 103 |
+
|
| 104 |
+
Process user messages and get AI responses.
|
| 105 |
+
|
| 106 |
+
#### Endpoint Details
|
| 107 |
+
|
| 108 |
+
- **API Name:** `/safe_gpu_chat_handler`
|
| 109 |
+
- **Method:** POST (via Gradio client)
|
| 110 |
+
- **Description:** Main chat interface that processes user messages and returns AI responses with metadata
|
| 111 |
+
|
| 112 |
+
#### Request Parameters
|
| 113 |
+
|
| 114 |
+
| Parameter | Type | Required | Default | Description |
|
| 115 |
+
|-----------|------|----------|---------|-------------|
|
| 116 |
+
| `message` | `str` | Yes | - | User message (max 10,000 characters) |
|
| 117 |
+
| `history` | `list[dict]` | No | `[]` | Chat history in Gradio format |
|
| 118 |
+
| `user_id` | `Literal[str]` | No | `"Test_Any"` | User identifier: `'Admin_J'`, `'Dev_K'`, `'Dev_H'`, `'Dev_A'`, `'Test_Any'` |
|
| 119 |
+
| `session_text` | `str` | No | `"Session: ... \| User: ... \| Interactions: 0"` | Session information string |
|
| 120 |
+
|
| 121 |
+
#### Response Structure
|
| 122 |
+
|
| 123 |
+
Returns a tuple of **7 elements**:
|
| 124 |
+
|
| 125 |
+
```python
|
| 126 |
+
(
|
| 127 |
+
chatbot_history, # list[dict] - Updated chat history
|
| 128 |
+
message_input, # str - Empty string (cleared after send)
|
| 129 |
+
reasoning_data, # dict - Chain of thought and reasoning
|
| 130 |
+
performance_data, # dict - Agent trace, token count, timing
|
| 131 |
+
context_data, # dict - Session context, interaction ID
|
| 132 |
+
session_info, # str - Updated session information
|
| 133 |
+
skills_html # str - HTML for identified skills display
|
| 134 |
+
)
|
| 135 |
+
```
|
| 136 |
+
|
| 137 |
+
#### Response Format Details
|
| 138 |
+
|
| 139 |
+
**1. `chatbot_history` (list[dict])**
|
| 140 |
+
```python
|
| 141 |
+
[
|
| 142 |
+
{
|
| 143 |
+
"role": "user",
|
| 144 |
+
"content": "What is machine learning?"
|
| 145 |
+
},
|
| 146 |
+
{
|
| 147 |
+
"role": "assistant",
|
| 148 |
+
"content": "Machine learning is a subset of artificial intelligence..."
|
| 149 |
+
}
|
| 150 |
+
]
|
| 151 |
+
```
|
| 152 |
+
|
| 153 |
+
**2. `reasoning_data` (dict)**
|
| 154 |
+
```python
|
| 155 |
+
{
|
| 156 |
+
"chain_of_thought": {
|
| 157 |
+
"step_1": {
|
| 158 |
+
"hypothesis": "User intent classification",
|
| 159 |
+
"evidence": ["Keywords detected", "Context analyzed"],
|
| 160 |
+
"confidence": 0.85,
|
| 161 |
+
"reasoning": "Intent identified as information_request"
|
| 162 |
+
}
|
| 163 |
+
},
|
| 164 |
+
"confidence_calibration": {
|
| 165 |
+
"overall_confidence": 0.85,
|
| 166 |
+
"calibration_method": "temperature_scaling"
|
| 167 |
+
}
|
| 168 |
+
}
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
**3. `performance_data` (dict)**
|
| 172 |
+
```python
|
| 173 |
+
{
|
| 174 |
+
"agent_trace": [
|
| 175 |
+
{"agent": "intent_recognition", "duration": 0.234, "status": "success"},
|
| 176 |
+
{"agent": "response_synthesis", "duration": 1.456, "status": "success"}
|
| 177 |
+
],
|
| 178 |
+
"token_count": 1250,
|
| 179 |
+
"processing_time": 2.34,
|
| 180 |
+
"confidence_score": 0.85,
|
| 181 |
+
"agents_used": ["intent_recognition", "response_synthesis"]
|
| 182 |
+
}
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
+
**4. `context_data` (dict)**
|
| 186 |
+
```python
|
| 187 |
+
{
|
| 188 |
+
"interaction_id": "uuid-string",
|
| 189 |
+
"session_id": "abc12345",
|
| 190 |
+
"timestamp": "2024-12-28T10:30:00",
|
| 191 |
+
"warnings": [],
|
| 192 |
+
"context_mode": "relevant"
|
| 193 |
+
}
|
| 194 |
+
```
|
| 195 |
+
|
| 196 |
+
**5. `session_info` (str)**
|
| 197 |
+
```python
|
| 198 |
+
"Session: abc12345 | User: Test_Any | Interactions: 5"
|
| 199 |
+
```
|
| 200 |
+
|
| 201 |
+
**6. `skills_html` (str)**
|
| 202 |
+
```html
|
| 203 |
+
"<div class='skills-header'>🎯 Relevant Skills:</div>
|
| 204 |
+
<span class='skill-tag high-confidence'>Machine Learning</span>
|
| 205 |
+
<span class='skill-tag medium-confidence'>Python</span>"
|
| 206 |
+
```
|
| 207 |
+
|
| 208 |
+
#### Code Example
|
| 209 |
+
|
| 210 |
+
```python
|
| 211 |
+
from gradio_client import Client
|
| 212 |
+
|
| 213 |
+
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 214 |
+
|
| 215 |
+
# Make chat request
|
| 216 |
+
result = client.predict(
|
| 217 |
+
message="Explain quantum computing in simple terms",
|
| 218 |
+
history=[],
|
| 219 |
+
user_id="Test_Any",
|
| 220 |
+
session_text="Session: abc12345 | User: Test_Any | Interactions: 0",
|
| 221 |
+
api_name="/safe_gpu_chat_handler"
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
# Unpack results
|
| 225 |
+
chatbot_history, message_input, reasoning, performance, context, session_info, skills = result
|
| 226 |
+
|
| 227 |
+
# Access response
|
| 228 |
+
latest_message = chatbot_history[-1]["content"]
|
| 229 |
+
print(f"Assistant: {latest_message}")
|
| 230 |
+
|
| 231 |
+
# Access metadata
|
| 232 |
+
processing_time = performance.get("processing_time", 0)
|
| 233 |
+
print(f"Response time: {processing_time:.2f}s")
|
| 234 |
+
```
|
| 235 |
+
|
| 236 |
+
#### Validation Rules
|
| 237 |
+
|
| 238 |
+
- **Message**: Must be non-empty string, max 10,000 characters
|
| 239 |
+
- **User ID**: Must be one of: `Admin_J`, `Dev_K`, `Dev_H`, `Dev_A`, `Test_Any`
|
| 240 |
+
- **History**: Must be list (empty list if None)
|
| 241 |
+
- **Session Text**: Format: `"Session: <8-char-id> | User: <user_id> | Interactions: <count>"`
|
| 242 |
+
|
| 243 |
+
#### Error Responses
|
| 244 |
+
|
| 245 |
+
If validation fails or processing errors occur:
|
| 246 |
+
|
| 247 |
+
```python
|
| 248 |
+
{
|
| 249 |
+
"error": "Message cannot be empty" # or other error message
|
| 250 |
+
}
|
| 251 |
+
```
|
| 252 |
+
|
| 253 |
+
Error responses maintain the same tuple structure with error information in metadata fields.
|
| 254 |
+
|
| 255 |
+
---
|
| 256 |
+
|
| 257 |
+
### 2. New Session - `/new_session`
|
| 258 |
+
|
| 259 |
+
Create a new conversation session.
|
| 260 |
+
|
| 261 |
+
#### Endpoint Details
|
| 262 |
+
|
| 263 |
+
- **API Name:** `/new_session`
|
| 264 |
+
- **Method:** POST (via Gradio client)
|
| 265 |
+
- **Description:** Creates a new session ID and initializes it in the database
|
| 266 |
+
|
| 267 |
+
#### Request Parameters
|
| 268 |
+
|
| 269 |
+
| Parameter | Type | Required | Default | Description |
|
| 270 |
+
|-----------|------|----------|---------|-------------|
|
| 271 |
+
| `user_id` | `Literal[str]` | No | `"Test_Any"` | User identifier |
|
| 272 |
+
|
| 273 |
+
#### Response
|
| 274 |
+
|
| 275 |
+
Returns a single string:
|
| 276 |
+
|
| 277 |
+
```python
|
| 278 |
+
"Session: abc12345 | User: Test_Any | Interactions: 0"
|
| 279 |
+
```
|
| 280 |
+
|
| 281 |
+
#### Code Example
|
| 282 |
+
|
| 283 |
+
```python
|
| 284 |
+
result = client.predict(
|
| 285 |
+
user_id="Admin_J",
|
| 286 |
+
api_name="/new_session"
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
session_info = result
|
| 290 |
+
print(session_info) # "Session: xyz67890 | User: Admin_J | Interactions: 0"
|
| 291 |
+
```
|
| 292 |
+
|
| 293 |
+
#### Behavior
|
| 294 |
+
|
| 295 |
+
- Generates new 8-character hexadecimal session ID
|
| 296 |
+
- Validates and normalizes user_id
|
| 297 |
+
- Initializes session in database via context_manager
|
| 298 |
+
- Returns formatted session info string
|
| 299 |
+
- Continues execution even if database initialization fails
|
| 300 |
+
|
| 301 |
+
---
|
| 302 |
+
|
| 303 |
+
### 3. Update Session Info - `/update_session_info`
|
| 304 |
+
|
| 305 |
+
Update session metadata (typically called when user changes).
|
| 306 |
+
|
| 307 |
+
#### Endpoint Details
|
| 308 |
+
|
| 309 |
+
- **API Name:** `/update_session_info`
|
| 310 |
+
- **Method:** POST (via Gradio client)
|
| 311 |
+
- **Description:** Updates session information, typically when user_id changes
|
| 312 |
+
|
| 313 |
+
#### Request Parameters
|
| 314 |
+
|
| 315 |
+
| Parameter | Type | Required | Default | Description |
|
| 316 |
+
|-----------|------|----------|---------|-------------|
|
| 317 |
+
| `user_id` | `Literal[str]` | No | `"Test_Any"` | New user identifier |
|
| 318 |
+
| `session_text` | `str` | No | `"Session: ... \| User: ... \| Interactions: 0"` | Current session text |
|
| 319 |
+
|
| 320 |
+
#### Response
|
| 321 |
+
|
| 322 |
+
Returns updated session info string:
|
| 323 |
+
|
| 324 |
+
```python
|
| 325 |
+
"Session: abc12345 | User: Admin_J | Interactions: 5"
|
| 326 |
+
```
|
| 327 |
+
|
| 328 |
+
#### Code Example
|
| 329 |
+
|
| 330 |
+
```python
|
| 331 |
+
result = client.predict(
|
| 332 |
+
user_id="Admin_J",
|
| 333 |
+
session_text="Session: abc12345 | User: Test_Any | Interactions: 3",
|
| 334 |
+
api_name="/update_session_info"
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
+
updated_session = result
|
| 338 |
+
print(updated_session) # "Session: abc12345 | User: Admin_J | Interactions: 3"
|
| 339 |
+
```
|
| 340 |
+
|
| 341 |
+
#### Important Notes
|
| 342 |
+
|
| 343 |
+
- **Session Continuity**: Never generates new session ID
|
| 344 |
+
- **Interaction Count**: Fetches actual count from database
|
| 345 |
+
- **Preservation**: Returns original session_text if parsing fails
|
| 346 |
+
|
| 347 |
+
---
|
| 348 |
+
|
| 349 |
+
### 4. Toggle Settings - `/toggle_settings`
|
| 350 |
+
|
| 351 |
+
Toggle the settings panel visibility.
|
| 352 |
+
|
| 353 |
+
#### Endpoint Details
|
| 354 |
+
|
| 355 |
+
- **API Name:** `/toggle_settings`
|
| 356 |
+
- **Method:** POST (via Gradio client)
|
| 357 |
+
- **Description:** Toggles the settings panel visibility state
|
| 358 |
+
|
| 359 |
+
#### Request Parameters
|
| 360 |
+
|
| 361 |
+
None required.
|
| 362 |
+
|
| 363 |
+
#### Response
|
| 364 |
+
|
| 365 |
+
Returns Gradio update object (handled internally by client).
|
| 366 |
+
|
| 367 |
+
#### Code Example
|
| 368 |
+
|
| 369 |
+
```python
|
| 370 |
+
client.predict(
|
| 371 |
+
api_name="/toggle_settings"
|
| 372 |
+
)
|
| 373 |
+
```
|
| 374 |
+
|
| 375 |
+
#### Behavior
|
| 376 |
+
|
| 377 |
+
- Uses global state tracking (`_settings_panel_visible`)
|
| 378 |
+
- Toggles between visible/hidden states
|
| 379 |
+
- Falls back to visible on error
|
| 380 |
+
|
| 381 |
+
---
|
| 382 |
+
|
| 383 |
+
### 5. Toggle Settings from Nav - `/toggle_settings_from_nav`
|
| 384 |
+
|
| 385 |
+
Toggle settings panel from mobile navigation.
|
| 386 |
+
|
| 387 |
+
#### Endpoint Details
|
| 388 |
+
|
| 389 |
+
- **API Name:** `/toggle_settings_from_nav`
|
| 390 |
+
- **Method:** POST (via Gradio client)
|
| 391 |
+
- **Description:** Same as `/toggle_settings` but triggered from mobile nav
|
| 392 |
+
|
| 393 |
+
#### Request Parameters
|
| 394 |
+
|
| 395 |
+
None required.
|
| 396 |
+
|
| 397 |
+
#### Response
|
| 398 |
+
|
| 399 |
+
Returns Gradio update object.
|
| 400 |
+
|
| 401 |
+
#### Code Example
|
| 402 |
+
|
| 403 |
+
```python
|
| 404 |
+
client.predict(
|
| 405 |
+
api_name="/toggle_settings_from_nav"
|
| 406 |
+
)
|
| 407 |
+
```
|
| 408 |
+
|
| 409 |
+
---
|
| 410 |
+
|
| 411 |
+
### 6. Handle Mode Change - `/handle_mode_change`
|
| 412 |
+
|
| 413 |
+
Change context mode (Fresh or Relevant).
|
| 414 |
+
|
| 415 |
+
#### Endpoint Details
|
| 416 |
+
|
| 417 |
+
- **API Name:** `/handle_mode_change`
|
| 418 |
+
- **Method:** POST (via Gradio client)
|
| 419 |
+
- **Description:** Updates the context mode for the current session
|
| 420 |
+
|
| 421 |
+
#### Request Parameters
|
| 422 |
+
|
| 423 |
+
| Parameter | Type | Required | Default | Description |
|
| 424 |
+
|-----------|------|----------|---------|-------------|
|
| 425 |
+
| `mode` | `Literal['fresh', 'relevant']` | Yes | `"fresh"` | Context mode: `'fresh'` = no user context, `'relevant'` = only relevant context |
|
| 426 |
+
| `session_id_text` | `str` | Yes | `"Session: ... \| User: ... \| Interactions: 0"` | Session information string |
|
| 427 |
+
|
| 428 |
+
#### Response
|
| 429 |
+
|
| 430 |
+
Returns status message string:
|
| 431 |
+
|
| 432 |
+
```python
|
| 433 |
+
"*Current: Fresh Context*" # or "*Current: Relevant Context*"
|
| 434 |
+
```
|
| 435 |
+
|
| 436 |
+
#### Code Example
|
| 437 |
+
|
| 438 |
+
```python
|
| 439 |
+
result = client.predict(
|
| 440 |
+
mode="relevant",
|
| 441 |
+
session_id_text="Session: abc12345 | User: Test_Any | Interactions: 3",
|
| 442 |
+
api_name="/handle_mode_change"
|
| 443 |
+
)
|
| 444 |
+
|
| 445 |
+
status = result
|
| 446 |
+
print(status) # "*Current: Relevant Context*"
|
| 447 |
+
```
|
| 448 |
+
|
| 449 |
+
#### Mode Descriptions
|
| 450 |
+
|
| 451 |
+
- **`fresh`**: Each response generated without user context from previous sessions
|
| 452 |
+
- **`relevant`**: System identifies and includes only relevant past discussions related to current topic
|
| 453 |
+
|
| 454 |
+
#### Validation
|
| 455 |
+
|
| 456 |
+
- Invalid mode values default to `'fresh'`
|
| 457 |
+
- Returns error message if session ID extraction fails
|
| 458 |
+
|
| 459 |
+
---
|
| 460 |
+
|
| 461 |
+
### 7. Save Preferences - `/save_preferences`
|
| 462 |
+
|
| 463 |
+
Save user preferences to database.
|
| 464 |
+
|
| 465 |
+
#### Endpoint Details
|
| 466 |
+
|
| 467 |
+
- **API Name:** `/save_preferences`
|
| 468 |
+
- **Method:** POST (via Gradio client)
|
| 469 |
+
- **Description:** Saves user preferences with database persistence
|
| 470 |
+
|
| 471 |
+
#### Request Parameters
|
| 472 |
+
|
| 473 |
+
| Parameter | Type | Required | Default | Description |
|
| 474 |
+
|-----------|------|----------|---------|-------------|
|
| 475 |
+
| `param_0` | `bool` | No | `True` | Show reasoning chain |
|
| 476 |
+
| `param_1` | `bool` | No | `False` | Show agent execution trace |
|
| 477 |
+
| `param_2` | `Literal['Fast', 'Balanced', 'Thorough']` | No | `"Balanced"` | Response speed preference |
|
| 478 |
+
| `param_3` | `bool` | No | `True` | Enable context caching |
|
| 479 |
+
|
| 480 |
+
#### Response
|
| 481 |
+
|
| 482 |
+
Returns status dictionary:
|
| 483 |
+
|
| 484 |
+
```python
|
| 485 |
+
{
|
| 486 |
+
"status": "success", # or "partial" if cache-only
|
| 487 |
+
"message": "Preferences saved"
|
| 488 |
+
}
|
| 489 |
+
```
|
| 490 |
+
|
| 491 |
+
#### Code Example
|
| 492 |
+
|
| 493 |
+
```python
|
| 494 |
+
result = client.predict(
|
| 495 |
+
param_0=True, # show_reasoning
|
| 496 |
+
param_1=False, # show_agent_trace
|
| 497 |
+
param_2="Fast", # response_speed
|
| 498 |
+
param_3=True, # cache_enabled
|
| 499 |
+
api_name="/save_preferences"
|
| 500 |
+
)
|
| 501 |
+
|
| 502 |
+
status = result["status"]
|
| 503 |
+
message = result["message"]
|
| 504 |
+
```
|
| 505 |
+
|
| 506 |
+
#### Preferences Schema
|
| 507 |
+
|
| 508 |
+
Preferences are stored as JSON:
|
| 509 |
+
|
| 510 |
+
```json
|
| 511 |
+
{
|
| 512 |
+
"show_reasoning": true,
|
| 513 |
+
"show_agent_trace": false,
|
| 514 |
+
"response_speed": "Fast",
|
| 515 |
+
"cache_enabled": true,
|
| 516 |
+
"timestamp": "2024-12-28T10:30:00.123456"
|
| 517 |
+
}
|
| 518 |
+
```
|
| 519 |
+
|
| 520 |
+
#### Storage
|
| 521 |
+
|
| 522 |
+
- **Primary**: Database table `user_preferences`
|
| 523 |
+
- **Fallback**: In-memory cache (if database unavailable)
|
| 524 |
+
- **Scope**: Session-specific or global (if no session_id)
|
| 525 |
+
|
| 526 |
+
---
|
| 527 |
+
|
| 528 |
+
## Error Handling
|
| 529 |
+
|
| 530 |
+
### Error Response Format
|
| 531 |
+
|
| 532 |
+
All endpoints return standardized error information:
|
| 533 |
+
|
| 534 |
+
```python
|
| 535 |
+
# Chat endpoints - errors in metadata
|
| 536 |
+
{
|
| 537 |
+
"error": "Error message here",
|
| 538 |
+
"type": "validation_error" | "processing_error" | "database_error"
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
# Other endpoints - error strings or error dictionaries
|
| 542 |
+
"*Error: Invalid session information*"
|
| 543 |
+
```
|
| 544 |
+
|
| 545 |
+
### Error Types
|
| 546 |
+
|
| 547 |
+
#### 1. Validation Errors
|
| 548 |
+
|
| 549 |
+
**Cause**: Invalid input parameters
|
| 550 |
+
**HTTP Equivalent**: 400 Bad Request
|
| 551 |
+
**Recovery**: Fix input parameters and retry
|
| 552 |
+
|
| 553 |
+
```python
|
| 554 |
+
# Example
|
| 555 |
+
{
|
| 556 |
+
"error": "Message cannot be empty",
|
| 557 |
+
"type": "validation_error"
|
| 558 |
+
}
|
| 559 |
+
```
|
| 560 |
+
|
| 561 |
+
#### 2. Processing Errors
|
| 562 |
+
|
| 563 |
+
**Cause**: Internal processing failures
|
| 564 |
+
**HTTP Equivalent**: 500 Internal Server Error
|
| 565 |
+
**Recovery**: Retry request, check system status
|
| 566 |
+
|
| 567 |
+
```python
|
| 568 |
+
# Example
|
| 569 |
+
{
|
| 570 |
+
"error": "Processing error: LLM API timeout",
|
| 571 |
+
"type": "processing_error"
|
| 572 |
+
}
|
| 573 |
+
```
|
| 574 |
+
|
| 575 |
+
#### 3. Database Errors
|
| 576 |
+
|
| 577 |
+
**Cause**: Database connection or query failures
|
| 578 |
+
**HTTP Equivalent**: 503 Service Unavailable
|
| 579 |
+
**Recovery**: System falls back to in-memory cache, retry if needed
|
| 580 |
+
|
| 581 |
+
```python
|
| 582 |
+
# Example
|
| 583 |
+
{
|
| 584 |
+
"error": "Failed to save preferences to database",
|
| 585 |
+
"type": "database_error",
|
| 586 |
+
"fallback": "saved_to_cache"
|
| 587 |
+
}
|
| 588 |
+
```
|
| 589 |
+
|
| 590 |
+
### Error Handling Best Practices
|
| 591 |
+
|
| 592 |
+
```python
|
| 593 |
+
from gradio_client import Client
|
| 594 |
+
import time
|
| 595 |
+
|
| 596 |
+
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 597 |
+
|
| 598 |
+
def safe_chat_request(message, history, max_retries=3):
|
| 599 |
+
"""Chat request with retry logic"""
|
| 600 |
+
for attempt in range(max_retries):
|
| 601 |
+
try:
|
| 602 |
+
result = client.predict(
|
| 603 |
+
message=message,
|
| 604 |
+
history=history,
|
| 605 |
+
user_id="Test_Any",
|
| 606 |
+
session_text="Session: abc12345 | User: Test_Any | Interactions: 0",
|
| 607 |
+
api_name="/safe_gpu_chat_handler"
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
+
# Check for errors in metadata
|
| 611 |
+
if isinstance(result[3], dict) and "error" in result[3]:
|
| 612 |
+
error_msg = result[3]["error"]
|
| 613 |
+
if "timeout" in error_msg.lower() and attempt < max_retries - 1:
|
| 614 |
+
time.sleep(2 ** attempt) # Exponential backoff
|
| 615 |
+
continue
|
| 616 |
+
raise Exception(error_msg)
|
| 617 |
+
|
| 618 |
+
return result
|
| 619 |
+
|
| 620 |
+
except Exception as e:
|
| 621 |
+
if attempt == max_retries - 1:
|
| 622 |
+
raise
|
| 623 |
+
time.sleep(2 ** attempt)
|
| 624 |
+
|
| 625 |
+
raise Exception("Max retries exceeded")
|
| 626 |
+
```
|
| 627 |
+
|
| 628 |
+
---
|
| 629 |
+
|
| 630 |
+
## Code Examples
|
| 631 |
+
|
| 632 |
+
### Complete Integration Example
|
| 633 |
+
|
| 634 |
+
```python
|
| 635 |
+
from gradio_client import Client
|
| 636 |
+
import os
|
| 637 |
+
from typing import List, Dict, Optional
|
| 638 |
+
|
| 639 |
+
class ResearchAIAssistant:
|
| 640 |
+
"""Wrapper class for Research AI Assistant API"""
|
| 641 |
+
|
| 642 |
+
def __init__(self, hf_token: Optional[str] = None):
|
| 643 |
+
"""Initialize client"""
|
| 644 |
+
self.client = Client(
|
| 645 |
+
"JatinAutonomousLabs/Research_AI_Assistant",
|
| 646 |
+
hf_token=hf_token or os.getenv("HF_TOKEN")
|
| 647 |
+
)
|
| 648 |
+
self.session_info = None
|
| 649 |
+
self.user_id = "Test_Any"
|
| 650 |
+
self.chat_history = []
|
| 651 |
+
|
| 652 |
+
def create_session(self, user_id: str = "Test_Any") -> str:
|
| 653 |
+
"""Create new session"""
|
| 654 |
+
self.user_id = user_id
|
| 655 |
+
self.session_info = self.client.predict(
|
| 656 |
+
user_id=user_id,
|
| 657 |
+
api_name="/new_session"
|
| 658 |
+
)
|
| 659 |
+
self.chat_history = []
|
| 660 |
+
return self.session_info
|
| 661 |
+
|
| 662 |
+
def send_message(self, message: str) -> Dict:
|
| 663 |
+
"""Send message and get response"""
|
| 664 |
+
if not self.session_info:
|
| 665 |
+
self.create_session()
|
| 666 |
+
|
| 667 |
+
result = self.client.predict(
|
| 668 |
+
message=message,
|
| 669 |
+
history=self.chat_history,
|
| 670 |
+
user_id=self.user_id,
|
| 671 |
+
session_text=self.session_info,
|
| 672 |
+
api_name="/safe_gpu_chat_handler"
|
| 673 |
+
)
|
| 674 |
+
|
| 675 |
+
# Unpack results
|
| 676 |
+
history, _, reasoning, performance, context, session_info, skills = result
|
| 677 |
+
|
| 678 |
+
# Update state
|
| 679 |
+
self.chat_history = history
|
| 680 |
+
self.session_info = session_info
|
| 681 |
+
|
| 682 |
+
return {
|
| 683 |
+
"response": history[-1]["content"] if history else "",
|
| 684 |
+
"reasoning": reasoning,
|
| 685 |
+
"performance": performance,
|
| 686 |
+
"context": context,
|
| 687 |
+
"skills": skills
|
| 688 |
+
}
|
| 689 |
+
|
| 690 |
+
def set_context_mode(self, mode: str) -> str:
|
| 691 |
+
"""Change context mode"""
|
| 692 |
+
if not self.session_info:
|
| 693 |
+
raise ValueError("No active session")
|
| 694 |
+
|
| 695 |
+
result = self.client.predict(
|
| 696 |
+
mode=mode,
|
| 697 |
+
session_id_text=self.session_info,
|
| 698 |
+
api_name="/handle_mode_change"
|
| 699 |
+
)
|
| 700 |
+
|
| 701 |
+
return result
|
| 702 |
+
|
| 703 |
+
def save_preferences(self, **preferences) -> Dict:
|
| 704 |
+
"""Save user preferences"""
|
| 705 |
+
return self.client.predict(
|
| 706 |
+
param_0=preferences.get("show_reasoning", True),
|
| 707 |
+
param_1=preferences.get("show_agent_trace", False),
|
| 708 |
+
param_2=preferences.get("response_speed", "Balanced"),
|
| 709 |
+
param_3=preferences.get("cache_enabled", True),
|
| 710 |
+
api_name="/save_preferences"
|
| 711 |
+
)
|
| 712 |
+
|
| 713 |
+
# Usage
|
| 714 |
+
assistant = ResearchAIAssistant(hf_token="your_token")
|
| 715 |
+
|
| 716 |
+
# Create session
|
| 717 |
+
session = assistant.create_session(user_id="Admin_J")
|
| 718 |
+
print(f"Session created: {session}")
|
| 719 |
+
|
| 720 |
+
# Set context mode
|
| 721 |
+
assistant.set_context_mode("relevant")
|
| 722 |
+
print("Context mode set to relevant")
|
| 723 |
+
|
| 724 |
+
# Send message
|
| 725 |
+
response = assistant.send_message("Explain machine learning")
|
| 726 |
+
print(f"Response: {response['response']}")
|
| 727 |
+
print(f"Processing time: {response['performance'].get('processing_time', 0):.2f}s")
|
| 728 |
+
|
| 729 |
+
# Save preferences
|
| 730 |
+
assistant.save_preferences(
|
| 731 |
+
show_reasoning=True,
|
| 732 |
+
response_speed="Fast",
|
| 733 |
+
cache_enabled=True
|
| 734 |
+
)
|
| 735 |
+
```
|
| 736 |
+
|
| 737 |
+
### Async Integration Example
|
| 738 |
+
|
| 739 |
+
```python
|
| 740 |
+
import asyncio
|
| 741 |
+
from gradio_client import Client
|
| 742 |
+
|
| 743 |
+
async def async_chat_request(message: str, history: List, session_text: str):
|
| 744 |
+
"""Async wrapper for chat requests"""
|
| 745 |
+
loop = asyncio.get_event_loop()
|
| 746 |
+
|
| 747 |
+
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 748 |
+
|
| 749 |
+
# Run in thread pool to avoid blocking
|
| 750 |
+
result = await loop.run_in_executor(
|
| 751 |
+
None,
|
| 752 |
+
lambda: client.predict(
|
| 753 |
+
message=message,
|
| 754 |
+
history=history,
|
| 755 |
+
user_id="Test_Any",
|
| 756 |
+
session_text=session_text,
|
| 757 |
+
api_name="/safe_gpu_chat_handler"
|
| 758 |
+
)
|
| 759 |
+
)
|
| 760 |
+
|
| 761 |
+
return result
|
| 762 |
+
|
| 763 |
+
# Usage
|
| 764 |
+
async def main():
|
| 765 |
+
result = await async_chat_request(
|
| 766 |
+
message="Hello",
|
| 767 |
+
history=[],
|
| 768 |
+
session_text="Session: abc12345 | User: Test_Any | Interactions: 0"
|
| 769 |
+
)
|
| 770 |
+
print(result[0][-1]["content"])
|
| 771 |
+
|
| 772 |
+
asyncio.run(main())
|
| 773 |
+
```
|
| 774 |
+
|
| 775 |
+
---
|
| 776 |
+
|
| 777 |
+
## Integration Guide
|
| 778 |
+
|
| 779 |
+
### Step 1: Install Dependencies
|
| 780 |
+
|
| 781 |
+
```bash
|
| 782 |
+
pip install gradio_client requests
|
| 783 |
+
```
|
| 784 |
+
|
| 785 |
+
### Step 2: Initialize Client
|
| 786 |
+
|
| 787 |
+
```python
|
| 788 |
+
from gradio_client import Client
|
| 789 |
+
|
| 790 |
+
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 791 |
+
```
|
| 792 |
+
|
| 793 |
+
### Step 3: Create Session
|
| 794 |
+
|
| 795 |
+
```python
|
| 796 |
+
session_info = client.predict(
|
| 797 |
+
user_id="Test_Any",
|
| 798 |
+
api_name="/new_session"
|
| 799 |
+
)
|
| 800 |
+
```
|
| 801 |
+
|
| 802 |
+
### Step 4: Send Messages
|
| 803 |
+
|
| 804 |
+
```python
|
| 805 |
+
result = client.predict(
|
| 806 |
+
message="Your question here",
|
| 807 |
+
history=[],
|
| 808 |
+
user_id="Test_Any",
|
| 809 |
+
session_text=session_info,
|
| 810 |
+
api_name="/safe_gpu_chat_handler"
|
| 811 |
+
)
|
| 812 |
+
|
| 813 |
+
chatbot_history, _, reasoning, performance, context, session_info, skills = result
|
| 814 |
+
```
|
| 815 |
+
|
| 816 |
+
### Step 5: Handle Context Mode
|
| 817 |
+
|
| 818 |
+
```python
|
| 819 |
+
# Switch to relevant context mode
|
| 820 |
+
client.predict(
|
| 821 |
+
mode="relevant",
|
| 822 |
+
session_id_text=session_info,
|
| 823 |
+
api_name="/handle_mode_change"
|
| 824 |
+
)
|
| 825 |
+
```
|
| 826 |
+
|
| 827 |
+
### Step 6: Save Preferences
|
| 828 |
+
|
| 829 |
+
```python
|
| 830 |
+
client.predict(
|
| 831 |
+
param_0=True,
|
| 832 |
+
param_1=False,
|
| 833 |
+
param_2="Balanced",
|
| 834 |
+
param_3=True,
|
| 835 |
+
api_name="/save_preferences"
|
| 836 |
+
)
|
| 837 |
+
```
|
| 838 |
+
|
| 839 |
+
---
|
| 840 |
+
|
| 841 |
+
## Testing
|
| 842 |
+
|
| 843 |
+
### Unit Test Example
|
| 844 |
+
|
| 845 |
+
```python
|
| 846 |
+
import unittest
|
| 847 |
+
from gradio_client import Client
|
| 848 |
+
|
| 849 |
+
class TestResearchAIAssistant(unittest.TestCase):
|
| 850 |
+
|
| 851 |
+
def setUp(self):
|
| 852 |
+
self.client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 853 |
+
self.session_info = None
|
| 854 |
+
|
| 855 |
+
def test_create_session(self):
|
| 856 |
+
"""Test session creation"""
|
| 857 |
+
result = self.client.predict(
|
| 858 |
+
user_id="Test_Any",
|
| 859 |
+
api_name="/new_session"
|
| 860 |
+
)
|
| 861 |
+
self.assertIsInstance(result, str)
|
| 862 |
+
self.assertIn("Session:", result)
|
| 863 |
+
self.session_info = result
|
| 864 |
+
|
| 865 |
+
def test_chat_message(self):
|
| 866 |
+
"""Test chat message processing"""
|
| 867 |
+
if not self.session_info:
|
| 868 |
+
self.test_create_session()
|
| 869 |
+
|
| 870 |
+
result = self.client.predict(
|
| 871 |
+
message="Hello",
|
| 872 |
+
history=[],
|
| 873 |
+
user_id="Test_Any",
|
| 874 |
+
session_text=self.session_info,
|
| 875 |
+
api_name="/safe_gpu_chat_handler"
|
| 876 |
+
)
|
| 877 |
+
|
| 878 |
+
self.assertEqual(len(result), 7)
|
| 879 |
+
self.assertIsInstance(result[0], list) # history
|
| 880 |
+
self.assertIsInstance(result[2], dict) # reasoning
|
| 881 |
+
|
| 882 |
+
def test_context_mode_change(self):
|
| 883 |
+
"""Test context mode change"""
|
| 884 |
+
if not self.session_info:
|
| 885 |
+
self.test_create_session()
|
| 886 |
+
|
| 887 |
+
result = self.client.predict(
|
| 888 |
+
mode="relevant",
|
| 889 |
+
session_id_text=self.session_info,
|
| 890 |
+
api_name="/handle_mode_change"
|
| 891 |
+
)
|
| 892 |
+
|
| 893 |
+
self.assertIn("Context", result)
|
| 894 |
+
|
| 895 |
+
if __name__ == '__main__':
|
| 896 |
+
unittest.main()
|
| 897 |
+
```
|
| 898 |
+
|
| 899 |
+
### Integration Test Example
|
| 900 |
+
|
| 901 |
+
```python
|
| 902 |
+
def test_full_conversation_flow():
|
| 903 |
+
"""Test complete conversation flow"""
|
| 904 |
+
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 905 |
+
|
| 906 |
+
# 1. Create session
|
| 907 |
+
session = client.predict(user_id="Test_Any", api_name="/new_session")
|
| 908 |
+
assert "Session:" in session
|
| 909 |
+
|
| 910 |
+
# 2. Set context mode
|
| 911 |
+
client.predict(mode="relevant", session_id_text=session, api_name="/handle_mode_change")
|
| 912 |
+
|
| 913 |
+
# 3. Send messages
|
| 914 |
+
history = []
|
| 915 |
+
for message in ["Hello", "What is AI?", "Explain further"]:
|
| 916 |
+
result = client.predict(
|
| 917 |
+
message=message,
|
| 918 |
+
history=history,
|
| 919 |
+
user_id="Test_Any",
|
| 920 |
+
session_text=session,
|
| 921 |
+
api_name="/safe_gpu_chat_handler"
|
| 922 |
+
)
|
| 923 |
+
history = result[0]
|
| 924 |
+
session = result[5] # Updated session info
|
| 925 |
+
|
| 926 |
+
# 4. Verify conversation context
|
| 927 |
+
assert len(history) == 6 # 3 user + 3 assistant messages
|
| 928 |
+
|
| 929 |
+
# 5. Save preferences
|
| 930 |
+
prefs = client.predict(
|
| 931 |
+
param_0=True,
|
| 932 |
+
param_1=False,
|
| 933 |
+
param_2="Fast",
|
| 934 |
+
param_3=True,
|
| 935 |
+
api_name="/save_preferences"
|
| 936 |
+
)
|
| 937 |
+
assert prefs["status"] in ["success", "partial"]
|
| 938 |
+
|
| 939 |
+
print("✅ All integration tests passed")
|
| 940 |
+
```
|
| 941 |
+
|
| 942 |
+
---
|
| 943 |
+
|
| 944 |
+
## Rate Limits & Best Practices
|
| 945 |
+
|
| 946 |
+
### Rate Limits
|
| 947 |
+
|
| 948 |
+
Currently, there are **no explicit rate limits** enforced. However, best practices:
|
| 949 |
+
|
| 950 |
+
- **Chat Requests**: Limit to 1-2 requests per second per session
|
| 951 |
+
- **Session Creation**: No strict limit, but avoid rapid session creation
|
| 952 |
+
- **Preference Updates**: Limit to reasonable frequency (not per-request)
|
| 953 |
+
|
| 954 |
+
### Best Practices
|
| 955 |
+
|
| 956 |
+
#### 1. Session Management
|
| 957 |
+
|
| 958 |
+
```python
|
| 959 |
+
# ✅ Good: Reuse sessions
|
| 960 |
+
session = create_session()
|
| 961 |
+
for message in messages:
|
| 962 |
+
send_message(message, session)
|
| 963 |
+
|
| 964 |
+
# ❌ Bad: Create new session for each message
|
| 965 |
+
for message in messages:
|
| 966 |
+
session = create_session() # Don't do this
|
| 967 |
+
send_message(message, session)
|
| 968 |
+
```
|
| 969 |
+
|
| 970 |
+
#### 2. Error Handling
|
| 971 |
+
|
| 972 |
+
```python
|
| 973 |
+
# ✅ Good: Handle errors gracefully
|
| 974 |
+
try:
|
| 975 |
+
result = send_message(message)
|
| 976 |
+
except Exception as e:
|
| 977 |
+
logger.error(f"Request failed: {e}")
|
| 978 |
+
# Implement retry or fallback
|
| 979 |
+
|
| 980 |
+
# ❌ Bad: Ignore errors
|
| 981 |
+
result = send_message(message) # No error handling
|
| 982 |
+
```
|
| 983 |
+
|
| 984 |
+
#### 3. Context Mode
|
| 985 |
+
|
| 986 |
+
```python
|
| 987 |
+
# ✅ Good: Set mode once at session start
|
| 988 |
+
set_context_mode("relevant") # Once per session
|
| 989 |
+
send_message("Question 1")
|
| 990 |
+
send_message("Question 2") # Mode persists
|
| 991 |
+
|
| 992 |
+
# ❌ Bad: Change mode frequently
|
| 993 |
+
set_context_mode("fresh")
|
| 994 |
+
send_message("Q1")
|
| 995 |
+
set_context_mode("relevant") # Unnecessary switching
|
| 996 |
+
send_message("Q2")
|
| 997 |
+
```
|
| 998 |
+
|
| 999 |
+
#### 4. Message Length
|
| 1000 |
+
|
| 1001 |
+
```python
|
| 1002 |
+
# ✅ Good: Reasonable message length
|
| 1003 |
+
message = "Your question here" # < 1000 chars typically
|
| 1004 |
+
|
| 1005 |
+
# ❌ Bad: Extremely long messages
|
| 1006 |
+
message = "A" * 10000 # Max allowed but not recommended
|
| 1007 |
+
```
|
| 1008 |
+
|
| 1009 |
+
---
|
| 1010 |
+
|
| 1011 |
+
## Support & Troubleshooting
|
| 1012 |
+
|
| 1013 |
+
### Common Issues
|
| 1014 |
+
|
| 1015 |
+
#### 1. Connection Errors
|
| 1016 |
+
|
| 1017 |
+
**Symptom**: `ConnectionError` or timeout
|
| 1018 |
+
**Solution**:
|
| 1019 |
+
```python
|
| 1020 |
+
# Add retry logic
|
| 1021 |
+
import time
|
| 1022 |
+
max_retries = 3
|
| 1023 |
+
for attempt in range(max_retries):
|
| 1024 |
+
try:
|
| 1025 |
+
result = client.predict(...)
|
| 1026 |
+
break
|
| 1027 |
+
except Exception as e:
|
| 1028 |
+
if attempt < max_retries - 1:
|
| 1029 |
+
time.sleep(2 ** attempt)
|
| 1030 |
+
else:
|
| 1031 |
+
raise
|
| 1032 |
+
```
|
| 1033 |
+
|
| 1034 |
+
#### 2. Invalid Session ID
|
| 1035 |
+
|
| 1036 |
+
**Symptom**: `ValueError: Could not extract session_id`
|
| 1037 |
+
**Solution**: Ensure session_text format is correct:
|
| 1038 |
+
```
|
| 1039 |
+
"Session: abc12345 | User: Test_Any | Interactions: 0"
|
| 1040 |
+
```
|
| 1041 |
+
|
| 1042 |
+
#### 3. Empty Responses
|
| 1043 |
+
|
| 1044 |
+
**Symptom**: Response tuple contains empty strings
|
| 1045 |
+
**Possible Causes**:
|
| 1046 |
+
- LLM API timeout
|
| 1047 |
+
- Invalid message format
|
| 1048 |
+
- System overload
|
| 1049 |
+
|
| 1050 |
+
**Solution**: Check `performance_data` for error information
|
| 1051 |
+
|
| 1052 |
+
#### 4. Preferences Not Saving
|
| 1053 |
+
|
| 1054 |
+
**Symptom**: Preferences return "partial" status
|
| 1055 |
+
**Cause**: Database unavailable, using cache fallback
|
| 1056 |
+
**Solution**: Preferences still work via cache, database will sync when available
|
| 1057 |
+
|
| 1058 |
+
### Debugging Tips
|
| 1059 |
+
|
| 1060 |
+
#### Enable Detailed Logging
|
| 1061 |
+
|
| 1062 |
+
```python
|
| 1063 |
+
import logging
|
| 1064 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 1065 |
+
|
| 1066 |
+
# Client will show detailed request/response logs
|
| 1067 |
+
```
|
| 1068 |
+
|
| 1069 |
+
#### Check Response Metadata
|
| 1070 |
+
|
| 1071 |
+
```python
|
| 1072 |
+
result = client.predict(...)
|
| 1073 |
+
reasoning = result[2] # Check for errors
|
| 1074 |
+
performance = result[3] # Check processing info
|
| 1075 |
+
context = result[4] # Check session context
|
| 1076 |
+
```
|
| 1077 |
+
|
| 1078 |
+
#### Validate Session Info
|
| 1079 |
+
|
| 1080 |
+
```python
|
| 1081 |
+
session_info = "Session: abc12345 | User: Test_Any | Interactions: 0"
|
| 1082 |
+
|
| 1083 |
+
# Validate format
|
| 1084 |
+
import re
|
| 1085 |
+
match = re.search(r'Session: ([a-f0-9]{8})', session_info)
|
| 1086 |
+
if not match:
|
| 1087 |
+
raise ValueError("Invalid session format")
|
| 1088 |
+
```
|
| 1089 |
+
|
| 1090 |
+
---
|
| 1091 |
+
|
| 1092 |
+
## API Versioning
|
| 1093 |
+
|
| 1094 |
+
### Current Version: 1.0.0
|
| 1095 |
+
|
| 1096 |
+
- **Base URL**: Stable (Hugging Face Spaces URL)
|
| 1097 |
+
- **Endpoint Names**: Stable
|
| 1098 |
+
- **Response Formats**: Stable
|
| 1099 |
+
- **Parameters**: Backward compatible additions only
|
| 1100 |
+
|
| 1101 |
+
### Version History
|
| 1102 |
+
|
| 1103 |
+
- **v1.0.0** (2024-12-28): Initial stable release with all endpoints
|
| 1104 |
+
|
| 1105 |
+
### Breaking Changes Policy
|
| 1106 |
+
|
| 1107 |
+
- Major version increments indicate breaking changes
|
| 1108 |
+
- Deprecated endpoints will be announced 30 days in advance
|
| 1109 |
+
- Old endpoint versions maintained for compatibility
|
| 1110 |
+
|
| 1111 |
+
---
|
| 1112 |
+
|
| 1113 |
+
## Additional Resources
|
| 1114 |
+
|
| 1115 |
+
### Documentation
|
| 1116 |
+
|
| 1117 |
+
- **Implementation Details**: See `API_ENDPOINTS_IMPLEMENTATION_COMPLETE.md`
|
| 1118 |
+
- **Application Features**: See `APPLICATION_FEATURES_REPORT.md`
|
| 1119 |
+
- **Context Management**: See `CONTEXT_RELEVANCE_IMPLEMENTATION_MILESTONE.md`
|
| 1120 |
+
|
| 1121 |
+
### Support Channels
|
| 1122 |
+
|
| 1123 |
+
- **GitHub Issues**: [Repository Link]
|
| 1124 |
+
- **Email Support**: [Support Email]
|
| 1125 |
+
- **Documentation**: [Documentation Link]
|
| 1126 |
+
|
| 1127 |
+
### Changelog
|
| 1128 |
+
|
| 1129 |
+
See `CHANGELOG.md` for detailed change history.
|
| 1130 |
+
|
| 1131 |
+
---
|
| 1132 |
+
|
| 1133 |
+
## Appendix
|
| 1134 |
+
|
| 1135 |
+
### A. Complete Request/Response Examples
|
| 1136 |
+
|
| 1137 |
+
#### Chat Request (Complete)
|
| 1138 |
+
|
| 1139 |
+
```python
|
| 1140 |
+
from gradio_client import Client
|
| 1141 |
+
|
| 1142 |
+
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 1143 |
+
|
| 1144 |
+
# Full request
|
| 1145 |
+
result = client.predict(
|
| 1146 |
+
message="What are the applications of quantum computing?",
|
| 1147 |
+
history=[
|
| 1148 |
+
{"role": "user", "content": "Hello"},
|
| 1149 |
+
{"role": "assistant", "content": "Hello! How can I help you?"}
|
| 1150 |
+
],
|
| 1151 |
+
user_id="Admin_J",
|
| 1152 |
+
session_text="Session: abc12345 | User: Admin_J | Interactions: 1",
|
| 1153 |
+
api_name="/safe_gpu_chat_handler"
|
| 1154 |
+
)
|
| 1155 |
+
|
| 1156 |
+
# Response unpacking
|
| 1157 |
+
history = result[0]
|
| 1158 |
+
reasoning = result[2]
|
| 1159 |
+
performance = result[3]
|
| 1160 |
+
context = result[4]
|
| 1161 |
+
session_info = result[5]
|
| 1162 |
+
skills = result[6]
|
| 1163 |
+
|
| 1164 |
+
print(f"Latest response: {history[-1]['content']}")
|
| 1165 |
+
print(f"Processing time: {performance.get('processing_time', 0):.2f}s")
|
| 1166 |
+
print(f"Tokens used: {performance.get('token_count', 0)}")
|
| 1167 |
+
```
|
| 1168 |
+
|
| 1169 |
+
### B. Session Lifecycle Example
|
| 1170 |
+
|
| 1171 |
+
```python
|
| 1172 |
+
# 1. Initialize
|
| 1173 |
+
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 1174 |
+
session = None
|
| 1175 |
+
history = []
|
| 1176 |
+
|
| 1177 |
+
# 2. Create session
|
| 1178 |
+
session = client.predict(user_id="Test_Any", api_name="/new_session")
|
| 1179 |
+
print(f"Created: {session}")
|
| 1180 |
+
|
| 1181 |
+
# 3. Set context mode
|
| 1182 |
+
client.predict(mode="relevant", session_id_text=session, api_name="/handle_mode_change")
|
| 1183 |
+
|
| 1184 |
+
# 4. Conversation loop
|
| 1185 |
+
for i in range(3):
|
| 1186 |
+
user_message = input("You: ")
|
| 1187 |
+
|
| 1188 |
+
result = client.predict(
|
| 1189 |
+
message=user_message,
|
| 1190 |
+
history=history,
|
| 1191 |
+
user_id="Test_Any",
|
| 1192 |
+
session_text=session,
|
| 1193 |
+
api_name="/safe_gpu_chat_handler"
|
| 1194 |
+
)
|
| 1195 |
+
|
| 1196 |
+
history = result[0]
|
| 1197 |
+
session = result[5] # Updated session with interaction count
|
| 1198 |
+
|
| 1199 |
+
print(f"Assistant: {history[-1]['content']}")
|
| 1200 |
+
print(f"Session: {session}")
|
| 1201 |
+
|
| 1202 |
+
# 5. Save preferences
|
| 1203 |
+
client.predict(
|
| 1204 |
+
param_0=True,
|
| 1205 |
+
param_1=False,
|
| 1206 |
+
param_2="Balanced",
|
| 1207 |
+
param_3=True,
|
| 1208 |
+
api_name="/save_preferences"
|
| 1209 |
+
)
|
| 1210 |
+
```
|
| 1211 |
+
|
| 1212 |
+
### C. Error Recovery Patterns
|
| 1213 |
+
|
| 1214 |
+
```python
|
| 1215 |
+
def robust_chat_request(message, history, session_info, max_retries=3):
|
| 1216 |
+
"""Chat request with comprehensive error handling"""
|
| 1217 |
+
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 1218 |
+
|
| 1219 |
+
for attempt in range(max_retries):
|
| 1220 |
+
try:
|
| 1221 |
+
result = client.predict(
|
| 1222 |
+
message=message,
|
| 1223 |
+
history=history,
|
| 1224 |
+
user_id="Test_Any",
|
| 1225 |
+
session_text=session_info,
|
| 1226 |
+
api_name="/safe_gpu_chat_handler"
|
| 1227 |
+
)
|
| 1228 |
+
|
| 1229 |
+
# Check for errors in response
|
| 1230 |
+
if isinstance(result[3], dict) and "error" in result[3]:
|
| 1231 |
+
error = result[3]["error"]
|
| 1232 |
+
if attempt < max_retries - 1 and "timeout" in error.lower():
|
| 1233 |
+
time.sleep(2 ** attempt)
|
| 1234 |
+
continue
|
| 1235 |
+
raise Exception(error)
|
| 1236 |
+
|
| 1237 |
+
return result
|
| 1238 |
+
|
| 1239 |
+
except ConnectionError as e:
|
| 1240 |
+
if attempt < max_retries - 1:
|
| 1241 |
+
time.sleep(2 ** attempt)
|
| 1242 |
+
continue
|
| 1243 |
+
raise
|
| 1244 |
+
except Exception as e:
|
| 1245 |
+
if attempt < max_retries - 1:
|
| 1246 |
+
time.sleep(1)
|
| 1247 |
+
continue
|
| 1248 |
+
raise
|
| 1249 |
+
|
| 1250 |
+
raise Exception("Max retries exceeded")
|
| 1251 |
+
```
|
| 1252 |
+
|
| 1253 |
+
---
|
| 1254 |
+
|
| 1255 |
+
**Document End**
|
| 1256 |
+
*For questions or issues, please refer to the Support section above.*
|
| 1257 |
+
|
API_QUICK_REFERENCE.md
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# API Quick Reference Guide
|
| 2 |
+
|
| 3 |
+
**Quick lookup for engineering team integration**
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## Installation
|
| 8 |
+
|
| 9 |
+
```bash
|
| 10 |
+
pip install gradio_client
|
| 11 |
+
```
|
| 12 |
+
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
## Client Initialization
|
| 16 |
+
|
| 17 |
+
```python
|
| 18 |
+
from gradio_client import Client
|
| 19 |
+
|
| 20 |
+
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
## All Available Endpoints
|
| 26 |
+
|
| 27 |
+
### 1. Chat Handler
|
| 28 |
+
```python
|
| 29 |
+
result = client.predict(
|
| 30 |
+
message="Your question",
|
| 31 |
+
history=[],
|
| 32 |
+
user_id="Test_Any",
|
| 33 |
+
session_text="Session: abc12345 | User: Test_Any | Interactions: 0",
|
| 34 |
+
api_name="/safe_gpu_chat_handler"
|
| 35 |
+
)
|
| 36 |
+
# Returns: (history, input, reasoning, performance, context, session_info, skills)
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
### 2. New Session
|
| 40 |
+
```python
|
| 41 |
+
session = client.predict(
|
| 42 |
+
user_id="Test_Any",
|
| 43 |
+
api_name="/new_session"
|
| 44 |
+
)
|
| 45 |
+
# Returns: "Session: xyz67890 | User: Test_Any | Interactions: 0"
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
### 3. Update Session Info
|
| 49 |
+
```python
|
| 50 |
+
updated = client.predict(
|
| 51 |
+
user_id="Admin_J",
|
| 52 |
+
session_text="Session: abc12345 | User: Test_Any | Interactions: 3",
|
| 53 |
+
api_name="/update_session_info"
|
| 54 |
+
)
|
| 55 |
+
# Returns: "Session: abc12345 | User: Admin_J | Interactions: 3"
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
### 4. Toggle Settings
|
| 59 |
+
```python
|
| 60 |
+
client.predict(api_name="/toggle_settings")
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
### 5. Toggle Settings from Nav
|
| 64 |
+
```python
|
| 65 |
+
client.predict(api_name="/toggle_settings_from_nav")
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
### 6. Handle Mode Change
|
| 69 |
+
```python
|
| 70 |
+
status = client.predict(
|
| 71 |
+
mode="relevant",
|
| 72 |
+
session_id_text="Session: abc12345 | User: Test_Any | Interactions: 3",
|
| 73 |
+
api_name="/handle_mode_change"
|
| 74 |
+
)
|
| 75 |
+
# Returns: "*Current: Relevant Context*"
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
### 7. Save Preferences
|
| 79 |
+
```python
|
| 80 |
+
result = client.predict(
|
| 81 |
+
param_0=True, # show_reasoning
|
| 82 |
+
param_1=False, # show_agent_trace
|
| 83 |
+
param_2="Fast", # response_speed: 'Fast', 'Balanced', 'Thorough'
|
| 84 |
+
param_3=True, # cache_enabled
|
| 85 |
+
api_name="/save_preferences"
|
| 86 |
+
)
|
| 87 |
+
# Returns: {"status": "success", "message": "Preferences saved"}
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
---
|
| 91 |
+
|
| 92 |
+
## Response Structures
|
| 93 |
+
|
| 94 |
+
### Chat Response Tuple
|
| 95 |
+
```python
|
| 96 |
+
(
|
| 97 |
+
list[dict], # chatbot_history - [{"role": "user/assistant", "content": "..."}]
|
| 98 |
+
str, # message_input - "" (cleared)
|
| 99 |
+
dict, # reasoning_data - Chain of thought
|
| 100 |
+
dict, # performance_data - Timing, tokens, agents
|
| 101 |
+
dict, # context_data - Session context
|
| 102 |
+
str, # session_info - "Session: ... | User: ... | Interactions: N"
|
| 103 |
+
str # skills_html - HTML string for skills display
|
| 104 |
+
)
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
### Reasoning Data Structure
|
| 108 |
+
```python
|
| 109 |
+
{
|
| 110 |
+
"chain_of_thought": {
|
| 111 |
+
"step_1": {
|
| 112 |
+
"hypothesis": "...",
|
| 113 |
+
"evidence": [...],
|
| 114 |
+
"confidence": 0.85,
|
| 115 |
+
"reasoning": "..."
|
| 116 |
+
}
|
| 117 |
+
},
|
| 118 |
+
"confidence_calibration": {"overall_confidence": 0.85}
|
| 119 |
+
}
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
### Performance Data Structure
|
| 123 |
+
```python
|
| 124 |
+
{
|
| 125 |
+
"agent_trace": [...],
|
| 126 |
+
"processing_time": 2.34,
|
| 127 |
+
"token_count": 1250,
|
| 128 |
+
"confidence_score": 0.85,
|
| 129 |
+
"agents_used": [...]
|
| 130 |
+
}
|
| 131 |
+
```
|
| 132 |
+
|
| 133 |
+
---
|
| 134 |
+
|
| 135 |
+
## Validation Rules
|
| 136 |
+
|
| 137 |
+
| Parameter | Validation |
|
| 138 |
+
|-----------|------------|
|
| 139 |
+
| `message` | Non-empty, max 10,000 chars |
|
| 140 |
+
| `user_id` | One of: `Admin_J`, `Dev_K`, `Dev_H`, `Dev_A`, `Test_Any` |
|
| 141 |
+
| `mode` | One of: `'fresh'`, `'relevant'` |
|
| 142 |
+
| `response_speed` | One of: `'Fast'`, `'Balanced'`, `'Thorough'` |
|
| 143 |
+
| `session_text` | Format: `"Session: <8-hex> | User: <user_id> | Interactions: <num>"` |
|
| 144 |
+
|
| 145 |
+
---
|
| 146 |
+
|
| 147 |
+
## Common Patterns
|
| 148 |
+
|
| 149 |
+
### Full Conversation Flow
|
| 150 |
+
```python
|
| 151 |
+
# 1. Create session
|
| 152 |
+
session = client.predict(user_id="Test_Any", api_name="/new_session")
|
| 153 |
+
|
| 154 |
+
# 2. Set context mode
|
| 155 |
+
client.predict(mode="relevant", session_id_text=session, api_name="/handle_mode_change")
|
| 156 |
+
|
| 157 |
+
# 3. Send messages
|
| 158 |
+
history = []
|
| 159 |
+
for msg in ["Q1", "Q2", "Q3"]:
|
| 160 |
+
result = client.predict(
|
| 161 |
+
message=msg, history=history,
|
| 162 |
+
user_id="Test_Any", session_text=session,
|
| 163 |
+
api_name="/safe_gpu_chat_handler"
|
| 164 |
+
)
|
| 165 |
+
history = result[0]
|
| 166 |
+
session = result[5] # Update session
|
| 167 |
+
print(result[0][-1]["content"]) # Print response
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
### Error Handling
|
| 171 |
+
```python
|
| 172 |
+
try:
|
| 173 |
+
result = client.predict(...)
|
| 174 |
+
if isinstance(result[3], dict) and "error" in result[3]:
|
| 175 |
+
print(f"Error: {result[3]['error']}")
|
| 176 |
+
except Exception as e:
|
| 177 |
+
print(f"Request failed: {e}")
|
| 178 |
+
```
|
| 179 |
+
|
| 180 |
+
---
|
| 181 |
+
|
| 182 |
+
## Parameter Quick Reference
|
| 183 |
+
|
| 184 |
+
### `/safe_gpu_chat_handler`
|
| 185 |
+
- `message` (str, required)
|
| 186 |
+
- `history` (list, default: [])
|
| 187 |
+
- `user_id` (str, default: "Test_Any")
|
| 188 |
+
- `session_text` (str, default: "Session: ... | User: ... | Interactions: 0")
|
| 189 |
+
|
| 190 |
+
### `/new_session`
|
| 191 |
+
- `user_id` (str, default: "Test_Any")
|
| 192 |
+
|
| 193 |
+
### `/update_session_info`
|
| 194 |
+
- `user_id` (str, default: "Test_Any")
|
| 195 |
+
- `session_text` (str, required)
|
| 196 |
+
|
| 197 |
+
### `/handle_mode_change`
|
| 198 |
+
- `mode` (str, required: "fresh" | "relevant")
|
| 199 |
+
- `session_id_text` (str, required)
|
| 200 |
+
|
| 201 |
+
### `/save_preferences`
|
| 202 |
+
- `param_0` (bool, default: True) - show_reasoning
|
| 203 |
+
- `param_1` (bool, default: False) - show_agent_trace
|
| 204 |
+
- `param_2` (str, default: "Balanced") - response_speed
|
| 205 |
+
- `param_3` (bool, default: True) - cache_enabled
|
| 206 |
+
|
| 207 |
+
---
|
| 208 |
+
|
| 209 |
+
## Status Codes / Return Values
|
| 210 |
+
|
| 211 |
+
### Success Indicators
|
| 212 |
+
- Chat: Valid tuple with 7 elements
|
| 213 |
+
- Preferences: `{"status": "success"}` or `{"status": "partial"}`
|
| 214 |
+
- Mode Change: `"*Current: Fresh Context*"` or `"*Current: Relevant Context*"`
|
| 215 |
+
|
| 216 |
+
### Error Indicators
|
| 217 |
+
- Chat: Error message in `performance_data` or `context_data`
|
| 218 |
+
- Mode Change: `"*Error: Invalid session information*"`
|
| 219 |
+
- Other: Error strings or dictionaries with `"error"` key
|
| 220 |
+
|
| 221 |
+
---
|
| 222 |
+
|
| 223 |
+
**See `API_DOCUMENTATION.md` for complete documentation.**
|
| 224 |
+
|
app.py
CHANGED
|
@@ -1906,6 +1906,9 @@ if __name__ == "__main__":
|
|
| 1906 |
demo.launch(
|
| 1907 |
server_name="0.0.0.0",
|
| 1908 |
server_port=7860,
|
| 1909 |
-
share=False
|
|
|
|
|
|
|
|
|
|
| 1910 |
)
|
| 1911 |
|
|
|
|
| 1906 |
demo.launch(
|
| 1907 |
server_name="0.0.0.0",
|
| 1908 |
server_port=7860,
|
| 1909 |
+
share=False,
|
| 1910 |
+
show_api=True, # Explicitly enable API (recommended for API access)
|
| 1911 |
+
allowed_paths=[], # Empty list = don't serve any files (prevents file URLs)
|
| 1912 |
+
blocked_paths=["/tmp", "/var", "/etc", "/home"] # Explicitly block sensitive paths (extra security)
|
| 1913 |
)
|
| 1914 |
|