A market stall keeps a long list of sales. The owner asks how much money one item took on one day.
The first try pastes the whole list and asks the model to add it up. The model has room to show its working. The reply sounds sure, but the figure is often wrong.
The model only reads the question. It fills in a query. Plain code does the sum.
- The model turns the question into a query. This is structured output.
- A guard checks each value is one the stall knows.
- Code adds up the matching rows.
For example, "card sales on Sunday" becomes a day and a way of paying. The model never sees the sales.
sequenceDiagram
participant Code as Your code
participant Model
Code->>Model: the question
Model-->>Code: a query
alt every value is known
Code->>Code: add up the rows
Code->>Code: show the figure
else a value is made up
Code->>Code: stop with an error
end
- The form the model fills in is a query.
- It is a kind of structured output.
- The check on its values is a guard.
uv run lab data-processing naive
uv run lab data-processingAdd --case 2 or --case 3 to try the other cases.
There are six questions. Code works out each true figure from the same list. A reply counts only if it is exact. More is better.
| Version | Correct figures |
|---|---|
| Model adds up | 1 |
| Code adds up | 6 |
Now break it. Open the pattern file and find check_query. Make it return None at once. Then run the test:
uv run pytest tests/promises/test_data_processing.pyThe test fails, because an unknown day is now reported as zero.
- A wrong figure here means the query was wrong. Read the printed query.
- A question the form cannot hold needs a new field in the form.
The form limits what people can ask. That is also what keeps it safe. Show the query beside the figure, so the owner can see what was counted.
Go to Separate events from facts. It sorts what to keep.