saivivek6 commited on
Commit
d81cbdf
Β·
1 Parent(s): 7bd97ee

Deploy: docs refresh (ARCHITECTURE.md + READMEs); components-only architecture

Browse files
Files changed (4) hide show
  1. README.md +7 -1
  2. vivek/ARCHITECTURE.md +153 -0
  3. vivek/README.md +33 -19
  4. vivek/UpdatedReadme.md +6 -0
README.md CHANGED
@@ -9,6 +9,12 @@ pinned: false
9
 
10
  # AdaptiveUI (vivek)
11
 
12
- Claude-style single-call response + widget HTML rendered in an iframe.
 
 
 
 
 
 
13
 
14
  This Space runs the `vivek` server on port 7860.
 
9
 
10
  # AdaptiveUI (vivek)
11
 
12
+ Claude-style single-call response **+ an interactive widget** for each answer. Widgets are
13
+ built **only from the app's own UI components** (a registry), expressed as JSON and rendered
14
+ by Vue components / ECharts β€” **no LLM-generated HTML, no iframe, no external chart libraries**.
15
+ A Thompson-Sampling bandit adapts the prose-answer style; the widget is independent of it.
16
+
17
+ See `vivek/ARCHITECTURE.md` for the full pipeline (registry β†’ prompt menu β†’ synthesizer β†’
18
+ validate β†’ components).
19
 
20
  This Space runs the `vivek` server on port 7860.
