fix(extraction): improve JSON extraction prompts and model options for invoice and bank statement tests
This commit is contained in:
@@ -51,11 +51,21 @@ If there is an image in the document and image caption is not present, add a sma
|
||||
Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>.
|
||||
Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number>.`;
|
||||
|
||||
// JSON extraction prompt for GPT-OSS 20B
|
||||
const JSON_EXTRACTION_PROMPT = `Extract ALL transactions from this bank statement as JSON array. Each transaction: {"date": "YYYY-MM-DD", "counterparty": "NAME", "amount": -25.99}. Amount negative for debits, positive for credits. Only include actual transactions, not balances. Return ONLY JSON array, no explanation.
|
||||
// JSON extraction prompt for GPT-OSS 20B (sent AFTER the statement text is provided)
|
||||
const JSON_EXTRACTION_PROMPT = `Extract ALL transactions from the bank statement. Return ONLY valid JSON array.
|
||||
|
||||
STATEMENT:
|
||||
`;
|
||||
WHERE TO FIND DATA:
|
||||
- Transactions are typically in TABLES with columns: Date, Description/Counterparty, Debit, Credit, Balance
|
||||
- Look for rows with actual money movements, NOT header rows or summary totals
|
||||
|
||||
RULES:
|
||||
1. date: Convert to YYYY-MM-DD format
|
||||
2. counterparty: The name/description of who the money went to/from
|
||||
3. amount: NEGATIVE for debits/withdrawals, POSITIVE for credits/deposits
|
||||
4. Only include actual transactions, NOT opening/closing balances
|
||||
|
||||
JSON array only:
|
||||
[{"date":"YYYY-MM-DD","counterparty":"NAME","amount":-25.99}]`;
|
||||
|
||||
// Constants for smart batching
|
||||
const MAX_VISUAL_TOKENS = 28000; // ~32K context minus prompt/output headroom
|
||||
@@ -246,12 +256,8 @@ async function ensureExtractionModel(): Promise<boolean> {
|
||||
*/
|
||||
async function extractTransactionsFromMarkdown(markdown: string, queryId: string): Promise<ITransaction[]> {
|
||||
const startTime = Date.now();
|
||||
const fullPrompt = JSON_EXTRACTION_PROMPT + markdown;
|
||||
|
||||
// Log exact prompt
|
||||
console.log(`\n [${queryId}] ===== PROMPT =====`);
|
||||
console.log(fullPrompt);
|
||||
console.log(` [${queryId}] ===== END PROMPT (${fullPrompt.length} chars) =====\n`);
|
||||
console.log(` [${queryId}] Statement: ${markdown.length} chars, Prompt: ${JSON_EXTRACTION_PROMPT.length} chars`);
|
||||
|
||||
const response = await fetch(`${OLLAMA_URL}/api/chat`, {
|
||||
method: 'POST',
|
||||
@@ -261,9 +267,15 @@ async function extractTransactionsFromMarkdown(markdown: string, queryId: string
|
||||
messages: [
|
||||
{ role: 'user', content: 'Hi there, how are you?' },
|
||||
{ role: 'assistant', content: 'Good, how can I help you today?' },
|
||||
{ role: 'user', content: fullPrompt },
|
||||
{ role: 'user', content: `Here is a bank statement document:\n\n${markdown}` },
|
||||
{ role: 'assistant', content: 'I have read the bank statement document you provided. I can see all the transaction data. What would you like me to do with it?' },
|
||||
{ role: 'user', content: JSON_EXTRACTION_PROMPT },
|
||||
],
|
||||
stream: true,
|
||||
options: {
|
||||
num_ctx: 32768, // Larger context for long statements + thinking
|
||||
temperature: 0, // Deterministic for JSON extraction
|
||||
},
|
||||
}),
|
||||
signal: AbortSignal.timeout(600000), // 10 minute timeout
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user