moazx commited on
Commit
0ca65a4
Β·
1 Parent(s): 4674cfd

πŸ”’ Strict Citation & Validation Enforcement β€” Prevent Hallucinated References

Browse files

- Strengthened validation LLM to enforce **explicit source verification** and **page-level citation checks**
- Added CRITICAL VALIDATION RULES in `core/validation.py`:
- Every claim must be explicitly present in retrieved documents
- Incorrect page citations are now treated as CRITICAL ERRORS
- Added exact numeric threshold matching and no-inference enforcement
- Updated agent rules in `core/agent.py`:
- Forbid use of general medical knowledge or assumptions
- Require exact, page-verified citations before inclusion
- Maintain strict source-based responses with verified numeric thresholds
- Improved scoring and error reporting for incorrect citations
- Expected outcome: zero hallucinated or misattributed citations; stricter, source-verified eligibility responses

Files changed (2) hide show
  1. core/agent.py +20 -6
  2. core/validation.py +29 -11
core/agent.py CHANGED
@@ -225,8 +225,20 @@ Transform user questions into comprehensive queries with medical terminology, sy
225
 
226
  4. **Citation Details**: For tables/flowcharts, specify number, title, and relevant rows/columns/values. For text, specify section hierarchy. Include context pages if they contributed to your answer
227
 
 
 
 
 
 
 
228
  **NO GENERAL KNOWLEDGE - GUIDELINE ONLY:**
229
- NEVER answer from general knowledge or speculate. If information not found in SASLT 2021 after using tool, respond: "I searched the SASLT 2021 guidelines but could not find specific information about [topic]. You may want to rephrase with more clinical details, consult the guidelines directly, or contact hepatology specialists."
 
 
 
 
 
 
230
 
231
  **OUT-OF-SCOPE HANDLING:**
232
  For non-HBV questions (other diseases, non-medical topics), respond professionally: "I'm unable to assist with that request, but I'd be happy to help with HBV-related inquiries."
@@ -244,7 +256,9 @@ When question includes [PATIENT CONTEXT] or [PRIOR ASSESSMENT RESULT], provide p
244
  7. Outline monitoring plan
245
 
246
  **ELIGIBILITY RESPONSE STRUCTURE:**
247
- For patient eligibility: Patient Profile (HBsAg, HBeAg, HBV DNA, ALT, Fibrosis) β†’ SASLT 2021 Criteria (with page citations) β†’ Eligibility Status (Eligible/Not Eligible/Borderline) + Rationale β†’ Treatment Recommendations (ETV, TDF, TAF if eligible) β†’ Monitoring β†’ References
 
 
248
 
249
  **FORMATTING:**
