Spaces:
Sleeping
Sleeping
File size: 8,918 Bytes
439ebb4 4b28fb0 439ebb4 4b28fb0 439ebb4 4b28fb0 439ebb4 4b28fb0 439ebb4 4b28fb0 439ebb4 4b28fb0 439ebb4 4b28fb0 439ebb4 4b28fb0 439ebb4 4b28fb0 439ebb4 4b28fb0 439ebb4 4b28fb0 439ebb4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | # Anonymous Chat API Examples
This document provides sample API calls for using the Atlas Chat API in anonymous mode (without user authentication).
## API Endpoint
**Base URL:** `http://localhost:8000` (or your deployed URL)
**Endpoint:** `POST /chat`
**Content-Type:** `application/json`
## Request Structure
```json
{
"prompt": "Your question or message here",
"max_new_tokens": 500,
"use_search": true,
"temperature": 0.7,
"user_id": null,
"force_search": null,
"search_decision_mode": "balanced"
}
```
### Parameters
- **`prompt`** (required): Your question or message to the AI
- **`max_new_tokens`** (optional): Maximum tokens in response (default: 500)
- **`use_search`** (optional): Whether to use web search (default: true)
- **`temperature`** (optional): Response creativity (0.0-1.0, default: 0.7)
- **`user_id`** (optional): User identifier (null/omitted for anonymous)
- **`force_search`** (optional): Override smart search optimization (true/false/null)
- **`search_decision_mode`** (optional): Search sensitivity ("conservative"/"balanced"/"aggressive")
- **`history`** (optional): Conversation history for context-aware responses
## Anonymous Request Examples
### 1. Basic Anonymous Request (No user_id field)
```bash
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{
"prompt": "What is the capital of France?",
"use_search": false
}'
```
### 2. Anonymous Request with Explicit null user_id
```bash
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{
"prompt": "Explain quantum computing in simple terms",
"user_id": null,
"use_search": true,
"temperature": 0.5
}'
```
### 3. Anonymous Request with Search Optimization
```bash
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{
"prompt": "What are the latest developments in AI?",
"user_id": null,
"search_decision_mode": "aggressive",
"max_new_tokens": 300
}'
```
### 4. Anonymous Request with Forced Search
```bash
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{
"prompt": "What is 2+2?",
"force_search": true,
"max_new_tokens": 200
}'
```
### 5. Anonymous Request with Conversation History
```bash
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{
"prompt": "Can you elaborate on neural networks?",
"user_id": null,
"search_decision_mode": "conservative",
"history": [
{"role": "user", "content": "What is machine learning?"},
{"role": "assistant", "content": "Machine learning is a subset of AI that enables computers to learn from data..."}
]
}'
```
## JavaScript/Fetch Examples
### Basic Anonymous Request
```javascript
const response = await fetch('http://localhost:8000/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: "How does machine learning work?",
use_search: true,
temperature: 0.6,
search_decision_mode: "balanced"
})
});
const data = await response.json();
console.log(data.response);
console.log('Search decision:', data.search_decision);
console.log('Cache info:', data.cache_info);
```
### Advanced JavaScript Example with Optimization
```javascript
// Smart chat client with optimization features
async function smartChat(prompt, conversationHistory = []) {
const requestBody = {
prompt: prompt,
use_search: true,
temperature: 0.7,
history: conversationHistory
};
// Use aggressive mode for news/current events
if (prompt.includes('latest') || prompt.includes('current') || prompt.includes('today')) {
requestBody.search_decision_mode = 'aggressive';
}
// Use conservative mode for follow-up questions
else if (conversationHistory.length > 0 && (
prompt.includes('elaborate') ||
prompt.includes('more about') ||
prompt.includes('explain')
)) {
requestBody.search_decision_mode = 'conservative';
}
const response = await fetch('http://localhost:8000/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestBody)
});
const data = await response.json();
// Log optimization details
console.log(`Search performed: ${data.search_decision?.should_search}`);
console.log(`Reason: ${data.search_decision?.reason}`);
console.log(`Cache hit: ${data.cache_info?.cache_hit}`);
return data;
}
// Usage examples
await smartChat("What is artificial intelligence?");
await smartChat("Tell me more about that", previousHistory);
await smartChat("What's the latest AI news?");
```
## Python Examples
### Using requests library
```python
import requests
# Basic anonymous request
url = "http://localhost:8000/chat"
payload = {
"prompt": "Explain the theory of relativity",
"use_search": False,
"temperature": 0.5
}
response = requests.post(url, json=payload)
data = response.json()
print(data['response'])
```
## Response Format
All requests return a JSON response with this structure:
```json
{
"response": "The AI's response to your prompt...",
"search_results": [
{
"title": "Search Result Title",
"body": "Search result description...",
"href": "https://example.com",
"source": "Brave"
}
],
"search_decision": {
"should_search": true,
"reason": "New information request detected",
"confidence": 0.9,
"decision_method": "rule_based"
},
"cache_info": {
"cache_hit": false,
"flow_type": "cache_first_miss",
"cache_type": "chromadb_vector"
}
}
```
### Response Fields Explained
- **`response`**: The AI's text response to your prompt
- **`search_results`**: Array of web search results used (if search was performed)
- **`search_decision`**: Details about the search optimization decision
- `should_search`: Whether search was determined necessary
- `reason`: Human-readable explanation for the decision
- `confidence`: Decision confidence score (0.0-1.0)
- `decision_method`: Method used ("rule_based", "hybrid", "fallback")
- **`cache_info`**: Information about caching and performance
- `cache_hit`: Whether results came from cache
- `flow_type`: Processing flow used (e.g., "cache_first_miss", "search_decision_skip")
- `cache_type`: Type of caching system (e.g., "chromadb_vector")
## Session Tracking
Anonymous requests automatically create sessions for analytics purposes:
- Each request gets a unique session ID (returned in `X-Session-ID` header)
- Sessions are tracked anonymously (no personal data stored)
- Analytics count anonymous vs authenticated usage
- No individual user tracking for anonymous requests
## Analytics Endpoints
You can also check anonymous usage analytics:
### Get Basic Stats
```bash
curl "http://localhost:8000/analytics/stats"
```
### View Dashboard
```bash
curl "http://localhost:8000/analytics/dashboard"
```
## Search Optimization Examples
### Conservative Mode (Minimize Searches)
```bash
# Good for cost optimization and follow-up heavy conversations
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{
"prompt": "Can you elaborate on that point?",
"search_decision_mode": "conservative",
"history": [
{"role": "user", "content": "What is renewable energy?"},
{"role": "assistant", "content": "Renewable energy comes from natural sources..."}
]
}'
```
### Aggressive Mode (Prioritize Fresh Information)
```bash
# Good for current events and news
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{
"prompt": "What happened in tech today?",
"search_decision_mode": "aggressive"
}'
```
### Force Search Override
```bash
# Force search even for simple questions
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{
"prompt": "What is 2+2?",
"force_search": true
}'
# Disable search completely
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-d '{
"prompt": "Tell me about this topic",
"force_search": false,
"history": [
{"role": "user", "content": "Explain machine learning"},
{"role": "assistant", "content": "Machine learning is..."}
]
}'
```
## Notes
- Anonymous requests have identical functionality to authenticated requests
- No user data is stored or tracked for anonymous requests
- **Smart search optimization** reduces unnecessary searches by 40-60%
- **ChromaDB caching** provides instant responses for similar queries
- **Context-aware processing** uses conversation history intelligently
- Response quality and speed are optimized through intelligent search decisions
- Sessions are created automatically for analytics but contain no personal information |