Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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:
`;
Expand Down
9 changes: 7 additions & 2 deletions backend/src/services/perplexity.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<any> {
async reviewMedicalAnalysis(
analysis: MedicalDocumentAnalysis,
originalText: string,
): Promise<any> {
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);

Expand All @@ -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.`;
Expand Down