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.
Prerequisites
Section titled “Prerequisites”pip install "dyui[openai]"- An API key, in a
.envfile (step 2).
Save and run
Section titled “Save and run”-
Save the code below as
data_analyst.pyin a new, empty file, or download data_analyst.py. -
Put your key in a
.envfile next to it:NVIDIA_API_KEY=nvapi-...(free at build.nvidia.com). For another provider, add its key andDYUI_MODEL; see API keys & environment. -
Run it:
Terminal window pip install "dyui[openai]"dyui run data_analyst.py -
Open http://localhost:8000. Click Revenue trend by region.
What to look at
Section titled “What to look at”{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
statsrow 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 strictrender_*tools on OpenAI and Anthropic, with no other code changes. AddDYUI_UI_MODE=toolsto.envto try it.
The code
Section titled “The code”"""Data analyst: the model reasons over a small in-file dataset and answers with stats, charts and tables."""import csvimport ioimport 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,ordersJan,EU,41200,310Jan,US,58800,402Feb,EU,43900,331Feb,US,61400,415Mar,EU,47800,352Mar,US,66100,447Apr,EU,52100,380Apr,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 thequestion, 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_messageasync 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 partExtend it
Section titled “Extend it”- Load your own CSV with
csv.DictReader(open("sales.csv"))instead ofDATA. - Let users upload the file: read
msg.files[0].pathin the handler. - Add a
quick_replieshint to the prompt so every answer ends with follow-up questions.