Skip to content

fix(rag): ground answers strictly in the retrieved context - #15

Merged
devops-thiago merged 1 commit into
mainfrom
fix/rag-grounding-guardrail
Jul 23, 2026
Merged

fix(rag): ground answers strictly in the retrieved context#15
devops-thiago merged 1 commit into
mainfrom
fix/rag-grounding-guardrail

Conversation

@devops-thiago

Copy link
Copy Markdown
Owner

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 only Context / Question / Answer with no instructions, so the model received zero grounding constraints.

Fix

  • Embed strict grounding instructions into the prompt that is actually sent to the model: answer using only the retrieved context, no outside/prior knowledge, otherwise return a fixed out-of-scope reply (OUT_OF_SCOPE_ANSWER).
  • Suppress source references in QueryService when 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)

Query Answer Sources
"chocolate chip cookies recipe" (off-topic) the fixed refusal sentence 0
"new pattern matching features in Java 25" (on-topic) full, accurate answer 5

The WebSocket/UI path shares QueryService.processQuery, so the chat UI is covered too.

Notes

  • No behavior change for legitimate Java 25 questions (retrieval + threshold unchanged); the guardrail is purely a prompt-level + citation-level constraint.
  • Scope is limited to PromptBuilder and QueryService (2 files).

🤖 Generated with Claude Code

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

Copy link
Copy Markdown

🤖 ThrillhouseBot PR Summary

What this PR does

This 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

  • Files changed: 2
  • Lines added: +29
  • Lines removed: -8

Changed Files

File Change Summary
src/main/java/br/com/arquivolivre/myjavagenie/service/PromptBuilder.java Modified Added strict grounding instructions to SYSTEM_PROMPT and integrated it into buildPrompt output.
src/main/java/br/com/arquivolivre/myjavagenie/service/QueryService.java Modified Added substring check on the answer to suppress source references when the model returns the out-of-scope refusal.

Risk Assessment

Risk Count
🔴 Critical 0
🟠 High 0
🟡 Medium 0
🔵 Low 0

No new issues found in this PR, but the review cannot be approved until CI is confirmed green.

⚠️ CI Checks Status

Some checks are still pending or have failed:

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.

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
70.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@devops-thiago
devops-thiago merged commit 4c08506 into main Jul 23, 2026
9 of 10 checks passed
@devops-thiago
devops-thiago deleted the fix/rag-grounding-guardrail branch July 23, 2026 16:07
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