feat(labs): PDF upload, text extraction, LLM draft pipeline
This commit is contained in:
15
server/src/lib/extract.ts
Normal file
15
server/src/lib/extract.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { ExtractedDraft } from "@helios/shared";
|
||||
import { chatJSON, type LlmDeps } from "./llm";
|
||||
|
||||
const SYSTEM = `You extract structured blood-test results from the raw text of a lab report.
|
||||
Return ONLY a JSON object: {"collectedDate": "YYYY-MM-DD" or null, "labName": string or null, "markers": [{"panel": string or null, "name": string, "value": string, "unit": string or null, "referenceRange": string or null, "flagged": boolean}]}.
|
||||
Rules: copy names, values, units, and reference ranges EXACTLY as printed — do not convert units or round values. "value" is always a string (keep comparators like "<0.3"). Set "flagged" true only when the report marks the result abnormal (H, L, *, bold, out-of-range annotation). Use the specimen collection date, not the report date. Skip commentary, footers, and reference-only rows with no result.`;
|
||||
|
||||
const MAX_CHARS = 40_000;
|
||||
|
||||
export async function extractFromText(deps: LlmDeps, text: string): Promise<ExtractedDraft> {
|
||||
const out = await chatJSON(deps, { system: SYSTEM, user: text.slice(0, MAX_CHARS) });
|
||||
const parsed = ExtractedDraft.safeParse(out);
|
||||
if (!parsed.success) throw new Error("llm_error: draft failed schema validation");
|
||||
return parsed.data;
|
||||
}
|
||||
7
server/src/lib/pdf.ts
Normal file
7
server/src/lib/pdf.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { extractText, getDocumentProxy } from "unpdf";
|
||||
|
||||
export async function pdfToText(data: Uint8Array): Promise<string> {
|
||||
const doc = await getDocumentProxy(data);
|
||||
const { text } = await extractText(doc, { mergePages: true });
|
||||
return text;
|
||||
}
|
||||
Reference in New Issue
Block a user