Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

feat(parser): add MiniCPM-OCR as a PDF parser (#21785)

Caldalisabafb5228658
diff --git a/conf/models/minicpm_ocr.json b/conf/models/minicpm_ocr.json
new file mode 100644
index 0000000000..9ccf9b99db
--- /dev/null
+++ b/conf/models/minicpm_ocr.json
@@ -0,0 +1,19 @@
+{
+ "name": "MiniCPM-OCR",
+ "url": {
+ "default": "https://api.modelbest.cn/v1",
+ "global": "https://api.modelbest.ai/v1"
+ },
+ "url_suffix": {
+ "ocr": "document-parse/jobs"
+ },
+ "class": "minicpm-ocr",
+ "models": [
+ {
+ "name": "MiniCPM-OCR-1.0",
+ "model_types": [
+ "ocr"
+ ]
+ }
+ ]
+}
diff --git a/internal/entity/models/factory.go b/internal/entity/models/factory.go
index 598516793e..08ca9b5c8b 100644
--- a/internal/entity/models/factory.go
+++ b/internal/entity/models/factory.go
@@ -177,6 +177,8 @@ func (f *ModelFactory) CreateModelDriver(providerName string, baseURL map[string
return NewBedrockModel(baseURL, urlSuffix), nil
case "paddleocr.local":
return NewPaddleOCRLocalModel(baseURL, urlSuffix), nil
+ case "minicpm-ocr":
+ return NewMiniCPMOCRModel(baseURL, urlSuffix), nil
case "orcarouter":
return NewOrcaRouterModel(baseURL, urlSuffix), nil
case "huaweicloud":
diff --git a/internal/entity/models/minicpm_ocr.go b/internal/entity/models/minicpm_ocr.go
new file mode 100644
index 0000000000..71b54a0f41
--- /dev/null
+++ b/internal/entity/models/minicpm_ocr.go
@@ -0,0 +1,419 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package models
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "math"
+ "mime/multipart"
+ "net/http"
+ neturl "net/url"
+ "sort"
+ "strings"
+ "time"
+
+ "ragflow/internal/common"
+
+ "go.uber.org/zap"
+)
+
+// MiniCPM-OCR parses documents through the ModelBest document-parse jobs API:
+// the file is submitted with the job, the job is polled until it reaches a
+// terminal state, and every page comes back as Markdown plus layout blocks
+// whose boxes are pixels of that page's image.
+const (
+ miniCPMOCRMaxPollInterval = 10 * time.Second
+ miniCPMOCRMaxPollFailures = 3
+ miniCPMOCRCancelTimeout = 30 * time.Second
+ // miniCPMOCRMaxResponseBytes bounds one job poll. Every poll repeats the
+ // pages finished so far, so the limit covers a whole document.
+ miniCPMOCRMaxResponseBytes int64 = 64 << 20
+)
+
+// miniCPMOCRPollInterval is the first wait between job polls; it grows up to
+// miniCPMOCRMaxPollInterval. Tests shorten it.
+var miniCPMOCRPollInterval = 2 * time.Second
+
+// MiniCPMOCRModel drives the MiniCPM-OCR document parsing API.
+type MiniCPMOCRModel struct {
+ baseModel BaseModel
+}
+
+// NewMiniCPMOCRModel creates a MiniCPM-OCR driver.
+func NewMiniCPMOCRModel(baseURL map[string]string, urlSuffix URLSuffix) *MiniCPMOCRModel {
+ return &MiniCPMOCRModel{
+ baseModel: BaseModel{
+ BaseURL: baseURL,
+ URLSuffix: urlSuffix,
+ httpClient: common.GetSSRFHTTPClient(),
+ },
+ }
+}
+
+func (m *MiniCPMOCRModel) NewInstance(baseURL map[string]string) ModelDriver {
+ return NewMiniCPMOCRModel(baseURL, m.baseModel.URLSuffix)
+}
+
+func (m *MiniCPMOCRModel) Name() string {
+ return "minicpm-ocr"
+}
+
+func (m *MiniCPMOCRModel) ChatWithMessages(ctx context.Context, modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig, modelUsage *common.ModelUsage) (*ChatResponse, error) {
+ return nil, fmt.Errorf("%s: no such method: ChatWithMessages", m.Name())
+}
+
+func (m *MiniCPMOCRModel) ChatStreamlyWithSender(ctx context.Context, modelName string, messages []Message, apiConfig *APIConfig, modelConfig *ChatConfig, modelUsage *common.ModelUsage, sender func(*string, *string) error) error {
+ return fmt.Errorf("%s: no such method: ChatStreamlyWithSender", m.Name())
+}
+
+func (m *MiniCPMOCRModel) Embed(ctx context.Context, modelName *string, request EmbedRequest, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig, modelUsage *common.ModelUsage) ([]EmbeddingData, error) {
+ return nil, fmt.Errorf("%s: no such method: Embed", m.Name())
+}
+
+func (m *MiniCPMOCRModel) Rerank(ctx context.Context, modelName *string, request RerankRequest, apiConfig *APIConfig, rerankConfig *RerankConfig, modelUsage *common.ModelUsage) (*RerankResponse, error) {
+ return nil, fmt.Errorf("%s: no such method: Rerank", m.Name())
+}
+
+func (m *MiniCPMOCRModel) TranscribeAudio(ctx context.Context, modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig, modelUsage *common.ModelUsage) (*ASRResponse, error) {
+ return nil, fmt.Errorf("%s: no such method: TranscribeAudio", m.Name())
+}
+
+func (m *MiniCPMOCRModel) TranscribeAudioWithSender(ctx context.Context, modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig, modelUsage *common.ModelUsage, sender func(*string, *string) error) error {
+ return fmt.Errorf("%s: no such method: TranscribeAudioWithSender", m.Name())
+}
+
+func (m *MiniCPMOCRModel) AudioSpeech(ctx context.Context, modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, modelUsage *common.ModelUsage) (*TTSResponse, error) {
+ return nil, fmt.Errorf("%s: no such method: AudioSpeech", m.Name())
+}
+
+func (m *MiniCPMOCRModel) AudioSpeechWithSender(ctx context.Context, modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, modelUsage *common.ModelUsage, sender func(*string, *string) error) error {
+ return fmt.Errorf("%s: no such method: AudioSpeechWithSender", m.Name())
+}
+
+// OCRFile parses content (a PDF or image) or, when content is empty, the
+// document at the http(s) URL. The response carries the page Markdown as
+// text and the located layout blocks of every page.
+func (m *MiniCPMOCRModel) OCRFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *APIConfig, ocrConfig *OCRConfig, modelUsage *common.ModelUsage) (*OCRFileResponse, error) {
+ if err := m.baseModel.APIConfigCheck(apiConfig); err != nil {
+ return nil, err
+ }
+ if modelName == nil || strings.TrimSpace(*modelName) == "" {
+ return nil, fmt.Errorf("model name is required")
+ }
+ remoteURL := ""
+ if url != nil {
+ remoteURL = strings.TrimSpace(*url)
+ }
+ if len(content) == 0 && !strings.HasPrefix(remoteURL, "http://") && !strings.HasPrefix(remoteURL, "https://") {
+ return nil, fmt.Errorf("file url or content is required")
+ }
+ jobsURL, err := m.baseModel.EndpointURL(apiConfig, m.baseModel.URLSuffix.OCR)
+ if err != nil {
+ return nil, err
+ }
+
+ ctx, cancel := context.WithTimeout(ctx, longOpCallTimeout)
+ defer cancel()
+
+ req, err := m.newSubmitRequest(ctx, jobsURL, strings.TrimSpace(*modelName), content, remoteURL, apiConfig)
+ if err != nil {
+ return nil, fmt.Errorf("MiniCPM-OCR: %w", err)
+ }
+ job, err := m.send(req)
+ if err != nil {
+ return nil, fmt.Errorf("MiniCPM-OCR: submit job: %w", err)
+ }
+ if job.ID == "" {
+ return nil, fmt.Errorf("MiniCPM-OCR: submit response carries no job id")
+ }
+ common.Info("MiniCPM-OCR job submitted", zap.String("job_id", job.ID), zap.Int("content_bytes", len(content)))
+
+ job, err = m.wait(ctx, jobsURL+"/"+neturl.PathEscape(job.ID), job, apiConfig)
+ if err != nil {
+ return nil, fmt.Errorf("MiniCPM-OCR: %w", err)
+ }
+ response := job.response()
+ common.Info("MiniCPM-OCR job finished", zap.String("job_id", job.ID), zap.Int("page_count", len(response.Pages)), zap.Int("text_len", len(*response.Text)))
+ return response, nil
+}
+
+// newSubmitRequest uploads content as multipart form data, or references a
+// public document URL in a JSON body.
+func (m *MiniCPMOCRModel) newSubmitRequest(ctx context.Context, jobsURL, modelName string, content []byte, remoteURL string, apiConfig *APIConfig) (*http.Request, error) {
+ if len(content) == 0 {
+ return m.baseModel.newJSONPostRequest(ctx, jobsURL, apiConfig, map[string]any{"model": modelName, "url": remoteURL})
+ }
+ var body bytes.Buffer
+ writer := multipart.NewWriter(&body)
+ _ = writer.WriteField("model", modelName)
+ part, err := writer.CreateFormFile("file", ocrUploadFilename(content))
+ if err != nil {
+ return nil, err
+ }
+ if _, err = part.Write(content); err != nil {
+ return nil, err
+ }
+ if err = writer.Close(); err != nil {
+ return nil, err
+ }
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, jobsURL, &body)
+ if err != nil {
+ return nil, err
+ }
+ req.Header.Set("Content-Type", writer.FormDataContentType())
+ m.baseModel.applyAuth(req, apiConfig)
+ return req, nil
+}
+
+// wait polls the job until it reaches a terminal state. Transient poll
+// failures are retried a few times. A job given up on is cancelled on the
+// server, which counts active jobs against the account.
+func (m *MiniCPMOCRModel) wait(ctx context.Context, jobURL string, job *miniCPMOCRJob, apiConfig *APIConfig) (*miniCPMOCRJob, error) {
+ interval := miniCPMOCRPollInterval
+ failures := 0
+ for {
+ switch job.Status {
+ case "Success":
+ return job, nil
+ case "Failed":
+ return nil, fmt.Errorf("job %s failed: %s", job.ID, job.errorMessage())
+ case "Cancelled":
+ return nil, fmt.Errorf("job %s was cancelled", job.ID)
+ case "Running":
+ default:
+ m.cancel(ctx, jobURL, apiConfig)
+ return nil, fmt.Errorf("job %s returned unknown status %q", job.ID, job.Status)
+ }
+ select {
+ case <-time.After(interval):
+ case <-ctx.Done():
+ m.cancel(ctx, jobURL, apiConfig)
+ return nil, ctx.Err()
+ }
+ interval = min(interval*3/2, miniCPMOCRMaxPollInterval)
+
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, jobURL, nil)
+ if err != nil {
+ return nil, err
+ }
+ m.baseModel.applyAuth(req, apiConfig)
+ next, err := m.send(req)
+ if err != nil {
+ if ctx.Err() == nil && miniCPMOCRTransient(err) && failures < miniCPMOCRMaxPollFailures {
+ failures++
+ common.Warn("MiniCPM-OCR: job poll failed, retrying", zap.String("job_id", job.ID), zap.Error(err))
+ continue
+ }
+ m.cancel(ctx, jobURL, apiConfig)
+ if ctx.Err() != nil {
+ return nil, ctx.Err()
+ }
+ return nil, fmt.Errorf("poll job %s: %w", job.ID, err)
+ }
+ failures = 0
+ job = next
+ }
+}
+
+// miniCPMOCRTransient reports whether a failed poll may succeed when retried:
+// a transport error, rate limiting or a server error.
+func miniCPMOCRTransient(err error) bool {
+ var statusErr *APIStatusError
+ if !errors.As(err, &statusErr) {
+ return true
+ }
+ return statusErr.Status == http.StatusTooManyRequests || statusErr.Status >= http.StatusInternalServerError
+}
+
+// cancel asks the server to stop the job. It runs after ctx is done, so it
+// uses a detached context bounded by its own timeout.
+func (m *MiniCPMOCRModel) cancel(ctx context.Context, jobURL string, apiConfig *APIConfig) {
+ cancelCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), miniCPMOCRCancelTimeout)
+ defer cancel()
+ req, err := http.NewRequestWithContext(cancelCtx, http.MethodDelete, jobURL, nil)
+ if err == nil {
+ m.baseModel.applyAuth(req, apiConfig)
+ var resp *http.Response
+ if resp, err = m.baseModel.httpClient.Do(req); err == nil {
+ resp.Body.Close()
+ if resp.StatusCode < 200 || resp.StatusCode > 299 {
+ err = fmt.Errorf("API request failed with status %d", resp.StatusCode)
+ }
+ }
+ }
+ if err != nil {
+ common.Warn("MiniCPM-OCR: failed to cancel job", zap.String("job_url", jobURL), zap.Error(err))
+ }
+}
+
+// send performs req and decodes the job carried by a 2xx reply.
+func (m *MiniCPMOCRModel) send(req *http.Request) (*miniCPMOCRJob, error) {
+ resp, err := m.baseModel.httpClient.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("failed to send request: %w", err)
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode < 200 || resp.StatusCode > 299 {
+ body, err := readModelErrorBody(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("API request failed with status %d; failed to read error response: %w", resp.StatusCode, err)
+ }
+ return nil, &APIStatusError{Status: resp.StatusCode, Body: string(body)}
+ }
+ body, err := readModelResponseBodyLimited(resp.Body, miniCPMOCRMaxResponseBytes)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read response: %w", err)
+ }
+ var job miniCPMOCRJob
+ if err = json.Unmarshal(body, &job); err != nil {
+ return nil, fmt.Errorf("failed to parse response: %w", err)
+ }
+ return &job, nil
+}
+
+type miniCPMOCRJob struct {
+ ID string `json:"id"`
+ Status string `json:"status"`
+ Pages []miniCPMOCRPage `json:"pages"`
+ // Error is a message string or an object carrying one.
+ Error json.RawMessage `json:"error"`
+}
+
+type miniCPMOCRPage struct {
+ Metadata struct {
+ PageIndex int `json:"page_index"`
+ } `json:"metadata"`
+ Choices []struct {
+ Message struct {
+ Content string `json:"content"`
+ } `json:"message"`
+ } `json:"choices"`
+ OCR struct {
+ ImageWidth float64 `json:"image_width"`
+ ImageHeight float64 `json:"image_height"`
+ Blocks []miniCPMOCRBlock `json:"blocks"`
+ Rotation struct {
+ Applied bool `json:"applied"`
+ OrientationAngle float64 `json:"orientation_angle"`
+ } `json:"rotation"`
+ } `json:"ocr"`
+}
+
+type miniCPMOCRBlock struct {
+ Order int `json:"order"`
+ Label string `json:"label"`
+ BBox []float64 `json:"bbox"`
+ Content string `json:"content"`
+ Status string `json:"status"`
+}
+
+func (job *miniCPMOCRJob) errorMessage() string {
+ var message string
+ if json.Unmarshal(job.Error, &message) == nil && message != "" {
+ return message
+ }
+ var detail struct {
+ Message string `json:"message"`
+ }
+ if json.Unmarshal(job.Error, &detail) == nil && detail.Message != "" {
+ return detail.Message
+ }
+ return "no error detail"
+}
+
+// response joins the page Markdown in page order and keeps the blocks of every
+// page in reading order. Blocks the API leaves out of the page Markdown
+// (headers, footers and their images, page numbers, footnotes and margin
+// text) are dropped, so the blocks and the text describe the same content.
+//
+// The API turns a sideways or upside-down page upright before recognizing it,
+// and its boxes then locate content on the turned page. Such a page keeps its
+// text but not its boxes, which would point elsewhere on the source page. A
+// page that was only deskewed by a few degrees keeps its boxes.
+func (job *miniCPMOCRJob) response() *OCRFileResponse {
+ pages := append([]miniCPMOCRPage(nil), job.Pages...)
+ sort.SliceStable(pages, func(i, j int) bool { return pages[i].Metadata.PageIndex < pages[j].Metadata.PageIndex })
+
+ response := &OCRFileResponse{}
+ texts := make([]string, 0, len(pages))
+ for _, page := range pages {
+ ocrPage := OCRPage{Index: page.Metadata.PageIndex}
+ turned := page.OCR.Rotation.Applied && math.Mod(page.OCR.Rotation.OrientationAngle, 360) != 0
+ if turned {
+ response.Warnings = append(response.Warnings, fmt.Sprintf("MiniCPM-OCR page %d was turned %g degrees for recognition; its coordinates are omitted", page.Metadata.PageIndex+1, page.OCR.Rotation.OrientationAngle))
+ } else {
+ ocrPage.Width, ocrPage.Height = page.OCR.ImageWidth, page.OCR.ImageHeight
+ }
+ blocks := append([]miniCPMOCRBlock(nil), page.OCR.Blocks...)
+ sort.SliceStable(blocks, func(i, j int) bool { return blocks[i].Order < blocks[j].Order })
+ for _, block := range blocks {
+ switch strings.ToLower(strings.TrimSpace(block.Label)) {
+ case "header", "header_image", "footer", "footer_image", "number", "footnote", "aside_text":
+ continue
+ }
+ if block.Status == "failed" {
+ response.Warnings = append(response.Warnings, fmt.Sprintf("MiniCPM-OCR page %d: %s block %d was not recognized", page.Metadata.PageIndex+1, block.Label, block.Order))
+ }
+ bbox := block.BBox
+ if turned {
+ bbox = nil
+ }
+ ocrPage.Blocks = append(ocrPage.Blocks, OCRBlock{Text: block.Content, Label: block.Label, BBox: bbox})
+ }
+ response.Pages = append(response.Pages, ocrPage)
+ if len(page.Choices) > 0 {
+ if text := strings.TrimSpace(page.Choices[0].Message.Content); text != "" {
+ texts = append(texts, text)
+ }
+ }
+ }
+ text := strings.Join(texts, "\n\n")
+ response.Text = &text
+ return response
+}
+
+func (m *MiniCPMOCRModel) ParseFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig, modelUsage *common.ModelUsage) (*ParseFileResponse, error) {
+ return nil, fmt.Errorf("%s: no such method: ParseFile", m.Name())
+}
+
+func (m *MiniCPMOCRModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]ListModelResponse, error) {
+ return nil, fmt.Errorf("%s: no such method: ListModels", m.Name())
+}
+
+func (m *MiniCPMOCRModel) Balance(ctx context.Context, apiConfig *APIConfig) (map[string]interface{}, error) {
+ return nil, fmt.Errorf("%s: no such method: Balance", m.Name())
+}
+
+func (m *MiniCPMOCRModel) CheckConnection(ctx context.Context, apiConfig *APIConfig) error {
+ return fmt.Errorf("%s: no such method: CheckConnection", m.Name())
+}
+
+func (m *MiniCPMOCRModel) ListTasks(ctx context.Context, apiConfig *APIConfig) ([]ListTaskStatus, error) {
+ return nil, fmt.Errorf("%s: no such method: ListTasks", m.Name())
+}
+
+func (m *MiniCPMOCRModel) ShowTask(ctx context.Context, taskID string, apiConfig *APIConfig) (*TaskResponse, error) {
+ return nil, fmt.Errorf("%s: no such method: ShowTask", m.Name())
+}
+
+func (m *MiniCPMOCRModel) Decide(ctx context.Context, modelName *string, request *DecideRequest, apiConfig *APIConfig, modelUsage *common.ModelUsage) (*DecideResponse, error) {
+ return nil, fmt.Errorf("%s: no such method: Decide", m.Name())
+}
diff --git a/internal/entity/models/minicpm_ocr_test.go b/internal/entity/models/minicpm_ocr_test.go
new file mode 100644
index 0000000000..dc04197eb8
--- /dev/null
+++ b/internal/entity/models/minicpm_ocr_test.go
@@ -0,0 +1,424 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package models
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "reflect"
+ "strings"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+func withFastMiniCPMOCRPolling(t *testing.T) {
+ t.Helper()
+ prev := miniCPMOCRPollInterval
+ miniCPMOCRPollInterval = time.Millisecond
+ t.Cleanup(func() { miniCPMOCRPollInterval = prev })
+}
+
+func newMiniCPMOCRForTest(baseURL string) *MiniCPMOCRModel {
+ return NewMiniCPMOCRModel(map[string]string{"default": baseURL}, URLSuffix{OCR: "document-parse/jobs"})
+}
+
+func miniCPMOCRCall(t *testing.T, driver *MiniCPMOCRModel, content []byte, url *string) (*OCRFileResponse, error) {
+ t.Helper()
+ model, key := "MiniCPM-OCR-1.0", "test-key"
+ return driver.OCRFile(t.Context(), &model, content, url, &APIConfig{ApiKey: &key}, nil, nil)
+}
+
+// The shipped config must resolve to the MiniCPM-OCR driver: CreateModelDriver
+// dispatches on the lower-cased provider name, so a mismatch would silently
+// fall through to DummyModel.
+func TestMiniCPMOCRProviderConfig(t *testing.T) {
+ dir, restore := setupProviderTestDir(t, "minicpm_ocr.json")
+ defer restore()
+
+ if err := InitProviderManager(dir); err != nil {
+ t.Fatalf("InitProviderManager: %v", err)
+ }
+ provider := GetProviderManager().FindProvider("MiniCPM-OCR")
+ if provider == nil {
+ t.Fatal("MiniCPM-OCR provider not found")
+ }
+ driver, ok := provider.ModelDriver.(*MiniCPMOCRModel)
+ if !ok {
+ t.Fatalf("ModelDriver=%T, want *MiniCPMOCRModel", provider.ModelDriver)
+ }
+ if driver.Name() != "minicpm-ocr" {
+ t.Fatalf("Name()=%q", driver.Name())
+ }
+ if _, ok := driver.NewInstance(map[string]string{"default": "http://other"}).(*MiniCPMOCRModel); !ok {
+ t.Fatal("NewInstance did not return *MiniCPMOCRModel")
+ }
+ if len(provider.Models) != 1 || provider.Models[0].Name != "MiniCPM-OCR-1.0" || !provider.Models[0].ModelTypeMap["ocr"] {
+ t.Fatalf("models=%+v", provider.Models)
+ }
+}
+
+// OCRFile uploads the document with the job request, polls until the job
+// succeeds and keeps every page's blocks in reading order.
+func TestMiniCPMOCRFileRunsJob(t *testing.T) {
+ withSSRFBypass(t)
+ withFastMiniCPMOCRPolling(t)
+
+ var polls atomic.Int32
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if got := r.Header.Get("Authorization"); got != "Bearer test-key" {
+ t.Errorf("Authorization=%q", got)
+ }
+ w.Header().Set("Content-Type", "application/json")
+ switch {
+ case r.Method == http.MethodPost && r.URL.Path == "/v1/document-parse/jobs":
+ if err := r.ParseMultipartForm(1 << 20); err != nil {
+ t.Errorf("ParseMultipartForm: %v", err)
+ }
+ if got := r.FormValue("model"); got != "MiniCPM-OCR-1.0" {
+ t.Errorf("model=%q", got)
+ }
+ file, header, err := r.FormFile("file")
+ if err != nil {
+ t.Errorf("FormFile: %v", err)
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+ data, _ := io.ReadAll(file)
+ if header.Filename != "document.pdf" || string(data) != "%PDF-1.7 test" {
+ t.Errorf("file=%q content=%q", header.Filename, data)
+ }
+ w.WriteHeader(http.StatusAccepted)
+ _, _ = w.Write([]byte(`{"id":"ocr_1","status":"Running","pages_done":0,"pages_total":0}`))
+ case r.Method == http.MethodGet && r.URL.Path == "/v1/document-parse/jobs/ocr_1":
+ if polls.Add(1) == 1 {
+ _, _ = w.Write([]byte(`{"id":"ocr_1","status":"Running","pages_done":0,"pages_total":2,"pages":[]}`))
+ return
+ }
+ _, _ = w.Write([]byte(`{
+ "id": "ocr_1", "status": "Success", "pages_done": 2, "pages_total": 2,
+ "pages": [
+ {"metadata": {"page_index": 1},
+ "choices": [{"message": {"content": "Second page."}}],
+ "ocr": {"image_width": 1240, "image_height": 1754, "blocks": [
+ {"order": 1, "label": "text", "bbox": [90, 100, 1100, 160], "regions": [[90, 100, 1100, 160]], "content": "Second page.", "status": "ok"}]}},
+ {"metadata": {"page_index": 0},
+ "choices": [{"message": {"content": "# Title\n\nBody text."}}],
+ "ocr": {"image_width": 1240, "image_height": 1754, "blocks": [
+ {"order": 3, "label": "image", "bbox": [100, 400, 900, 800], "content": "", "status": "skipped_no_vlm"},
+ {"order": 2, "label": "text", "bbox": [100, 200, 1000, 260], "content": "Body text.", "status": "ok"},
+ {"order": 0, "label": "header", "bbox": [100, 20, 600, 50], "content": "Running header", "status": "ok"},
+ {"order": 1, "label": "doc_title", "bbox": [100, 80, 800, 150], "content": "Title", "status": "ok"},
+ {"order": 4, "label": "table", "bbox": [100, 900, 1100, 1200], "content": "", "status": "failed"},
+ {"order": 5, "label": "number", "bbox": [600, 1700, 640, 1730], "content": "1", "status": "ok"},
+ {"order": 6, "label": "footer_image", "bbox": [1000, 1700, 1100, 1740], "content": "", "status": "skipped_no_vlm"}]}}
+ ],
+ "markdown": "# Title\n\nBody text.\n\nSecond page."
+ }`))
+ default:
+ http.NotFound(w, r)
+ }
+ }))
+ defer server.Close()
+
+ response, err := miniCPMOCRCall(t, newMiniCPMOCRForTest(server.URL+"/v1"), []byte("%PDF-1.7 test"), nil)
+ if err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ if got := *response.Text; got != "# Title\n\nBody text.\n\nSecond page." {
+ t.Fatalf("text=%q", got)
+ }
+ if len(response.Pages) != 2 || response.Pages[0].Index != 0 || response.Pages[1].Index != 1 {
+ t.Fatalf("pages=%+v", response.Pages)
+ }
+ first := response.Pages[0]
+ if first.Width != 1240 || first.Height != 1754 {
+ t.Fatalf("page size=%vx%v", first.Width, first.Height)
+ }
+ want := []OCRBlock{
+ {Text: "Title", Label: "doc_title", BBox: []float64{100, 80, 800, 150}},
+ {Text: "Body text.", Label: "text", BBox: []float64{100, 200, 1000, 260}},
+ {Text: "", Label: "image", BBox: []float64{100, 400, 900, 800}},
+ {Text: "", Label: "table", BBox: []float64{100, 900, 1100, 1200}},
+ }
+ if !reflect.DeepEqual(first.Blocks, want) {
+ t.Fatalf("blocks=%+v\nwant %+v", first.Blocks, want)
+ }
+ if len(response.Warnings) != 1 || !strings.Contains(response.Warnings[0], "page 1: table block 4") {
+ t.Fatalf("warnings=%q", response.Warnings)
+ }
+ if polls.Load() != 2 {
+ t.Fatalf("polls=%d, want 2", polls.Load())
+ }
+}
+
+// A page the API turned upright for recognition keeps its text but loses its
+// boxes, which locate content on the turned page; a deskewed page keeps them.
+// The rotation records follow a hosted response for an upright, an
+// upside-down and a sideways copy of one scanned page.
+func TestMiniCPMOCRTurnedPagesKeepTextWithoutCoordinates(t *testing.T) {
+ var job miniCPMOCRJob
+ if err := json.Unmarshal([]byte(`{"id": "ocr_7", "status": "Success", "pages": [
+ {"metadata": {"page_index": 0}, "choices": [{"message": {"content": "Upright form."}}],
+ "ocr": {"image_width": 748, "image_height": 972,
+ "rotation": {"applied": true, "enabled": true, "total_angle": -1.0, "deskew_angle": -1.0, "orientation_angle": 0},
+ "blocks": [{"order": 0, "label": "doc_title", "bbox": [256, 199, 422, 227], "content": "Upright form.", "status": "ok"}]}},
+ {"metadata": {"page_index": 1}, "choices": [{"message": {"content": "Upside-down form."}}],
+ "ocr": {"image_width": 748, "image_height": 972,
+ "rotation": {"applied": true, "enabled": true, "total_angle": 179.0, "deskew_angle": -1.0, "orientation_angle": 180},
+ "blocks": [{"order": 0, "label": "doc_title", "bbox": [256, 198, 422, 226], "content": "Upside-down form.", "status": "ok"}]}},
+ {"metadata": {"page_index": 2}, "choices": [{"message": {"content": "Sideways form."}}],
+ "ocr": {"image_width": 748, "image_height": 972,
+ "rotation": {"applied": true, "enabled": true, "total_angle": 269.0, "deskew_angle": -1.0, "orientation_angle": 270},
+ "blocks": [{"order": 0, "label": "doc_title", "bbox": [256, 199, 422, 227], "content": "Sideways form.", "status": "ok"}]}}
+ ]}`), &job); err != nil {
+ t.Fatal(err)
+ }
+
+ response := job.response()
+ if *response.Text != "Upright form.\n\nUpside-down form.\n\nSideways form." || len(response.Pages) != 3 {
+ t.Fatalf("response=%+v", response)
+ }
+ upright := response.Pages[0]
+ if upright.Width != 748 || upright.Height != 972 || !reflect.DeepEqual(upright.Blocks[0].BBox, []float64{256, 199, 422, 227}) {
+ t.Fatalf("deskewed page lost its coordinates: %+v", upright)
+ }
+ for _, page := range response.Pages[1:] {
+ if page.Width != 0 || page.Height != 0 || len(page.Blocks) != 1 || page.Blocks[0].BBox != nil || page.Blocks[0].Text == "" {
+ t.Fatalf("turned page %d: %+v", page.Index, page)
+ }
+ }
+ if len(response.Warnings) != 2 || !strings.Contains(response.Warnings[0], "page 2 was turned 180 degrees") || !strings.Contains(response.Warnings[1], "page 3 was turned 270 degrees") {
+ t.Fatalf("warnings=%q", response.Warnings)
+ }
+}
+
+// A document reachable at a public URL is referenced in a JSON body instead
+// of being uploaded.
+func TestMiniCPMOCRFileSubmitsURL(t *testing.T) {
+ withSSRFBypass(t)
+ withFastMiniCPMOCRPolling(t)
+
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ if r.Method == http.MethodPost {
+ var body map[string]string
+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
+ t.Errorf("decode: %v", err)
+ }
+ if body["model"] != "MiniCPM-OCR-1.0" || body["url"] != "https://example.com/report.pdf" {
+ t.Errorf("body=%v", body)
+ }
+ w.WriteHeader(http.StatusAccepted)
+ }
+ _, _ = w.Write([]byte(`{"id":"ocr_2","status":"Success","pages":[{"metadata":{"page_index":0},"choices":[{"message":{"content":"Remote."}}],"ocr":{"blocks":[]}}]}`))
+ }))
+ defer server.Close()
+
+ url := "https://example.com/report.pdf"
+ response, err := miniCPMOCRCall(t, newMiniCPMOCRForTest(server.URL), nil, &url)
+ if err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ if *response.Text != "Remote." {
+ t.Fatalf("text=%q", *response.Text)
+ }
+}
+
+// An image without text still completes, so the provider check that sends a
+// blank image succeeds.
+func TestMiniCPMOCRFileAcceptsEmptyResult(t *testing.T) {
+ withSSRFBypass(t)
+ withFastMiniCPMOCRPolling(t)
+
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ if r.Method == http.MethodPost {
+ _ = r.ParseMultipartForm(1 << 20)
+ if _, header, err := r.FormFile("file"); err != nil || header.Filename != "document.png" {
+ t.Errorf("file=%v err=%v", header, err)
+ }
+ w.WriteHeader(http.StatusAccepted)
+ _, _ = w.Write([]byte(`{"id":"ocr_3","status":"Running"}`))
+ return
+ }
+ _, _ = w.Write([]byte(`{"id":"ocr_3","status":"Success","pages":[{"metadata":{"page_index":0},"choices":[{"message":{"content":""}}],"ocr":{"image_width":1,"image_height":1,"blocks":[]}}]}`))
+ }))
+ defer server.Close()
+
+ response, err := miniCPMOCRCall(t, newMiniCPMOCRForTest(server.URL), []byte("\x89PNG\r\n\x1a\nblank"), nil)
+ if err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ if response.Text == nil || *response.Text != "" || len(response.Pages) != 1 {
+ t.Fatalf("response=%+v", response)
+ }
+}
+
+func TestMiniCPMOCRFileReportsJobFailure(t *testing.T) {
+ withSSRFBypass(t)
+ withFastMiniCPMOCRPolling(t)
+
+ for name, errorField := range map[string]string{
+ "string": `"imageParseInvalidResult"`,
+ "object": `{"message":"imageParseInvalidResult","code":"invalid"}`,
+ } {
+ t.Run(name, func(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ if r.Method == http.MethodPost {
+ w.WriteHeader(http.StatusAccepted)
+ _, _ = w.Write([]byte(`{"id":"ocr_4","status":"Running"}`))
+ return
+ }
+ _, _ = w.Write([]byte(`{"id":"ocr_4","status":"Failed","pages_done":0,"pages_total":1,"error":` + errorField + `}`))
+ }))
+ defer server.Close()
+
+ _, err := miniCPMOCRCall(t, newMiniCPMOCRForTest(server.URL), []byte("%PDF-1.7"), nil)
+ if err == nil || !strings.Contains(err.Error(), "job ocr_4 failed: imageParseInvalidResult") {
+ t.Fatalf("err=%v", err)
+ }
+ })
+ }
+}
+
+func TestMiniCPMOCRFileReportsHTTPError(t *testing.T) {
+ withSSRFBypass(t)
+
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusUnauthorized)
+ _, _ = w.Write([]byte(`{"error":{"message":"invalid api key","type":"invalid_request_error"}}`))
+ }))
+ defer server.Close()
+
+ _, err := miniCPMOCRCall(t, newMiniCPMOCRForTest(server.URL), []byte("%PDF-1.7"), nil)
+ var statusErr *APIStatusError
+ if !errors.As(err, &statusErr) || statusErr.Status != http.StatusUnauthorized || !strings.Contains(err.Error(), "invalid api key") {
+ t.Fatalf("err=%v", err)
+ }
+}
+
+// A run that stops waiting cancels the job on the server, which limits how
+// many jobs an account may keep active.
+func TestMiniCPMOCRFileCancelsAbandonedJob(t *testing.T) {
+ withSSRFBypass(t)
+ withFastMiniCPMOCRPolling(t)
+
+ ctx, cancel := context.WithCancel(t.Context())
+ defer cancel()
+ cancelled := make(chan struct{})
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ switch r.Method {
+ case http.MethodPost:
+ w.WriteHeader(http.StatusAccepted)
+ _, _ = w.Write([]byte(`{"id":"ocr_5","status":"Running"}`))
+ case http.MethodGet:
+ cancel()
+ _, _ = w.Write([]byte(`{"id":"ocr_5","status":"Running"}`))
+ case http.MethodDelete:
+ if r.URL.Path != "/document-parse/jobs/ocr_5" || r.Header.Get("Authorization") != "Bearer test-key" {
+ t.Errorf("cancel path=%q auth=%q", r.URL.Path, r.Header.Get("Authorization"))
+ }
+ _, _ = w.Write([]byte(`{"id":"ocr_5","status":"Cancelled"}`))
+ close(cancelled)
+ }
+ }))
+ defer server.Close()
+
+ model, key := "MiniCPM-OCR-1.0", "test-key"
+ _, err := newMiniCPMOCRForTest(server.URL).OCRFile(ctx, &model, []byte("%PDF-1.7"), nil, &APIConfig{ApiKey: &key}, nil, nil)
+ if !errors.Is(err, context.Canceled) {
+ t.Fatalf("err=%v, want context.Canceled", err)
+ }
+ select {
+ case <-cancelled:
+ case <-time.After(5 * time.Second):
+ t.Fatal("job was not cancelled on the server")
+ }
+}
+
+// Polling retries rate limiting and server errors; a job the driver gives up
+// on is cancelled on the server.
+func TestMiniCPMOCRFilePollFailures(t *testing.T) {
+ withSSRFBypass(t)
+ withFastMiniCPMOCRPolling(t)
+
+ for _, tc := range []struct {
+ name string
+ failures int
+ status int
+ wantErr bool
+ wantCancel bool
+ }{
+ {name: "transient failure is retried", failures: 2, status: http.StatusServiceUnavailable},
+ {name: "persistent server errors give up", failures: 100, status: http.StatusBadGateway, wantErr: true, wantCancel: true},
+ {name: "client error gives up at once", failures: 1, status: http.StatusNotFound, wantErr: true, wantCancel: true},
+ {name: "reply without a known status gives up", failures: 1, status: http.StatusOK, wantErr: true, wantCancel: true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ var polls, cancels atomic.Int32
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ switch r.Method {
+ case http.MethodPost:
+ w.WriteHeader(http.StatusAccepted)
+ _, _ = w.Write([]byte(`{"id":"ocr_6","status":"Running"}`))
+ case http.MethodGet:
+ if int(polls.Add(1)) <= tc.failures {
+ w.WriteHeader(tc.status)
+ _, _ = w.Write([]byte(`{"error":{"message":"unavailable"}}`))
+ return
+ }
+ _, _ = w.Write([]byte(`{"id":"ocr_6","status":"Success","pages":[{"metadata":{"page_index":0},"choices":[{"message":{"content":"Recovered."}}],"ocr":{"blocks":[]}}]}`))
+ case http.MethodDelete:
+ cancels.Add(1)
+ _, _ = w.Write([]byte(`{"id":"ocr_6","status":"Cancelled"}`))
+ }
+ }))
+ defer server.Close()
+
+ response, err := miniCPMOCRCall(t, newMiniCPMOCRForTest(server.URL), []byte("%PDF-1.7"), nil)
+ if tc.wantErr != (err != nil) {
+ t.Fatalf("err=%v", err)
+ }
+ if !tc.wantErr && *response.Text != "Recovered." {
+ t.Fatalf("text=%q", *response.Text)
+ }
+ if got := cancels.Load() > 0; got != tc.wantCancel {
+ t.Fatalf("cancelled=%v, want %v", got, tc.wantCancel)
+ }
+ })
+ }
+}
+
+func TestMiniCPMOCRFileRequiresAPIKeyAndInput(t *testing.T) {
+ driver := newMiniCPMOCRForTest("http://unused")
+ model, empty, key := "MiniCPM-OCR-1.0", "", "test-key"
+ if _, err := driver.OCRFile(t.Context(), &model, []byte("%PDF-1.7"), nil, &APIConfig{ApiKey: &empty}, nil, nil); err == nil || !strings.Contains(err.Error(), "api key is required") {
+ t.Fatalf("missing key err=%v", err)
+ }
+ if _, err := driver.OCRFile(t.Context(), &model, nil, nil, &APIConfig{ApiKey: &key}, nil, nil); err == nil || !strings.Contains(err.Error(), "file url or content is required") {
+ t.Fatalf("missing input err=%v", err)
+ }
+}
diff --git a/internal/entity/models/paddleocr.go b/internal/entity/models/paddleocr.go
index bb2b1c5dd7..a8a9a76d11 100644
--- a/internal/entity/models/paddleocr.go
+++ b/internal/entity/models/paddleocr.go
@@ -168,7 +168,7 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
_ = writer.WriteField("model", *modelName)
_ = writer.WriteField("optionalPayload", string(optBytes))
- part, err := writer.CreateFormFile("file", paddleOCRUploadFilename(content))
+ part, err := writer.CreateFormFile("file", ocrUploadFilename(content))
if err != nil {
return nil, fmt.Errorf("failed to create form file: %w", err)
}
@@ -398,9 +398,9 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
return response, nil
}
-// paddleOCRUploadFilename preserves the content-derived format expected by the
+// ocrUploadFilename preserves the content-derived format expected by the
// job API without trusting a user-supplied filename. Unknown data stays PDF.
-func paddleOCRUploadFilename(content []byte) string {
+func ocrUploadFilename(content []byte) string {
// net/http's sniffer does not recognize TIFF, which the image parser accepts.
if bytes.HasPrefix(content, []byte("II*\x00")) || bytes.HasPrefix(content, []byte("MM\x00*")) {
return "document.tiff"
diff --git a/internal/entity/models/paddleocr_test.go b/internal/entity/models/paddleocr_test.go
index 3371168733..e5b2503499 100644
--- a/internal/entity/models/paddleocr_test.go
+++ b/internal/entity/models/paddleocr_test.go
@@ -13,9 +13,9 @@ import (
"testing"
)
-// TestPaddleOCRUploadFilename checks content-derived image extensions and the
+// TestOCRUploadFilename checks content-derived image extensions and the
// unchanged PDF default, including both TIFF byte orders.
-func TestPaddleOCRUploadFilename(t *testing.T) {
+func TestOCRUploadFilename(t *testing.T) {
for _, tc := range []struct {
name string
data []byte
@@ -32,7 +32,7 @@ func TestPaddleOCRUploadFilename(t *testing.T) {
{"unrecognized", []byte("unknown"), "document.pdf"},
} {
t.Run(tc.name, func(t *testing.T) {
- if got := paddleOCRUploadFilename(tc.data); got != tc.want {
+ if got := ocrUploadFilename(tc.data); got != tc.want {
t.Fatalf("filename=%q, want %q", got, tc.want)
}
})
diff --git a/internal/ingestion/component/chunker/paddleocr_test.go b/internal/ingestion/component/chunker/paddleocr_test.go
index ef998f0b42..fb7ea9f359 100644
--- a/internal/ingestion/component/chunker/paddleocr_test.go
+++ b/internal/ingestion/component/chunker/paddleocr_test.go
@@ -42,7 +42,7 @@ func TestPaddleOCRHighlightsThroughPipeline(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- result := parser.PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ result := parser.OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response)
if result.Err != nil {
t.Fatal(result.Err)
}
@@ -116,7 +116,7 @@ func TestPaddleOCRChunkersPreserveColumnReadingOrder(t *testing.T) {
{Text: "RightFirst", BBox: []float64{600, 10, 1000, 60}},
{Text: "RightSecond", BBox: []float64{600, 200, 1000, 260}},
}}}}
- result := parser.PaddleOCRPDFResult(t.Context(), "columns.pdf", data, response)
+ result := parser.OCRPDFResult(t.Context(), "PaddleOCR", "columns.pdf", data, response)
if result.Err != nil {
t.Fatal(result.Err)
}
@@ -194,7 +194,7 @@ func TestPaddleOCRCaptionsRemainBodyInTitleChunking(t *testing.T) {
{Text: "Table caption", Label: "table_title"},
{Text: "Ordinary body.", Label: "text"},
}}}}
- result := parser.PaddleOCRPDFResult(t.Context(), "captions.pdf", nil, response)
+ result := parser.OCRPDFResult(t.Context(), "PaddleOCR", "captions.pdf", nil, response)
if result.Err != nil {
t.Fatal(result.Err)
}
diff --git a/internal/ingestion/component/minicpm_ocr_dispatch_test.go b/internal/ingestion/component/minicpm_ocr_dispatch_test.go
new file mode 100644
index 0000000000..3dbc46edfc
--- /dev/null
+++ b/internal/ingestion/component/minicpm_ocr_dispatch_test.go
@@ -0,0 +1,129 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package component
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "ragflow/internal/entity"
+ modelModule "ragflow/internal/entity/models"
+
+ "gorm.io/gorm"
+)
+
+// A tenant model UUID picked as the PDF parser runs a MiniCPM-OCR job and
+// yields structured PDF items instead of falling through to the image2text
+// VLM path. The model is found the way that path finds its own: parse_method
+// first, then layout_recognizer.
+func TestDispatch_PDFMiniCPMOCR_ModelUUID(t *testing.T) {
+ withSSRFBypass(t)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost || r.URL.Path != "/v1/document-parse/jobs" {
+ http.NotFound(w, r)
+ return
+ }
+ if got := r.Header.Get("Authorization"); got != "Bearer minicpm-secret" {
+ t.Errorf("Authorization=%q", got)
+ }
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusAccepted)
+ _, _ = w.Write([]byte(`{"id":"ocr_1","status":"Success","pages":[{"metadata":{"page_index":0},` +
+ `"choices":[{"message":{"content":"# MiniCPM Title\n\nMiniCPM body."}}],` +
+ `"ocr":{"image_width":1240,"image_height":1754,"blocks":[` +
+ `{"order":0,"label":"doc_title","bbox":[100,80,800,150],"content":"MiniCPM Title","status":"ok"},` +
+ `{"order":1,"label":"text","bbox":[100,200,1000,260],"content":"MiniCPM body.","status":"ok"}]}}]}`))
+ }))
+ defer server.Close()
+
+ origSelector, origResolve := isMiniCPMOCRModelSelector, resolveModelConfig
+ t.Cleanup(func() { isMiniCPMOCRModelSelector, resolveModelConfig = origSelector, origResolve })
+ modelID := "5b6f1d0c8a2e4f3b9c7d1e2f3a4b5c6d"
+ isMiniCPMOCRModelSelector = func(_ context.Context, _ *gorm.DB, tenantID, selector string) bool {
+ return tenantID == "test-tenant" && selector == modelID
+ }
+ baseURL, apiKey := server.URL+"/v1", "minicpm-secret"
+ resolveModelConfig = func(_ context.Context, _ *gorm.DB, tenantID string, modelType entity.ModelType, modelRef string) (modelModule.ModelDriver, string, *modelModule.APIConfig, int, error) {
+ if tenantID != "test-tenant" || modelType != entity.ModelTypeOCR || modelRef != modelID {
+ return nil, "", nil, 0, fmt.Errorf("unexpected model resolution: tenant=%q type=%v ref=%q", tenantID, modelType, modelRef)
+ }
+ driver := modelModule.NewMiniCPMOCRModel(nil, modelModule.URLSuffix{OCR: "document-parse/jobs"})
+ return driver, "MiniCPM-OCR-1.0", &modelModule.APIConfig{ApiKey: &apiKey, BaseURL: &baseURL}, 0, nil
+ }
+
+ for name, pdf := range map[string]map[string]string{
+ "parse_method": {"parse_method": modelID},
+ "layout_recognizer": {"layout_recognizer": modelID},
+ "parse_method beside a built-in layout_recognizer": {"parse_method": modelID, "layout_recognizer": "deepdoc"},
+ "parse_method beside another layout_recognizer model": {"parse_method": modelID, "layout_recognizer": "0f9e8d7c6b5a49382716051423324150"},
+ } {
+ t.Run(name, func(t *testing.T) {
+ setups := defaultSetups()
+ for field, value := range pdf {
+ setups["pdf"][field] = value
+ }
+ c := &ParserComponent{setups: setups}
+ out, err := c.Invoke(t.Context(), nil, map[string]any{
+ "binary": []byte("%PDF-1.4"),
+ "file_type": "pdf",
+ "name": "sample.pdf",
+ "tenant_id": "test-tenant",
+ })
+ if err != nil {
+ t.Fatalf("Invoke: %v", err)
+ }
+ requireJSONText(t, out, "MiniCPM Title")
+ requireJSONText(t, out, "MiniCPM body.")
+ })
+ }
+}
+
+func TestIsMiniCPMOCRModelSelector(t *testing.T) {
+ for _, tc := range []struct {
+ selector string
+ want bool
+ }{
+ {"MiniCPM-OCR-1.0@default@MiniCPM-OCR", true},
+ {"MiniCPM-OCR-1.0@MiniCPM-OCR", true},
+ {"PaddleOCR-VL-1.6@default@PaddleOCR", false},
+ // A bare model UUID is resolved within the tenant, which needs a database.
+ {"5b6f1d0c8a2e4f3b9c7d1e2f3a4b5c6d", false},
+ } {
+ if got := defaultIsMiniCPMOCRModelSelector(t.Context(), nil, "tenant", tc.selector); got != tc.want {
+ t.Errorf("selector %q: got %v, want %v", tc.selector, got, tc.want)
+ }
+ }
+}
+
+func TestDispatchMiniCPMOCRPDFRejectsOtherDrivers(t *testing.T) {
+ original := resolveModelConfig
+ t.Cleanup(func() { resolveModelConfig = original })
+ resolveModelConfig = func(context.Context, *gorm.DB, string, entity.ModelType, string) (modelModule.ModelDriver, string, *modelModule.APIConfig, int, error) {
+ return &paddleOCRFakeDriver{}, "PaddleOCR-VL-1.6", &modelModule.APIConfig{}, 0, nil
+ }
+ _, err := dispatchMiniCPMOCRPDF(t.Context(), nil, "sample.pdf", []byte("%PDF-1.4"), "tenant", "PaddleOCR-VL-1.6@default@PaddleOCR")
+ if err == nil || !strings.Contains(err.Error(), "requires a MiniCPM-OCR OCR model") {
+ t.Fatalf("err=%v", err)
+ }
+ if _, err = dispatchMiniCPMOCRPDF(t.Context(), nil, "sample.pdf", []byte("%PDF-1.4"), "", "model"); err == nil || !strings.Contains(err.Error(), "requires tenant_id") {
+ t.Fatalf("missing tenant err=%v", err)
+ }
+}
diff --git a/internal/ingestion/component/pdf_vision_dispatch.go b/internal/ingestion/component/pdf_vision_dispatch.go
index 413ea4552a..2ea50dc414 100644
--- a/internal/ingestion/component/pdf_vision_dispatch.go
+++ b/internal/ingestion/component/pdf_vision_dispatch.go
@@ -285,6 +285,15 @@ func dispatchPDFBackend(ctx context.Context, db *gorm.DB, filename string, binar
if !useVision {
return parser.ParseResult{}, false, nil
}
+ // A MiniCPM-OCR model is OCR-only and cannot serve the image2text VLM path
+ // below, so its selector goes to its own parser instead.
+ if isMiniCPMOCRModelSelector(ctx, db, tenantID, modelID) {
+ res, err := dispatchMiniCPMOCRPDF(ctx, db, filename, binary, tenantID, modelID)
+ if err != nil {
+ return parser.ParseResult{}, true, err
+ }
+ return res, true, nil
+ }
if tenantID == "" {
return parser.ParseResult{}, true, fmt.Errorf(
`parser: pdf parse_method %q requires tenant_id to resolve VLM model`, modelID)
@@ -946,7 +955,46 @@ func dispatchPaddleOCRPdf(
return parser.ParseResult{}, fmt.Errorf("parser: PaddleOCR OCRFile: %w", err)
}
- result := parser.PaddleOCRPDFResult(ctx, filename, binary, resp)
+ result := parser.OCRPDFResult(ctx, "PaddleOCR", filename, binary, resp)
+ return result, result.Err
+}
+
+// isMiniCPMOCRModelSelector reports whether selector — a tenant model UUID or
+// a model@instance@provider composite — names a MiniCPM-OCR model. A UUID
+// carries no provider spelling, so it is resolved within the tenant.
+var isMiniCPMOCRModelSelector = defaultIsMiniCPMOCRModelSelector
+
+func defaultIsMiniCPMOCRModelSelector(ctx context.Context, db *gorm.DB, tenantID, selector string) bool {
+ selector = strings.TrimSpace(selector)
+ if strings.Contains(selector, "@") {
+ _, _, providerName, err := common.ParseCompositeModelName(selector)
+ return err == nil && strings.EqualFold(providerName, "MiniCPM-OCR")
+ }
+ if db == nil || tenantID == "" || selector == "" {
+ return false
+ }
+ resolved, err := resolveModelConfigByID(ctx, db, tenantID, entity.ModelTypeOCR, selector)
+ return err == nil && resolved.driver.Name() == "minicpm-ocr"
+}
+
+// dispatchMiniCPMOCRPDF parses the PDF with the selected MiniCPM-OCR model and
+// projects its page blocks onto the PDF pages.
+func dispatchMiniCPMOCRPDF(ctx context.Context, db *gorm.DB, filename string, binary []byte, tenantID, selector string) (parser.ParseResult, error) {
+ if tenantID == "" {
+ return parser.ParseResult{}, fmt.Errorf("parser: MiniCPM-OCR requires tenant_id")
+ }
+ driver, modelName, apiConfig, _, err := resolveModelConfig(ctx, db, tenantID, entity.ModelTypeOCR, selector)
+ if err != nil {
+ return parser.ParseResult{}, fmt.Errorf("parser: MiniCPM-OCR model: %w", err)
+ }
+ if driver.Name() != "minicpm-ocr" {
+ return parser.ParseResult{}, fmt.Errorf("parser: MiniCPM-OCR requires a MiniCPM-OCR OCR model; found %q", driver.Name())
+ }
+ resp, err := driver.OCRFile(ctx, &modelName, binary, nil, apiConfig, nil, nil)
+ if err != nil {
+ return parser.ParseResult{}, fmt.Errorf("parser: MiniCPM-OCR OCRFile: %w", err)
+ }
+ result := parser.OCRPDFResult(ctx, "MiniCPM-OCR", filename, binary, resp)
return result, result.Err
}
diff --git a/internal/parser/parser/pdf_parser_paddleocr.go b/internal/parser/parser/pdf_parser_paddleocr.go
index 81acddf33b..6e8f2bd1b6 100644
--- a/internal/parser/parser/pdf_parser_paddleocr.go
+++ b/internal/parser/parser/pdf_parser_paddleocr.go
@@ -23,14 +23,15 @@ func isStructuralOCRTextLayout(fields map[string]any) bool {
return false
}
-// PaddleOCRPDFResult projects OCR regions onto the PDF's page geometry and
-// emits the structured items consumed by the existing PDF chunk pipeline.
-func PaddleOCRPDFResult(ctx context.Context, filename string, data []byte, response *models.OCRFileResponse) ParseResult {
+// OCRPDFResult projects the page blocks of an OCR provider onto the PDF's page
+// geometry and emits the structured items consumed by the existing PDF chunk
+// pipeline. provider names the OCR service in errors and warnings.
+func OCRPDFResult(ctx context.Context, provider, filename string, data []byte, response *models.OCRFileResponse) ParseResult {
if err := ctx.Err(); err != nil {
return ParseResult{Err: err}
}
if response == nil || len(response.Pages) == 0 {
- return ParseResult{Err: fmt.Errorf("parser: PaddleOCR returned no page blocks")}
+ return ParseResult{Err: fmt.Errorf("parser: %s returned no page blocks", provider)}
}
sizes, pageCount, geometryErr := paddleOCRPageSizes(ctx, data, response.Pages)
if err := ctx.Err(); err != nil {
@@ -39,7 +40,7 @@ func PaddleOCRPDFResult(ctx context.Context, filename string, data []byte, respo
result := ParseResult{Warnings: append([]string(nil), response.Warnings...)}
hasPageCount := pageCount > 0
if geometryErr != nil {
- result.Warnings = append(result.Warnings, "PaddleOCR PDF geometry unavailable: "+geometryErr.Error())
+ result.Warnings = append(result.Warnings, provider+" PDF geometry unavailable: "+geometryErr.Error())
}
markdown, err := NewMarkdownParser(GoMarkdown)
if err != nil {
@@ -52,7 +53,7 @@ func PaddleOCRPDFResult(ctx context.Context, filename string, data []byte, respo
}
size := sizes[page.Index]
if paddleOCRPositive(page.Width) && paddleOCRPositive(page.Height) && paddleOCRPositive(size[0]) && paddleOCRPositive(size[1]) && !paddleOCRAspectRatioMatches(page, size) {
- result.Warnings = append(result.Warnings, fmt.Sprintf("PaddleOCR page %d: OCR dimensions %gx%g do not match PDF aspect ratio %gx%g; coordinates omitted", page.Index+1, page.Width, page.Height, size[0], size[1]))
+ result.Warnings = append(result.Warnings, fmt.Sprintf("%s page %d: OCR dimensions %gx%g do not match PDF aspect ratio %gx%g; coordinates omitted", provider, page.Index+1, page.Width, page.Height, size[0], size[1]))
size = [2]float64{}
}
for _, block := range page.Blocks {
@@ -138,7 +139,7 @@ func PaddleOCRPDFResult(ctx context.Context, filename string, data []byte, respo
return ParseResult{Err: err}
}
if len(result.JSON) == 0 {
- return ParseResult{Err: fmt.Errorf("parser: PaddleOCR returned empty text")}
+ return ParseResult{Err: fmt.Errorf("parser: %s returned empty text", provider)}
}
result.File = pdfFileMeta(filename, pageCount)
return result
diff --git a/internal/parser/parser/pdf_parser_paddleocr_cgo_test.go b/internal/parser/parser/pdf_parser_paddleocr_cgo_test.go
index d711ac9ade..7361394883 100644
--- a/internal/parser/parser/pdf_parser_paddleocr_cgo_test.go
+++ b/internal/parser/parser/pdf_parser_paddleocr_cgo_test.go
@@ -23,7 +23,7 @@ func TestPaddleOCRPDFResultPageGeometry(t *testing.T) {
{Index: 1, Width: 900, Height: 1200},
{Index: 2, Width: 600, Height: 800, Blocks: []models.OCRBlock{{Text: "third", Label: "text", BBox: []float64{30, 40, 300, 160}}, {Text: "<table><tr><td>42</td></tr></table>", Label: "table", BBox: []float64{30, 200, 300, 400}}}},
}}
- result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response)
if result.Err != nil {
t.Fatal(result.Err)
}
@@ -50,7 +50,7 @@ func TestPaddleOCRPDFResultLocatesUnstructuredTableText(t *testing.T) {
t.Fatal(err)
}
response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{{Text: "Item: 42", Label: "table", BBox: []float64{20, 40, 100, 120}}}}}}
- result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response)
if result.Err != nil || len(result.JSON) != 1 {
t.Fatalf("result=%+v", result)
}
@@ -67,7 +67,7 @@ func TestPaddleOCRPDFResultKeepsGeometryAfterOnePageFails(t *testing.T) {
}
block := models.OCRBlock{Label: "text", Text: "body", BBox: []float64{10, 20, 100, 200}}
response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{block}}, {Index: 99, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{block}}}}
- result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response)
if result.Err != nil {
t.Fatal(result.Err)
}
@@ -88,7 +88,7 @@ func TestPaddleOCRPDFResultLocatesMediaRegions(t *testing.T) {
{Label: "figure", Text: "![pipeline](https://example.invalid/p.png)", BBox: []float64{10, 20, 100, 120}},
{Label: "image", Text: "Figure 1: pipeline", BBox: []float64{10, 130, 100, 230}},
}}}}
- result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response)
if result.Err != nil || len(result.JSON) != 2 {
t.Fatalf("result=%+v", result)
}
@@ -132,7 +132,7 @@ func TestPaddleOCRPDFResultDropsMismatchedPageGeometry(t *testing.T) {
{Text: "second", BBox: []float64{30, 200, 100, 240}},
},
}}}
- result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response)
if result.Err != nil || len(result.JSON) != 2 {
t.Fatalf("result=%+v", result)
}
@@ -171,7 +171,7 @@ func TestPaddleOCRPDFResultKeepsMediaItems(t *testing.T) {
{Label: "seal", Text: "![seal](https://example.invalid/s.png)", BBox: []float64{10, 350, 100, 450}},
{Text: "![uncaptioned](https://example.invalid/u.png)", BBox: []float64{10, 460, 100, 560}},
}}}}
- result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response)
if result.Err != nil || len(result.JSON) != 5 {
t.Fatalf("result=%+v", result)
}
@@ -199,7 +199,7 @@ func TestPaddleOCRMediaItemsSurviveNormalize(t *testing.T) {
{Label: "figure", Text: "![pipeline](https://example.invalid/p.png)", BBox: []float64{10, 20, 100, 120}},
{Label: "image", Text: "Figure 1: pipeline", BBox: []float64{10, 130, 100, 230}},
}}}}
- result := NewPDFParser().NormalizeResult(t.Context(), "sample.pdf", PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response))
+ result := NewPDFParser().NormalizeResult(t.Context(), "sample.pdf", OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response))
if result.Err != nil || len(result.JSON) != 2 {
t.Fatalf("result=%+v", result)
}
diff --git a/internal/parser/parser/pdf_parser_paddleocr_nocgo_test.go b/internal/parser/parser/pdf_parser_paddleocr_nocgo_test.go
index a4da92a8b8..758e078bd1 100644
--- a/internal/parser/parser/pdf_parser_paddleocr_nocgo_test.go
+++ b/internal/parser/parser/pdf_parser_paddleocr_nocgo_test.go
@@ -16,7 +16,7 @@ func TestPaddleOCRPDFResultWithoutCGOKeepsTextAndPageIdentity(t *testing.T) {
t.Fatal(err)
}
response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 2, Width: 900, Height: 800, Blocks: []models.OCRBlock{{Text: "body", Label: "text", BBox: []float64{30, 40, 300, 160}}}}}}
- result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response)
if result.Err != nil {
t.Fatal(result.Err)
}
diff --git a/internal/parser/parser/pdf_parser_paddleocr_test.go b/internal/parser/parser/pdf_parser_paddleocr_test.go
index 4c0bca8aa1..10ba126e3e 100644
--- a/internal/parser/parser/pdf_parser_paddleocr_test.go
+++ b/internal/parser/parser/pdf_parser_paddleocr_test.go
@@ -40,7 +40,7 @@ func TestPaddleOCRPDFResultDropsUnlocatedMedia(t *testing.T) {
t.Fatal(err)
}
response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Blocks: []models.OCRBlock{{Text: "keep me", Label: "text", BBox: []float64{10, 20, 30, 40}}, {Text: "![figure](https://example.invalid/a(1).png)", Label: "image"}}}}}
- result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response)
if result.Err != nil || len(result.JSON) != 1 {
t.Fatalf("result=%+v", result)
}
@@ -58,7 +58,7 @@ func TestPaddleOCRPDFResultKeepsUnstructuredTableText(t *testing.T) {
t.Fatal(err)
}
response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{{Text: "Item: 42", Label: "table", BBox: []float64{20, 40, 100, 120}}}}}}
- result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response)
if result.Err != nil || len(result.JSON) != 1 {
t.Fatalf("result=%+v", result)
}
@@ -74,7 +74,7 @@ func TestPaddleOCRMarkdownTable(t *testing.T) {
t.Fatal(err)
}
response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{{Label: "table", Text: "| Item | Value |\n| --- | --- |\n| A | 42 |", BBox: []float64{10, 20, 100, 200}}}}}}
- result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "sample.pdf", data, response)
if result.Err != nil {
t.Fatal(result.Err)
}
@@ -91,7 +91,7 @@ func TestPaddleOCRCanceledContext(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
cancel()
response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{{Label: "text", Text: "body", BBox: []float64{10, 20, 100, 200}}}}}}
- result := PaddleOCRPDFResult(ctx, "sample.pdf", data, response)
+ result := OCRPDFResult(ctx, "PaddleOCR", "sample.pdf", data, response)
if result.Err == nil {
t.Fatal("canceled parse returned success")
}
@@ -111,7 +111,7 @@ func TestPaddleOCRTitleLabels(t *testing.T) {
} {
t.Run(tc.label+tc.text, func(t *testing.T) {
response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Blocks: []models.OCRBlock{{Text: tc.text, Label: tc.label}}}}}
- result := PaddleOCRPDFResult(t.Context(), "title.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "title.pdf", data, response)
if result.Err != nil {
t.Fatal(result.Err)
}
@@ -141,7 +141,7 @@ func TestPaddleOCRBlocksPreserveMarkdownLayout(t *testing.T) {
// normalization. It must not overwrite a structural block kind,
// including inside a title-labelled block.
response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Blocks: []models.OCRBlock{{Text: tc.text, Label: tc.label}}}}}
- result := PaddleOCRPDFResult(t.Context(), "layout.pdf", data, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "layout.pdf", data, response)
if result.Err != nil {
t.Fatal(result.Err)
}
@@ -163,12 +163,12 @@ func TestPaddleOCRPDFResultRejectsRenderedEmptyText(t *testing.T) {
for _, label := range []string{"text", "doc_title", "paragraph_title"} {
t.Run(label, func(t *testing.T) {
response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Blocks: []models.OCRBlock{{Label: label, Text: "---"}}}}}
- result := PaddleOCRPDFResult(t.Context(), "separator.pdf", nil, response)
+ result := OCRPDFResult(t.Context(), "PaddleOCR", "separator.pdf", nil, response)
if result.Err == nil || !strings.Contains(result.Err.Error(), "empty text") {
t.Fatalf("rendered-empty document accepted: %+v", result)
}
response.Pages[0].Blocks = append(response.Pages[0].Blocks, models.OCRBlock{Text: "body"})
- result = PaddleOCRPDFResult(t.Context(), "mixed.pdf", nil, response)
+ result = OCRPDFResult(t.Context(), "PaddleOCR", "mixed.pdf", nil, response)
if result.Err != nil || len(result.JSON) != 1 || result.JSON[0].ToMap()["text"] != "body" {
t.Fatalf("empty placeholder survived beside body: %+v", result)
}
diff --git a/internal/service/model_service.go b/internal/service/model_service.go
index 21b3ce91eb..59da54d3fd 100644
--- a/internal/service/model_service.go
+++ b/internal/service/model_service.go
@@ -23,6 +23,9 @@ import (
"encoding/json"
"errors"
"fmt"
+ "image"
+ "image/color"
+ "image/png"
"os"
"ragflow/internal/common"
@@ -1763,19 +1766,15 @@ func verifyDecisionModel(ctx context.Context, driver modelModule.ModelDriver, mo
return nil
}
-// minimalPNG returns a 1×1 white PNG as a byte slice for OCR verification.
+// minimalPNG returns a 1×1 white PNG for OCR verification. OCR services decode
+// it before recognizing anything, so it has to be a well-formed image.
func minimalPNG() []byte {
- return []byte{
- 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
- 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
- 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
- 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53,
- 0xDE, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41,
- 0x54, 0x08, 0xD7, 0x63, 0x60, 0x60, 0xF8, 0x0F,
- 0x00, 0x01, 0x01, 0x00, 0x05, 0x18, 0xD8, 0x32,
- 0x48, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E,
- 0x44, 0xAE, 0x42, 0x60, 0x82,
- }
+ img := image.NewRGBA(image.Rect(0, 0, 1, 1))
+ img.Set(0, 0, color.White)
+ var buf bytes.Buffer
+ // Encoding an in-memory image into a bytes.Buffer cannot fail.
+ _ = png.Encode(&buf, img)
+ return buf.Bytes()
}
func taskDriverConfig(provider *entity.TenantModelProvider, instance *entity.TenantModelInstance) (modelModule.ModelDriver, *modelModule.APIConfig, error) {
diff --git a/internal/service/model_service_test.go b/internal/service/model_service_test.go
index 878e17a2bb..63000124d4 100644
--- a/internal/service/model_service_test.go
+++ b/internal/service/model_service_test.go
@@ -1,9 +1,11 @@
package service
import (
+ "bytes"
"context"
"encoding/json"
"errors"
+ "image/png"
"net/http"
"net/http/httptest"
"reflect"
@@ -336,6 +338,21 @@ func TestVerifyProviderModelNamesUnverifiableTypes(t *testing.T) {
}
}
+// OCR services decode the verification image before recognizing anything, so
+// an image that fails to decode fails verification of a working model.
+func TestMinimalPNGDecodes(t *testing.T) {
+ img, err := png.Decode(bytes.NewReader(minimalPNG()))
+ if err != nil {
+ t.Fatalf("decode verification image: %v", err)
+ }
+ if bounds := img.Bounds(); bounds.Dx() != 1 || bounds.Dy() != 1 {
+ t.Fatalf("verification image bounds = %v, want 1x1", bounds)
+ }
+ if r, g, b, _ := img.At(0, 0).RGBA(); r != 0xffff || g != 0xffff || b != 0xffff {
+ t.Fatalf("verification image pixel = %v, want white", img.At(0, 0))
+ }
+}
+
// Verification builds the request Decide sends, so it needs exercising against
// a real driver. The stand-in used above accepts any request — including one the
// endpoint would refuse — so it cannot catch verification asking for nothing.
diff --git a/web/src/assets/svg/llm/minicpm-ocr.svg b/web/src/assets/svg/llm/minicpm-ocr.svg
new file mode 100644
index 0000000000..520b165e73
--- /dev/null
+++ b/web/src/assets/svg/llm/minicpm-ocr.svg
@@ -0,0 +1,3 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 -14.5 174 174" fill="none">
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M107.871 95.0896L169.395 136.078L152.218 144.723L98.3607 101.362L107.871 95.0896ZM130.829 50.0383V63.8369L18.1331 144.502L24.9929 62.0745L34.7552 67.6561V113.738L130.829 50.0383ZM0 15.8596L55.9454 57.8105L46.4201 65.2244L15.6528 44.9672L10.5077 137.64L1.71495 128.996L0 15.8596ZM162.703 7.34171L171.496 15.9907L173.211 128.206L117.976 86.7089L126.791 79.763L157.559 100.021L162.703 7.34171ZM155.076 0.484674L148.216 82.9131L138.455 76.4747V31.2547L42.3815 94.9539V81.1523L155.076 0.484674ZM21.0007 0.268066L74.8514 43.6254L66.0509 49.4307L4.52833 8.44325L21.0007 0.268066Z" fill="#373ED8"/>
+</svg>
diff --git a/web/src/components/svg-icon.tsx b/web/src/components/svg-icon.tsx
index 856faa4169..f62a0b2406 100644
--- a/web/src/components/svg-icon.tsx
+++ b/web/src/components/svg-icon.tsx
@@ -89,6 +89,7 @@ const svgIcons = [
LLMFactory.MinerUNet,
LLMFactory.PaddleOCR,
LLMFactory.PaddleOCRLocal,
+ LLMFactory.MiniCPMOCR,
LLMFactory.N1n,
// LLMFactory.DeerAPI,
LLMFactory.Avian,
diff --git a/web/src/constants/llm.ts b/web/src/constants/llm.ts
index 960084a160..24a16c66bf 100644
--- a/web/src/constants/llm.ts
+++ b/web/src/constants/llm.ts
@@ -86,6 +86,7 @@ export enum LLMFactory {
MinerUNet = 'MinerU.Net',
PaddleOCR = 'PaddleOCR',
PaddleOCRLocal = 'PaddleOCR.local',
+ MiniCPMOCR = 'MiniCPM-OCR',
OpenDataLoader = 'OpenDataLoader',
SoMark = 'SoMark',
N1n = 'n1n',
@@ -194,6 +195,7 @@ export const IconMap = {
[LLMFactory.MinerUNet]: 'mineru',
[LLMFactory.PaddleOCR]: 'paddleocr',
[LLMFactory.PaddleOCRLocal]: 'paddleocr',
+ [LLMFactory.MiniCPMOCR]: 'minicpm-ocr',
[LLMFactory.N1n]: 'n1n',
[LLMFactory.Avian]: 'avian',
[LLMFactory.RAGcon]: 'ragcon',
@@ -338,6 +340,7 @@ export const APIMapUrl = {
[LLMFactory.TokenPony]: 'https://www.tokenpony.cn/#/user/keys',
[LLMFactory.DeepInfra]: 'https://deepinfra.com/dash/api_keys',
[LLMFactory.PaddleOCR]: 'https://aistudio.baidu.com/paddleocr',
+ [LLMFactory.MiniCPMOCR]: 'https://platform.modelbest.cn/console/keys',
[LLMFactory.SoMark]: 'https://somark.cn/workbench/apikey',
[LLMFactory.N1n]: 'https://docs.n1n.ai',
[LLMFactory.Avian]: 'https://avian.io',