Models, Retrieval, Tools, and Control Flow › A worked system: the catalog assistant

Michael BrenndoerferDecember 2, 20263 min read
Language AI HandbookAll chapters
Part 64 of 67Compound AI System Design
Chapter 485Models, Retrieval, Tools, and Control Flow › A worked system: the catalog assistant
Reading section 3 of 11 · A worked system: the catalog assistant · 3 minSection 3 of 11 · 3 min

Section 3 of 11 · 3 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

A product record has a display name, an ID, a price, and a list of supported sizes. The catalog assistant resolves the ID before it asks for stock for a particular size. We keep those product attributes and the stock data in two different fixtures to make their provenance visible in the code.

In the example, an assistant can offer a description and catalog price. It can also report the toy stock count that was available to it for the requested variant. The last feature is recording a simulated decision about a reservation. We therefore need two different interfaces for the last two operations: a reading operation that observes stock, and an optional decision record separate from the catalog answer. The decision record is generated entirely inside the notebook.

Each boundary passes a different kind of information.

  • Data. The parser returns a query plan, retrieval returns ranked IDs, and the inventory lookup returns a count.
  • Decision. The application chooses whether to continue, return partial results, or decline.
  • Status. The example records stage labels such as “Checking stock” and returns them alongside a completed outcome. A live interface would have to emit these labels as work progresses.
  • Effect gate. Both caller permission and an explicit product and size must match before the demo records a simulated reservation.

An ordered trace gives the tests a way to check decisions. We also keep the reader-facing status separate from the trace. An application can use the latter for diagnostics without turning its internal branch names into shopper-facing text.

PythonIn [3]
CollapseExpand
import re
from collections import Counter
from collections.abc import Callable, Mapping
from dataclasses import dataclass
from enum import Enum
from math import log1p


class Intent(Enum):
    CATALOG = "catalog"
    AVAILABILITY = "availability"
    UNSUPPORTED = "unsupported"


class Outcome(Enum):
    COMPLETE = "complete"
    PARTIAL = "partial"
    ABSTAIN = "abstain"

Records at the parser, catalog, and answer boundaries

The intent names the product operation the parser recognized. The outcome names what the assistant delivered. A request with availability intent can therefore end with any of the three outcomes. The application can put the outcome and progress status in fixed UI fields and render the text next to them.

PythonIn [4]
CollapseExpand
@dataclass(frozen=True)
class QueryPlan:
    intent: Intent
    keywords: str
    size: str | None
    price_limit_usd: int | None


@dataclass(frozen=True)
class Product:
    id: str
    name: str
    price_usd: int
    sizes: tuple[str, ...]


@dataclass(frozen=True)
class Reservation:
    product_id: str
    size: str
PythonIn [5]
CollapseExpand
@dataclass(frozen=True)
class Answer:
    outcome: Outcome
    text: str
    product_ids: tuple[str, ...]
    status: tuple[str, ...]
    trace: tuple[str, ...]
    simulated_reservation: Reservation | None = None

The records convey shape and intent. The parser's return annotation alone cannot establish that the query plan is valid or that it matches the user's request. The local program checks the fields it branches on. A separate Reservation arrives from the caller and identifies a product and size explicitly. Neither the parser nor the answer generator supplies it.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy