From ece026bea211a48a55e05c29dafd9a128d56112b Mon Sep 17 00:00:00 2001 From: yogeliu Date: Wed, 31 Dec 2025 17:53:12 +0800 Subject: [PATCH] feat: add new path for recognize --- internal/model/formula/response.go | 7 +++++ internal/service/recognition_service.go | 35 ++++++++++++++++--------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/internal/model/formula/response.go b/internal/model/formula/response.go index 10c6322..bf8fe22 100644 --- a/internal/model/formula/response.go +++ b/internal/model/formula/response.go @@ -22,3 +22,10 @@ type GetFormulaTaskResponse struct { type FormulaRecognitionResponse struct { Result string `json:"result"` } + +// ImageOCRResponse 图片OCR接口返回的响应 +type ImageOCRResponse struct { + Latex string `json:"latex"` // LaTeX 格式内容 + Markdown string `json:"markdown"` // Markdown 格式内容 + MathML string `json:"mathml"` // MathML 格式(无公式时为空) +} diff --git a/internal/service/recognition_service.go b/internal/service/recognition_service.go index 7ab6344..be755be 100644 --- a/internal/service/recognition_service.go +++ b/internal/service/recognition_service.go @@ -22,6 +22,7 @@ import ( "gitea.com/bitwsd/document_ai/pkg/constant" "gitea.com/bitwsd/document_ai/pkg/httpclient" "gitea.com/bitwsd/document_ai/pkg/oss" + "gitea.com/bitwsd/document_ai/pkg/requestid" "gitea.com/bitwsd/document_ai/pkg/utils" "gorm.io/gorm" ) @@ -511,8 +512,8 @@ func (s *RecognitionService) processFormulaTask(ctx context.Context, taskID int6 // 设置Content-Type头为application/json headers := map[string]string{"Content-Type": "application/json", utils.RequestIDHeaderKey: utils.GetRequestIDFromContext(ctx)} - // 发送请求时会使用带超时的context - resp, err := s.httpClient.RequestWithRetry(ctx, http.MethodPost, "https://cloud.texpixel.com:10443/vlm/formula/predict", bytes.NewReader(jsonData), headers) + // 发送请求到新的 OCR 接口 + resp, err := s.httpClient.RequestWithRetry(ctx, http.MethodPost, "https://cloud.texpixel.com:10443/doc_process/v1/image/ocr", bytes.NewReader(jsonData), headers) if err != nil { if ctx.Err() == context.DeadlineExceeded { log.Error(ctx, "func", "processFormulaTask", "msg", "请求超时") @@ -532,12 +533,18 @@ func (s *RecognitionService) processFormulaTask(ctx context.Context, taskID int6 log.Info(ctx, "func", "processFormulaTask", "msg", "响应内容", "body", body.String()) // 解析 JSON 响应 - var formulaResp formula.FormulaRecognitionResponse - if err := json.Unmarshal(body.Bytes(), &formulaResp); err != nil { + var ocrResp formula.ImageOCRResponse + if err := json.Unmarshal(body.Bytes(), &ocrResp); err != nil { log.Error(ctx, "func", "processFormulaTask", "msg", "解析响应JSON失败", "error", err) return err } - err = resultDao.Create(tx, dao.RecognitionResult{TaskID: taskID, TaskType: dao.TaskTypeFormula, Latex: formulaResp.Result}) + err = resultDao.Create(tx, dao.RecognitionResult{ + TaskID: taskID, + TaskType: dao.TaskTypeFormula, + Latex: ocrResp.Latex, + Markdown: ocrResp.Markdown, + MathML: ocrResp.MathML, + }) if err != nil { log.Error(ctx, "func", "processFormulaTask", "msg", "保存任务结果失败", "error", err) return err @@ -705,15 +712,19 @@ func (s *RecognitionService) processOneTask(ctx context.Context) { } ctx = context.WithValue(ctx, utils.RequestIDKey, task.TaskUUID) - log.Info(ctx, "func", "processFormulaQueue", "msg", "获取任务成功", "task_id", taskID) - err = s.processBaiduOCRTask(ctx, taskID, task.FileURL) - if err != nil { - log.Error(ctx, "func", "processFormulaQueue", "msg", "处理任务失败", "error", err) - return - } + // 使用 gls 设置 request_id,确保在整个任务处理过程中可用 + requestid.SetRequestID(task.TaskUUID, func() { + log.Info(ctx, "func", "processFormulaQueue", "msg", "获取任务成功", "task_id", taskID) - log.Info(ctx, "func", "processFormulaQueue", "msg", "处理任务成功", "task_id", taskID) + err = s.processFormulaTask(ctx, taskID, task.FileURL) + if err != nil { + log.Error(ctx, "func", "processFormulaQueue", "msg", "处理任务失败", "error", err) + return + } + + log.Info(ctx, "func", "processFormulaQueue", "msg", "处理任务成功", "task_id", taskID) + }) } // processMathpixTask 使用 Mathpix API 处理公式识别任务(用于增强识别)