Skip to content

Data analyst

You’ll build an analyst that answers questions about a small CSV embedded in the system prompt, with a stats row, a chart or a table, and a sentence that says what they mean.

  • pip install "dyui[openai]"
  • An API key, in a .env file (step 2).
  1. Save the code below as data_analyst.py in a new, empty file, or download data_analyst.py.

  2. Put your key in a .env file next to it: NVIDIA_API_KEY=nvapi-... (free at build.nvidia.com). For another provider, add its key and DYUI_MODEL; see API keys & environment.

  3. Run it:

    Terminal window
    pip install "dyui[openai]"
    dyui run data_analyst.py
  4. Open http://localhost:8000. Click Revenue trend by region.

  • {dyui_components} in the system prompt marks where the catalog section goes, so the dataset and the instructions stay together. In the f-string it’s written {{dyui_components}}.
  • Steering in plain words (“Prefer a stats row for headline numbers…”) works better than restricting the catalog.
  • A small catalog (app.catalog.only(...)) keeps the prompt short, which matters on rate-limited free tiers.
  • ui_mode="tools" switches to strict render_* tools on OpenAI and Anthropic, with no other code changes. Add DYUI_UI_MODE=tools to .env to try it.
data_analyst.py
"""Data analyst: the model reasons over a small in-file dataset and answers with stats, charts and tables."""
import csv
import io
import os
import dyui
MODEL = os.getenv("DYUI_MODEL", "nvidia/moonshotai/kimi-k2.6")
# "fence" (default, works everywhere) or "tools": strict structured outputs via render_* tools.
UI_MODE = os.getenv("DYUI_UI_MODE", "fence")
DATA = """month,region,revenue,orders
Jan,EU,41200,310
Jan,US,58800,402
Feb,EU,43900,331
Feb,US,61400,415
Mar,EU,47800,352
Mar,US,66100,447
Apr,EU,52100,380
Apr,US,64900,431
"""
ROWS = list(csv.DictReader(io.StringIO(DATA)))
SYSTEM = f"""You are a data analyst. Answer only from this dataset (CSV):
{DATA}
Prefer a `stats` row for headline numbers, a `chart` for trends or comparisons, and a
`table` for lists. Always explain in words too: open with a sentence that answers the
question, show the components, then close with the insight a reader should take away.
Compute numbers carefully.
{{dyui_components}}"""
app = dyui.App(
title="Data Analyst",
description=f"Ask questions about {len(ROWS)} rows of monthly sales.",
starters=["Revenue trend by region", "Which month had the most orders?", "Show the raw data"],
)
# Only the components an analyst needs. A smaller catalog means a smaller prompt, which
# matters on rate-limited free tiers (Groq, NVIDIA NIM), most of all with UI_MODE=tools.
CATALOG = app.catalog.only("stat", "stats", "chart", "table", "keyvalue", "callout", "quick_replies")
@app.on_message
async def reply(msg: dyui.Message, chat: dyui.Chat):
# "{dyui_components}" in SYSTEM is replaced with the live component catalog.
async for part in dyui.llm.stream(MODEL, chat.history, system=SYSTEM, components=CATALOG,
ui_mode=UI_MODE):
yield part
  • Load your own CSV with csv.DictReader(open("sales.csv")) instead of DATA.
  • Let users upload the file: read msg.files[0].path in the handler.
  • Add a quick_replies hint to the prompt so every answer ends with follow-up questions.

DyUI Docs assistant

Answers come from the DyUI docs. gpt-oss-120b via Groq. Press Escape or the close button to return to the page.