-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.js
More file actions
85 lines (70 loc) · 2.52 KB
/
Copy pathextract.js
File metadata and controls
85 lines (70 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { validateExtraction } from "../utils/validation.js";
/**
* Extract specific information from text
* Uses rule-based validation to prevent hallucinations
* @param {LLMEngine} engine - The LLM engine instance
* @param {string} text - Text to extract from
* @param {Object} options - Extraction options
* @returns {Promise<string>}
*/
export async function extract(engine, text, options = {}) {
const logger = engine.getLogger();
const {
what = "key information", // What to extract
format = "text", // "text", "json", "list"
fields = [], // Specific fields to extract (for JSON)
validate = true, // Enable rule-based validation
strict = false, // If true, throw error on validation failure
} = options;
// Log prompt construction
const originalPrompt = `Extract ${what} from the following text`;
let prompt = originalPrompt;
if (format === "json") {
if (fields.length > 0) {
prompt += ` in JSON format with these fields: ${fields.join(", ")}`;
} else {
prompt += ` in JSON format`;
}
} else if (format === "list") {
prompt += ` as a list`;
}
prompt += `:\n\n${text}`;
logger.logPromptConstruction('extract', originalPrompt, prompt, options);
// Run LLM extraction
const llmResult = await engine.run(prompt, { temperature: 0.3 });
const rawResult = llmResult.trim();
logger.log('info', 'EXTRACT', 'LLM extraction completed', {
format,
fields,
resultLength: rawResult.length
});
// Apply validation if enabled and format is JSON
if (validate && format === "json") {
logger.log('info', 'VALIDATION', 'Starting rule-based validation');
const validation = validateExtraction(rawResult, text, {
format,
fields,
strict
});
logger.logValidation('extract', text, validation.validated || rawResult, validation.isValid,
validation.error ? new Error(validation.error) : null);
if (!validation.isValid) {
if (strict) {
throw new Error(`Extraction validation failed: ${validation.error}`);
} else {
logger.log('warn', 'VALIDATION', 'Validation failed but continuing (non-strict mode)', {
error: validation.error,
issues: validation.issues
});
// Return raw result with warning
return rawResult;
}
}
// Return validated result
if (validation.validated) {
logger.log('info', 'VALIDATION', 'Validation passed, returning validated data');
return JSON.stringify(validation.validated, null, 2);
}
}
return rawResult;
}