Skip to content

Travel bot

You’ll build a travel agent that plans trips with your components. The model renders flight and hotel cards, their buttons reach @app.on_action handlers, and a booking waits for the user’s approval with chat.ask(ui.Confirm(...)).

  • pip install "dyui[openai]"
  • An API key, in a .env file (step 2).
  1. Save the code below as travel_bot.py in a new, empty file, or download travel_bot.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 travel_bot.py
  4. Open http://localhost:8000. Click Plan a weekend in Lisbon, then Book on a flight.

  • The docstrings are prompts. “Use when listing specific flights” tells the model when to reach for the component.
  • examples are validated at import, so a field rename that breaks an example fails immediately.
  • render() returns built-ins. The browser never needs to know what a Flight is.
  • book_flight mixes all three response styles: an approval (chat.ask), a live step and a closing callout with quick replies.
travel_bot.py
"""Travel agent: an LLM that renders custom Flight / Hotel components and books with confirmation."""
import os
import dyui
from 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", "Find flights from Berlin to Tokyo"],
)
@app.component
class Flight(dyui.Component):
"""A bookable flight option. Use when listing specific flights."""
airline: str
depart: str
arrive: str | None = None
price: float
examples = [{"airline": "TAP", "depart": "Fri 18:05", "price": 189}]
def render(self):
return ui.Card(
self.airline,
ui.Stat("Departs", self.depart),
ui.Stat("Price", f"${self.price:.0f}"),
actions=[ui.Button("Book", action="book_flight", data=self.model_dump())],
)
@app.component
class Hotel(dyui.Component):
"""A hotel option with nightly price and rating. Use when suggesting places to stay."""
name: str
area: str | None = None
price_per_night: float
rating: float | None = None
examples = [{"name": "Casa Alfama", "area": "Alfama", "price_per_night": 120, "rating": 4.6}]
def render(self):
return ui.Card(
self.name,
ui.Stat("Per night", f"${self.price_per_night:.0f}"),
subtitle=self.area,
actions=[ui.Button("Reserve", action="book_hotel", data=self.model_dump(), style="secondary")],
)
SYSTEM = """You are a friendly travel agent. Suggest realistic options (invent plausible
prices and times). Show flights with the flight component and stays with the hotel
component, then offer quick replies for next steps."""
@app.on_message
async def reply(msg: dyui.Message, chat: dyui.Chat):
async for part in dyui.llm.stream(MODEL, chat.history, system=SYSTEM, components=app.catalog):
yield part
@app.on_action("book_flight")
async def book_flight(action: dyui.Action, chat: dyui.Chat):
f = action.data
# Human in the loop: render a confirmation and wait for the click.
answer = await chat.ask(ui.Confirm(
f"Book {f['airline']} {f['depart']}?", text=f"Total ${f['price']:.0f}", confirm_label="Book it",
))
if answer.values.get("confirmed"):
async with chat.step("Booking flight", type="tool"):
pass # call your booking API here
yield ui.Callout(f"Booked {f['airline']}, {f['depart']}.", level="success", title="Confirmed")
yield ui.QuickReplies(["Find me a hotel too"])
else:
yield "Okay, I have not booked anything."
@app.on_action("book_hotel")
async def book_hotel(action: dyui.Action, chat: dyui.Chat):
yield ui.Callout(f"Reserved {action.data['name']}.", level="success")
  • Call a real flight API inside the chat.step(...) block and put its result in step.output.
  • Add a Hotel filter with ui.Form and handle its form_submit.
  • Offer only booking components at checkout: components=app.catalog.only("flight", "confirm", "quick_replies").

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.