Spaces:
Sleeping
Sleeping
File size: 11,398 Bytes
ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 935d7f2 ceacbf1 | 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 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 | # 09 - API Reference
> Base URL in local dev: `http://127.0.0.1:7860`. The Vite dev server at
> `http://127.0.0.1:5173` proxies the same paths.
---
## Authentication
Public app endpoints:
```text
GET /health
POST /turn
POST /turn/stream
POST /feedback
GET /sessions/*
GET /users/*
DELETE /sessions/*
```
Protected operational endpoints:
```text
/config*
/admin/*
/analytics/*
```
Protected endpoints require one of:
```http
X-APE-Admin-Token: <APE_ADMIN_TOKEN>
Authorization: Bearer <APE_ADMIN_TOKEN>
```
If `APE_ADMIN_TOKEN` is not configured on the server, protected endpoints return
`503`. If the header is missing or wrong, they return `401`.
---
## Core Flow
### `GET /health`
```json
{ "status": "ok" }
```
### `POST /turn`
Generate one assistant response using the non-streaming path.
Request:
```json
{
"user_id": "alex_retiree",
"session_id": "optional-session-id",
"query": "Compare Roth IRA vs Traditional IRA",
"generate_response": true
}
```
Response:
```json
{
"response_id": "resp_...",
"session_id": "sess_...",
"assistant_message_id": "msg_...",
"classification": {
"intent": "Comparison",
"topic": "roth_vs_traditional_ira",
"signal": "no_signal"
},
"selection": {
"selected_strategy": "comparison_table",
"strategies_available": [
"standard_llm",
"comparison_table",
"pros_cons_table",
"bullet_contrast"
],
"ucb_at_selection": 1.522,
"policy_version": "v1"
},
"answer": "| Feature | Roth IRA | Traditional IRA | ...",
"rendered_format": "comparison_table",
"timings_ms": {
"total": 2100.4
}
}
```
### `POST /turn/stream`
Same request body as `/turn`, but returns Server-Sent Events with
`Content-Type: text/event-stream`.
Event payloads are JSON objects sent as SSE `data:` lines.
`metadata` event:
```json
{
"event": "metadata",
"response_id": "resp_...",
"session_id": "sess_...",
"intent": "Comparison",
"topic": "roth_vs_traditional_ira",
"selected_strategy": "comparison_table",
"candidate_strategies": ["standard_llm", "comparison_table"],
"select_timings_ms": {}
}
```
`delta` event:
```json
{ "event": "delta", "text": "partial raw model text" }
```
`done` event:
```json
{
"event": "done",
"response_id": "resp_...",
"session_id": "sess_...",
"assistant_message_id": "msg_...",
"answer": "final parsed answer",
"rendered_format": "comparison_table",
"format_compliance": 1,
"selection": {},
"classification": {},
"timings_ms": {}
}
```
`error` event:
```json
{ "event": "error", "message": "..." }
```
Streaming and non-streaming paths both write `ape_messages`, `ape_turn_record`,
and pending format compliance signals.
### `POST /feedback`
Append a UI signal to one response. The response may finalize immediately or
queue the signal for later finalization.
Request:
```json
{
"response_id": "resp_...",
"user_id": "alex_retiree",
"signal": "copy_save"
}
```
Possible response when queued:
```json
{
"status": "queued",
"response_id": "resp_..."
}
```
Possible response when applied:
```json
{
"status": "applied",
"response_id": "resp_...",
"signal": "pattern_engaged_positive",
"reward_category": "weak_positive",
"normalized_reward": 0.5,
"strategy_row_after": {
"strategy": "comparison_table",
"count": 4,
"avg_reward": 0.625,
"cached_ucb": 1.457
}
}
```
Possible response with no bandit update:
```json
{
"status": "applied_no_bandit_update",
"response_id": "resp_...",
"signal": "thumbs_up",
"reward_category": null,
"normalized_reward": null
}
```
Possible rejected response:
```json
{
"status": "rejected",
"response_id": "resp_...",
"reason": "already_finalized",
"current_status": "APPLIED"
}
```
Feedback statuses:
| Status | Meaning |
|---|---|
| `queued` | Signal buffered; turn remains PENDING |
| `applied` | Turn finalized and bandit updated |
| `applied_no_bandit_update` | Turn finalized but no format-relevant reward existed |
| `rejected` | Missing response, wrong user, already finalized, or race |
| `skipped` | Unknown signal |
---
## Sessions and History
### `GET /sessions/{session_id}/messages?user_id=X&limit=200`
Returns raw chat messages for one session, scoped to the requesting user.
`user_id` is required.
### `GET /sessions/{session_id}/turns?user_id=X&limit=100`
Returns `ape_turn_record` rows for one session, scoped to the requesting user.
`user_id` is required.
### `GET /users/{user_id}/sessions?limit=20`
Returns recent session summaries for a user.
### `GET /users/{user_id}/latest-session`
Returns:
```json
{ "session_id": "sess_..." }
```
or:
```json
{ "session_id": null }
```
### `GET /users/{user_id}/responses?limit=50`
Returns recent turn records for one user.
### `DELETE /sessions/{session_id}?user_id=X`
Deletes one user's messages and response records for that session. Bandit state
is preserved.
---
## Config Reads
All config endpoints require admin token.
| Endpoint | Purpose |
|---|---|
| `GET /config/intents` | List intents |
| `GET /config/strategies` | List strategies |
| `GET /config/policies` | List policies |
| `GET /config/signal-rules` | List signal routing rules |
| `GET /config/reward-scale` | List reward categories |
| `GET /config/instructions?strategy_id=X&status=Y` | List instruction versions |
| `GET /config/offers?status=Y` | List outreach policies |
---
## Config Writes
### `POST /config/intents`
```json
{
"intent_id": "Comparison",
"description": "User wants two or more options compared side by side.",
"changed_by": "admin_user"
}
```
### `POST /config/strategies`
```json
{
"strategy_id": "comparison_table",
"format_type": "comparison_table",
"changed_by": "admin_user"
}
```
### `POST /config/signal-rules`
```json
{
"signal_name": "format_change_request",
"format_relevant": true,
"content_relevant": false,
"format_category": "strong_negative",
"content_category": null,
"source": "llm",
"feature_id": 1,
"expected_frequency": "rare",
"evidence_quality": "high",
"consumers": ["bandit", "instruction_quality"],
"changed_by": "admin_user"
}
```
### `POST /config/reward-scale`
```json
{
"category": "strong_positive",
"normalized_reward": 1.0,
"changed_by": "admin_user"
}
```
`raw_reward` is not part of the current request model.
### `POST /config/policies`
```json
{
"domain": "finance",
"intent": "Comparison",
"topic": "_default",
"strategy_id": "comparison_table",
"policy_version": "v1",
"exploration_constant": 1.0,
"changed_by": "admin_user"
}
```
### `POST /config/instructions`
```json
{
"strategy_id": "comparison_table",
"version": "v2",
"instruction_text": "Format as a markdown table comparing the options.",
"instruction_uri": null,
"activate": true,
"changed_by": "admin_user"
}
```
### `POST /config/instructions/activate?strategy_id=X&version=Y&changed_by=admin_user`
Activates an existing instruction version.
### `POST /config/status`
```json
{
"entity_type": "strategy",
"entity_id": "comparison_table",
"version": "v1",
"status": "INACTIVE",
"changed_by": "admin_user"
}
```
`version` is required for instruction rows and optional for most other entity
types.
### `POST /config/offers`
```json
{
"topic": "retirement_accounts",
"offer_type": "retirement_planning_consultation",
"description": "Schedule a 30-minute planning call",
"min_interest_score": 0.8,
"weight_frequency": 0.4,
"weight_recency": 0.25,
"weight_engagement": 0.25,
"weight_followup": 0.1,
"changed_by": "admin_user"
}
```
---
## Config Deletes
| Endpoint | Purpose |
|---|---|
| `DELETE /config/intents/{intent_id}` | Delete an intent |
| `DELETE /config/strategies/{strategy_id}` | Delete a strategy and its instructions |
| `DELETE /config/signal-rules/{signal_name}` | Delete a signal rule |
| `DELETE /config/reward-scale/{category}` | Delete a reward value |
| `DELETE /config/policies?intent=Y&topic=Z&strategy_id=S` | Delete a policy |
| `DELETE /config/instructions/{strategy_id}/{version}` | Delete an instruction version |
| `DELETE /config/offers/{topic}` | Delete an outreach policy |
All deletes are audited.
---
## Admin and Ops
All admin endpoints require admin token.
| Endpoint | Purpose |
|---|---|
| `DELETE /admin/clear-user/{user_id}` | Remove one user's runtime data |
| `DELETE /admin/clear-all` | Clear runtime collections while preserving config/audit |
| `POST /admin/seed` | Seed default config |
| `GET /admin/db-snapshot?user_id=X&limit=N` | Diagnostic snapshot |
| `GET /admin/bandit-state?user_id=X&only_pulled=true` | Inspect bandit rows |
| `DELETE /admin/bandit-state/cell?user_id=X&domain=D&intent=I&topic=T&strategy=S` | Delete one bandit cell or one arm when `strategy` is supplied |
| `GET /admin/audit?date=YYYY-MM-DD&limit=N` | Audit log |
There is no `/admin/rebuild-bandit` endpoint. UCB cache refresh happens during
feedback finalization, and bulk repair should be done with a script if needed.
---
## Analytics
All analytics endpoints require admin token.
| Endpoint | Purpose |
|---|---|
| `POST /analytics/recompute?days=N` | Rebuild derived collections from `ape_turn_record` |
| `GET /analytics/user-interests?user_id=X&limit=K&refresh=bool` | User topic interest |
| `GET /analytics/topic-users?topic=X&limit=K&min_score=Y` | Users interested in topic |
| `GET /analytics/trends?days=N&limit=K&refresh=bool` | Trending topics |
| `GET /analytics/topic-timeseries?topic=X&days=N` | Daily trend for one topic |
| `GET /analytics/platform-timeseries?days=N` | Daily platform activity |
| `GET /analytics/topics-timeseries?days=N&top_n=K` | Daily series for top topics |
| `GET /analytics/user-timeseries?user_id=X&days=N` | Daily user activity |
| `GET /analytics/offers/{user_id}?domain=Y` | Outreach recommendations |
| `GET /analytics/customer-health?days=N&cohort_weeks=K` | Retention/satisfaction health |
| `GET /analytics/rag-quality?days=N&min_turns=K&sample_limit=S` | Content-quality indicators |
| `GET /analytics/instruction-quality?days=N&min_turns=K&sample_limit=S` | Format/instruction quality |
| `GET /analytics/strategy-performance?user_id=X&min_pulls=N` | Strategy performance tiers |
| `GET /analytics/platform-overview?days=N&top_n=K` | Dashboard overview |
| `GET /analytics/active-users?days=N&min_interest=X&limit=K` | Outreach roster |
| `GET /analytics/user-profile?user_id=X&domain=Y` | 12-facet user profile |
| `GET /analytics/cognitive-facets?user_id=X&min_interactions=N` | Facets; omit user_id for global |
---
## Status Codes
| Code | Meaning |
|---|---|
| `200` | Success |
| `204` | Successful delete with no body |
| `400` | Invalid request value |
| `401` | Protected endpoint missing valid admin token |
| `404` | Resource not found |
| `422` | FastAPI validation error, such as missing required query param |
| `500` | Store/orchestrator not initialized or unexpected server error |
| `503` | Protected endpoint called while `APE_ADMIN_TOKEN` is not configured |
Feedback errors are usually returned as `200` with `status: "rejected"` or
`status: "skipped"` so the UI can show clean inline state without treating user
feedback races as transport failures.
---
## See Also
- [02 - Runtime paths](./02-runtime-paths.md)
- [03 - Admin config](./03-admin-config.md)
- [04 - Analytics layer](./04-analytics-layer.md)
|