improve date time recognition in AI image recognition

This commit is contained in:
MaysWind
2025-09-22 22:15:05 +08:00
parent fc5f8e4633
commit 5a9877588f
2 changed files with 22 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"strings" "strings"
"time"
"github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/errs" "github.com/mayswind/ezbookkeeping/pkg/errs"
@@ -52,6 +53,7 @@ func (a *LargeLanguageModelsApi) RecognizeReceiptImageHandler(c *core.WebContext
return nil, errs.ErrClientTimezoneOffsetInvalid return nil, errs.ErrClientTimezoneOffsetInvalid
} }
timezone := time.FixedZone("Client Timezone", int(utcOffset)*60)
uid := c.GetCurrentUid() uid := c.GetCurrentUid()
user, err := a.users.GetUserById(c, uid) user, err := a.users.GetUserById(c, uid)
@@ -192,6 +194,7 @@ func (a *LargeLanguageModelsApi) RecognizeReceiptImageHandler(c *core.WebContext
} }
systemPromptParams := map[string]any{ systemPromptParams := map[string]any{
"CurrentDateTime": utils.FormatUnixTimeToLongDateTime(time.Now().Unix(), timezone),
"AllExpenseCategoryNames": strings.Join(expenseCategoryNames, "\n"), "AllExpenseCategoryNames": strings.Join(expenseCategoryNames, "\n"),
"AllIncomeCategoryNames": strings.Join(incomeCategoryNames, "\n"), "AllIncomeCategoryNames": strings.Join(incomeCategoryNames, "\n"),
"AllTransferCategoryNames": strings.Join(transferCategoryNames, "\n"), "AllTransferCategoryNames": strings.Join(transferCategoryNames, "\n"),
@@ -281,7 +284,8 @@ func (a *LargeLanguageModelsApi) parseRecognizedReceiptImageResponse(c *core.Web
} }
if len(recognizedResult.Time) > 0 { if len(recognizedResult.Time) > 0 {
timestamp, err := utils.ParseFromLongDateTime(recognizedResult.Time, utcOffset) longDateTime := a.getLongDateTime(recognizedResult.Time)
timestamp, err := utils.ParseFromLongDateTime(longDateTime, utcOffset)
if err != nil { if err != nil {
log.Warnf(c, "[large_language_models.parseRecognizedReceiptImageResponse] recoginzed time \"%s\" is invalid", recognizedResult.Time) log.Warnf(c, "[large_language_models.parseRecognizedReceiptImageResponse] recoginzed time \"%s\" is invalid", recognizedResult.Time)
@@ -349,3 +353,19 @@ func (a *LargeLanguageModelsApi) parseRecognizedReceiptImageResponse(c *core.Web
return recognizedReceiptImageResponse, nil return recognizedReceiptImageResponse, nil
} }
func (a *LargeLanguageModelsApi) getLongDateTime(dateTime string) string {
if utils.IsValidLongDateTimeFormat(dateTime) {
return dateTime
}
if utils.IsValidLongDateTimeWithoutSecondFormat(dateTime) {
return dateTime + ":00"
}
if utils.IsValidLongDateFormat(dateTime) {
return dateTime + " 00:00:00"
}
return dateTime
}

View File

@@ -27,6 +27,7 @@ Your task is to extract structured transaction data from images provided by the
3. If the image contains multiple items, please combine them into a single transaction. 3. If the image contains multiple items, please combine them into a single transaction.
4. If the image contains no transaction information, simply return an empty JSON object. 4. If the image contains no transaction information, simply return an empty JSON object.
5. Always return valid JSON. 5. Always return valid JSON.
6. The current time is {{.CurrentDateTime}}.
## Options ## Options
### Expense categories: ### Expense categories: