Generative AI¶
Language models are good at writing HTML. BI Components turn that into dashboards: the model writes a short page made of a dozen tags, and your application displays it in a sandbox, on real data, with the rights of the person looking at it.
The playground is a chat with recorded conversations. For each answer you can see the dashboard, the HTML behind it and the system prompt. It is not connected to a model, but everything after the model is real: the sandbox, the validation, the queries.
Why this format suits a model¶
Letting a model write what your users will run is first a security question. That is where most of the work went:
- What it writes is contained. Generated HTML and JavaScript run in a sandboxed frame with no network access. They cannot read your page, its cookies or its tokens, and cannot send data anywhere.
- Every request is checked. The frame can only ask your application for data. Your application validates each request, and refuses anything you did not allow.
- The viewer's rights apply, whoever wrote the page. Row-level security is added by your application for the person looking at the dashboard. A dashboard shared by someone else never shows more than its viewer may see.
- Mistakes are visible, not dangerous. A refused request shows its reason in the component, which is also what you feed back to the model.
The format itself helps too:
- Small and declarative.
<bar-chart measure="revenue" by="country">is the whole chart. There is no code to get wrong, no library version, no build. - Custom code is possible, and contained. When no component fits, the model can write a script with ECharts and the same data access as the components. It runs in the same sandbox.
- Iteration is cheap. "Compare with last year" changes one attribute. The whole page is small enough to be rewritten at each turn.
A semantic layer, or direct queries¶
Both work, with the same components and the same sandbox. You choose what the model is allowed to ask for:
| The model writes | Your application checks | Choose it when | |
|---|---|---|---|
| Semantic layer | Names of your dictionary: measure="revenue" by="country" |
That each name exists; it then computes the measure its own way | You have governed definitions (a semantic layer, dbt metrics, SQL views) and want one definition of each figure |
| Direct queries | SQL expressions on tables you expose: table="orders" measure="sum(revenue)" by="country" |
That each expression only uses the columns you listed and a list of safe functions: no sub-query, no other table | You want the model to explore tables freely, without defining measures first |
In both cases the model never sends a full SQL statement: it sends a description of a chart, and your application builds the query. The playground uses a semantic layer; the direct queries example shows the other mode, with the SQL that was run.
How it fits together¶
person ──► your chat ──► model (with the system prompt)
│ HTML
▼
sandboxed frame: BI Components, custom scripts
│ "revenue by country, last 90 days" (never SQL)
▼
Host (your page): checks every request against your dictionary
│
▼
your backend: applies the viewer's rights, computes, answers
- Your chat sends the conversation to a model, with a system prompt that describes the components and your data dictionary.
- The model answers with HTML. Your application puts it in a sandboxed frame (
buildSrcdoc), which has no network access. - Components ask for data: names of your dictionary, or expressions on the tables you expose. The Host (
HostBridge) rejects anything else. - Your backend adds what depends on the person (row-level security) and computes the answer.
Steps 2 to 4 are what this project provides. See Embed in your app for the code, and Architecture and security for what the sandbox guarantees.
The system prompt¶
This is the prompt used by the playground. The two placeholders at the end are filled with your own measures and dimensions.
You build dashboards for a retail company. The person you talk to cannot code.
Answer with one or two plain sentences, then ONE block of HTML. No explanation of the code.
## What you write
An HTML fragment (no <html>, <head> or <body>). It is displayed in a sandboxed frame that already
provides the components below and a stylesheet.
- The frame has no network access: never reference external scripts, styles, images or fonts.
- Layout classes: "toolbar" (filters), "grid" (charts), "grid cards" (key figures),
"card" (one block), "card wide" (full-width block). One <h1>, then an <h2> inside each card.
- Use ONLY the measures and dimensions of the data dictionary. Any other name is refused.
## Components
Key figure <score-card measure="M" compare="yoy"></score-card>
Trend over time <line-chart measure="M" granularity="month" compare="yoy"></line-chart>
Comparison <bar-chart measure="M" by="D" breakdown-by="D2" stacked horizontal sort-by="M" limit="10"></bar-chart>
Share <pie-chart measure="M" by="D"></pie-chart>
Table <data-table by="D" measures="M1,M2,M3" sort-by="M1" limit="20"></data-table>
Period <date-range-filter presets="all,30d,90d,365d,ytd"></date-range-filter>
Filter on a dimension <ui-filter dimension="D" type="select" label="..."></ui-filter> (type: select | segmented)
Business switch <ui-filter option="exclude_discounts" type="toggle" label="..."></ui-filter>
- One measure per chart; a table can show several.
- granularity: day | week | month | quarter | year. compare: yoy | mom | prev_period.
- Optional on charts: height="300px", title="...".
- Clicking a bar, a point or a table row filters the other components. You have nothing to code for that.
## Custom charts
Only when no component fits. Write a <script type="module"> using the two globals of the frame:
const chart = window.echarts.init(element, 'app'); // 'app' is the theme of the page
const { rows, meta } = await window.dashboard.query({ metrics: ['M1', 'M2'], dimensions: ['D'] });
window.dashboard.subscribe(render); // filters, period or theme changed
window.dashboard.filters.toggle('D', value); // filter the rest of the page
meta.labels and meta.formats describe each measure (formats: eur, pct, int).
## Data dictionary
Measures:
{{metrics}}
Dimensions:
{{dimensions}}
Things that work well in practice:
- Give labels and units with each measure (
avg_order_value: Average order value (eur)), so the model picks the right one from a business question. - Describe your layout classes, as the prompt does with
toolbar,gridandcard. Generated pages then look like the rest of your product. - Feed errors back. When a component shows "Unknown metrics: profit_margin", send that text to the model: it corrects the page in one turn. The playground has a recorded example of this.
- Keep one measure per chart. It is a rule of the components, and it keeps generated charts readable.
What a generated dashboard can and cannot do¶
A dashboard written by a model, or by another user, is untrusted code. It runs with the rights of the viewer, not of its author.
| It can | It cannot |
|---|---|
| Ask for what you allowed (measures of the dictionary, or expressions on exposed tables), within the viewer's rights | Send a SQL statement, or name a measure, table or column you did not declare |
| Display anything inside its frame, including custom charts | Read your page, cookies, storage or tokens |
| Set filters and options | See more rows than the viewer is allowed to |
| Send data to another server |
The details, including what remains your application's responsibility, are in the security model.
Want this on your data?¶
The playground runs on sample data. Setting it up in a company means plugging your measures (a semantic layer, dbt metrics, or plain SQL views) behind the Host, and connecting the model of your choice. Contact me on LinkedIn or open an issue.