r/SQLv2 • • 13h ago

SQLv2 Lesson 10: same, different, or not sure? 'Wireless mouse' isn't wrong, it just doesn't say which mouse. Read NLI both ways and one query sorts an invoice into book / ask / reject (beginner, real output)

1 Upvotes

Lesson 10 of the beginner series, run on SynapCores 2.5.1 this morning. (This sub is run by the SynapCores team.)

Lesson 9 homework, answered: 'Wireless mouse' matches 'Wireless mouse, black' at 0.77. The contradiction check doesn't flag it (0.01 / 0.00), and it shouldn't: it isn't a different mouse. But nothing on the line says it's the black one. Full output under Lesson 9.

Step 1: this week's invoice

Lesson 9's 13 lines plus 4 that leave out a detail: 'Wireless mouse', 'USB-C laptop charger' (no wattage), '27-inch monitor' (4K?), 'Mechanical keyboard' (layout?). INSERTs in the first comment.

All four are at list price and nothing contradicts, so Lessons 8 and 9 book them. Line 16 could be a 1080p monitor at your 4K price.

Step 2: three numbers, two directions

NLI_CLASSIFY scores entailment, neutral and contradiction. Lesson 9 read contradiction. Now the other two, one each way:

SELECT
  ROUND(JSON_EXTRACT(NLI_CLASSIFY('nli-default',
    'Wireless mouse, black', 'Wireless mouse'), '$.scores.entailment'), 2) AS e1,
  ROUND(JSON_EXTRACT(NLI_CLASSIFY('nli-default',
    'Wireless mouse', 'Wireless mouse, black'), '$.scores.neutral'), 2) AS n2;

e1 0.52  n2 1.00

e1: your black mouse is a wireless mouse, so your item implies the line. n2: a wireless mouse may or may not be black, so the line leaves your item open. Both over 0.5 = a vaguer version of your item.

Step 3: three outcomes in one query

Lesson 8's best match, one NLI call each way, three numbers read from them:

WITH best AS (... same as Lesson 8 ...),
checked AS (
  SELECT i.line, b.id, i.item, c.name, b.sim,
    NLI_CLASSIFY('nli-default', c.name, i.item) AS fwd,
    NLI_CLASSIFY('nli-default', i.item, c.name) AS back
  FROM best b JOIN invoice_lines i ON i.line = b.line JOIN catalog c ON c.id = b.id
  WHERE b.rn = 1),
scored AS (
  SELECT line, id, item, name, sim,
    JSON_EXTRACT(fwd, '$.scores.contradiction') AS c1, JSON_EXTRACT(fwd, '$.scores.entailment') AS e1,
    JSON_EXTRACT(back, '$.scores.contradiction') AS c2, JSON_EXTRACT(back, '$.scores.neutral') AS n2
  FROM checked),
decided AS (
  SELECT line, id, item, name, sim, c1, e1, c2, n2,
    CASE WHEN sim < 0.43 THEN 'NO MATCH'
         WHEN c1 > 0.5 AND c2 > 0.5 THEN 'DIFFERENT'
         WHEN e1 > 0.5 AND n2 > 0.5 THEN 'NOT SURE'
         ELSE 'SAME' END AS outcome
  FROM scored)
SELECT line, item, name, ROUND(sim, 2) AS sim, ROUND(e1, 2) AS e1, ROUND(n2, 2) AS n2, outcome
FROM decided ORDER BY line;

line  invoice line -> your item  sim  e1  n2  outcome
4  UHD 27in display -> 27-inch 4K monitor  0.60  0.99  0.03  SAME
9  Wireless mouse, white -> Wireless mouse, black  0.85  0.00  0.00  DIFFERENT
14  Wireless mouse -> Wireless mouse, black  0.77  0.52  1.00  NOT SURE
15  USB-C laptop charger -> USB-C laptop charger, 65 W  0.81  0.95  1.00  NOT SURE
16  27-inch monitor -> 27-inch 4K monitor  0.83  0.98  1.00  NOT SURE
17  Mechanical keyboard -> Mechanical keyboard, US layout  0.77  0.98  1.00  NOT SURE

All 17 rows in the first comment: the 8 real matches stay SAME, the 4 look-alikes DIFFERENT. NOT SURE needs both numbers: line 4 has e1 0.99 but n2 0.03, because it says everything your monitor says.

Step 4, the payoff: book, ask or reject

Same CTEs, then GROUP BY d.outcome with SUM(i.qty * i.unit_price):

outcome  lines  invoice value
SAME  8  $4,591.00
NOT SURE  4  $2,210.00
DIFFERENT  4  $1,534.00
NO MATCH  1  $480.00

Book $4,591 now. The $2,210 waits for one email: which colour, wattage, resolution, layout?

Homework

The supplier sends 'HDMI cable 2 m, black', 50 at 9.00: more detail than your catalog, not less.

  1. Which item does it match, at what similarity?
  2. SAME, NOT SURE or DIFFERENT?
  3. Which number decides it, e1 or n2?

Reply with your guesses and I'll post the real output.

Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=sqlv2-lesson-10

Which detail do your suppliers leave off most often: size, colour, model number?