Your first LLM bot
This guide builds a small travel bot that uses everything that makes DyUI different:
- the model renders built-in components;
- the model renders a component you define;
- a button click reaches a Python handler;
- the user approves a booking before it happens;
- a live step shows work in progress.
Before you start
Section titled “Before you start”pip install "dyui[openai]"Put your key in a .env file in the folder where you’ll save app.py:
NVIDIA_API_KEY=nvapi-...The key is free at build.nvidia.com. To use OpenAI, Anthropic or another provider, add its key instead and set DYUI_MODEL; see API keys & environment.
The app
Section titled “The app”Save this as app.py in a new, empty file, or download app.py:
"""Travel Bot: built-in and custom components, a button handled in Python,an approval before booking, and a live step.
Needs: pip install "dyui[openai]" and NVIDIA_API_KEY in a .env file next to this one.Save as app.py, then run: dyui run app.py"""import os
import dyuifrom dyui import ui
MODEL = os.getenv("DYUI_MODEL", "nvidia/moonshotai/kimi-k2.6")
app = dyui.App( title="Travel Bot", description="Plans trips and books flights.", starters=["Plan a weekend in Lisbon", dyui.Starter("Find flights", "Flights to Tokyo next Friday", icon="plane")],)
@app.componentclass Flight(dyui.Component): """A bookable flight option. Use when listing specific flights."""
airline: str depart: str price: float
examples = [{"airline": "TAP", "depart": "Fri 18:05", "price": 189}] # validated at import
def render(self): return ui.Card( self.airline, ui.KeyValue({"Departs": self.depart, "Price": f"${self.price:.0f}"}), actions=[ui.Button("Book", action="book_flight", data=self.model_dump(), style="primary")], )
@app.on_messageasync def reply(msg: dyui.Message, chat: dyui.Chat): async for part in dyui.llm.stream( MODEL, chat.history, system="You are a calm, expert travel agent. Offer 2-3 concrete flights when asked.", components=app.catalog, ): yield part
@app.on_action("book_flight")async def book(action: dyui.Action, chat: dyui.Chat): flight = action.data answer = await chat.ask(ui.Confirm( f"Book {flight['airline']} at {flight['depart']}?", text=f"${flight['price']:.0f}, charged now.", confirm_label="Book", )) if not answer.values.get("confirmed"): yield "No problem — nothing was booked." return async with chat.step("Booking the seat", type="tool") as step: step.output = {"reference": "TP-7Q4XK2"} # call your booking API here yield ui.Callout("Booked. Your confirmation is on its way.", level="success")Run it:
dyui run app.pyOpen http://localhost:8000, click Find flights, then Book on one of the cards.
What happens
Section titled “What happens”- The prompt grows with your app.
components=app.catalogadds a line for every enabled built-in, plus yourflightcomponent. The class docstring becomes its description and the fields become its props. Rundyui catalog app.pyto see the exact text. - The model writes a component. When it offers flights, the model writes
{"ui":"flight","airline":"TAP","depart":"Fri 18:05","price":189}inside adyuifence. DyUI validates it againstFlightand callsrender(). The browser receives the built-in card thatrender()returned. examplesare checked at import. The example props onFlightare validated when the class is defined, so a renamed field fails immediately instead of in a chat.- The click comes back to Python. Book sends the action
book_flightwith the flight asdata.@app.on_action("book_flight")handles it. Without a handler, the click would go toon_messageas a message for the model; see Actions. - The agent waits for a human.
await chat.ask(ui.Confirm(...))renders a confirmation and pauses the handler until the user answers. - Work shows progress.
chat.step(...)renders a live step. It shows running, then done (or error) with its duration.
Switch models
Section titled “Switch models”Change one string. Any provider/model works. Put it in .env so the command is the same on every operating system:
NVIDIA_API_KEY=nvapi-...DYUI_MODEL=nvidia/deepseek-ai/deepseek-v4.1-flash# DYUI_MODEL=anthropic/claude-sonnet-5-5 (needs ANTHROPIC_API_KEY and dyui[anthropic])# DYUI_MODEL=ollama/llama3.2 (local, no key)See Models & providers for every provider.
- How DyUI works: what happens between your handler and the browser.
- Travel bot: the same idea with hotels and more actions.