Models, Retrieval, Tools, and Control Flow › A worked system: the catalog assistant
Language AI HandbookAll chapters 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]Expand
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]Expand
@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: strPythonIn [5]Expand
@dataclass(frozen=True)
class Answer:
outcome: Outcome
text: str
product_ids: tuple[str, ...]
status: tuple[str, ...]
trace: tuple[str, ...]
simulated_reservation: Reservation | None = NoneThe 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!