JDogo
Open playground

Grounded compute

For counts, totals and averages, the model writes a one-line query and JDogo computes the answer in your process. Every answer comes with the rows it used and a second, independent computation of the same result.

Why

Models reading rows miscount. In our recorded runs, models answering counting and totals questions by reading the rows got 74 of 150 right; writing a query that JDogo ran, they got 150 of 150. See Questions about large tables.

Example: a 2,000-row ledger

Five questions over examples/data/ledger.json, 2,000 generated orders and payments (seed 42). The expressions are the kind a model writes; this page runs them directly, and scripts/generate_grounded_compute_docs.py rebuilds it from the data.

Question and expression Answer Rows used and check
How many completed transactions are there?
count where status = "COMPLETED"
1,518 1,518 rows, starting ORD-1001, ORD-1003, ORD-1004, …
Check: passed
What is the total fee collected from completed credit card transactions?
sum(fee) where status = "COMPLETED" and method = "credit_card"
8,896.69 374 rows, starting ORD-1004, ORD-1006, ORD-1008, …
Check: passed
What is the average transaction amount for USD orders?
avg(amount) where currency = "USD" and type = "ORDER"
897.63 420 rows, starting ORD-1005, ORD-1009, ORD-1017, …
Check: passed
How many distinct customers had pending transactions?
count distinct(customer_id) where status = "PENDING"
177 296 rows, starting ORD-1002, ORD-1021, ORD-1022, …
Check: passed
What is the maximum transaction amount among crypto payments?
max(amount) where method = "crypto"
1,848.61 506 rows, starting ORD-1002, ORD-1016, ORD-1018, …
Check: passed

In code

ask() returns the answer with everything needed to audit it:

from jdogo import ask

result = ask("How many completed transactions are there?", ledger, llm=call_model)
print(result.answer)        # 1518
print(result.expression)    # count where status = "COMPLETED"
print(result.matched_rows)  # 1518
print(result.row_refs[:5])  # ["ORD-1001", "ORD-1003", "ORD-1004", ...]
print(result.check)         # "passed"

Which rows are named

  1. ID column. The first column named id, *_id or *Id whose values are all present and unique.
  2. Otherwise, row positions. Zero-based indexes, for example [0, 2], including when an ID column has duplicates.
  3. At most 1,000. Beyond that, row_refs_capped is true; matched_rows is always the full count.
  4. Only when asked for. ask() always includes them. For a direct call, pass with_refs=True ({ withRefs: true } in TypeScript, runCompute(expr, data, true) in Java); without it, compute skips the extra pass over the data.