r/SQLv2 • u/Alternative_Pin9598 • 3d ago
SQLv2 Lesson 9: similar isn't the same item. A white mouse scores 0.85 against your black one, the real mouse line only 0.49. One contradiction check in the JOIN catches it and 3 more look-alikes (beginner, real output)
Lesson 9 of the beginner series, run on SynapCores 2.5.1 this morning. (This sub is run by the SynapCores team.)
Lesson 8 homework, answered: 'Wireless mouse, white' matches 'Wireless mouse, black' at 0.85, higher than all 7 real matches on that invoice (the real mouse line: 0.49). Full output under Lesson 8.
Similarity asks: same kind of thing? A white and a black mouse are. To book stock or check prices, you need: same item?
Step 1: next week's invoice
Lesson 8's tables plus 5 new supplier lines (lines 9 to 13; INSERTs in the first comment). You stock the black mouse, 2 m cable, 65 W charger, US-layout keyboard and aluminium stand.
Lesson 8's JOIN matches all five, at 0.85 to 0.94. Four are wrong, and no threshold can split them off: real matches sit both below (0.49 to 0.72) and above (the stand, 0.94).
Step 2: a different question
NLI (natural language inference) is a small model in the engine that scores whether one text contradicts another:
SELECT ROUND(JSON_EXTRACT(NLI_CLASSIFY('nli-default',
'Wireless mouse, black', 'Wireless mouse, white'), '$.scores.contradiction'), 3);
0.998The real mouse line, 'Cordless optical mouse, blk': 0.018.
Step 3: both checks in one query
WITH best AS (... same as Lesson 8 ...),
checked AS (
SELECT i.line, b.id, i.item, c.name, b.sim,
JSON_EXTRACT(NLI_CLASSIFY('nli-default', c.name, i.item), '$.scores.contradiction') AS c1,
JSON_EXTRACT(NLI_CLASSIFY('nli-default', i.item, c.name), '$.scores.contradiction') AS c2
FROM best b JOIN invoice_lines i ON i.line = b.line JOIN catalog c ON c.id = b.id
WHERE b.rn = 1)
SELECT line, item, ROUND(sim, 2) AS sim, ROUND(c1, 2) AS c1, ROUND(c2, 2) AS c2,
CASE WHEN sim < 0.43 THEN 'NO MATCH: ask purchasing'
WHEN c1 > 0.5 AND c2 > 0.5 THEN 'DIFFERENT ITEM: ask purchasing'
ELSE name END AS matched
FROM checked ORDER BY line;
1 Type-C power adapter 65W for notebooks 0.72 0.00 0.05 USB-C laptop charger, 65 W
2 Cordless optical mouse, blk 0.49 0.02 0.00 Wireless mouse, black
3 2m high speed HDMI lead 0.64 0.00 0.01 HDMI cable, 2 m
4 UHD 27in display 0.60 0.00 0.00 27-inch 4K monitor
5 Keyboard w/ mechanical switches, ANSI 0.63 0.01 0.99 Mechanical keyboard, US layout
6 Ergonomic mesh office chair 0.37 1.00 1.00 NO MATCH: ask purchasing
7 ANC headphones with boom mic 0.50 0.61 0.03 Noise-cancelling headset with microphone
8 Dock, USB-C, 2x HDMI out 0.70 0.00 0.00 USB-C docking station, dual display
9 Wireless mouse, white 0.85 1.00 1.00 DIFFERENT ITEM: ask purchasing
10 HDMI cable, 5 m 0.92 1.00 1.00 DIFFERENT ITEM: ask purchasing
11 USB-C laptop charger, 100 W 0.90 1.00 1.00 DIFFERENT ITEM: ask purchasing
12 Mechanical keyboard, UK layout 0.85 1.00 1.00 DIFFERENT ITEM: ask purchasing
13 Laptop stand, aluminum 0.94 0.00 0.00 Aluminium laptop standWhy both ways? NLI takes the first text as given and checks the second against it, so a pair can clash one way only (lines 5 and 7). Each wrong item clashes both ways (9 to 12). Requiring both keeps all 8 real matches.
Step 4, the payoff: Lesson 8's price check
On the new invoice: $429 over list.
USB-C laptop charger, 100 W 10 x 59.00 vs 39.00 +200.00
UHD 27in display 4 x 315.00 vs 289.00 +104.00
HDMI cable, 5 m 20 x 14.00 vs 9.00 +100.00
Type-C power adapter 65W for notebooks 10 x 41.50 vs 39.00 +25.00$300 of that is the 100 W charger and the 5 m cable, priced as your 65 W charger and 2 m cable. Run it on checked k with AND NOT (k.c1 > 0.5 AND k.c2 > 0.5) and it's Lesson 8's $129 again. The white mouse and UK keyboard are at list price, so only Step 3 stops them being booked as the black mouse and US keyboard.
Homework
The supplier sends 'Wireless mouse', no colour, 10 at 19.00.
- Which product does the JOIN pick, and at what similarity?
- Does the contradiction check flag it?
- Should it? What would you do with a line that leaves a detail out?
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-9
What's the worst look-alike that got past your matching: wrong size, colour, wattage?
1
u/Alternative_Pin9598 3d ago
Already have Lesson 8's tables? Add next week's 5 lines and their vectors (the DELETE first clears line 9 if you kept it from Lesson 8's homework):
With these tables the script below works too: skip
setup.Starting fresh? The full script (Python, standard library only) is in the reply below.
python3 lesson9.py setupcreates both tables with all 13 lines and their vectors;matchruns Lesson 8's query,checkStep 3,pricesStep 4 (before and after), andcleanupdrops everything.NLI is a one-time opt-in:
synapcores pull nli-default, thensynapcores models enable-nli nli-default, both with the--configyour server uses, then restart the server.Ran it twice from scratch on 2.5.1: same scores, same result.
Same two signals, different job: Recipe of the week #2 on r/synapcores uses similarity + NLI to catch new facts that contradict an AI agent's memory before they're saved: https://www.reddit.com/r/synapcores/comments/1xru0wg/
New to SynapCores? Install steps and the 5 recipes to try first: https://www.reddit.com/r/synapcores/comments/1xfxqej/