Skip to content

Commit e8aaf1a

Browse files
committed
Refactor AwsBedrockService medical analysis prompt and enhance PerplexityService prompts for clarity
- Updated the medical analysis prompt in AwsBedrockService to streamline instructions and focus on key extraction points. - Improved the system and user prompts in PerplexityService for reviewing medical document analyses, emphasizing accuracy and clarity in the verification process.
1 parent 618d4d6 commit e8aaf1a

File tree

3 files changed

+75
-128
lines changed

3 files changed

+75
-128
lines changed

backend/src/document-processor/services/aws-bedrock.service.ts

Lines changed: 30 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,17 @@ export class AwsBedrockService {
4545
private readonly inferenceProfileArn?: string;
4646

4747
// Medical document analysis prompt
48-
private readonly medicalAnalysisPrompt = `Please analyze this medical document carefully, with specific attention to medical lab reports.
49-
50-
Look for and extract the following information:
51-
1. Document title or main subject based on content
52-
2. Document category based on organ system focus
53-
3. Lab test values with their normal ranges and whether they are normal, high, or low (particularly important for blood work, metabolic panels, etc.)
54-
4. Any diagnoses, findings, or medical observations with details and recommendations
55-
5. Analyze if this is a medical document (lab report, test result, medical chart, prescription, etc.) and provide confidence level
56-
57-
This document may be a lab report showing blood work or other test results, so please pay special attention to tables, numeric values, reference ranges, and medical terminology.
58-
59-
IMPORTANT: Base your analysis on information from multiple trusted medical sources and authorities, including but not limited to:
60-
- Mayo Clinic
61-
- Cleveland Clinic
62-
- CDC (Centers for Disease Control and Prevention)
63-
- NIH (National Institutes of Health)
64-
- WHO (World Health Organization)
65-
- American Medical Association
66-
- American Heart Association
67-
- American Academy of Pediatrics
68-
- UpToDate
69-
- MedlinePlus
70-
71-
Format the response as a JSON object with the following structure:
48+
private readonly medicalAnalysisPrompt = `Analyze this medical document with focus on lab reports. Extract:
49+
50+
1. Title/subject from content
51+
2. Category: "heart" (cardiac focus), "brain" (neurological focus), or "general" (all else)
52+
3. Lab values with ranges and status (normal/high/low)
53+
4. Diagnoses, findings, and recommendations
54+
5. Medical document verification with confidence level
55+
56+
Reference trusted sources: Mayo Clinic, Cleveland Clinic, CDC, NIH, WHO, AMA, etc.
57+
58+
Return ONLY a JSON object with this structure:
7259
{
7360
"title": string,
7461
"category": string,
@@ -81,89 +68,25 @@ Format the response as a JSON object with the following structure:
8168
}
8269
}
8370
84-
For the title field, create a concise title that summarizes what the document is about (e.g., "Complete Blood Count Results", "Liver Function Test", "MRI Report").
85-
For the category field, you MUST choose exactly one of these three values:
86-
- "heart" - if the document focuses primarily on cardiac/cardiovascular issues or tests
87-
- "brain" - if the document focuses primarily on neurological issues or brain-related tests
88-
- "general" - for all other medical documents, or when the focus spans multiple systems
89-
90-
Set isMedicalReport to true if you see ANY medical content such as lab values, medical terminology, doctor's notes, or prescription information.
91-
Set confidence between 0 and 1 based on document clarity and how confident you are about the medical nature of the document.
92-
93-
94-
This is extremely important: If you see ANY lab values, numbers with units, or medical terminology, please consider this a medical document even if you're not 100% certain.
95-
96-
When extracting lab values:
97-
1. Look for tables with numeric values and reference ranges
98-
2. Include any values even if you're not sure of the meaning
99-
3. For each lab value, use "status" field with values "normal", "high", or "low" based on whether the value falls within, above, or below the normal range
100-
4. Set "isCritical" to true when the value indicates an urgent medical situation. Set it to false for values that are normal or only slightly abnormal.
101-
5. Include a "conclusion" field that provides a brief interpretation of what this value indicates about the patient's health
102-
6. Include a "suggestions" field that provides brief recommendations based on this value
103-
7. IMPORTANT: If reference ranges are missing from the document, add "reference-ranges-missing" to the missingInformation array in metadata, and use standard reference ranges from trusted medical sources to determine the status.
104-
105-
EXTREMELY IMPORTANT FORMATTING INSTRUCTIONS:
106-
1. ABSOLUTELY DO NOT START YOUR RESPONSE WITH ANY TEXT. Begin immediately with the JSON object.
107-
2. Return ONLY the JSON object without any introduction, explanation, or text like "This appears to be a medical report..."
108-
3. Do NOT include phrases like "Here is the information" or "formatted in the requested JSON structure"
109-
4. Do NOT write any text before the opening brace { or after the closing brace }
110-
5. Do NOT wrap the JSON in code blocks or add comments
111-
6. Do NOT nest JSON inside other JSON fields
112-
7. Start your response with the opening brace { and end with the closing brace }
113-
8. CRITICAL: Do NOT place JSON data inside a definition field or any other field. Return only the direct JSON format requested.
114-
9. Do NOT put explanatory text about how you structured the analysis inside the JSON.
115-
10. Always provide empty arrays ([]) rather than null for empty fields.
116-
11. YOU MUST NOT create a "term" called "Here is the information extracted" or similar phrases.
117-
12. NEVER put actual data inside a "definition" field of a medical term.
118-
119-
YOU REPEATEDLY MAKE THESE MISTAKES:
120-
- You create a "term" field with text like "Here is the information extracted"
121-
- You start your response with "This appears to be a medical report..."
122-
- You write "Here is the information extracted in the requested JSON format:" before the JSON
123-
- THESE ARE WRONG and cause our system to fail
124-
125-
INCORRECT RESPONSE FORMATS (DO NOT DO THESE):
126-
127-
1) DO NOT DO THIS - Adding explanatory text before JSON:
128-
"This appears to be a medical report. Here is the information extracted in the requested JSON format:
129-
130-
{
131-
\"category\": \"heart\",
132-
...
133-
}"
134-
135-
2) DO NOT DO THIS - Nested JSON:
136-
{
137-
"labValues": [
138-
{
139-
"name": "Here is the information extracted",
140-
"value": "{\"labValues\": [{\"name\": \"RBC\", \"value\": \"14.2\"}]}"
141-
}
142-
]
143-
}
144-
145-
CORRECT FORMAT (DO THIS):
146-
{
147-
"title": "Complete Blood Count Results",
148-
"category": "heart",
149-
"labValues": [
150-
{
151-
"name": "Hemoglobin",
152-
"value": "14.2",
153-
"unit": "g/dL",
154-
"normalRange": "13.5-17.5",
155-
"status": "normal",
156-
"isCritical": false,
157-
"conclusion": "Normal hemoglobin levels indicate adequate oxygen-carrying capacity.",
158-
"suggestions": "Continue regular health maintenance."
159-
}
160-
],
161-
"diagnoses": [...],
162-
"metadata": {...}
163-
}
164-
165-
If any information is not visible or unclear in the document, list those items in the missingInformation array.
166-
Ensure all visible medical terms are explained in plain language. Mark lab values as abnormal if they fall outside the normal range.
71+
For lab values:
72+
- Set "isCritical" to true for urgent medical situations
73+
- Provide brief "conclusion" about what the value means for health
74+
- Add brief "suggestions" based on the value
75+
- If reference ranges are missing, add "reference-ranges-missing" to missingInformation and use standard ranges
76+
77+
CRITICAL FORMATTING RULES:
78+
- Begin immediately with { and end with }
79+
- No text before/after the JSON
80+
- No introduction, explanations, code blocks, or comments
81+
- No nested JSON or definition fields
82+
- Empty arrays ([]) for null fields
83+
- No "term" fields with phrases like "Here is the information extracted"
84+
85+
Common errors to avoid:
86+
- Adding explanatory text before JSON
87+
- Starting with "This appears to be a medical report..."
88+
- Creating nested JSON structures
89+
- Placing data inside definition fields
16790
16891
Document text:
16992
`;

backend/src/reports/reports.service.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,21 +373,51 @@ export class ReportsService {
373373
}
374374

375375
try {
376+
// First check if the report exists and belongs to the user
377+
await this.findOne(report.id, report.userId);
378+
379+
// Set the updatedAt timestamp
380+
report.updatedAt = new Date().toISOString();
381+
376382
// Update report in DynamoDB
377-
const command = new PutItemCommand({
383+
const command = new UpdateItemCommand({
378384
TableName: this.tableName,
379-
Item: marshall(report),
380-
ConditionExpression: 'userId = :userId',
385+
Key: marshall({
386+
userId: report.userId, // Partition key
387+
id: report.id, // Sort key
388+
}),
389+
UpdateExpression:
390+
'SET title = :title, bookmarked = :bookmarked, category = :category, ' +
391+
'processingStatus = :processingStatus, labValues = :labValues, summary = :summary, ' +
392+
'confidence = :confidence, status = :status, updatedAt = :updatedAt',
393+
ConditionExpression: 'userId = :userId', // Ensure the report belongs to the user
381394
ExpressionAttributeValues: marshall({
395+
':title': report.title,
396+
':bookmarked': report.bookmarked,
397+
':category': report.category,
398+
':processingStatus': report.processingStatus,
399+
':labValues': report.labValues,
400+
':summary': report.summary,
401+
':confidence': report.confidence,
402+
':status': report.status,
403+
':updatedAt': report.updatedAt,
382404
':userId': report.userId,
383405
}),
406+
ReturnValues: 'ALL_NEW',
384407
});
385408

386-
await this.dynamoClient.send(command);
387-
this.logger.log(`Successfully updated report with ID ${report.id}`);
409+
const response = await this.dynamoClient.send(command);
388410

389-
return report;
411+
if (!response.Attributes) {
412+
return report; // Return the updated report if no Attributes returned
413+
}
414+
415+
return unmarshall(response.Attributes) as Report;
390416
} catch (error: unknown) {
417+
if (error instanceof NotFoundException) {
418+
throw error;
419+
}
420+
391421
this.logger.error(`Error updating report with ID ${report.id}:`);
392422
this.logger.error(error);
393423

backend/src/services/perplexity.service.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,25 +174,19 @@ export class PerplexityService {
174174
this.logger.log('Reviewing medical document analysis with Perplexity');
175175

176176
const systemPrompt =
177-
'You are an AI assistant specializing in medical information verification.\n' +
178-
'Your task is to review a medical document analysis and verify it against trusted medical sources.\n' +
179-
'You must ensure all information is accurate, especially lab value reference ranges and interpretations.\n' +
180-
'Use authoritative medical sources like Mayo Clinic, Cleveland Clinic, CDC, NIH, WHO, and medical journals.\n';
177+
'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.';
181178

182179
const analysisJson = JSON.stringify(analysis, null, 2);
183180

184181
const userPrompt =
185-
`Please review the following medical document analysis for accuracy and completeness. ` +
186-
`Check if the lab value reference ranges, interpretations, and recommendations align with trusted medical sources. ` +
187-
`Focus on these key aspects:\n` +
188-
`1. Verify lab value reference ranges\n` +
189-
`2. Confirm interpretations of abnormal values\n` +
190-
`3. Validate medical conclusions and recommendations\n` +
191-
`4. Ensure all lab values are correctly categorized\n\n` +
192-
`If you find any discrepancies, provide corrections in your response by returning the corrected JSON directly.\n\n` +
193-
`Medical Document Analysis:\n${analysisJson}\n\n` +
194-
`Original Medical Document Text:\n${originalText}\n\n` +
195-
`Return the corrected JSON analysis with the same structure, no preamble or explanation needed.`;
182+
`Review this medical analysis for accuracy. Verify:\n` +
183+
`1. Lab value reference ranges\n` +
184+
`2. Interpretations of abnormal values\n` +
185+
`3. Medical conclusions and recommendations\n` +
186+
`4. Lab value categorizations\n\n` +
187+
`Analysis JSON:\n${analysisJson}\n\n` +
188+
`Original Text:\n${originalText}\n\n` +
189+
`Return ONLY corrected JSON with identical structure. No preamble, explanation, or text before/after JSON.`;
196190

197191
const messages: PerplexityMessage[] = [
198192
{ role: 'system', content: systemPrompt },

0 commit comments

Comments
 (0)