Build an Agentic RAG System with Document Relevance Grading in C#
Implement a bounded C# RAG loop that retrieves passages, grades relevance and rewrites the query once when the initial evidence is weak.
Architecture
Question → retrieval → relevance grading → optional rewrite → cited answer or abstention
Prerequisites and configuration
Define the retriever around a question, authenticated corpus scope, maximum result count and cancellation token. Give evidence immutable passage IDs and metadata. Grade each passage against the original question, capture its support level and record which part of the question it addresses. Parse a strict structured grade and validate returned IDs.
Implementation
Perform at most two retrieval passes with a shared time budget and token ceiling. Rewrite only when the first pass yields insufficient evidence. Preserve the intent of the original question and use the same scope on both passes. Deduplicate passages by ID and generate an answer only with sufficient support. Confirm citations are in the eligible evidence set.
Implementation example
if sufficient(relevant): return cite(relevant)
return insufficient_evidenceTesting and troubleshooting
Evaluate answerable questions, questions whose first phrasing retrieves the wrong page, and unsupported questions. Include a retrieved chunk containing directions to disclose secrets and ensure it cannot influence control flow. Track first-pass recall, second-pass contribution, abstention accuracy and latency against a one-pass baseline.
Frequently asked question
When should the loop stop early?
Stop after finding sufficient evidence, hitting the deadline, exhausting the retry budget or reaching a condition that calls for abstention.
Comments
Post a Comment