r/SQLv2 • u/Alternative_Pin9598 • 24d ago
SQLv2 Lesson 3: why "I don't know" beats a confident wrong answer (pick a similarity threshold from data, beginner)
Lesson 3 of the beginner series, every query run on SynapCores 2.3.2 with real output. (This sub is run by the SynapCores team.)
The problem
Similarity search always returns a top result, even when nothing in the table answers the question. Ask a shop FAQ bot "do you sell gift cards?" and it replies with the closest thing it has. A confident wrong answer is worse than "let me get a human", so let's pick a cut-off from data.
Step 1: a small FAQ with a VECTOR column
CREATE TABLE shop_faq (id INTEGER PRIMARY KEY, question TEXT,
answer TEXT, embedding VECTOR(384));
INSERT INTO shop_faq (id, question, answer) VALUES
(1, 'How long does shipping take?', 'Orders ship in 1-2 days and arrive in 3-5 business days.'),
(2, 'How do I return an item?', 'Start a return from Orders within 30 days; the label is free.'),
(3, 'When will I get my refund?', 'Refunds land 5-7 days after we receive the return.'),
(4, 'Do you ship internationally?', 'We ship to the US, Canada and the EU.'),
(5, 'How do I change my password?', 'Settings, then Security, then Change password.'),
(6, 'Can I cancel my order?', 'Yes, until it ships. Use Cancel on the order page.');
UPDATE shop_faq SET embedding = EMBED(question);Step 2: the confident wrong answer
SELECT question, answer,
COSINE_SIMILARITY(embedding, EMBED('do you sell gift cards')) AS score
FROM shop_faq ORDER BY score DESC LIMIT 1;
Do you ship internationally? We ship to the US, Canada and the EU. 0.36Nothing here is about gift cards, but LIMIT 1 still answers.
Step 3: score test questions you know the answer to
Ten questions: six the FAQ covers (should_match = right FAQ id), four it doesn't (NULL). Each one's best FAQ match via CROSS JOIN + ROW_NUMBER (setup and query in the first comment):
id | asked | should | faq | score
2 | how do I send an item back | 2 | 2 | 0.79
6 | stop my order please | 6 | 6 | 0.69
1 | when does my package arrive | 1 | 1 | 0.67
5 | forgot my login, how do I reset it | 5 | 5 | 0.57
4 | can you deliver to Germany | 4 | 4 | 0.56
3 | still waiting on my money back | 3 | 3 | 0.49
7 | do you sell gift cards | NULL | 4 | 0.36
10 | do you have a store in Chicago | NULL | 4 | 0.35
8 | are you hiring | NULL | 4 | 0.23
9 | is the blue jacket waterproof | NULL | 4 | 0.14All six covered questions find the right article, even with no shared keywords ("money back" → refunds). The off-topic four top out at 0.36. That gap is your threshold.
Step 4: let SQL grade each threshold
Candidate thresholds go in a table; one query counts right / wrong / "I don't know" for each (query in the first comment):
threshold | right | wrong | i_dont_know
0.00 | 6 | 4 | 0
0.30 | 6 | 2 | 2
0.40 | 6 | 0 | 4
0.45 | 6 | 0 | 4
0.50 | 5 | 0 | 5
0.60 | 3 | 0 | 7No threshold: 4 wrong answers out of 10. At 0.40-0.45: 6 right, 0 wrong, and the off-topic four get "I don't know". At 0.60 you turn away customers you could have helped. Pick the middle of the gap: 0.45.
Step 5: the answer gate
SELECT question,
COSINE_SIMILARITY(embedding, EMBED('how long until my refund shows up')) AS score,
CASE WHEN COSINE_SIMILARITY(embedding, EMBED('how long until my refund shows up')) >= 0.45
THEN answer ELSE 'Not sure yet. Passing this to a human.' END AS reply
FROM shop_faq ORDER BY score DESC LIMIT 1;
When will I get my refund? 0.84 Refunds land 5-7 days after we receive the return.Same query with "do you sell gift cards":
Do you ship internationally? 0.36 Not sure yet. Passing this to a human.Takeaway: similarity always has a top result. Score questions you know the answers to, let SQL grade each threshold, then gate with CASE WHEN score >= 0.45, all in the same query.
Free Community Edition (runs on a laptop): https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=sqlv2-03
Homework: add "what are your opening hours" to test_questions (should_match NULL). What's its best score, and does 0.45 still say "I don't know"? Post your number below.
Next lesson: agent memory in SQL: REMEMBER, RECALL and CURRENT.
1
u/Alternative_Pin9598 24d ago
Test questions for Step 3:
Step 3, best match per question:
The Step 4 query (thresholds as a table, graded against the test questions):
Want to run this on the real engine? r/synapcores is the product home: install steps, the 5 recipes to try first, and a new recipe every Monday. Start here: https://www.reddit.com/r/synapcores/comments/1xfxqej/start_here_what_synapcores_is_how_to_install_it/
Missed Lesson 2 (meaning + normal filters in one WHERE)? https://www.reddit.com/r/SQLv2/comments/1xikzr4/sqlv2_lesson_2_meaning_normal_filters_in_one/