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(...)).
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
travel_bot.pyin a new, empty file, or download travel_bot.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 travel_bot.py -
Open http://localhost:8000. Click Plan a weekend in Lisbon, then Book on a flight.
What to look at
Section titled “What to look at”- The docstrings are prompts. “Use when listing specific flights” tells the model when to reach for the component.
examplesare validated at import, so a field rename that breaks an example fails immediately.render()returns built-ins. The browser never needs to know what aFlightis.book_flightmixes all three response styles: an approval (chat.ask), a live step and a closing callout with quick replies.
The code
Section titled “The code”"""Travel agent: an LLM that renders custom Flight / Hotel components and books with confirmation."""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", "Find flights from Berlin to Tokyo"],)
@app.componentclass 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.componentclass 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 plausibleprices and times). Show flights with the flight component and stays with the hotelcomponent, then offer quick replies for next steps."""
@app.on_messageasync 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")Extend it
Section titled “Extend it”- Call a real flight API inside the
chat.step(...)block and put its result instep.output. - Add a
Hotelfilter withui.Formand handle itsform_submit. - Offer only booking components at checkout:
components=app.catalog.only("flight", "confirm", "quick_replies").