fix(rag): ground answers strictly in the retrieved context - #15
Merged
Conversation
The system prompt was never sent to the model (getSystemPrompt() was unused),
so buildPrompt emitted only "Context/Question/Answer" with no instructions. The
LLM therefore answered off-topic questions from its own knowledge and still
cited the (irrelevant) nearest chunks as sources.
- Embed strict grounding instructions into the prompt that is actually sent:
answer using ONLY the retrieved context, no outside knowledge, and otherwise
return a fixed out-of-scope reply.
- Suppress source references when that out-of-scope reply is returned, so an
off-topic answer carries no misleading citations.
Verified end-to-end: an off-topic question ("chocolate chip cookies") now
returns the refusal with 0 sources, while a Java 25 question still answers
with sources. The WebSocket/UI path shares QueryService, so it is covered too.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
🤖 ThrillhouseBot PR SummaryWhat this PR doesThis PR modifies PromptBuilder to prepend the previously unused SYSTEM_PROMPT (with strict grounding instructions) to the LLM prompt, and updates QueryService to check the answer for the out-of-scope refusal string and suppress extracted source references when it matches. Changes Overview
Changed Files
Risk Assessment
No new issues found in this PR, but the review cannot be approved until CI is confirmed green.
|
| Check | Type | Status | Detail |
|---|---|---|---|
| Build & Test | check-run | ⏳ Pending | - |
| SonarCloud Code Analysis | check-run | ❌ Failed | failure |
Automated review by ThrillhouseBot. Reply with /review to re-run.
|
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
The RAG assistant would answer questions outside the ingested Java 25 documentation using the model's own knowledge, and still attach the nearest (irrelevant) chunks as "sources". Example: asked for a chocolate-chip-cookie recipe, it returned a full recipe citing Java Security Standard Algorithm Names.
Root cause:
PromptBuilder.getSystemPrompt()was never used.buildPrompt()emitted onlyContext / Question / Answerwith no instructions, so the model received zero grounding constraints.Fix
OUT_OF_SCOPE_ANSWER).QueryServicewhen the model returns that out-of-scope reply, so an off-topic answer carries no misleading citations.Verification (end-to-end against the running stack)
The WebSocket/UI path shares
QueryService.processQuery, so the chat UI is covered too.Notes
PromptBuilderandQueryService(2 files).🤖 Generated with Claude Code