vivek/ARCHITECTURE.md ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adaptive Presentation Engine β€” Architecture
2
+
3
+ A chat app where an LLM (Anthropic Claude) answers a question **and** generates an
4
+ interactive widget for it. The widget is built **only from the app's own UI components**
5
+ (a registry), expressed as **JSON** β€” there is **no LLM-generated HTML, no iframe, and no
6
+ external chart libraries shipped by the model**. Charts render with bundled **ECharts**.
7
+
8
+ There are two independent parts in every turn:
9
+
10
+ - **Part A β€” Adaptive text strategy** (the bandit): picks *how the prose answer is written*.
11
+ - **Part B β€” Visualization** (this doc's focus): decides *whether/what to visualize* and
12
+ generates the widget from the component registry.
13
+
14
+ The two are decoupled: the bandit strategy only shapes the `<RESPONSE>` text; it never
15
+ controls the `<WIDGET>`.
16
+
17
+ ---
18
+
19
+ ## The single source of truth: `frontend-vue/src/widget-registry.json`
20
+
21
+ One JSON catalog of everything the app can render. It is read in **three** places so they
22
+ can never drift:
23
+
24
+ | Use | Reader | Purpose |
25
+ |-----|--------|---------|
26
+ | **GENERATE** | `backend/combined_prompt.py β†’ build_json_widget_rule()` | turns the registry into the prompt "menu" sent to the LLM |
27
+ | **VALIDATE** | `rag_finance/registry.py β†’ allowed_types() / allowed_chart_kinds()` | drops anything off-menu in the LLM's output |
28
+ | **RENDER** | `frontend-vue/src/lib/widgetRegistry.ts (COMPONENTS map)` + `lib/echartsOption.ts` | maps each block `type`/chart `kind` to a Vue component / ECharts option |
29
+
30
+ ### Block types (9)
31
+ `text, kpi_row, chart, table, action_row, image, stat_card, progress, badge_row`
32
+
33
+ ### Chart kinds (33) and their data shapes
34
+ ```
35
+ bar | hbar | line | area | stacked | combo | histogram | timeseries
36
+ β†’ x_categories + series[].values (also accepts items[] as one series)
37
+ pie | donut | rose | funnel | treemap | sunburst
38
+ β†’ items[{label,value}]
39
+ scatter | bubble β†’ series[].values = [x,y] (bubble: [x,y,size])
40
+ boxplot β†’ boxes[[min,q1,med,q3,max]]
41
+ candlestick β†’ candles[[open,close,low,high]]
42
+ waterfall β†’ items[{label,value=delta}]
43
+ heatmap β†’ x_labels + y_labels + matrix
44
+ sankey | graph β†’ nodes[] + links[{source,target,value}]
45
+ tree | mindmap | org β†’ root{name,children[]}
46
+ scatter3d | bar3d | line3d β†’ series[].values = [x,y,z] (echarts-gl)
47
+ polar | parallel | themeriver | gauge | radar
48
+ ```
49
+ (Removed deliberately: `calendar`, `surface` β€” they need hundreds of hand-typed values,
50
+ which an LLM with no data backend can't author reliably.)
51
+
52
+ ---
53
+
54
+ ## Part B β€” Visualization flow (step by step)
55
+
56
+ **Example query:** `Show NVIDIA's FY2024 revenue by segment as a bar chart`
57
+
58
+ ### 1. Assemble the prompt (`server.py β†’ _build_adaptive_prompt β†’ build_combined_system_prompt`)
59
+ The system message includes the registry-derived **WIDGET RULES** menu plus the guardrails:
60
+ - **OUTPUT CONTRACT** β€” return exactly `<RESPONSE>…</RESPONSE><WIDGET>{json}</WIDGET>`.
61
+ - **RENDER NOW** β€” if a visual is warranted and buildable, output the widget *this* turn;
62
+ never defer (β€œI will plot…”), ask permission, or ask β€œwhich dataset?” when implied.
63
+ - **WIDGET WARRANT** β€” include a widget only if a visual helps **and** you can fill it with
64
+ **real** values; else empty + say so.
65
+ - **NO FABRICATED DATA** β€” never invent/estimate/β€œmock”/β€œillustrative” data; if you don't
66
+ have it, say so and render nothing.
67
+ - **STRICT grounding** β€” every number/label in the widget must equal what's in `<RESPONSE>`.
68
+ - **Decline** β€” if a requested chart kind isn't supported, say so and offer supported ones.
69
+ - **action_row = visuals-only** β€” buttons only redraw the *current* data as another kind.
70
+
71
+ The user message is just history + the question (no data backend, no tools).
72
+
73
+ ### 2. Synthesizer decides + generates (one LLM call)
74
+ After writing the `<RESPONSE>` text, the model:
75
+ 1. **WARRANT** β€” is a visual helpful, do I have real data, is there a supported kind?
76
+ - no data β†’ say so, empty widget Β· unsupported kind β†’ decline Β· not worth it β†’ text only.
77
+ 2. **SELECT KIND** β€” match the data shape to a kind in the menu (here: categorical
78
+ comparison β†’ `bar`; an explicit request wins if supported).
79
+ 3. **FILL THE SHAPE** β€” populate that kind's keys with the **same** numbers used in `<RESPONSE>`.
80
+ 4. **EMIT:**
81
+ ```json
82
+ <RESPONSE>
83
+ In FY2024, NVIDIA's revenue was led by Data Center at $47.5B, Gaming $10.4B,
84
+ Professional Visualization $1.6B, and Automotive $1.1B.
85
+ </RESPONSE>
86
+ <WIDGET>
87
+ {"version":"1.0","layout":[
88
+ {"type":"text","content":"FY2024 revenue by segment (USD B)."},
89
+ {"type":"chart","title":"NVIDIA FY2024 Revenue by Segment",
90
+ "chart":{"kind":"bar","x_label":"Segment","y_label":"USD B",
91
+ "x_categories":["Data Center","Gaming","Professional Visualization","Automotive"],
92
+ "series":[{"name":"FY2024","values":[47.5,10.4,1.6,1.1]}]}},
93
+ {"type":"action_row","buttons":[
94
+ {"label":"Show as treemap"},{"label":"View as horizontal bars"}]}
95
+ ]}
96
+ </WIDGET>
97
+ ```
98
+
99
+ ### 3. Parse + validate against the registry (`combined_prompt.py`, deterministic)
100
+ `parse_combined_output` splits RESPONSE/WIDGET β†’ `_dispatch_json_mode_widget`:
101
+ - normalize type aliases (`_TYPE_ALIASES`), keep only `_BLOCK_TYPES`
102
+ - per block `_block_is_renderable` β†’ charts checked by `_chart_has_data`
103
+ (kind in registry **and** the data-bearing key present)
104
+ - off-menu / no-data blocks are **dropped** (no empty cards)
105
+ - returns `widget_schema` (JSON) + `widget_html=""` (the HTML path does not exist)
106
+
107
+ ### 4. Stream to the UI (SSE)
108
+ `strategy β†’ response_delta β†’ widget_delta β†’ done`. While streaming, the response text is
109
+ run through `cleanAssistantText`/`stripLeakedJson` (no raw JSON/ASCII ever shows), and the
110
+ partial widget renders **block-by-block** via the salvage parser (the chart appears the
111
+ moment its JSON block closes).
112
+
113
+ ### 5. Render with your components (`WidgetRegistryRenderer.vue`)
114
+ `parseLayout` (full parse β†’ salvage β†’ drop empty charts) β†’ for each block:
115
+ ```
116
+ resolveWidget("text") β†’ TextBlock
117
+ resolveWidget("chart") β†’ ChartBlock β†’ WidgetSchemaChart β†’ buildEChartsOption() β†’ ECharts <div>
118
+ resolveWidget("action_row") β†’ ActionRow (clickable β†’ re-prompts the same data)
119
+ ```
120
+ `<component :is>` mounts real Vue components inline. **No iframe, no HTML, no CDN.**
121
+
122
+ ### One-line summary
123
+ ```
124
+ widget-registry.json
125
+ β†’ build_json_widget_rule() β†’ "menu" in the prompt
126
+ β†’ synthesizer: WARRANT? β†’ pick KIND β†’ fill SHAPE with real values β†’ emit <WIDGET>{json}
127
+ β†’ validate against registry (drop off-menu/no-data; html="")
128
+ β†’ SSE stream β†’ WidgetRegistryRenderer β†’ resolveWidget(type) β†’ <component :is> β†’ components / ECharts
129
+ ```
130
+
131
+ ---
132
+
133
+ ## Guarantees (by design)
134
+ - **Components only** β€” everything renders via `<component :is>`; no iframe/HTML/CDN libs.
135
+ - **Registry is the single source** β€” generate, validate, render can't drift.
136
+ - **No raw JSON, no ASCII art, no mock data, no empty cards** ever reach the UI.
137
+ - **Decline honestly** β€” unsupported chart type or missing data β†’ say so, render nothing.
138
+ - **The model can only use plots it can actually draw** (the menu), and only with real data.
139
+
140
+ ---
141
+
142
+ ## Extras
143
+ - **HTML export** (`lib/exportWidgetHtml.ts`): deterministic `JSON β†’ self-contained
144
+ interactive HTML` using the **same** ECharts option builder (no LLM). Charts stay
145
+ interactive offline (ECharts via CDN; `echarts-gl` added only when a 3D chart is present).
146
+ - **Live block-by-block rendering** while streaming.
147
+ - **Clickable action buttons** that re-prompt (visuals-only).
148
+
149
+ ## Part A (for context)
150
+ A per-user Thompson-Sampling bandit (`backend/engine.py`) selects a text strategy from
151
+ `strategies.json` using a context feature vector (`x ∈ ℝ¹⁰`). It learns from explicit
152
+ (πŸ‘/πŸ‘Ž β†’ `/api/rate`) and implicit (next-message sentiment) reward. It shapes only the
153
+ `<RESPONSE>` text β€” never the widget.
vivek/README.md CHANGED
@@ -4,33 +4,47 @@ app_port: 7860
4
  ---
5
  # Adaptive Presentation Engine β€” Demo
6
 
7
- A web demo of the Contextual Hierarchical Bayesian Architecture pipeline.
 
 
 
8
 
9
- ## Project structure (modular layout)
 
 
 
10
 
11
  ```
12
  vivek/
13
- β”œβ”€β”€ frontend/ # UI assets
14
- β”‚ β”œβ”€β”€ index.html # Single-page app (chat, widgets, posterior viz)
15
- β”‚ └── README.md # Frontend documentation
16
- β”œβ”€β”€ backend/ # Python API and logic
17
- β”‚ β”œβ”€β”€ config.py # Env vars, LLM modes, Thompson Sampling params
18
- β”‚ β”œβ”€β”€ server.py # HTTP server, API routes, serves frontend at GET /
19
- β”‚ β”œβ”€β”€ llm.py # Anthropic and OpenAI-compatible LLM calls
20
- β”‚ β”œβ”€β”€ engine.py # Bayesian engine (Thompson Sampling)
21
- β”‚ β”œβ”€β”€ widget_prompt.py
22
- β”‚ β”œβ”€β”€ combined_prompt.py
23
- β”‚ └── primitives.json # Strategy definitions
24
- β”œβ”€β”€ app.py # Entry point
 
 
 
 
 
 
 
25
  β”œβ”€β”€ requirements.txt
26
- β”œβ”€β”€ .env.example # Template for API keys (copy to .env)
27
- └── .env # Your keys (gitignored)
28
  ```
29
 
30
  ## What it shows
31
- - **Live strategy selection** via Thompson Sampling over the Bayesian posterior
 
32
  - **Posterior updating in real-time** as you rate responses (πŸ‘ / πŸ‘Ž)
33
- - **Feature vector** used for each inference
34
  - **Per-strategy expected reward** estimates that evolve with each interaction
35
 
36
  ---
@@ -79,7 +93,7 @@ http://localhost:5051 # or PORT from env (Docker uses 7860)
79
  ## Pipeline stages shown
80
  | Stage | What the demo shows |
81
  |---|---|
82
- | Feature extraction | Feature vector panel (x ∈ ℝ⁸) |
83
  | Thompson Sampling | Expected reward % per strategy |
84
  | LLM rendering | Live response with strategy label |
85
  | Reward observation | πŸ‘/πŸ‘Ž buttons |
 
4
  ---
5
  # Adaptive Presentation Engine β€” Demo
6
 
7
+ A chat app where Claude answers a question **and** generates an interactive widget for it.
8
+ Widgets are built **only from the app's own UI components** (a registry), as JSON β€” **no
9
+ LLM-generated HTML, no iframe, no external chart libraries**. Charts render with bundled
10
+ ECharts. A per-user Thompson-Sampling bandit adapts the prose-answer style independently.
11
 
12
+ > **Architecture:** see [`ARCHITECTURE.md`](./ARCHITECTURE.md) for the full registry β†’
13
+ > prompt-menu β†’ synthesizer β†’ validate β†’ components pipeline (Part B / visualization).
14
+
15
+ ## Project structure
16
 
17
  ```
18
  vivek/
19
+ β”œβ”€β”€ frontend-vue/ # Vue 3 + Vite + TypeScript SPA (the live UI)
20
+ β”‚ β”œβ”€β”€ src/
21
+ β”‚ β”‚ β”œβ”€β”€ widget-registry.json # SINGLE SOURCE: block types + 33 chart kinds + data shapes
22
+ β”‚ β”‚ β”œβ”€β”€ lib/widgetRegistry.ts # type β†’ Vue component map (RENDER)
23
+ β”‚ β”‚ β”œβ”€β”€ lib/echartsOption.ts # shared ECharts option builder (live render + HTML export)
24
+ β”‚ β”‚ β”œβ”€β”€ lib/exportWidgetHtml.ts # deterministic widget β†’ standalone interactive HTML
25
+ β”‚ β”‚ β”œβ”€β”€ components/WidgetRegistryRenderer.vue # parses widget JSON β†’ <component :is>
26
+ β”‚ β”‚ β”œβ”€β”€ components/WidgetSchemaChart.vue # ECharts chart component
27
+ β”‚ β”‚ └── components/widgets/*.vue # TextBlock, KpiRow, ChartBlock, ...
28
+ β”‚ └── dist/ # built SPA (served by the backend in prod)
29
+ β”œβ”€β”€ backend/
30
+ β”‚ β”œβ”€β”€ config.py # env, LLM modes, bandit params, strategy loading
31
+ β”‚ β”œβ”€β”€ server.py # FastAPI: auth, /api/chat[_stream], /api/rate, serves SPA
32
+ β”‚ β”œβ”€β”€ llm.py # Anthropic + OpenAI-compatible calls
33
+ β”‚ β”œβ”€β”€ engine.py # Thompson-Sampling bandit (text strategy)
34
+ β”‚ β”œβ”€β”€ combined_prompt.py # builds the combined prompt; registry β†’ menu; validation
35
+ β”‚ β”œβ”€β”€ db.py / auth.py / utils.py
36
+ β”œβ”€β”€ strategies.json # bandit text strategies (admin-manageable)
37
+ β”œβ”€β”€ app.py # entry point
38
  β”œβ”€β”€ requirements.txt
39
+ β”œβ”€β”€ .env.example # template (copy to .env; .env is gitignored)
40
+ └── ARCHITECTURE.md
41
  ```
42
 
43
  ## What it shows
44
+ - **Components-only widgets** β€” 9 block types + 33 chart kinds, all from the registry
45
+ - **Live strategy selection** via Thompson Sampling (text style only)
46
  - **Posterior updating in real-time** as you rate responses (πŸ‘ / πŸ‘Ž)
47
+ - **Feature vector** (`x ∈ ℝ¹⁰`) used for each inference
48
  - **Per-strategy expected reward** estimates that evolve with each interaction
49
 
50
  ---
 
93
  ## Pipeline stages shown
94
  | Stage | What the demo shows |
95
  |---|---|
96
+ | Feature extraction | Feature vector panel (x ∈ ℝ¹⁰) |
97
  | Thompson Sampling | Expected reward % per strategy |
98
  | LLM rendering | Live response with strategy label |
99
  | Reward observation | πŸ‘/πŸ‘Ž buttons |
vivek/UpdatedReadme.md CHANGED
@@ -1,5 +1,11 @@
1
  # Vivek: Adaptive Presentation Engine β€” Complete Reference
2
 
 
 
 
 
 
 
3
  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.
4
 
5
  ---
 
1
  # Vivek: Adaptive Presentation Engine β€” Complete Reference
2
 
3
+ > ⚠️ **Legacy/partial.** Parts of this long reference predate the components-only widget
4
+ > refactor (it still references HTML/iframe widgets, `index.html` SPA, etc.). For the
5
+ > **current** widget pipeline (registry β†’ prompt menu β†’ synthesizer β†’ validate β†’ Vue
6
+ > components / ECharts, no HTML/iframe), see [`ARCHITECTURE.md`](./ARCHITECTURE.md). The
7
+ > bandit/Part-A sections below remain broadly accurate.
8
+
9
  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.
10
 
11
  ---