diff --git a/pkg/api/large_language_models.go b/pkg/api/large_language_models.go index 69f8d0f9..ca27dfc0 100644 --- a/pkg/api/large_language_models.go +++ b/pkg/api/large_language_models.go @@ -219,6 +219,10 @@ func (a *LargeLanguageModelsApi) RecognizeReceiptImageHandler(c *core.WebContext return nil, errs.Or(err, errs.ErrOperationFailed) } + if llmResponse == nil || len(llmResponse.Content) == 0 || strings.HasPrefix(llmResponse.Content, "{}") { + return nil, errs.ErrNoTransactionInformationInImage + } + var result *models.RecognizedReceiptImageResult if err := json.Unmarshal([]byte(llmResponse.Content), &result); err != nil { @@ -236,7 +240,7 @@ func (a *LargeLanguageModelsApi) parseRecognizedReceiptImageResponse(c *core.Web if recognizedResult == nil { log.Errorf(c, "[large_language_models.parseRecognizedReceiptImageResponse] recoginzed result is null") - return nil, errs.ErrOperationFailed + return nil, errs.ErrNoTransactionInformationInImage } if recognizedResult.Type == "income" { @@ -269,6 +273,8 @@ func (a *LargeLanguageModelsApi) parseRecognizedReceiptImageResponse(c *core.Web recognizedReceiptImageResponse.CategoryId = category.CategoryId } } + } else if len(recognizedResult.Type) == 0 { + return nil, errs.ErrNoTransactionInformationInImage } else { log.Errorf(c, "[large_language_models.parseRecognizedReceiptImageResponse] recoginzed transaction type \"%s\" is invalid", recognizedResult.Type) return nil, errs.ErrOperationFailed diff --git a/pkg/errs/large_language_model.go b/pkg/errs/large_language_model.go index fff49341..af12c2c6 100644 --- a/pkg/errs/large_language_model.go +++ b/pkg/errs/large_language_model.go @@ -8,4 +8,5 @@ var ( ErrNoAIRecognitionImage = NewNormalError(NormalSubcategoryLargeLanguageModel, 1, http.StatusBadRequest, "no image for AI recognition") ErrAIRecognitionImageIsEmpty = NewNormalError(NormalSubcategoryLargeLanguageModel, 2, http.StatusBadRequest, "image for AI recognition is empty") ErrExceedMaxAIRecognitionImageFileSize = NewNormalError(NormalSubcategoryLargeLanguageModel, 3, http.StatusBadRequest, "exceed the maximum size of image file for AI recognition") + ErrNoTransactionInformationInImage = NewNormalError(NormalSubcategoryLargeLanguageModel, 4, http.StatusBadRequest, "no transaction information detected") ) diff --git a/src/locales/de.json b/src/locales/de.json index 993a0f37..ebdc5ffb 100644 --- a/src/locales/de.json +++ b/src/locales/de.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "There is no image for AI recognition", "image for AI recognition is empty": "Image for AI recognition file is empty", "exceed the maximum size of image file for AI recognition": "The uploaded image for AI recognition exceeds the maximum allowed file size", + "no transaction information detected": "No transaction information detected", "query items cannot be blank": "Abfrageelemente dürfen nicht leer sein", "query items too much": "Zu viele Abfrageelemente", "query items have invalid item": "Ungültiges Element in Abfrageelementen", diff --git a/src/locales/en.json b/src/locales/en.json index 86f1d12d..5434cefc 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "There is no image for AI recognition", "image for AI recognition is empty": "Image for AI recognition file is empty", "exceed the maximum size of image file for AI recognition": "The uploaded image for AI recognition exceeds the maximum allowed file size", + "no transaction information detected": "No transaction information detected", "query items cannot be blank": "There are no query items", "query items too much": "There are too many query items", "query items have invalid item": "There is invalid item in query items", diff --git a/src/locales/es.json b/src/locales/es.json index 080f1e4c..68b2057d 100644 --- a/src/locales/es.json +++ b/src/locales/es.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "There is no image for AI recognition", "image for AI recognition is empty": "Image for AI recognition file is empty", "exceed the maximum size of image file for AI recognition": "The uploaded image for AI recognition exceeds the maximum allowed file size", + "no transaction information detected": "No transaction information detected", "query items cannot be blank": "--", "query items too much": "--", "query items have invalid item": "Hay un elemento no válido en los elementos de consulta", diff --git a/src/locales/it.json b/src/locales/it.json index 2c259d33..a593afe3 100644 --- a/src/locales/it.json +++ b/src/locales/it.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "There is no image for AI recognition", "image for AI recognition is empty": "Image for AI recognition file is empty", "exceed the maximum size of image file for AI recognition": "The uploaded image for AI recognition exceeds the maximum allowed file size", + "no transaction information detected": "No transaction information detected", "query items cannot be blank": "Non ci sono elementi di query", "query items too much": "Ci sono troppi elementi di query", "query items have invalid item": "C'è un elemento non valido negli elementi di query", diff --git a/src/locales/ja.json b/src/locales/ja.json index 2de4f368..cbcd05b1 100644 --- a/src/locales/ja.json +++ b/src/locales/ja.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "There is no image for AI recognition", "image for AI recognition is empty": "Image for AI recognition file is empty", "exceed the maximum size of image file for AI recognition": "The uploaded image for AI recognition exceeds the maximum allowed file size", + "no transaction information detected": "No transaction information detected", "query items cannot be blank": "クエリ項目がありません", "query items too much": "クエリ項目が多すぎます", "query items have invalid item": "クエリ項目に無効な項目があります", diff --git a/src/locales/nl.json b/src/locales/nl.json index 273a0a7c..bf889267 100644 --- a/src/locales/nl.json +++ b/src/locales/nl.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "There is no image for AI recognition", "image for AI recognition is empty": "Image for AI recognition file is empty", "exceed the maximum size of image file for AI recognition": "The uploaded image for AI recognition exceeds the maximum allowed file size", + "no transaction information detected": "No transaction information detected", "query items cannot be blank": "Geen zoekitems opgegeven", "query items too much": "Te veel zoekitems", "query items have invalid item": "Ongeldig item in zoekitems", diff --git a/src/locales/pt_BR.json b/src/locales/pt_BR.json index 032f7f29..a80c4096 100644 --- a/src/locales/pt_BR.json +++ b/src/locales/pt_BR.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "There is no image for AI recognition", "image for AI recognition is empty": "Image for AI recognition file is empty", "exceed the maximum size of image file for AI recognition": "The uploaded image for AI recognition exceeds the maximum allowed file size", + "no transaction information detected": "No transaction information detected", "query items cannot be blank": "Não há itens de consulta", "query items too much": "Há muitos itens de consulta", "query items have invalid item": "Há item inválido nos itens de consulta", diff --git a/src/locales/ru.json b/src/locales/ru.json index 53b90a9c..d7c5d3c8 100644 --- a/src/locales/ru.json +++ b/src/locales/ru.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "There is no image for AI recognition", "image for AI recognition is empty": "Image for AI recognition file is empty", "exceed the maximum size of image file for AI recognition": "The uploaded image for AI recognition exceeds the maximum allowed file size", + "no transaction information detected": "No transaction information detected", "query items cannot be blank": "Нет элементов запроса", "query items too much": "Слишком много элементов запроса", "query items have invalid item": "В элементах запроса присутствует недопустимый элемент", diff --git a/src/locales/uk.json b/src/locales/uk.json index b81ba869..a935a6fc 100644 --- a/src/locales/uk.json +++ b/src/locales/uk.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "There is no image for AI recognition", "image for AI recognition is empty": "Image for AI recognition file is empty", "exceed the maximum size of image file for AI recognition": "The uploaded image for AI recognition exceeds the maximum allowed file size", + "no transaction information detected": "No transaction information detected", "query items cannot be blank": "Елементи запиту не можуть бути порожніми", "query items too much": "Занадто багато елементів запиту", "query items have invalid item": "Запит містить недійсний елемент", diff --git a/src/locales/vi.json b/src/locales/vi.json index 0f2901fa..baaaf183 100644 --- a/src/locales/vi.json +++ b/src/locales/vi.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "There is no image for AI recognition", "image for AI recognition is empty": "Image for AI recognition file is empty", "exceed the maximum size of image file for AI recognition": "The uploaded image for AI recognition exceeds the maximum allowed file size", + "no transaction information detected": "No transaction information detected", "query items cannot be blank": "Không có mục truy vấn", "query items too much": "Có quá nhiều mục truy vấn", "query items have invalid item": "Có mục không hợp lệ trong các mục truy vấn", diff --git a/src/locales/zh_Hans.json b/src/locales/zh_Hans.json index bbca4ddb..f2d0c93b 100644 --- a/src/locales/zh_Hans.json +++ b/src/locales/zh_Hans.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "没有用于AI识别的图片", "image for AI recognition is empty": "用于AI识别的图片为空", "exceed the maximum size of image file for AI recognition": "用于AI识别的图片超出了允许的最大文件大小", + "no transaction information detected": "没有检测到交易信息", "query items cannot be blank": "请求项目不能为空", "query items too much": "请求项目过多", "query items have invalid item": "请求项目中有非法项目", diff --git a/src/locales/zh_Hant.json b/src/locales/zh_Hant.json index 3f811d24..7795201d 100644 --- a/src/locales/zh_Hant.json +++ b/src/locales/zh_Hant.json @@ -1228,6 +1228,7 @@ "no image for AI recognition": "沒有用於AI識別的圖片檔案", "image for AI recognition is empty": "用於AI識別的圖片檔案為空", "exceed the maximum size of image file for AI recognition": "用於AI識別的圖片超出了允許的最大檔案大小", + "no transaction information detected": "沒有檢測到交易資訊", "query items cannot be blank": "查詢項目不能為空", "query items too much": "查詢項目過多", "query items have invalid item": "查詢項目中有非法項目", diff --git a/templates/prompt/receipt_image_recognition.tmpl b/templates/prompt/receipt_image_recognition.tmpl index 46165c54..1b6804e3 100644 --- a/templates/prompt/receipt_image_recognition.tmpl +++ b/templates/prompt/receipt_image_recognition.tmpl @@ -25,7 +25,8 @@ Your task is to extract structured transaction data from images provided by the 1. Only include fields you can confidently identify. 2. If unsure about a value, omit the field (do not guess). 3. If the image contains multiple items, please combine them into a single transaction. -4. Always return valid JSON. +4. If the image contains no transaction information, simply return an empty JSON object. +5. Always return valid JSON. ## Options ### Expense categories: