r/SQLv2 • • 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.36

Nothing 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.14

All 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 | 7

No 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 Upvotes

1 comment sorted by

1

u/Alternative_Pin9598 24d ago

Test questions for Step 3:

CREATE TABLE test_questions (
  id INTEGER PRIMARY KEY, asked TEXT, should_match INTEGER, embedding VECTOR(384)
);
INSERT INTO test_questions (id, asked, should_match) VALUES
  (1, 'when does my package arrive', 1), (2, 'how do I send an item back', 2),
  (3, 'still waiting on my money back', 3), (4, 'can you deliver to Germany', 4),
  (5, 'forgot my login, how do I reset it', 5), (6, 'stop my order please', 6),
  (7, 'do you sell gift cards', NULL), (8, 'are you hiring', NULL),
  (9, 'is the blue jacket waterproof', NULL), (10, 'do you have a store in Chicago', NULL);
UPDATE test_questions SET embedding = EMBED(asked);

Step 3, best match per question:

SELECT id, asked, should_match, faq_id, score FROM (
  SELECT t.id, t.asked, t.should_match, f.id AS faq_id,
         COSINE_SIMILARITY(t.embedding, f.embedding) AS score,
         ROW_NUMBER() OVER (PARTITION BY t.id
           ORDER BY COSINE_SIMILARITY(t.embedding, f.embedding) DESC) AS rn
  FROM test_questions t CROSS JOIN shop_faq f) x
WHERE rn = 1 ORDER BY score DESC;

The Step 4 query (thresholds as a table, graded against the test questions):

CREATE TABLE thresholds (t FLOAT PRIMARY KEY);
INSERT INTO thresholds (t) VALUES (0.0), (0.3), (0.4), (0.45), (0.5), (0.6);

SELECT th.t AS threshold,
  SUM(CASE WHEN b.score >= th.t AND b.faq_id = b.should_match THEN 1 ELSE 0 END) AS right_answers,
  SUM(CASE WHEN b.score >= th.t AND (b.should_match IS NULL OR b.faq_id <> b.should_match)
           THEN 1 ELSE 0 END) AS wrong_answers,
  SUM(CASE WHEN b.score < th.t THEN 1 ELSE 0 END) AS i_dont_know
FROM thresholds th CROSS JOIN (
  SELECT t.id, t.should_match, f.id AS faq_id,
         COSINE_SIMILARITY(t.embedding, f.embedding) AS score,
         ROW_NUMBER() OVER (PARTITION BY t.id
           ORDER BY COSINE_SIMILARITY(t.embedding, f.embedding) DESC) AS rn
  FROM test_questions t CROSS JOIN shop_faq f) b
WHERE b.rn = 1
GROUP BY th.t ORDER BY th.t;

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/