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 {