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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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
# Research AI Assistant - API Documentation

**Version:** 1.0.0  
**Base URL:** `https://huggingface.co/spaces/JatinAutonomousLabs/Research_AI_Assistant`  
**API Type:** Gradio Client API  
**Last Updated:** December 2024

---

## Table of Contents

1. [Overview](#overview)
2. [Getting Started](#getting-started)
3. [Authentication](#authentication)
4. [API Endpoints](#api-endpoints)
5. [Error Handling](#error-handling)
6. [Code Examples](#code-examples)
7. [Integration Guide](#integration-guide)
8. [Testing](#testing)
9. [Rate Limits & Best Practices](#rate-limits--best-practices)
10. [Support & Troubleshooting](#support--troubleshooting)

---

## Overview

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.

### Key Features

- **Chat Interface**: Interactive conversation with AI assistant
- **Session Management**: Create and manage conversation sessions
- **Context Control**: Toggle between fresh and relevant context modes
- **Preferences**: Save and manage user preferences
- **Settings Control**: Toggle UI settings panel

### Response Format

All endpoints return data in standardized formats:
- **Chat endpoints**: Return tuples matching Gradio component outputs
- **Other endpoints**: Return strings or dictionaries
- **Errors**: Return standardized error messages with metadata

---

## Getting Started

### Installation

```bash
pip install gradio_client
```

### Basic Setup

```python
from gradio_client import Client

# Initialize client
client = Client("JatinAutonomousLabs/Research_AI_Assistant")

# Or for private spaces (requires HF token)
client = Client(
    "JatinAutonomousLabs/Research_AI_Assistant",
    hf_token="your_hf_token_here"
)
```

---

## Authentication

### Public Space Access

Public spaces require no authentication:

```python
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
```

### Private Space Access

For private spaces, pass your Hugging Face token:

```python
client = Client(
    "JatinAutonomousLabs/Research_AI_Assistant",
    hf_token=os.getenv("HF_TOKEN")
)
```

### Token Management

1. Get token from: https://huggingface.co/settings/tokens
2. Store securely (environment variables recommended)
3. Never commit tokens to version control

---

## User Management

### Base User
- `Admin_J` is the base/default API user
- Used when no `user_id` is provided or invalid format is provided

### Dynamic User Creation
The API supports dynamic user creation - any valid format `user_id` is automatically accepted and created in the database.

**Valid Format:**
- Alphanumeric characters + underscore only
- Length: 1-50 characters
- Pattern: `^[a-zA-Z0-9_]{1,50}$`
- Examples: `User123`, `External_API`, `MyUser_2024`, `API_Client_01`

**Auto-Creation:**
- New users are automatically inserted into the `user_contexts` database table on first use
- No manual user registration required
- User information is maintained by the backend database

**Validation:**
- Valid formats are accepted and auto-created
- Invalid formats default to `Admin_J`
- Database automatically maintains user information

### UI Restriction
- The HuggingFace Spaces UI is restricted to `ADMINONLY` user only
- Dynamic user creation is available **only via API calls**
- UI users cannot use dynamic user IDs

---

## API Endpoints

### 1. Chat Handler - `/safe_gpu_chat_handler`

Process user messages and get AI responses.

#### Endpoint Details

- **API Name:** `/safe_gpu_chat_handler`
- **Method:** POST (via Gradio client)
- **Description:** Main chat interface that processes user messages and returns AI responses with metadata

#### Request Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `message` | `str` | Yes | - | User message (max 10,000 characters) |
| `history` | `list[dict]` | No | `[]` | Chat history in Gradio format |
| `user_id` | `str` | No | `"Admin_J"` | User identifier. Base user is `Admin_J`. Any valid format user_id (alphanumeric + underscore, 1-50 chars) is accepted and automatically created in the database. |
| `session_text` | `str` | No | `"Session: ... \| User: ... \| Interactions: 0"` | Session information string |

#### Response Structure

Returns a tuple of **7 elements**:

```python
(
    chatbot_history,      # list[dict] - Updated chat history
    message_input,        # str - Empty string (cleared after send)
    reasoning_data,       # dict - Chain of thought and reasoning
    performance_data,    # dict - Agent trace, token count, timing
    context_data,        # dict - Session context, interaction ID
    session_info,        # str - Updated session information
    skills_html          # str - HTML for identified skills display
)
```

#### Response Format Details

**1. `chatbot_history` (list[dict])**
```python
[
    {
        "role": "user",
        "content": "What is machine learning?"
    },
    {
        "role": "assistant",
        "content": "Machine learning is a subset of artificial intelligence..."
    }
]
```

**2. `reasoning_data` (dict)**
```python
{
    "chain_of_thought": {
        "step_1": {
            "hypothesis": "User intent classification",
            "evidence": ["Keywords detected", "Context analyzed"],
            "confidence": 0.85,
            "reasoning": "Intent identified as information_request"
        }
    },
    "confidence_calibration": {
        "overall_confidence": 0.85,
        "calibration_method": "temperature_scaling"
    }
}
```

**3. `performance_data` (dict)**
```python
{
    "agent_trace": [
        {"agent": "intent_recognition", "duration": 0.234, "status": "success"},
        {"agent": "response_synthesis", "duration": 1.456, "status": "success"}
    ],
    "token_count": 1250,
    "processing_time": 2.34,
    "confidence_score": 0.85,
    "agents_used": ["intent_recognition", "response_synthesis"]
}
```

**4. `context_data` (dict)**
```python
{
    "interaction_id": "uuid-string",
    "session_id": "abc12345",
    "timestamp": "2024-12-28T10:30:00",
    "warnings": [],
    "context_mode": "relevant"
}
```

**5. `session_info` (str)**
```python
"Session: abc12345 | User: Admin_J | Interactions: 5"
```

**6. `skills_html` (str)**
```html
"<div class='skills-header'>🎯 Relevant Skills:</div>
 <span class='skill-tag high-confidence'>Machine Learning</span>
 <span class='skill-tag medium-confidence'>Python</span>"
```

#### Code Example

```python
from gradio_client import Client

client = Client("JatinAutonomousLabs/Research_AI_Assistant")

# Make chat request (using base Admin_J user)
result = client.predict(
    message="Explain quantum computing in simple terms",
    history=[],
    user_id="Admin_J",
    session_text="Session: abc12345 | User: Admin_J | Interactions: 0",
    api_name="/safe_gpu_chat_handler"
)

# Make chat request (using dynamic user - auto-created)
result = client.predict(
    message="What is machine learning?",
    history=[],
    user_id="MyNewUser_123",  # New user - automatically created in DB
    session_text="Session: abc12345 | User: MyNewUser_123 | Interactions: 0",
    api_name="/safe_gpu_chat_handler"
)

# Unpack results
chatbot_history, message_input, reasoning, performance, context, session_info, skills = result

# Access response
latest_message = chatbot_history[-1]["content"]
print(f"Assistant: {latest_message}")

# Access metadata
processing_time = performance.get("processing_time", 0)
print(f"Response time: {processing_time:.2f}s")
```

#### Validation Rules

- **Message**: Must be non-empty string, max 10,000 characters
- **User ID**: 
  - Base user: `Admin_J` (default)
  - Dynamic users: Any alphanumeric + underscore format (1-50 characters)
  - New users are automatically created in the database on first use
  - Invalid formats default to `Admin_J`
  - Format validation: `^[a-zA-Z0-9_]{1,50}$`
- **History**: Must be list (empty list if None)
- **Session Text**: Format: `"Session: <8-char-id> | User: <user_id> | Interactions: <count>"`

#### Error Responses

If validation fails or processing errors occur:

```python
{
    "error": "Message cannot be empty"  # or other error message
}
```

Error responses maintain the same tuple structure with error information in metadata fields.

---

### 2. New Session - `/new_session`

Create a new conversation session.

#### Endpoint Details

- **API Name:** `/new_session`
- **Method:** POST (via Gradio client)
- **Description:** Creates a new session ID and initializes it in the database

#### Request Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `user_id` | `str` | No | `"Admin_J"` | User identifier. Base user is `Admin_J`. Any valid format user_id is accepted and automatically created in the database. |

#### Response

Returns a single string:

```python
"Session: abc12345 | User: Admin_J | Interactions: 0"
```

#### Code Example

```python
result = client.predict(
    user_id="Admin_J",
    api_name="/new_session"
)

session_info = result
print(session_info)  # "Session: xyz67890 | User: Admin_J | Interactions: 0"
```

#### Behavior

- Generates new 8-character hexadecimal session ID
- Validates and normalizes user_id (defaults to `Admin_J` if invalid)
- Auto-creates new users in database on first use
- Initializes session in database via context_manager
- Returns formatted session info string
- Continues execution even if database initialization fails

---

### 3. Update Session Info - `/update_session_info`

Update session metadata (typically called when user changes).

#### Endpoint Details

- **API Name:** `/update_session_info`
- **Method:** POST (via Gradio client)
- **Description:** Updates session information, typically when user_id changes

#### Request Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `user_id` | `str` | No | `"Admin_J"` | New user identifier. Base user is `Admin_J`. Any valid format user_id is accepted and automatically created in the database. |
| `session_text` | `str` | No | `"Session: ... \| User: ... \| Interactions: 0"` | Current session text |

#### Response

Returns updated session info string:

```python
"Session: abc12345 | User: Admin_J | Interactions: 5"
```

#### Code Example

```python
result = client.predict(
    user_id="Admin_J",
    session_text="Session: abc12345 | User: Admin_J | Interactions: 3",
    api_name="/update_session_info"
)

updated_session = result
print(updated_session)  # "Session: abc12345 | User: Admin_J | Interactions: 3"
```

#### Important Notes

- **Session Continuity**: Never generates new session ID
- **Interaction Count**: Fetches actual count from database
- **Preservation**: Returns original session_text if parsing fails

---

### 4. Toggle Settings - `/toggle_settings`

Toggle the settings panel visibility.

#### Endpoint Details

- **API Name:** `/toggle_settings`
- **Method:** POST (via Gradio client)
- **Description:** Toggles the settings panel visibility state

#### Request Parameters

None required.

#### Response

Returns Gradio update object (handled internally by client).

#### Code Example

```python
client.predict(
    api_name="/toggle_settings"
)
```

#### Behavior

- Uses global state tracking (`_settings_panel_visible`)
- Toggles between visible/hidden states
- Falls back to visible on error

---

### 5. Toggle Settings from Nav - `/toggle_settings_from_nav`

Toggle settings panel from mobile navigation.

#### Endpoint Details

- **API Name:** `/toggle_settings_from_nav`
- **Method:** POST (via Gradio client)
- **Description:** Same as `/toggle_settings` but triggered from mobile nav

#### Request Parameters

None required.

#### Response

Returns Gradio update object.

#### Code Example

```python
client.predict(
    api_name="/toggle_settings_from_nav"
)
```

---

### 6. Handle Mode Change - `/handle_mode_change`

Change context mode (Fresh or Relevant).

#### Endpoint Details

- **API Name:** `/handle_mode_change`
- **Method:** POST (via Gradio client)
- **Description:** Updates the context mode for the current session

#### Request Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `mode` | `Literal['fresh', 'relevant']` | Yes | `"fresh"` | Context mode: `'fresh'` = no user context, `'relevant'` = only relevant context |
| `session_id_text` | `str` | Yes | `"Session: ... \| User: ... \| Interactions: 0"` | Session information string |

#### Response

Returns status message string:

```python
"*Current: Fresh Context*"  # or "*Current: Relevant Context*"
```

#### Code Example

```python
result = client.predict(
    mode="relevant",
    session_id_text="Session: abc12345 | User: Admin_J | Interactions: 3",
    api_name="/handle_mode_change"
)

status = result
print(status)  # "*Current: Relevant Context*"
```

#### Mode Descriptions

- **`fresh`**: Each response generated without user context from previous sessions
- **`relevant`**: System identifies and includes only relevant past discussions related to current topic

#### Validation

- Invalid mode values default to `'fresh'`
- Returns error message if session ID extraction fails

---

### 7. Save Preferences - `/save_preferences`

Save user preferences to database.

#### Endpoint Details

- **API Name:** `/save_preferences`
- **Method:** POST (via Gradio client)
- **Description:** Saves user preferences with database persistence

#### Request Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `param_0` | `bool` | No | `True` | Show reasoning chain |
| `param_1` | `bool` | No | `False` | Show agent execution trace |
| `param_2` | `Literal['Fast', 'Balanced', 'Thorough']` | No | `"Balanced"` | Response speed preference |
| `param_3` | `bool` | No | `True` | Enable context caching |

#### Response

Returns status dictionary:

```python
{
    "status": "success",  # or "partial" if cache-only
    "message": "Preferences saved"
}
```

#### Code Example

```python
result = client.predict(
    param_0=True,      # show_reasoning
    param_1=False,     # show_agent_trace
    param_2="Fast",    # response_speed
    param_3=True,      # cache_enabled
    api_name="/save_preferences"
)

status = result["status"]
message = result["message"]
```

#### Preferences Schema

Preferences are stored as JSON:

```json
{
    "show_reasoning": true,
    "show_agent_trace": false,
    "response_speed": "Fast",
    "cache_enabled": true,
    "timestamp": "2024-12-28T10:30:00.123456"
}
```

#### Storage

- **Primary**: Database table `user_preferences`
- **Fallback**: In-memory cache (if database unavailable)
- **Scope**: Session-specific or global (if no session_id)

---

## Error Handling

### Error Response Format

All endpoints return standardized error information:

```python
# Chat endpoints - errors in metadata
{
    "error": "Error message here",
    "type": "validation_error" | "processing_error" | "database_error"
}

# Other endpoints - error strings or error dictionaries
"*Error: Invalid session information*"
```

### Error Types

#### 1. Validation Errors

**Cause**: Invalid input parameters  
**HTTP Equivalent**: 400 Bad Request  
**Recovery**: Fix input parameters and retry

```python
# Example
{
    "error": "Message cannot be empty",
    "type": "validation_error"
}
```

#### 2. Processing Errors

**Cause**: Internal processing failures  
**HTTP Equivalent**: 500 Internal Server Error  
**Recovery**: Retry request, check system status

```python
# Example
{
    "error": "Processing error: LLM API timeout",
    "type": "processing_error"
}
```

#### 3. Database Errors

**Cause**: Database connection or query failures  
**HTTP Equivalent**: 503 Service Unavailable  
**Recovery**: System falls back to in-memory cache, retry if needed

```python
# Example
{
    "error": "Failed to save preferences to database",
    "type": "database_error",
    "fallback": "saved_to_cache"
}
```

### Error Handling Best Practices

```python
from gradio_client import Client
import time

client = Client("JatinAutonomousLabs/Research_AI_Assistant")

def safe_chat_request(message, history, max_retries=3):
    """Chat request with retry logic"""
    for attempt in range(max_retries):
        try:
            result = client.predict(
                message=message,
                history=history,
                user_id="Admin_J",
                session_text="Session: abc12345 | User: Admin_J | Interactions: 0",
                api_name="/safe_gpu_chat_handler"
            )
            
            # Check for errors in metadata
            if isinstance(result[3], dict) and "error" in result[3]:
                error_msg = result[3]["error"]
                if "timeout" in error_msg.lower() and attempt < max_retries - 1:
                    time.sleep(2 ** attempt)  # Exponential backoff
                    continue
                raise Exception(error_msg)
            
            return result
            
        except Exception as e:
            if attempt == max_retries - 1:
                raise
            time.sleep(2 ** attempt)
    
    raise Exception("Max retries exceeded")
```

---

## Code Examples

### Complete Integration Example

```python
from gradio_client import Client
import os
from typing import List, Dict, Optional

class ResearchAIAssistant:
    """Wrapper class for Research AI Assistant API"""
    
    def __init__(self, hf_token: Optional[str] = None):
        """Initialize client"""
        self.client = Client(
            "JatinAutonomousLabs/Research_AI_Assistant",
            hf_token=hf_token or os.getenv("HF_TOKEN")
        )
        self.session_info = None
        self.user_id = "Admin_J"
        self.chat_history = []
    
    def create_session(self, user_id: str = "Admin_J") -> str:
        """Create new session"""
        self.user_id = user_id
        self.session_info = self.client.predict(
            user_id=user_id,
            api_name="/new_session"
        )
        self.chat_history = []
        return self.session_info
    
    def send_message(self, message: str) -> Dict:
        """Send message and get response"""
        if not self.session_info:
            self.create_session()
        
        result = self.client.predict(
            message=message,
            history=self.chat_history,
            user_id=self.user_id,
            session_text=self.session_info,
            api_name="/safe_gpu_chat_handler"
        )
        
        # Unpack results
        history, _, reasoning, performance, context, session_info, skills = result
        
        # Update state
        self.chat_history = history
        self.session_info = session_info
        
        return {
            "response": history[-1]["content"] if history else "",
            "reasoning": reasoning,
            "performance": performance,
            "context": context,
            "skills": skills
        }
    
    def set_context_mode(self, mode: str) -> str:
        """Change context mode"""
        if not self.session_info:
            raise ValueError("No active session")
        
        result = self.client.predict(
            mode=mode,
            session_id_text=self.session_info,
            api_name="/handle_mode_change"
        )
        
        return result
    
    def save_preferences(self, **preferences) -> Dict:
        """Save user preferences"""
        return self.client.predict(
            param_0=preferences.get("show_reasoning", True),
            param_1=preferences.get("show_agent_trace", False),
            param_2=preferences.get("response_speed", "Balanced"),
            param_3=preferences.get("cache_enabled", True),
            api_name="/save_preferences"
        )

# Usage
assistant = ResearchAIAssistant(hf_token="your_token")

# Create session
session = assistant.create_session(user_id="Admin_J")
print(f"Session created: {session}")

# Set context mode
assistant.set_context_mode("relevant")
print("Context mode set to relevant")

# Send message
response = assistant.send_message("Explain machine learning")
print(f"Response: {response['response']}")
print(f"Processing time: {response['performance'].get('processing_time', 0):.2f}s")

# Save preferences
assistant.save_preferences(
    show_reasoning=True,
    response_speed="Fast",
    cache_enabled=True
)
```

### Async Integration Example

```python
import asyncio
from gradio_client import Client

async def async_chat_request(message: str, history: List, session_text: str):
    """Async wrapper for chat requests"""
    loop = asyncio.get_event_loop()
    
    client = Client("JatinAutonomousLabs/Research_AI_Assistant")
    
    # Run in thread pool to avoid blocking
    result = await loop.run_in_executor(
        None,
        lambda: client.predict(
            message=message,
            history=history,
            user_id="Admin_J",
            session_text=session_text,
            api_name="/safe_gpu_chat_handler"
        )
    )
    
    return result

# Usage
async def main():
    result = await async_chat_request(
        message="Hello",
        history=[],
        session_text="Session: abc12345 | User: Admin_J | Interactions: 0"
    )
    print(result[0][-1]["content"])

asyncio.run(main())
```

---

## Integration Guide

### Step 1: Install Dependencies

```bash
pip install gradio_client requests
```

### Step 2: Initialize Client

```python
from gradio_client import Client

client = Client("JatinAutonomousLabs/Research_AI_Assistant")
```

### Step 3: Create Session

```python
session_info = client.predict(
    user_id="Admin_J",
    api_name="/new_session"
)
```

### Step 4: Send Messages

```python
result = client.predict(
    message="Your question here",
    history=[],
    user_id="Admin_J",
    session_text=session_info,
    api_name="/safe_gpu_chat_handler"
)

chatbot_history, _, reasoning, performance, context, session_info, skills = result
```

### Step 5: Handle Context Mode

```python
# Switch to relevant context mode
client.predict(
    mode="relevant",
    session_id_text=session_info,
    api_name="/handle_mode_change"
)
```

### Step 6: Save Preferences

```python
client.predict(
    param_0=True,
    param_1=False,
    param_2="Balanced",
    param_3=True,
    api_name="/save_preferences"
)
```

---

## Testing

### Unit Test Example

```python
import unittest
from gradio_client import Client

class TestResearchAIAssistant(unittest.TestCase):
    
    def setUp(self):
        self.client = Client("JatinAutonomousLabs/Research_AI_Assistant")
        self.session_info = None
    
    def test_create_session(self):
        """Test session creation"""
        result = self.client.predict(
            user_id="Admin_J",
            api_name="/new_session"
        )
        self.assertIsInstance(result, str)
        self.assertIn("Session:", result)
        self.session_info = result
    
    def test_chat_message(self):
        """Test chat message processing"""
        if not self.session_info:
            self.test_create_session()
        
        result = self.client.predict(
            message="Hello",
            history=[],
            user_id="Admin_J",
            session_text=self.session_info,
            api_name="/safe_gpu_chat_handler"
        )
        
        self.assertEqual(len(result), 7)
        self.assertIsInstance(result[0], list)  # history
        self.assertIsInstance(result[2], dict)   # reasoning
    
    def test_context_mode_change(self):
        """Test context mode change"""
        if not self.session_info:
            self.test_create_session()
        
        result = self.client.predict(
            mode="relevant",
            session_id_text=self.session_info,
            api_name="/handle_mode_change"
        )
        
        self.assertIn("Context", result)

if __name__ == '__main__':
    unittest.main()
```

### Integration Test Example

```python
def test_full_conversation_flow():
    """Test complete conversation flow"""
    client = Client("JatinAutonomousLabs/Research_AI_Assistant")
    
    # 1. Create session
    session = client.predict(user_id="Admin_J", api_name="/new_session")
    assert "Session:" in session
    
    # 2. Set context mode
    client.predict(mode="relevant", session_id_text=session, api_name="/handle_mode_change")
    
    # 3. Send messages
    history = []
    for message in ["Hello", "What is AI?", "Explain further"]:
        result = client.predict(
            message=message,
            history=history,
            user_id="Admin_J",
            session_text=session,
            api_name="/safe_gpu_chat_handler"
        )
        history = result[0]
        session = result[5]  # Updated session info
    
    # 4. Verify conversation context
    assert len(history) == 6  # 3 user + 3 assistant messages
    
    # 5. Save preferences
    prefs = client.predict(
        param_0=True,
        param_1=False,
        param_2="Fast",
        param_3=True,
        api_name="/save_preferences"
    )
    assert prefs["status"] in ["success", "partial"]
    
    print("βœ… All integration tests passed")
```

---

## Rate Limits & Best Practices

### Rate Limits

Currently, there are **no explicit rate limits** enforced. However, best practices:

- **Chat Requests**: Limit to 1-2 requests per second per session
- **Session Creation**: No strict limit, but avoid rapid session creation
- **Preference Updates**: Limit to reasonable frequency (not per-request)

### Best Practices

#### 1. Session Management

```python
# βœ… Good: Reuse sessions
session = create_session()
for message in messages:
    send_message(message, session)

# ❌ Bad: Create new session for each message
for message in messages:
    session = create_session()  # Don't do this
    send_message(message, session)
```

#### 2. Error Handling

```python
# βœ… Good: Handle errors gracefully
try:
    result = send_message(message)
except Exception as e:
    logger.error(f"Request failed: {e}")
    # Implement retry or fallback

# ❌ Bad: Ignore errors
result = send_message(message)  # No error handling
```

#### 3. Context Mode

```python
# βœ… Good: Set mode once at session start
set_context_mode("relevant")  # Once per session
send_message("Question 1")
send_message("Question 2")  # Mode persists

# ❌ Bad: Change mode frequently
set_context_mode("fresh")
send_message("Q1")
set_context_mode("relevant")  # Unnecessary switching
send_message("Q2")
```

#### 4. Message Length

```python
# βœ… Good: Reasonable message length
message = "Your question here"  # < 1000 chars typically

# ❌ Bad: Extremely long messages
message = "A" * 10000  # Max allowed but not recommended
```

---

## Support & Troubleshooting

### Common Issues

#### 1. Connection Errors

**Symptom**: `ConnectionError` or timeout  
**Solution**:
```python
# Add retry logic
import time
max_retries = 3
for attempt in range(max_retries):
    try:
        result = client.predict(...)
        break
    except Exception as e:
        if attempt < max_retries - 1:
            time.sleep(2 ** attempt)
        else:
            raise
```

#### 2. Invalid Session ID

**Symptom**: `ValueError: Could not extract session_id`  
**Solution**: Ensure session_text format is correct:
```
"Session: abc12345 | User: Admin_J | Interactions: 0"
```

#### 3. Empty Responses

**Symptom**: Response tuple contains empty strings  
**Possible Causes**:
- LLM API timeout
- Invalid message format
- System overload

**Solution**: Check `performance_data` for error information

#### 4. Preferences Not Saving

**Symptom**: Preferences return "partial" status  
**Cause**: Database unavailable, using cache fallback  
**Solution**: Preferences still work via cache, database will sync when available

### Debugging Tips

#### Enable Detailed Logging

```python
import logging
logging.basicConfig(level=logging.DEBUG)

# Client will show detailed request/response logs
```

#### Check Response Metadata

```python
result = client.predict(...)
reasoning = result[2]  # Check for errors
performance = result[3]  # Check processing info
context = result[4]  # Check session context
```

#### Validate Session Info

```python
session_info = "Session: abc12345 | User: Admin_J | Interactions: 0"

# Validate format
import re
match = re.search(r'Session: ([a-f0-9]{8})', session_info)
if not match:
    raise ValueError("Invalid session format")
```

---

## API Versioning

### Current Version: 1.0.0

- **Base URL**: Stable (Hugging Face Spaces URL)
- **Endpoint Names**: Stable
- **Response Formats**: Stable
- **Parameters**: Backward compatible additions only

### Version History

- **v1.0.0** (2024-12-28): Initial stable release with all endpoints

### Breaking Changes Policy

- Major version increments indicate breaking changes
- Deprecated endpoints will be announced 30 days in advance
- Old endpoint versions maintained for compatibility

---

## Additional Resources

### Documentation

- **Implementation Details**: See `API_ENDPOINTS_IMPLEMENTATION_COMPLETE.md`
- **Application Features**: See `APPLICATION_FEATURES_REPORT.md`
- **Context Management**: See `CONTEXT_RELEVANCE_IMPLEMENTATION_MILESTONE.md`

### Support Channels

- **GitHub Issues**: [Repository Link]
- **Email Support**: [Support Email]
- **Documentation**: [Documentation Link]

### Changelog

See `CHANGELOG.md` for detailed change history.

---

## Appendix

### A. Complete Request/Response Examples

#### Chat Request (Complete)

```python
from gradio_client import Client

client = Client("JatinAutonomousLabs/Research_AI_Assistant")

# Full request
result = client.predict(
    message="What are the applications of quantum computing?",
    history=[
        {"role": "user", "content": "Hello"},
        {"role": "assistant", "content": "Hello! How can I help you?"}
    ],
    user_id="Admin_J",
    session_text="Session: abc12345 | User: Admin_J | Interactions: 1",
    api_name="/safe_gpu_chat_handler"
)

# Response unpacking
history = result[0]
reasoning = result[2]
performance = result[3]
context = result[4]
session_info = result[5]
skills = result[6]

print(f"Latest response: {history[-1]['content']}")
print(f"Processing time: {performance.get('processing_time', 0):.2f}s")
print(f"Tokens used: {performance.get('token_count', 0)}")
```

### B. Session Lifecycle Example

```python
# 1. Initialize
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
session = None
history = []

# 2. Create session
session = client.predict(user_id="Admin_J", api_name="/new_session")
print(f"Created: {session}")

# 3. Set context mode
client.predict(mode="relevant", session_id_text=session, api_name="/handle_mode_change")

# 4. Conversation loop
for i in range(3):
    user_message = input("You: ")
    
    result = client.predict(
        message=user_message,
        history=history,
        user_id="Admin_J",
        session_text=session,
        api_name="/safe_gpu_chat_handler"
    )
    
    history = result[0]
    session = result[5]  # Updated session with interaction count
    
    print(f"Assistant: {history[-1]['content']}")
    print(f"Session: {session}")

# 5. Save preferences
client.predict(
    param_0=True,
    param_1=False,
    param_2="Balanced",
    param_3=True,
    api_name="/save_preferences"
)
```

### C. Error Recovery Patterns

```python
def robust_chat_request(message, history, session_info, max_retries=3):
    """Chat request with comprehensive error handling"""
    client = Client("JatinAutonomousLabs/Research_AI_Assistant")
    
    for attempt in range(max_retries):
        try:
            result = client.predict(
                message=message,
                history=history,
                user_id="Admin_J",
                session_text=session_info,
                api_name="/safe_gpu_chat_handler"
            )
            
            # Check for errors in response
            if isinstance(result[3], dict) and "error" in result[3]:
                error = result[3]["error"]
                if attempt < max_retries - 1 and "timeout" in error.lower():
                    time.sleep(2 ** attempt)
                    continue
                raise Exception(error)
            
            return result
            
        except ConnectionError as e:
            if attempt < max_retries - 1:
                time.sleep(2 ** attempt)
                continue
            raise
        except Exception as e:
            if attempt < max_retries - 1:
                time.sleep(1)
                continue
            raise
    
    raise Exception("Max retries exceeded")
```

---

**Document End**  
*For questions or issues, please refer to the Support section above.*