File size: 18,660 Bytes
c97e8a9
0b491ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c97e8a9
 
0b491ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
# Vivek: Adaptive Presentation Engine β€” Complete Reference

A professional, modular backend demonstrating Bayesian strategy selection and real-time posterior updating. This app learns user preferences and adapts its response format (bullet points, prose, questions, etc.) based on observed rewards.

---

## Table of Contents
- [Project Structure](#project-structure)
- [File Manifest](#file-manifest)
- [System Architecture](#system-architecture)
- [How the App Works](#how-the-app-works)
- [Setup & Running](#setup--running)
- [Configuration](#configuration)
- [API Reference](#api-reference)

---

## Project Structure

```
backend/
β”œβ”€β”€ __init__.py           # Package entrypoint
β”œβ”€β”€ config.py             # Environment + constants
β”œβ”€β”€ utils.py              # Math, heuristics, post-processing
β”œβ”€β”€ llm.py                # LLM backends (Anthropic, OpenAI-compatible)
β”œβ”€β”€ engine.py             # Bayesian learner
└── server.py             # HTTP handlers + runner

Root folder
β”œβ”€β”€ app.py                # Launcher (imports backend.server.run_server)
β”œβ”€β”€ frontend/
β”‚   └── index.html        # Frontend demo UI
β”œβ”€β”€ Dockerfile            # Container image
β”œβ”€β”€ requirements.txt      # Python dependencies
β”œβ”€β”€ README.md             # Original readme (kept for reference)
└── UpdatedReadme.md      # This file
```

---

## File Manifest

### `backend/__init__.py`
**Purpose:** Package initialization and public API.

Exposes `run_server()` so callers only need:
```python
from backend import run_server
run_server()
```

---

### `backend/config.py`
**Purpose:** Centralized configuration and constants.

**Key variables:**
- `LLM_MODE` β€” selects backend: `"openai_compat"` or `"anthropic"`
- `OPENAI_BASE_URL`, `OPENAI_API_KEY`, `OPENAI_MODEL` β€” remote OpenAI-compatible API (Groq)
- `ANTHROPIC_API_KEY`, `ANTHROPIC_MODEL` β€” Anthropic Claude API configuration
- `D=10` β€” feature vector dimensionality
- `LAMBDA`, `GAMMA`, `ALPHA_G`, `TS_TEMPERATURE` β€” Bayesian hyperparameters
- `STRATEGIES` β€” dict of 5 response primitives (bulleted, narrative, concise, Socratic, step-by-step)
- `HERE`, `INDEX_HTML` β€” paths for serving the frontend

**Usage:** Other modules import from this single source of truth.

---

### `backend/utils.py`
**Purpose:** Small, reusable utilities.

**Functions:**
- `sigmoid(x)` β€” numerically stable logistic function
- `mean_uncertainty(sigma_inv)` β€” summarize posterior variance from precision matrix
- `fast_valence(message, prev_response)` β€” lightweight regex-based sentiment heuristic; returns `{"pos", "neg", "reason"}`
- `enforce_response(strategy, text)` β€” post-process LLM output to match the chosen format (strips questions, forces bullets/numbers, caps sentence counts)

**Helpers (module-level):**
- `_POS`, `_NEG`, `_REPHRASE` β€” regex patterns for auto-reward detection

**Usage:** Called by server during chat turns and posterior updates.

---

### `backend/llm.py`
**Purpose:** LLM API wrappers for Anthropic and OpenAI-compatible endpoints.

**Public functions:**
- `call_openai_compat(prompt, system, timeout=120)` β€” POST to OpenAI-compatible endpoint; returns `(text, elapsed_sec, mode)`
- `call_anthropic(prompt, system, timeout=120)` β€” call Anthropic Messages API; returns `(text, elapsed_sec, mode)`
- `openai_health(timeout=10)` β€” check OpenAI-compatible endpoint and available models
 - `anthropic_health()` β€” lightweight Anthropic config health summary

**Internal helpers:**
- `_post_json_url()`, `_get_json_url()` β€” raw HTTP wrappers

**Usage:** Server calls these during `/api/chat` to fetch responses from the LLM.

---

### `backend/engine.py`
**Purpose:** The core Bayesian learner for strategy selection and posterior updating.

**Class: `BayesianEngine`**
- `__init__()` β€” initialize global and per-user posterior means (`mu`) and precision matrices (`sigma_inv`)
- `get_user(uid)` β€” fetch or create user state
- `featurize(message, user)` β€” convert message + history into a fixed-length feature vector (10-dim)
- `select(uid, message)` β€” use Thompson Sampling to pick a strategy; return `(strategy, scores, x)`
- `update(uid, strategy, x, reward)` β€” Bayesian update for user and global posterior
- `apply_preferences(uid, strategy_names)` β€” apply soft bias or hard-lock if user selects one strategy
- `posterior_summary()`, `user_posterior()`, `global_posterior()` β€” compute compact summaries of expected reward + uncertainty

**Singletons:**
- `engine` β€” global instance used by the server
- `USERB_ID` β€” reserved user ID for a secondary reference posterior (demo artifact)

**Bayesian model:** Online logistic regression with exponential decay (Ξ³=0.99) and per-strategy Gaussian posteriors.

**Usage:** Server calls during `/api/chat`, `/api/reward`, `/api/preference` endpoints.

---

### `backend/server.py`
**Purpose:** HTTP server and request handlers.

**Class: `Handler(BaseHTTPRequestHandler)`**
Handles:
- `GET /` β€” serve `index.html` frontend
- `GET /api/health` β€” return LLM backend status
- `GET /api/state` β€” return user's current posterior and global stats
- `POST /api/chat` β€” accept user message, auto-reward previous turn, select strategy, call LLM, enforce format, persist state
- `POST /api/reward` β€” accept explicit user reward
- `POST /api/preference` β€” set user strategy preferences
- `POST /api/reset` β€” reset user state
- `OPTIONS *` β€” handle CORS preflight

**Private helpers:**
- `_json()` β€” JSON response with CORS headers
- `_html()` β€” serve frontend file
- `_body()` β€” parse JSON request body
- `_cors()` β€” set CORS headers
- `log_message()` β€” suppress access logs for cleanliness

**Function: `run_server()`**
- Prints startup banner
- Performs lightweight health checks on Anthropic/OpenAI endpoint
- Starts `ThreadedServer` on port 5051 (configurable via `PORT` env var)

**Class: `ThreadedServer(ThreadingMixIn, HTTPServer)`**
- Allows concurrent request handling

**Usage:** Imported and called by `app.py`.

---

### `app.py` (Root)
**Purpose:** Lightweight launcher script.

Simply imports and calls:
```python
from backend.server import run_server

if __name__ == "__main__":
    run_server()
```

This keeps a familiar entrypoint (`python app.py`) while the implementation lives in the package.

---

### `index.html`
**Purpose:** Frontend React/Preact demo.

Communicates with backend via:
- `GET /api/health` β€” check LLM status on load
- `GET /api/state` β€” fetch user posteriors
- `POST /api/chat` β€” send message, receive response + Bayesian state
- `POST /api/reward` β€” send user feedback
- `POST /api/preference` β€” set strategy lock
- `POST /api/reset` β€” reset session

Displays:
- Strategy label and instruction
- Expected reward scores per strategy
- Feature vector (x ∈ ℝ¹⁰)
- Posterior bar charts (mean + uncertainty)
- Auto-detected valence reason

---

### `Dockerfile`
**Purpose:** Container build for deployment.

Installs Python, dependencies, and runs `python app.py`.

---

### `requirements.txt`
**Purpose:** Python package dependencies.

Currently:
- `numpy` β€” linear algebra for Bayesian updates
- `python-dotenv` β€” load `.env` for API keys

---

## System Architecture

### High-Level Component Diagram

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         Frontend (index.html)                       β”‚
β”‚         Browser UI β†’ POST /api/chat, GET /api/state, etc.          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  app.py (Launcher)                                  β”‚
β”‚              from backend.server import run_server()                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚          HTTP Server + Handler (backend/server.py)                    β”‚
β”‚  β”œβ”€ GET /api/health     β†’ check LLM backend                        β”‚
β”‚  β”œβ”€ GET /api/state      β†’ fetch posteriors                         β”‚
β”‚  β”œβ”€ POST /api/chat      β†’ process user message                     β”‚
β”‚  β”œβ”€ POST /api/reward    β†’ apply explicit reward                    β”‚
β”‚  β”œβ”€ POST /api/preferenceβ†’ lock strategy                            β”‚
β”‚  └─ POST /api/reset     β†’ clear user data                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚         β”‚              β”‚
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚              └──────────────┐
         β”‚                         β”‚                             β”‚
         β–Ό                         β–Ό                             β–Ό
   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Bayesian     β”‚          β”‚ LLM Backendsβ”‚            β”‚ Utils (Heuristics
  β”‚ Engine       β”‚          β”‚ (backend/llm) β”‚            β”‚ & Post-process)
  β”‚ (backend/      β”‚          β”‚  β”Œβ”€ Anthropic β”‚          β”‚  β”œβ”€ sigmoid()
  β”‚  engine.py)  β”‚          β”‚  └─ OpenAI   β”‚            β”‚  β”œβ”€ fast_valence()
  β”‚              β”‚          β”‚             β”‚            β”‚  └─ enforce_response()
   β”‚ β€’ select()   │◄────────── β€’ call_*()  β”‚            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   β”‚ β€’ update()   β”‚          β”‚ β€’ health()  β”‚
   β”‚ β€’ featurize()β”‚          β”‚ β€’ format_*()β”‚
   β”‚ β€’ apply_prefβ”‚          β”‚             β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β–²
         β”‚ (reads hyperparams + strategy list)
         β”‚
         β–Ό
   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Config (backend/config.py)                 β”‚
  β”‚ β”œβ”€ LLM_MODE, OPENAI_*, ANTHROPIC_*      β”‚
   β”‚ β”œβ”€ D, LAMBDA, GAMMA, ALPHA_G, TS_TEMP   β”‚
   β”‚ β”œβ”€ STRATEGIES dict + STRATEGY_NAMES      β”‚
   β”‚ └─ HERE, INDEX_HTML paths               β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

---

## How the App Works

### 1. **Initialization**
- User loads `http://localhost:5051` β†’ frontend fetches `/api/health` and `/api/state`
- Server initializes or fetches user state (in-memory dict keyed by `uid`)
- User inherits global posterior (hierarchical Bayesian prior)

### 2. **User Sends a Message**
```
User message
    ↓
POST /api/chat {uid, message}
    ↓
[Server] fast_valence(message, prev_response)
    β”œβ”€ Auto-reward previous turn (if exists)
    └─ Call engine.update(uid, strategy, x, reward)
    ↓
[Server] engine.select(uid, message)
    β”œβ”€ featurize(message, user) β†’ x ∈ ℝ¹⁰
    β”œβ”€ Thompson Sampling per strategy
    └─ Return best strategy + scores
    ↓
[Server] Build system prompt with FORMAT RULE
    β”œβ”€ Include selected strategy instruction
    β”œβ”€ Add recent conversation history
    └─ Send to LLM
    ↓
[LLM] Generate response (single-call)
    └─ Return text + optional widget HTML
    ↓
[Server] enforce_response(strategy, text)
    β”œβ”€ Strip unwanted questions
    β”œβ”€ Force format (bullets, numbers, etc.)
    └─ Return polished response
    ↓
[Server] Persist conversation + state
    └─ Update user["history"], ["last_response"], ["last_x"]
    ↓
[Server] Return JSON response
    β”œβ”€ response text + strategy label
    β”œβ”€ instruction (what the system told the LLM)
    β”œβ”€ scores (expected reward per strategy)
    β”œβ”€ x_vec (feature vector used)
    β”œβ”€ posteriors (updated beliefs)
    β”œβ”€ auto_detected + auto_r (heuristic reward)
    └─ auto_reason (why the heuristic fired)
    ↓
[Frontend] Display response
    β”œβ”€ Show strategy + instruction
    β”œβ”€ Show bar charts for expected reward
    β”œβ”€ Show feature vector
    └─ Display πŸ‘/πŸ‘Ž buttons
```

### 3. **User Rates Response**
```
User clicks πŸ‘ or πŸ‘Ž
    ↓
POST /api/reward {uid, strategy, x_vec, reward}
    ↓
[Server] engine.update(uid, strategy, x, reward)
    β”œβ”€ Update user["mu"][strategy] via logistic regression
    β”œβ”€ Update user["sigma_inv"][strategy] (precision matrix)
    β”œβ”€ Also update global posterior (with weight Ξ±=0.05)
    └─ Append to user["reward_log"]
    ↓
[Server] Recompute posteriors
    └─ Return new bar chart data
    ↓
[Frontend] Animate bar chart updates
    └─ Show how beliefs changed
```

### 4. **Posterior Update (Bayesian Mechanics)**
The engine maintains per-user **Gaussian posteriors** Ξ² ~ N(ΞΌ, Ξ£) for each strategy.

**Model:** Logistic regression where reward rΜ‚ = sigmoid(x^T Ξ²)

**Update rule:**
- rΜ‚_old = sigmoid(x^T ΞΌ_old)
- Ξ£_new = Ξ£_old^{-1} + x x^T Β· w + Ξ» I  (w = rΜ‚(1 - rΜ‚))
- ΞΌ_new = ΞΌ_old + Ξ£_new^{-1} x (r - rΜ‚_old)

**Global posterior** gets a small, weighted update (Ξ±=0.05) so all users benefit from collective learning.

**Exponential decay** (Ξ³=0.99) biases the engine toward recent history.

---

## Setup & Running

### 1. Install Python 3.8+
```bash
python3 --version
```

### 2. Clone/download the repo
```bash
cd backend
```

### 3. Create a virtual environment (recommended)
```bash
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
```

### 4. Install dependencies
```bash
pip install -r requirements.txt
```

### 5. LLM backend
This repository supports OpenAI-compatible providers and Anthropic Claude.

### 6. Run the app
```bash
python app.py
```

You should see a startup banner and then the server will be live at `http://localhost:5051`.

### 7. Open browser
```
http://localhost:5051
```

---

## Configuration

### Environment Variables

Create a `.env` file in the root folder:

```env
# Backend selection
LLM_MODE=openai_compat  # or "anthropic"

# OpenAI-compatible (Groq, etc.)
OPENAI_BASE_URL=https://api.groq.com/openai/v1
OPENAI_API_KEY=your_groq_api_key_here
OPENAI_MODEL=llama-3.1-8b-instant

# Anthropic (Claude)
ANTHROPIC_API_KEY=your_anthropic_key_here
ANTHROPIC_MODEL=claude-opus-4-6

# Port
PORT=5051
```

### Bayesian Hyperparameters

Edit `backend/config.py`:

```python
D = 10              # Feature vector dimension
LAMBDA = 0.01       # L2 regularization on posteriors
GAMMA = 0.99        # Exponential decay (prefer recent history)
ALPHA_G = 0.05      # Global posterior update weight
TS_TEMPERATURE = 2.0  # Thompson Sampling variance scale (exploration)
```

---

## API Reference

### `GET /api/health`
**Returns:** LLM backend status.

**Response:**
```json
{
  "server": "ok",
  "mode": "openai_compat",
  "openai_base_url": "https://api.groq.com/openai/v1",
  "model": "llama-3.1-8b-instant",
  "ok": true,
  "reachable": true,
  "models": ["llama-3.1-8b-instant", "mixtral-8x7b-32768"]
}
```

---

### `GET /api/state?uid=demo`
**Returns:** Current user state and posteriors.

**Response:**
```json
{
  "posterior": {
    "structured_bullets": {"r": 0.52, "u": 0.34},
    "narrative_prose": {"r": 0.48, "u": 0.35},
    ...
  },
  "global": {...},
  "userb": {...},
  "global_n": 42,
  "n_users": 3,
  "msg_count": 5
}
```

---

### `POST /api/chat`
**Body:**
```json
{
  "uid": "demo",
  "message": "How do I make pasta?"
}
```

**Response:**
```json
{
  "response": "- Cook 1 liter of water\n- Add salt\n- ...",
  "strategy": "step_by_step",
  "instruction": "Numbered list of 3-6 steps only.",
  "elapsed": 2.3,
  "llm_mode": "anthropic",
  "scores": {
    "structured_bullets": 0.54,
    "narrative_prose": 0.48,
    ...
  },
  "x_vec": [0.12, 0.34, ...],
  "posterior": {...},
  "global": {...},
  "auto_detected": true,
  "auto_r": 0.75,
  "auto_reason": "positive signal(s)"
}
```

---

### `POST /api/reward`
**Body:**
```json
{
  "uid": "demo",
  "strategy": "step_by_step",
  "x_vec": [0.12, 0.34, ...],
  "reward": 0.9
}
```

**Response:**
```json
{
  "posterior": {...},
  "global": {...},
  "global_n": 43
}
```

---

### `POST /api/preference`
**Body:**
```json
{
  "uid": "demo",
  "strategies": ["structured_bullets"]
}
```

**Response:**
```json
{
  "posterior": {...}
}
```

---

### `POST /api/reset`
**Body:**
```json
{
  "uid": "demo"
}
```

**Response:**
```json
{
  "ok": true
}
```

---

## Troubleshooting

### Server won't start
- Ensure port 5051 is available: `lsof -i :5051`
- Check Python version: `python --version` (need 3.8+)
- Verify dependencies: `pip install -r requirements.txt`

### LLM returns empty responses
- If using OpenAI-compatible: verify API key in `.env`
- If using Anthropic: verify `ANTHROPIC_API_KEY` and `ANTHROPIC_MODEL`
- Check model name matches `OPENAI_MODEL` / `ANTHROPIC_MODEL`

### Feature vector or posterior looks weird
- This is expected! Bayesian posteriors start with high uncertainty.
- Send a few more messages and rate them β€” posteriors will stabilize.

### Valence heuristic seems wrong
- The regex patterns in `utils.py` are intentionally simple.
- For production, replace with a small classifier model.

---

## License & Attribution

Built as a demonstration of hierarchical Bayesian architecture. Feel free to adapt for your use case.

For questions or contributions, reach out to the team.