Skip to content

feat: score threshold filtering, .txt support, API key trimming#2

Open
biswajeetdev wants to merge 1 commit into
mainfrom
feat/score-threshold-txt-support
Open

feat: score threshold filtering, .txt support, API key trimming#2
biswajeetdev wants to merge 1 commit into
mainfrom
feat/score-threshold-txt-support

Conversation

@biswajeetdev
Copy link
Copy Markdown
Owner

What

Three quality-of-life fixes found while doing a thorough test pass:

Fix 1 — Low-relevance chunks passed to LLM (rag.py)

retrieve() returned all TOP_K=4 chunks regardless of their cosine similarity score. When a user asks something not covered by the document, FAISS still returns the closest matches even if their scores are 0.05–0.12 — far below useful similarity. The LLM then generates a hallucinated answer from those irrelevant chunks instead of saying "not in the document."

Added MIN_SCORE = 0.20 floor. If all retrieved chunks are below threshold, retrieve() returns [], and generate_answer already handles empty retrieval gracefully with:

"No relevant content found in the document for that question."

Fix 2 — .txt files not supported (app.py)

The uploader accepted only type="pdf". Plain text files (notes, logs, transcripts, Markdown exports) are a primary use case for a doc QA tool. Added type=["pdf", "txt"] and a branch for UTF-8 decoding via .read().decode() instead of PdfReader.

Fix 3 — Whitespace in pasted API keys causes silent failure (app.py)

Keys copied from browser often include a trailing newline or space. make_chat_client(api_key) creates a client with the whitespace-padded key, which fails on first API call with a cryptic 401. Added .strip() immediately after each st.text_input.

Test

# Install deps
pip install -r requirements.txt

# Run
streamlit run app.py

# Test low-relevance filtering:
# Upload any PDF, ask a completely unrelated question
# → should get "No relevant content found..." not a hallucinated answer

# Test .txt upload:
# Upload any .txt file, ask questions about its content

# Test key trimming:
# Paste a Groq key with leading/trailing spaces — should still authenticate

- retrieve() now filters out chunks below MIN_SCORE=0.20 cosine similarity;
  without this the LLM receives irrelevant chunks when the document doesn't
  contain an answer, causing hallucinated or misleading responses
- file uploader now accepts .txt files in addition to PDF; plain text is
  decoded directly instead of going through PdfReader
- API key inputs now call .strip() before use — whitespace-padded keys from
  copy-paste were silently creating invalid clients
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant