From 977a8de9f9cb23a6db617ef6ee8b48f9a253c13a Mon Sep 17 00:00:00 2001 From: AJ Date: Sat, 11 Oct 2025 22:35:54 -0400 Subject: [PATCH] Include transaction name in receipt parsing prompt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pass the transaction name from the bank statement to OpenAI when parsing receipts. This helps identify merchants when receipts have cryptic or unclear merchant names (e.g., Arby's receipts). The transaction name is included as additional context in the prompt: "This transaction was recorded as 'XXX' in the bank statement, which may help identify the merchant if the receipt is unclear." 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- MoneyMap/Services/OpenAIReceiptParser.cs | 47 ++++++++++++++---------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/MoneyMap/Services/OpenAIReceiptParser.cs b/MoneyMap/Services/OpenAIReceiptParser.cs index 7214688..f150a1b 100644 --- a/MoneyMap/Services/OpenAIReceiptParser.cs +++ b/MoneyMap/Services/OpenAIReceiptParser.cs @@ -88,8 +88,9 @@ namespace MoneyMap.Services mediaType = receipt.ContentType; } - // Call OpenAI Vision API - var parseData = await CallOpenAIVisionAsync(apiKey, base64Data, mediaType); + // Call OpenAI Vision API with transaction name context + var transactionName = receipt.Transaction?.Name; + var parseData = await CallOpenAIVisionAsync(apiKey, base64Data, mediaType, transactionName); // Update receipt with parsed data receipt.Merchant = parseData.Merchant; @@ -172,22 +173,10 @@ namespace MoneyMap.Services }); } - private async Task CallOpenAIVisionAsync(string apiKey, string base64Image, string mediaType) + private async Task CallOpenAIVisionAsync(string apiKey, string base64Image, string mediaType, string? transactionName = null) { - var requestBody = new - { - model = "gpt-4o-mini", - messages = new[] - { - new - { - role = "user", - content = new object[] - { - new - { - type = "text", - text = @"Analyze this receipt image and extract the following information as JSON: + // Build the prompt with optional transaction context + var promptText = @"Analyze this receipt image and extract the following information as JSON: { ""merchant"": ""store name"", ""receiptDate"": ""YYYY-MM-DD"" (or null if not found), @@ -211,9 +200,29 @@ Extract all line items you can see on the receipt. For each item: - unitPrice: Price per unit if quantity applies, otherwise null - lineTotal: The total amount for this line (required) -For utility bills, service charges, fees, and taxes - these are NOT products with quantities, so set quantity and unitPrice to null. +For utility bills, service charges, fees, and taxes - these are NOT products with quantities, so set quantity and unitPrice to null."; -Respond ONLY with valid JSON, no other text." + if (!string.IsNullOrWhiteSpace(transactionName)) + { + promptText += $"\n\nNote: This transaction was recorded as \"{transactionName}\" in the bank statement, which may help identify the merchant if the receipt is unclear."; + } + + promptText += "\n\nRespond ONLY with valid JSON, no other text."; + + var requestBody = new + { + model = "gpt-4o-mini", + messages = new[] + { + new + { + role = "user", + content = new object[] + { + new + { + type = "text", + text = promptText }, new {