File size: 6,546 Bytes
6b92e3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3fd5ce
 
 
 
 
 
 
 
 
6b92e3c
 
 
 
e3fd5ce
6b92e3c
 
 
e3fd5ce
 
6b92e3c
 
 
e3fd5ce
 
 
 
 
 
 
 
 
6b92e3c
 
 
 
e3fd5ce
 
 
 
 
 
 
 
6b92e3c
e3fd5ce
6b92e3c
 
e3fd5ce
6b92e3c
 
 
 
 
 
e3fd5ce
6b92e3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3fd5ce
6b92e3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3fd5ce
6b92e3c
 
 
 
 
 
 
 
 
 
e3fd5ce
 
6b92e3c
 
 
 
 
 
 
 
 
e3fd5ce
6b92e3c
 
 
 
 
e3fd5ce
 
 
 
 
6b92e3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3fd5ce
6b92e3c
 
 
e3fd5ce
6b92e3c
 
e3fd5ce
6b92e3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# API Quick Reference Guide

**Quick lookup for engineering team integration**

---

## Installation

```bash
pip install gradio_client
```

---

## Client Initialization

```python
from gradio_client import Client

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

---

## User Management

**Base User:** `Admin_J` (default)  
**Dynamic Users:** Any valid format (alphanumeric + underscore, 1-50 chars) - auto-created in DB  
**Format:** `^[a-zA-Z0-9_]{1,50}$`  
**UI Restriction:** HuggingFace Spaces UI limited to `ADMINONLY` only

---

## All Available Endpoints

### 1. Chat Handler
```python
# Using base Admin_J user
result = client.predict(
    message="Your question",
    history=[],
    user_id="Admin_J",
    session_text="Session: abc12345 | User: Admin_J | Interactions: 0",
    api_name="/safe_gpu_chat_handler"
)
# Returns: (history, input, reasoning, performance, context, session_info, skills)

# Using dynamic user (auto-created)
result = client.predict(
    message="Your question",
    history=[],
    user_id="ExternalAPI_User123",  # New user - auto-created in DB
    session_text="Session: abc12345 | User: ExternalAPI_User123 | Interactions: 0",
    api_name="/safe_gpu_chat_handler"
)
```

### 2. New Session
```python
# Base user
session = client.predict(
    user_id="Admin_J",
    api_name="/new_session"
)
# Returns: "Session: xyz67890 | User: Admin_J | Interactions: 0"

# Dynamic user (auto-created)
session = client.predict(
    user_id="MyNewUser_2024",
    api_name="/new_session"
)
# Returns: "Session: xyz67890 | User: MyNewUser_2024 | Interactions: 0"
```

### 3. Update Session Info
```python
updated = client.predict(
    user_id="Admin_J",
    session_text="Session: abc12345 | User: Admin_J | Interactions: 3",
    api_name="/update_session_info"
)
# Returns: "Session: abc12345 | User: Admin_J | Interactions: 3"
```

### 4. Toggle Settings
```python
client.predict(api_name="/toggle_settings")
```

### 5. Toggle Settings from Nav
```python
client.predict(api_name="/toggle_settings_from_nav")
```

### 6. Handle Mode Change
```python
status = client.predict(
    mode="relevant",
    session_id_text="Session: abc12345 | User: Admin_J | Interactions: 3",
    api_name="/handle_mode_change"
)
# Returns: "*Current: Relevant Context*"
```

### 7. Save Preferences
```python
result = client.predict(
    param_0=True,      # show_reasoning
    param_1=False,     # show_agent_trace
    param_2="Fast",    # response_speed: 'Fast', 'Balanced', 'Thorough'
    param_3=True,      # cache_enabled
    api_name="/save_preferences"
)
# Returns: {"status": "success", "message": "Preferences saved"}
```

---

## Response Structures

### Chat Response Tuple
```python
(
    list[dict],  # chatbot_history - [{"role": "user/assistant", "content": "..."}]
    str,         # message_input - "" (cleared)
    dict,        # reasoning_data - Chain of thought
    dict,        # performance_data - Timing, tokens, agents
    dict,        # context_data - Session context
    str,         # session_info - "Session: ... | User: ... | Interactions: N"
    str          # skills_html - HTML string for skills display
)
```

### Reasoning Data Structure
```python
{
    "chain_of_thought": {
        "step_1": {
            "hypothesis": "...",
            "evidence": [...],
            "confidence": 0.85,
            "reasoning": "..."
        }
    },
    "confidence_calibration": {"overall_confidence": 0.85}
}
```

### Performance Data Structure
```python
{
    "agent_trace": [...],
    "processing_time": 2.34,
    "token_count": 1250,
    "confidence_score": 0.85,
    "agents_used": [...]
}
```

---

## Validation Rules

| Parameter | Validation |
|-----------|------------|
| `message` | Non-empty, max 10,000 chars |
| `user_id` | Base: `Admin_J` (default). Dynamic: Any `[a-zA-Z0-9_]{1,50}` - auto-created |
| `mode` | One of: `'fresh'`, `'relevant'` |
| `response_speed` | One of: `'Fast'`, `'Balanced'`, `'Thorough'` |
| `session_text` | Format: `"Session: <8-hex> | User: <user_id> | Interactions: <num>"` |

---

## Common Patterns

### Full Conversation Flow
```python
# 1. Create session (using base Admin_J user)
session = client.predict(user_id="Admin_J", api_name="/new_session")

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

# 3. Send messages
history = []
for msg in ["Q1", "Q2", "Q3"]:
    result = client.predict(
        message=msg, history=history,
        user_id="Admin_J", session_text=session,
        api_name="/safe_gpu_chat_handler"
    )
    history = result[0]
    session = result[5]  # Update session
    print(result[0][-1]["content"])  # Print response

# Example with dynamic user (auto-created on first use)
dynamic_user = "ExternalAPI_Client01"
session = client.predict(user_id=dynamic_user, api_name="/new_session")
# User automatically created in database
```

### Error Handling
```python
try:
    result = client.predict(...)
    if isinstance(result[3], dict) and "error" in result[3]:
        print(f"Error: {result[3]['error']}")
except Exception as e:
    print(f"Request failed: {e}")
```

---

## Parameter Quick Reference

### `/safe_gpu_chat_handler`
- `message` (str, required)
- `history` (list, default: [])
- `user_id` (str, default: "Admin_J") - Base user or any valid format (auto-created)
- `session_text` (str, default: "Session: ... | User: ... | Interactions: 0")

### `/new_session`
- `user_id` (str, default: "Admin_J") - Base user or any valid format (auto-created)

### `/update_session_info`
- `user_id` (str, default: "Admin_J") - Base user or any valid format (auto-created)
- `session_text` (str, required)

### `/handle_mode_change`
- `mode` (str, required: "fresh" | "relevant")
- `session_id_text` (str, required)

### `/save_preferences`
- `param_0` (bool, default: True) - show_reasoning
- `param_1` (bool, default: False) - show_agent_trace
- `param_2` (str, default: "Balanced") - response_speed
- `param_3` (bool, default: True) - cache_enabled

---

## Status Codes / Return Values

### Success Indicators
- Chat: Valid tuple with 7 elements
- Preferences: `{"status": "success"}` or `{"status": "partial"}`
- Mode Change: `"*Current: Fresh Context*"` or `"*Current: Relevant Context*"`

### Error Indicators
- Chat: Error message in `performance_data` or `context_data`
- Mode Change: `"*Error: Invalid session information*"`
- Other: Error strings or dictionaries with `"error"` key

---

**See `API_DOCUMENTATION.md` for complete documentation.**