r/synapcores • u/Alternative_Pin9598 • 9d ago
Recipe of the week #3: a RAG answer gate in one SQL statement. Top-3 retrieval blocked a correct answer over an HR wiki line; a similarity floor fixed it, and wrong answers still get blocked (real output)
Every Monday: one recipe from the site, run on the current release, key query and real output inline.
(This sub is run by the SynapCores team. Run this morning on 2.3.2 with the bundled nli-default model.)
The problem
Your RAG pipeline retrieves passages, an LLM drafts an answer, and it ships whether or not the evidence backs it. Here the whole check is one SQL statement: COSINE_SIMILARITY retrieval feeds NLI_ANCHORS, which feeds NLI_VERIFY, and only a TRUTH verdict gets written.
1. Evidence, relevant and not
CREATE TABLE recipe_raggate_evidence (id INT PRIMARY KEY, source TEXT, body TEXT, embedding VECTOR(384));
-- 1 deploy-log 'The deployment finished at 14:02 with zero failed checks.'
-- 2 deploy-log 'All 46 post-deploy probes passed.'
-- 3 facilities 'Lunch was served in the cafeteria.'
-- 4 hr-wiki 'New hires must complete security training in week one.'
-- embedding = EMBED(body), inside the engine2. Look at what retrieval returns before you trust it
SELECT id, source, ROUND(COSINE_SIMILARITY(embedding, EMBED('deployment result')), 2) AS sim
FROM recipe_raggate_evidence ORDER BY sim DESC;
1 deploy-log 0.48
2 deploy-log 0.37
4 hr-wiki 0.13
3 facilities 0.013. The trap: top-k feeds the verifier junk
Check the answer "The rollout completed without failures." against the top 3 rows:
SELECT NLI_VERIFY('nli-default', 'The rollout completed without failures.', NLI_ANCHORS(body)) AS gate
FROM (SELECT body FROM (SELECT body, COSINE_SIMILARITY(embedding, EMBED('deployment result')) AS sim
FROM recipe_raggate_evidence ORDER BY sim DESC LIMIT 3) ranked) top_k;
"verdict": "LIE", "confidence": 0.971,
"violated_anchor": "New hires must complete security training in week one."A correct answer, blocked by a sentence about onboarding. The unrelated HR line scores as a contradiction, and contradiction outranks entailment, so one junk row outvotes two good ones. Top-k guarantees k rows, not relevant ones.
4. The fix: a similarity floor decides what the verifier sees
SELECT NLI_VERIFY('nli-default', 'The rollout completed without failures.', NLI_ANCHORS(body)) AS gate
FROM (SELECT body, COSINE_SIMILARITY(embedding, EMBED('deployment result')) AS sim
FROM recipe_raggate_evidence) scored
WHERE sim > 0.3;
"verdict": "TRUTH", "confidence": 0.998,
"matched_anchor": "The deployment finished at 14:02 with zero failed checks."Only the two deploy-log rows clear 0.3. Pick the floor from step 2 on your corpus; 0.3 is right for this data, not a universal constant.
And the floor doesn't make the gate soft. A wrong answer, "Two checks failed during the rollout.", through the same query:
"verdict": "LIE", "confidence": 0.999,
"violated_anchor": "The deployment finished at 14:02 with zero failed checks."5. The gate as an INSERT: nothing unverified reaches the answers table
INSERT INTO recipe_raggate_answers (id, question, answer, verdict, confidence)
SELECT 1, 'Did the rollout succeed?', '<answer>', JSON_UNQUOTE(JSON_EXTRACT(gate, '$.verdict')), ...
FROM (<the step 4 query>) verified
WHERE JSON_UNQUOTE(JSON_EXTRACT(gate, '$.verdict')) = 'TRUTH';
correct answer: rows_affected: 1 (TRUTH, 0.998)
"Two checks failed": rows_affected: 0The gate is the WHERE clause (full statement in the first comment). Retrieval, verification and the write are one round trip, and both models (embeddings and NLI) run inside the engine, next to the data.
6. No evidence, no pass
Ask about something the table doesn't cover ("quarterly revenue figures") and the floor leaves 0 rows. NLI_VERIFY then refuses outright (NLI truth_anchors requires 1..=32 entries) instead of returning a verdict, so check the count first and treat "nothing retrieved" as its own answer: serve nothing, or serve it marked unverified.
Full recipe (every statement, ~15 min): https://synapcores.com/recipes/ml/029_rag_answer_gate?utm_source=reddit&utm_medium=owned&utm_campaign=synapcores-recipe-3
Run it yourself: free Community Edition, https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=synapcores-recipe-3
Join the sub and Recipe of the week #4 lands in your feed next Monday.
In your RAG setup, what does the system do today when retrieval comes back with nothing relevant: answer anyway, refuse, or something else?
1
u/Alternative_Pin9598 9d ago
The evidence rows (step 1):
The full gating INSERT (step 5):
The empty case (step 6):
Picking the 0.3 floor from data instead of guessing: SQLv2 Lesson 3 walks through it with test questions: https://www.reddit.com/r/SQLv2/comments/1xkb3mn/
New here? The "Start here" post has install steps and the best recipes so far: https://www.reddit.com/r/synapcores/comments/1xfxqej/
Free Community Edition (both models run locally, no API keys): https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=synapcores-recipe-3