From 81d04d7a9d340f91b0489a779fdcb491f56465f8 Mon Sep 17 00:00:00 2001 From: Adam Refaey Date: Tue, 29 Apr 2025 16:44:30 +0300 Subject: [PATCH] fix: enhance medical analysis instructions and enforce metadata integrity --- .../document-processor/services/aws-bedrock.service.ts | 4 +++- backend/src/services/perplexity.service.ts | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/backend/src/document-processor/services/aws-bedrock.service.ts b/backend/src/document-processor/services/aws-bedrock.service.ts index bac57fa..6d8dd08 100644 --- a/backend/src/document-processor/services/aws-bedrock.service.ts +++ b/backend/src/document-processor/services/aws-bedrock.service.ts @@ -72,7 +72,8 @@ For lab values: - Set "isCritical" to true for urgent medical situations - Provide brief "conclusion" about what the value means for health - Add brief "suggestions" based on the value -- If reference ranges are missing, add "reference-ranges-missing" to missingInformation and use standard ranges +- IMPORTANT: For ANY lab value where reference/normal ranges are not explicitly stated in the document, you MUST add "reference-ranges-missing" to the missingInformation array and use standard medical ranges +- If you use standard ranges because the document lacks them, clearly mark this in your response CRITICAL FORMATTING RULES: - Begin immediately with { and end with } @@ -87,6 +88,7 @@ Common errors to avoid: - Starting with "This appears to be a medical report..." - Creating nested JSON structures - Placing data inside definition fields +- IMPORTANT: Failing to add "reference-ranges-missing" to missingInformation when ANY lab value lacks explicit ranges Document text: `; diff --git a/backend/src/services/perplexity.service.ts b/backend/src/services/perplexity.service.ts index 38c283c..0d3b292 100644 --- a/backend/src/services/perplexity.service.ts +++ b/backend/src/services/perplexity.service.ts @@ -2,6 +2,7 @@ import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import axios from 'axios'; import { AwsSecretsService } from './aws-secrets.service'; +import { MedicalDocumentAnalysis } from 'src/document-processor/services/aws-bedrock.service'; export interface PerplexityMessage { role: 'system' | 'user' | 'assistant'; @@ -275,11 +276,14 @@ export class PerplexityService { * @param originalText The original text of the medical document * @returns The corrected medical document analysis */ - async reviewMedicalAnalysis(analysis: any, originalText: string): Promise { + async reviewMedicalAnalysis( + analysis: MedicalDocumentAnalysis, + originalText: string, + ): Promise { this.logger.log('Reviewing medical document analysis with Perplexity'); const systemPrompt = - 'Medical information verification specialist. Verify analysis against trusted sources (Mayo Clinic, Cleveland Clinic, CDC, NIH, WHO, medical journals). Ensure accuracy of lab ranges, interpretations, and recommendations. Return only corrected JSON.'; + 'Medical information verification specialist. Verify analysis against trusted sources (Mayo Clinic, Cleveland Clinic, CDC, NIH, WHO, medical journals). Ensure accuracy of lab ranges, interpretations, and recommendations. Return only corrected JSON. IMPORTANT: Do not modify the metadata object, especially preserve the metadata.missingInformation array exactly as provided.'; const analysisJson = JSON.stringify(analysis, null, 2); @@ -289,6 +293,7 @@ export class PerplexityService { `2. Interpretations of abnormal values\n` + `3. Medical conclusions and recommendations\n` + `4. Lab value categorizations\n\n` + + `CRITICAL INSTRUCTION: Do NOT modify the metadata object. The metadata.missingInformation array must remain exactly as provided without any additions, removals, or changes.\n\n` + `Analysis JSON:\n${analysisJson}\n\n` + `Original Text:\n${originalText}\n\n` + `Return ONLY corrected JSON with identical structure. No preamble, explanation, or text before/after JSON.`;