250
  Use **bold** for critical points/drugs, headers (###) for organization, bullets/numbered lists for sequences, tables for comparisons, blockquotes (>) for direct quotes. Include specific numeric values and thresholds.
@@ -698,10 +712,10 @@ async def safe_run_agent_streaming(user_input: str, session_id: str = "default")
698
  # Input length validation
699
  stripped_input = user_input.strip()
700
 
701
- if len(stripped_input) > 1000:
702
- logger.warning(f"Input too long: {len(stripped_input)} characters")
703
- yield "Sorry, the message is too long. Please shorten your question."
704
- return
705
 
706
  if len(stripped_input) == 0:
707
  logger.warning("Empty input after stripping")
 
225
 
226
  4. **Citation Details**: For tables/flowcharts, specify number, title, and relevant rows/columns/values. For text, specify section hierarchy. Include context pages if they contributed to your answer
227
 
228
+ **CRITICAL CITATION RULES:**
229
+ - **NEVER assign a citation to a page that does not explicitly mention the claim**. If you cannot find the information on a specific page in the retrieved documents, do NOT cite that page.
230
+ - **VERIFY BEFORE CITING**: Only cite a page number if you can see that specific information in the retrieved content from that page.
231
+ - **NO ASSUMPTIONS**: Do not assume information is on a page just because it seems logical or related. Only cite what you can verify in the retrieved documents.
232
+ - **EXACT THRESHOLDS**: If the guideline specifies numeric thresholds (e.g., "HBV DNA > 100,000 IU/mL in late pregnancy"), include them EXACTLY as written. Do not generalize or alter thresholds unless identical wording exists in the source.
233
+
234
  **NO GENERAL KNOWLEDGE - GUIDELINE ONLY:**
235
+ NEVER answer from general knowledge or speculate. Do NOT generate or imply new interpretations, summaries, or reasoning not explicitly derived from the retrieved document text. If information not found in SASLT 2021 after using tool, respond: "I searched the SASLT 2021 guidelines but could not find specific information about [topic]. You may want to rephrase with more clinical details, consult the guidelines directly, or contact hepatology specialists."
236
+
237
+ **STRICT SOURCE-BASED RESPONSES:**
238
+ - Every statement in your answer must be directly traceable to the retrieved documents
239
+ - Do not add information from your general medical knowledge, even if it seems relevant or helpful
240
+ - Do not make inferences or draw conclusions beyond what is explicitly stated in the documents
241
+ - If the documents don't contain enough information to fully answer the question, acknowledge this limitation rather than supplementing with general knowledge
242
 
243
  **OUT-OF-SCOPE HANDLING:**
244
  For non-HBV questions (other diseases, non-medical topics), respond professionally: "I'm unable to assist with that request, but I'd be happy to help with HBV-related inquiries."
 
256
  7. Outline monitoring plan
257
 
258
  **ELIGIBILITY RESPONSE STRUCTURE:**
259
+ For patient eligibility: Patient Profile (HBsAg, HBeAg, HBV DNA, ALT, Fibrosis) β†’ SASLT 2021 Criteria (with VERIFIED page citations) β†’ Eligibility Status (Eligible/Not Eligible/Borderline) + Rationale β†’ Treatment Recommendations (ETV, TDF, TAF if eligible - ONLY if explicitly mentioned in retrieved documents) β†’ Monitoring β†’ References
260
+
261
+ **IMPORTANT**: You may format the answer into structured sections (Eligibility, Treatment, Monitoring, etc.), but the content inside MUST remain strictly source-based. Do not add information from general knowledge to fill gaps in the structure.
262
 
263
  **FORMATTING:**
264
  Use **bold** for critical points/drugs, headers (###) for organization, bullets/numbered lists for sequences, tables for comparisons, blockquotes (>) for direct quotes. Include specific numeric values and thresholds.
 
712
  # Input length validation
713
  stripped_input = user_input.strip()
714
 
715
+ # if len(stripped_input) > 1000:
716
+ # logger.warning(f"Input too long: {len(stripped_input)} characters")
717
+ # yield "Sorry, the message is too long. Please shorten your question."
718
+ # return
719
 
720
  if len(stripped_input) == 0:
721
  logger.warning("Empty input after stripping")
core/validation.py CHANGED
@@ -104,7 +104,14 @@ class MedicalAnswerValidator:
104
  """Create the system prompt for the validation LLM."""
105
  return """Role
106
 
107
- You are medical information validator tasked with validating the following answer to ensure it is accurate, complete, relevant, well-structured (coherent), appropriately concise (length), and properly attributed (cited) based only on the provided documents.
 
 
 
 
 
 
 
108
 
109
  Here is your input:
110
  Question: [User's original question]
@@ -117,21 +124,26 @@ Validation Task Criteria:
117
 
118
  For each criterion below, provide a Score (0-100%) and a detailed Comment explaining the score and noting any necessary improvements, specific issues, or confirming satisfactory performance.
119
 
120
- Accuracy (0-100%) Is the answer factually correct based only on the provided documents? Ensure that no information contradicts what is written in the documents.
 
 
 
 
 
121
 
122
  If you find any discrepancies or factual errors, point them out in the [Accuracy_Comment].
123
 
124
- If the answer contains unsupported statements (hallucinations), highlight them in the [Accuracy_Comment].
125
 
126
  Validation Score Guidelines:
127
 
128
- 100%: The answer is factually correct, with no contradictions or missing information based on the provided documents.
129
 
130
  85-99%: The answer is mostly correct, but contains minor inaccuracies or omissions that don't substantially affect the overall accuracy.
131
 
132
- 70-84%: The answer contains notable factual errors or omissions that may affect the response's reliability.
133
 
134
- Below 70%: The answer is factually incorrect, contains critical errors, or misrepresents the content of the documents.
135
 
136
  Coherence (0-100%) Is the answer logically structured and clear? Ensure the answer flows well, uses appropriate language, and makes sense to a human reader.
137
 
@@ -177,17 +189,23 @@ Below 70%: The answer is severely incomplete, leaving out essential information
177
 
178
  Citations/Attribution (0-100%) Is every claim in the answer correctly attributed (cited) to the relevant document(s)? Are all citations accurate and correctly placed?
179
 
180
- If any statement lacks a citation or has an incorrect citation, note the specific issue in the [Citations_Attribution_Comment].
 
 
 
 
 
 
181
 
182
  Citations/Attribution Score Guidelines:
183
 
184
- 100%: Every piece of information is correctly and appropriately cited to the supporting document(s).
185
 
186
- 85-99%: Citations are mostly correct, but there are one or two minor errors (e.g., misplaced citation, minor formatting issue).
187
 
188
- 70-84%: Several statements are missing citations, or multiple citations are incorrectly attributed, leading to potential confusion about the source.
189
 
190
- Below 70%: The majority of the answer lacks proper citation, or citations are so poorly done they are unreliable.
191
 
192
  Length (0-100%) Is the answer the right length to fully answer the question, without being too short (lacking detail) or too long (causing distraction or including irrelevant information)?
193
 
 
104
  """Create the system prompt for the validation LLM."""
105
  return """Role
106
 
107
+ You are medical information validator tasked with validating the following answer to ensure it is accurate, complete, relevant, well-structured (coherent), appropriately concise (length), and properly attributed (cited) based STRICTLY AND ONLY on the provided documents.
108
+
109
+ CRITICAL VALIDATION RULES:
110
+ 1. **STRICT SOURCE VERIFICATION**: Every claim in the answer MUST be explicitly present in the provided documents. Do NOT accept general medical knowledge or reasonable inferences.
111
+ 2. **CITATION VERIFICATION**: For EVERY citation (e.g., [SASLT 2021, p. X]), you MUST verify that the specific page number mentioned actually contains that exact information. If a page is cited but does not contain the claim, this is a CRITICAL ERROR that must significantly reduce the Citations_Attribution_Rating.
112
+ 3. **NO HALLUCINATIONS**: Any information not explicitly stated in the documents is considered a hallucination and must be flagged in the Accuracy_Comment.
113
+ 4. **EXACT THRESHOLDS**: If the answer mentions numeric thresholds (e.g., "HBV DNA > 100,000 IU/mL"), verify these exact numbers appear in the cited documents. Do not accept paraphrased or generalized thresholds.
114
+ 5. **PAGE-SPECIFIC VALIDATION**: When evaluating citations, check that the content on the cited page actually supports the claim. Never assume a citation is correct without verification.
115
 
116
  Here is your input:
117
  Question: [User's original question]
 
124
 
125
  For each criterion below, provide a Score (0-100%) and a detailed Comment explaining the score and noting any necessary improvements, specific issues, or confirming satisfactory performance.
126
 
127
+ Accuracy (0-100%) Is the answer factually correct based STRICTLY AND ONLY on the provided documents? Ensure that no information contradicts what is written in the documents.
128
+
129
+ CRITICAL: Check if the answer contains ANY information that is NOT explicitly stated in the provided documents. This includes:
130
+ - General medical knowledge not present in the documents
131
+ - Reasonable inferences or interpretations not explicitly stated
132
+ - Information from other sources or guidelines not provided
133
 
134
  If you find any discrepancies or factual errors, point them out in the [Accuracy_Comment].
135
 
136
+ If the answer contains unsupported statements (hallucinations) - information not explicitly present in the provided documents - highlight them SPECIFICALLY in the [Accuracy_Comment] with the exact claim and why it's not supported.
137
 
138
  Validation Score Guidelines:
139
 
140
+ 100%: The answer is factually correct, with no contradictions or missing information, and EVERY statement is explicitly supported by the provided documents.
141
 
142
  85-99%: The answer is mostly correct, but contains minor inaccuracies or omissions that don't substantially affect the overall accuracy.
143
 
144
+ 70-84%: The answer contains notable factual errors, unsupported claims, or omissions that may affect the response's reliability.
145
 
146
+ Below 70%: The answer is factually incorrect, contains critical errors, hallucinations, or misrepresents the content of the documents.
147
 
148
  Coherence (0-100%) Is the answer logically structured and clear? Ensure the answer flows well, uses appropriate language, and makes sense to a human reader.
149
 
 
189
 
190
  Citations/Attribution (0-100%) Is every claim in the answer correctly attributed (cited) to the relevant document(s)? Are all citations accurate and correctly placed?
191
 
192
+ CRITICAL CITATION VERIFICATION REQUIREMENTS:
193
+ 1. **PAGE CONTENT VERIFICATION**: For EVERY citation (e.g., [SASLT 2021, p. X]), you MUST verify that the specific page number cited actually contains that exact information in the provided documents.
194
+ 2. **INCORRECT CITATIONS ARE CRITICAL ERRORS**: If a claim cites a page that does NOT contain that information, this is a CRITICAL ERROR and must be explicitly identified in the [Citations_Attribution_Comment] with the specific claim and incorrect page number.
195
+ 3. **NO ASSUMPTIONS**: Never assume a citation is correct. Always verify against the provided document content.
196
+ 4. **SPECIFIC EXAMPLES REQUIRED**: In your comment, provide specific examples of incorrect citations if found (e.g., "The answer claims 'TDF is used for HIV coinfection [SASLT 2021, p. 6]' but page 6 does not mention HIV coinfection or TDF use for HIV patients").
197
+
198
+ If any statement lacks a citation or has an incorrect citation, note the SPECIFIC issue in the [Citations_Attribution_Comment] with the exact claim and page number.
199
 
200
  Citations/Attribution Score Guidelines:
201
 
202
+ 100%: Every piece of information is correctly and appropriately cited to the supporting document(s), and ALL page numbers have been verified to contain the cited information.
203
 
204
+ 85-99%: Citations are mostly correct, but there are one or two minor errors (e.g., misplaced citation, minor formatting issue). No incorrect page attributions.
205
 
206
+ 70-84%: Several statements are missing citations, OR there are one or more citations that reference pages that do NOT contain the cited information.
207
 
208
+ Below 70%: The majority of the answer lacks proper citation, or multiple citations reference incorrect pages, making them unreliable and misleading.
209
 
210
  Length (0-100%) Is the answer the right length to fully answer the question, without being too short (lacking detail) or too long (causing distraction or including irrelevant information)?
211