Skip to content

Latest commit

 

History

History
87 lines (60 loc) · 2.44 KB

data-processing.md

File metadata and controls

87 lines (60 loc) · 2.44 KB

Compute answers with code

The problem

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.

How it works

Let code add up the sales.

The model only reads the question. It fills in a query. Plain code does the sum.

  1. The model turns the question into a query. This is structured output.
  2. A guard checks each value is one the stall knows.
  3. 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.

The flow

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
Loading

Concepts

Run it

uv run lab data-processing naive
uv run lab data-processing

Add --case 2 or --case 3 to try the other cases.

Prove it

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.py

The test fails, because an unknown day is now reported as zero.

Stuck points

  • 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.

What to watch for

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.

Where to go next

Go to Separate events from facts. It sorts what to keep.