Fix RAG zero-recall with HNSW index and add empty-state guidance#56
Merged
Conversation
There was a problem hiding this comment.
1 issue found across 4 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
There was a problem hiding this comment.
1 issue found across 1 file (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="ai-pdf-chatbot/app/api/chat/route.ts">
<violation number="1" location="ai-pdf-chatbot/app/api/chat/route.ts:155">
P2: Raw database error messages are sent to clients through the stream error payload, which can leak internal details.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
5 tasks
Add GEO metadata: OG tags, JSON-LD, canonical, favicon, robots, sitemap, llms.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Production chat logs showed 14 of 22 assistant messages were "I couldn't find that in the provided documents", including for perfectly reasonable questions ("What position is Stephen Turoscy applying for?" against an uploaded cover letter). Every one of those responses had zero citations — vector search was returning nothing.
Root cause
The
document_chunksembedding index wasivfflatwithlists = 100. ivfflat partitions vectors into clusters and probes only 1 by default; with typical per-user collections of 6-300 chunks spread across 100 clusters, recall collapses to near zero.lists = 100is tuned for ~100k vectors, not template-scale data.Fixes
HNSW index (
migrations/db_init.sql): replaces ivfflat. No cluster count to mistune; 95%+ recall at any collection size. Migration drops the old index by name and creates the new one idempotently. Verified on the production database: a question against the Bitcoin whitepaper that previously returned 0 citations now returns 8 with correct page numbers.Empty-state guidance, NotebookLM-style, three layers:
/chatis disabled with an upload call-to-action (mirrors NotebookLM's add-sources gating), and/api/chatshort-circuits with a deterministic guidance message instead of an LLM round-trip.suggested_questionsas concrete next steps. No LLM call.RAG_SYSTEM_PROMPTnow forbids the bare refusal; the model must say what the sources DO cover and suggest one concrete question. This is one step friendlier than NotebookLM, which hard-refuses with "NotebookLM can't answer this question".Already verified in production
The HNSW index swap and all three guidance paths are deployed to https://aipdfchat.insforge.site and verified live; this PR brings the template source in line with what's running.
Test plan
npm run builddb_init.sqlapplies cleanly (drop-if-exists tolerates both old and new index names)🤖 Generated with Claude Code
Summary by cubic
Fixes RAG zero-recall by switching the
document_chunksindex to HNSW and adds clear empty-state guidance in chat and API. Also adds site metadata and assets for better sharing and indexing, and returns a generic error on document lookup failures.Bug Fixes
document_chunksinmigrations/db_init.sql(drops old index; createsdocument_chunks_embedding_hnsw_idxidempotently) to recover recall on small collections./api/chat: on ready-document lookup failure, log the raw error server-side and send a generic stream error instead of showing “no documents” guidance.New Features
/chat./api/chatwhen no ready docs or zero retrieval hits; list document names and up to 3suggested_questions.RAG_SYSTEM_PROMPTto avoid bare refusals for vague inputs and suggest one concrete question.robots.ts,sitemap.ts,app/icon.png,app/opengraph-image.png, andpublic/llms.txt(URLs derive fromNEXT_PUBLIC_BETTER_AUTH_URL).Written for commit 367948d. Summary will update on new commits.