Skip to content

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

fix(model): move PaddleOCR api_key unwrapping from the generic provider flow into the drivers (#19823)

euvre1705cc0d2e23
diff --git a/internal/entity/models/paddleocr.go b/internal/entity/models/paddleocr.go
index 3b53293466..2c9751fa36 100644
--- a/internal/entity/models/paddleocr.go
+++ b/internal/entity/models/paddleocr.go
@@ -248,6 +248,8 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
return nil, err
}
+ apiConfig = paddleOCRResolvedAPIConfig(apiConfig)
+
if (content == nil || len(content) == 0) && (fileURL == nil || *fileURL == "") {
return nil, fmt.Errorf("content and fileURL cannot be both empty")
}
@@ -554,14 +556,15 @@ func (p *PaddleOCRModel) ShowTask(ctx context.Context, taskID string, apiConfig
return nil, fmt.Errorf("%s, no such method", p.Name())
}
-// PaddleOCRConfigFromAPIKey parses the tenant PaddleOCR api_key payload the
+// paddleOCRConfigFromAPIKey parses the tenant PaddleOCR api_key payload the
// same way Python's PaddleOCROcrModel does: the api_key is a JSON object
-// carrying paddleocr_base_url / paddleocr_api_url, paddleocr_access_token and
-// paddleocr_algorithm, optionally nested under an "api_key" key. A non-JSON
-// api_key — the PaddleOCR.local plain bearer token — yields zero values and
-// the caller falls back to plain-text semantics. base_url prefers
-// paddleocr_base_url over paddleocr_api_url, mirroring Python.
-func PaddleOCRConfigFromAPIKey(apiKey string) (baseURL, accessToken, algorithm string) {
+// carrying the server endpoint, access token and algorithm, either with the
+// lower-case keys written by the model UI or the upper-case env-style keys
+// written by the env auto-provisioning, optionally nested under an "api_key"
+// key. A non-JSON api_key — the PaddleOCR.local plain bearer token — yields
+// zero values and the caller falls back to plain-text semantics. base_url
+// prefers paddleocr_base_url over paddleocr_api_url, mirroring Python.
+func paddleOCRConfigFromAPIKey(apiKey string) (baseURL, accessToken, algorithm string) {
if strings.TrimSpace(apiKey) == "" {
return "", "", ""
}
@@ -573,8 +576,12 @@ func PaddleOCRConfigFromAPIKey(apiKey string) (baseURL, accessToken, algorithm s
raw = nested
}
get := func(key string) string {
- value, _ := raw[key].(string)
- return strings.TrimSpace(value)
+ for _, k := range []string{key, strings.ToUpper(key)} {
+ if value, _ := raw[k].(string); strings.TrimSpace(value) != "" {
+ return strings.TrimSpace(value)
+ }
+ }
+ return ""
}
baseURL = get("paddleocr_base_url")
if baseURL == "" {
@@ -582,3 +589,59 @@ func PaddleOCRConfigFromAPIKey(apiKey string) (baseURL, accessToken, algorithm s
}
return baseURL, get("paddleocr_access_token"), get("paddleocr_algorithm")
}
+
+// paddleOCRResolvedAPIConfig resolves the wire config for the PaddleOCR
+// drivers: the bearer token comes from the api_key JSON payload when present,
+// and the base url falls back from the instance field through the payload to
+// the PADDLEOCR_BASE_URL / PADDLEOCR_API_URL env vars. A plain-text api_key
+// passes through untouched; an empty base url is left for the driver's catalog
+// default.
+func paddleOCRResolvedAPIConfig(apiConfig *APIConfig) *APIConfig {
+ if apiConfig == nil {
+ return nil
+ }
+ apiKey := ""
+ if apiConfig.ApiKey != nil {
+ apiKey = *apiConfig.ApiKey
+ }
+ baseURL := ""
+ if apiConfig.BaseURL != nil {
+ baseURL = strings.TrimSpace(*apiConfig.BaseURL)
+ }
+ keyBaseURL, keyAccessToken, _ := paddleOCRConfigFromAPIKey(apiKey)
+ if keyAccessToken != "" {
+ apiKey = keyAccessToken
+ }
+ if baseURL == "" {
+ baseURL = keyBaseURL
+ }
+ if baseURL == "" {
+ baseURL = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRBaseUrl))
+ }
+ if baseURL == "" {
+ baseURL = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRAPIURL))
+ }
+ return &APIConfig{ApiKey: &apiKey, Region: apiConfig.Region, BaseURL: &baseURL}
+}
+
+// paddleOCRAlgorithm resolves the OCR algorithm the drivers advertise: an
+// explicit OCRConfig override (parser setup) wins, then the api_key payload,
+// then the PADDLEOCR_ALGORITHM env var, then the PaddleOCR-VL default.
+func paddleOCRAlgorithm(ocrConfig *OCRConfig, apiConfig *APIConfig) string {
+ if ocrConfig != nil {
+ if algorithm := strings.TrimSpace(ocrConfig.Algorithm); algorithm != "" {
+ return algorithm
+ }
+ }
+ apiKey := ""
+ if apiConfig != nil && apiConfig.ApiKey != nil {
+ apiKey = *apiConfig.ApiKey
+ }
+ if _, _, algorithm := paddleOCRConfigFromAPIKey(apiKey); strings.TrimSpace(algorithm) != "" {
+ return strings.TrimSpace(algorithm)
+ }
+ if algorithm := strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRAlgorithm)); algorithm != "" {
+ return algorithm
+ }
+ return "PaddleOCR-VL"
+}
diff --git a/internal/entity/models/paddleocr_local.go b/internal/entity/models/paddleocr_local.go
index 3e07cbee5a..b90f2b12e3 100644
--- a/internal/entity/models/paddleocr_local.go
+++ b/internal/entity/models/paddleocr_local.go
@@ -106,6 +106,9 @@ func (p *PaddleOCRLocalModel) OCRFile(ctx context.Context, modelName *string, co
return nil, fmt.Errorf("local PaddleOCR requires file content, but content is empty")
}
+ algorithm := paddleOCRAlgorithm(ocrConfig, apiConfig)
+ apiConfig = paddleOCRResolvedAPIConfig(apiConfig)
+
resolvedBaseURL, err := p.baseModel.GetBaseURL(apiConfig)
if err != nil {
return nil, err
@@ -124,11 +127,9 @@ func (p *PaddleOCRLocalModel) OCRFile(ctx context.Context, modelName *string, co
}
reqData := map[string]interface{}{
- "file": base64Str,
- "fileType": fileType,
- }
- if ocrConfig != nil && strings.TrimSpace(ocrConfig.Algorithm) != "" {
- reqData["algorithm"] = ocrConfig.Algorithm
+ "file": base64Str,
+ "fileType": fileType,
+ "algorithm": algorithm,
}
jsonData, err := json.Marshal(reqData)
@@ -149,10 +150,6 @@ func (p *PaddleOCRLocalModel) OCRFile(ctx context.Context, modelName *string, co
req.Header.Set("Authorization", auth)
}
- algorithm := ""
- if ocrConfig != nil {
- algorithm = ocrConfig.Algorithm
- }
common.Info("paddleocr local submit: sending",
zap.String("driver", p.Name()),
zap.String("url", url),
diff --git a/internal/entity/models/paddleocr_test.go b/internal/entity/models/paddleocr_test.go
index a9c5bf38ee..3219f7c1d0 100644
--- a/internal/entity/models/paddleocr_test.go
+++ b/internal/entity/models/paddleocr_test.go
@@ -2,6 +2,10 @@ package models
import (
"context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
"strings"
"testing"
)
@@ -40,8 +44,10 @@ func TestPaddleOCRLocalListModels(t *testing.T) {
// TestPaddleOCRConfigFromAPIKey pins the api_key JSON contract shared with
// Python's PaddleOCROcrModel: the cloud provider stores
// paddleocr_base_url / paddleocr_api_url, paddleocr_access_token and
-// paddleocr_algorithm in the api_key payload, while a plain-text api_key
-// (PaddleOCR.local bearer token) yields zero values.
+// paddleocr_algorithm in the api_key payload — either with the lower-case UI
+// keys or the upper-case env-style keys the env auto-provisioning writes —
+// while a plain-text api_key (PaddleOCR.local bearer token) yields zero
+// values.
func TestPaddleOCRConfigFromAPIKey(t *testing.T) {
cases := []struct {
name string
@@ -92,11 +98,25 @@ func TestPaddleOCRConfigFromAPIKey(t *testing.T) {
wantToken: "",
wantAlgo: "",
},
+ {
+ name: "upper-case env-style payload",
+ apiKey: `{"PADDLEOCR_BASE_URL":"http://envbase.test","PADDLEOCR_ACCESS_TOKEN":"tok-env","PADDLEOCR_ALGORITHM":"PP-OCRv5"}`,
+ wantURL: "http://envbase.test",
+ wantToken: "tok-env",
+ wantAlgo: "PP-OCRv5",
+ },
+ {
+ name: "upper-case api_url fallback",
+ apiKey: `{"PADDLEOCR_API_URL":"http://envapi.test","PADDLEOCR_ACCESS_TOKEN":"tok-env2"}`,
+ wantURL: "http://envapi.test",
+ wantToken: "tok-env2",
+ wantAlgo: "",
+ },
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
- url, token, algo := PaddleOCRConfigFromAPIKey(tc.apiKey)
+ url, token, algo := paddleOCRConfigFromAPIKey(tc.apiKey)
if url != tc.wantURL {
t.Errorf("baseURL = %q, want %q", url, tc.wantURL)
}
@@ -150,6 +170,173 @@ const sampleArrayResult = `[
}
]`
+// TestPaddleOCRResolvedAPIConfig pins the wire-config resolution order shared
+// by both PaddleOCR drivers: bearer token from the api_key payload, base url
+// from the instance field, then the payload, then the PADDLEOCR_* env vars.
+func TestPaddleOCRResolvedAPIConfig(t *testing.T) {
+ t.Run("json payload supplies token and base url", func(t *testing.T) {
+ apiKey := `{"paddleocr_api_url":"http://key.test/api","paddleocr_access_token":"tok-456"}`
+ emptyBaseURL := ""
+ resolved := paddleOCRResolvedAPIConfig(&APIConfig{ApiKey: &apiKey, BaseURL: &emptyBaseURL})
+ if got := *resolved.ApiKey; got != "tok-456" {
+ t.Errorf("ApiKey = %q, want tok-456", got)
+ }
+ if got := *resolved.BaseURL; got != "http://key.test/api" {
+ t.Errorf("BaseURL = %q, want http://key.test/api", got)
+ }
+ })
+
+ t.Run("instance base url wins over payload", func(t *testing.T) {
+ apiKey := `{"paddleocr_base_url":"http://key.test","paddleocr_access_token":"tok-1"}`
+ baseURL := "http://instance.test"
+ resolved := paddleOCRResolvedAPIConfig(&APIConfig{ApiKey: &apiKey, BaseURL: &baseURL})
+ if got := *resolved.BaseURL; got != "http://instance.test" {
+ t.Errorf("BaseURL = %q, want http://instance.test", got)
+ }
+ if got := *resolved.ApiKey; got != "tok-1" {
+ t.Errorf("ApiKey = %q, want tok-1", got)
+ }
+ })
+
+ t.Run("plain token passes through", func(t *testing.T) {
+ apiKey := "tok-plain"
+ baseURL := "http://instance.test"
+ resolved := paddleOCRResolvedAPIConfig(&APIConfig{ApiKey: &apiKey, BaseURL: &baseURL})
+ if got := *resolved.ApiKey; got != "tok-plain" {
+ t.Errorf("ApiKey = %q, want tok-plain", got)
+ }
+ if got := *resolved.BaseURL; got != "http://instance.test" {
+ t.Errorf("BaseURL = %q, want http://instance.test", got)
+ }
+ })
+
+ t.Run("env base url fallback", func(t *testing.T) {
+ t.Setenv("PADDLEOCR_BASE_URL", "http://envbase.test")
+ apiKey := `{"paddleocr_access_token":"tok-env"}`
+ emptyBaseURL := ""
+ resolved := paddleOCRResolvedAPIConfig(&APIConfig{ApiKey: &apiKey, BaseURL: &emptyBaseURL})
+ if got := *resolved.BaseURL; got != "http://envbase.test" {
+ t.Errorf("BaseURL = %q, want http://envbase.test", got)
+ }
+ })
+
+ t.Run("env auto-provisioned payload resolves", func(t *testing.T) {
+ apiKey := `{"PADDLEOCR_BASE_URL":"http://envauto.test","PADDLEOCR_ACCESS_TOKEN":"tok-auto","PADDLEOCR_ALGORITHM":"PaddleOCR-VL"}`
+ emptyBaseURL := ""
+ resolved := paddleOCRResolvedAPIConfig(&APIConfig{ApiKey: &apiKey, BaseURL: &emptyBaseURL})
+ if got := *resolved.ApiKey; got != "tok-auto" {
+ t.Errorf("ApiKey = %q, want tok-auto", got)
+ }
+ if got := *resolved.BaseURL; got != "http://envauto.test" {
+ t.Errorf("BaseURL = %q, want http://envauto.test", got)
+ }
+ })
+
+ t.Run("whitespace instance base url falls back to payload", func(t *testing.T) {
+ apiKey := `{"paddleocr_base_url":"http://key.test","paddleocr_access_token":"tok-2"}`
+ baseURL := " "
+ resolved := paddleOCRResolvedAPIConfig(&APIConfig{ApiKey: &apiKey, BaseURL: &baseURL})
+ if got := *resolved.BaseURL; got != "http://key.test" {
+ t.Errorf("BaseURL = %q, want http://key.test", got)
+ }
+ if got := *resolved.ApiKey; got != "tok-2" {
+ t.Errorf("ApiKey = %q, want tok-2", got)
+ }
+ })
+
+ t.Run("padded instance base url is trimmed", func(t *testing.T) {
+ apiKey := `{"paddleocr_access_token":"tok-3"}`
+ baseURL := " http://instance.test "
+ resolved := paddleOCRResolvedAPIConfig(&APIConfig{ApiKey: &apiKey, BaseURL: &baseURL})
+ if got := *resolved.BaseURL; got != "http://instance.test" {
+ t.Errorf("BaseURL = %q, want http://instance.test", got)
+ }
+ })
+}
+
+// TestPaddleOCRModelOCRFileUnwrapsAPIKeyPayload pins the layering contract the
+// cloud driver owns: OCRFile receives the tenant api_key JSON payload verbatim
+// and resolves the bearer token and server endpoint itself, mirroring Python's
+// PaddleOCROcrModel constructor.
+func TestPaddleOCRModelOCRFileUnwrapsAPIKeyPayload(t *testing.T) {
+ withSSRFBypass(t)
+
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ switch {
+ case r.Method == http.MethodPost && r.URL.Path == "/api/v2/ocr/jobs":
+ if got, want := r.Header.Get("Authorization"), "Bearer tok-123"; got != want {
+ t.Errorf("submit Authorization = %q, want %q", got, want)
+ }
+ _, _ = w.Write([]byte(`{"data":{"jobId":"job-1"}}`))
+ case r.Method == http.MethodGet && r.URL.Path == "/api/v2/ocr/jobs/job-1":
+ if got, want := r.Header.Get("Authorization"), "Bearer tok-123"; got != want {
+ t.Errorf("poll Authorization = %q, want %q", got, want)
+ }
+ _, _ = w.Write([]byte(`{"data":{"state":"done","resultUrl":{"jsonUrl":"http://` + r.Host + `/result"}}}`))
+ case r.Method == http.MethodGet && r.URL.Path == "/result":
+ _, _ = w.Write([]byte(`[{"logId":"l1","errorCode":0,"result":{"layoutParsingResults":[{"markdown":{"text":"# Unwrapped\n"}}]}}]`))
+ default:
+ http.NotFound(w, r)
+ }
+ }))
+ defer server.Close()
+
+ driver := NewPaddleOCRModel(nil, URLSuffix{OCR: "v2/ocr/jobs"})
+ apiKey := fmt.Sprintf(`{"paddleocr_api_url":%q,"paddleocr_access_token":"tok-123"}`, server.URL+"/api")
+ emptyBaseURL := ""
+ modelName := "PaddleOCR-VL-1.6"
+
+ resp, err := driver.OCRFile(context.Background(), &modelName, []byte("%PDF-1.4"), nil, &APIConfig{ApiKey: &apiKey, BaseURL: &emptyBaseURL}, nil, nil)
+ if err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ if resp == nil || resp.Text == nil || !strings.Contains(*resp.Text, "Unwrapped") {
+ t.Fatalf("OCRFile text = %v, want Unwrapped", resp)
+ }
+}
+
+// TestPaddleOCRLocalModelOCRFileResolvesPayload pins the local driver's share
+// of the same contract: bearer token and algorithm are resolved inside the
+// driver from the tenant api_key payload.
+func TestPaddleOCRLocalModelOCRFileResolvesPayload(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 != "/layout-parsing" {
+ http.NotFound(w, r)
+ return
+ }
+ if got, want := r.Header.Get("Authorization"), "Bearer tok-local"; got != want {
+ t.Errorf("Authorization = %q, want %q", got, want)
+ }
+ var body map[string]any
+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
+ t.Errorf("decode body: %v", err)
+ return
+ }
+ if got, want := body["algorithm"], "PaddleOCR-VL"; got != want {
+ t.Errorf("algorithm = %v, want %v", got, want)
+ }
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write([]byte(`{"errorCode":0,"result":{"layoutParsingResults":[{"markdown":{"text":"# Local Title\n"}}]}}`))
+ }))
+ defer server.Close()
+
+ driver := NewPaddleOCRLocalModel(nil, URLSuffix{OCR: "layout-parsing"})
+ apiKey := fmt.Sprintf(`{"paddleocr_base_url":%q,"paddleocr_access_token":"tok-local","paddleocr_algorithm":"PaddleOCR-VL"}`, server.URL)
+ emptyBaseURL := ""
+ modelName := "PaddleOCR-VL"
+
+ resp, err := driver.OCRFile(context.Background(), &modelName, []byte("%PDF-1.4"), nil, &APIConfig{ApiKey: &apiKey, BaseURL: &emptyBaseURL}, nil, nil)
+ if err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ if resp == nil || resp.Text == nil || !strings.Contains(*resp.Text, "Local Title") {
+ t.Fatalf("OCRFile text = %v, want Local Title", resp)
+ }
+}
+
func TestParseOCRResultBodyArray(t *testing.T) {
p := &PaddleOCRModel{}
var md strings.Builder
diff --git a/internal/ingestion/component/parser_dispatch_test.go b/internal/ingestion/component/parser_dispatch_test.go
index d24b1b5521..897e80220b 100644
--- a/internal/ingestion/component/parser_dispatch_test.go
+++ b/internal/ingestion/component/parser_dispatch_test.go
@@ -28,10 +28,7 @@ import (
"archive/zip"
"bytes"
"context"
- "encoding/base64"
- "encoding/json"
"fmt"
- "io"
"net"
"net/http"
"net/http/httptest"
@@ -889,7 +886,7 @@ func TestDispatch_PDFPaddleOCRMarkdown_UsesTenantModel(t *testing.T) {
if got, want := providerName, "PaddleOCR"; got != want {
t.Fatalf("providerName = %q, want %q", got, want)
}
- return &paddleocrTestDriver{}, "PaddleOCR-VL", &models.APIConfig{ApiKey: &apiKey, BaseURL: &baseURL}, 0, nil
+ return models.NewPaddleOCRLocalModel(nil, models.URLSuffix{OCR: "layout-parsing"}), "PaddleOCR-VL", &models.APIConfig{ApiKey: &apiKey, BaseURL: &baseURL}, 0, nil
}
setups := defaultSetups()
@@ -916,30 +913,28 @@ func TestDispatch_PDFPaddleOCRMarkdown_UsesTenantModel(t *testing.T) {
// PaddleOCR configuration contract: the tenant api_key is a JSON payload
// (paddleocr_api_url / paddleocr_access_token / paddleocr_algorithm) and the
// instance base_url field stays empty, mirroring Python's PaddleOCROcrModel.
-// Dispatch must unwrap that payload into a concrete base url, bearer token and
-// algorithm before handing the driver its API config.
+// The driver, not the dispatch layer, unwraps that payload into the wire
+// bearer token and server endpoint.
func TestDispatch_PDFPaddleOCRMarkdown_UsesAPIKeyPayload(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 != "/api/layout-parsing" {
+ w.Header().Set("Content-Type", "application/json")
+ switch {
+ case r.Method == http.MethodPost && r.URL.Path == "/api/v2/ocr/jobs":
+ if got, want := r.Header.Get("Authorization"), "Bearer tok-123"; got != want {
+ t.Errorf("Authorization = %q, want %q (driver must unwrap api_key payload)", got, want)
+ }
+ _, _ = w.Write([]byte(`{"data":{"jobId":"job-1"}}`))
+ case r.Method == http.MethodGet && r.URL.Path == "/api/v2/ocr/jobs/job-1":
+ if got, want := r.Header.Get("Authorization"), "Bearer tok-123"; got != want {
+ t.Errorf("poll Authorization = %q, want %q", got, want)
+ }
+ _, _ = w.Write([]byte(`{"data":{"state":"done","resultUrl":{"jsonUrl":"http://` + r.Host + `/result"}}}`))
+ case r.Method == http.MethodGet && r.URL.Path == "/result":
+ _, _ = w.Write([]byte(`[{"logId":"l1","errorCode":0,"result":{"layoutParsingResults":[{"markdown":{"text":"# Unwrapped Title\n\nUnwrapped body.\n"}}]}}]`))
+ default:
http.NotFound(w, r)
- return
- }
- if got, want := r.Header.Get("Authorization"), "Bearer tok-123"; got != want {
- t.Errorf("Authorization = %q, want %q (must unwrap api_key payload)", got, want)
- return
}
- var body map[string]any
- if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
- t.Errorf("decode body: %v", err)
- return
- }
- if got, want := body["algorithm"], "PaddleOCR-VL"; got != want {
- t.Errorf("algorithm = %v, want %v (must unwrap api_key payload)", got, want)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- _, _ = w.Write([]byte(`{"errorCode":0,"result":{"layoutParsingResults":[{"markdown":{"text":"# Unwrapped Title\n\nUnwrapped body.\n"}}]}}`))
}))
defer server.Close()
@@ -953,7 +948,7 @@ func TestDispatch_PDFPaddleOCRMarkdown_UsesAPIKeyPayload(t *testing.T) {
if got, want := providerName, "PaddleOCR"; got != want {
t.Fatalf("providerName = %q, want %q", got, want)
}
- return &paddleocrTestDriver{}, "PaddleOCR-VL", &models.APIConfig{ApiKey: &apiKey, BaseURL: &emptyBaseURL}, 0, nil
+ return models.NewPaddleOCRModel(nil, models.URLSuffix{OCR: "v2/ocr/jobs"}), "PaddleOCR-VL", &models.APIConfig{ApiKey: &apiKey, BaseURL: &emptyBaseURL}, 0, nil
}
setups := defaultSetups()
@@ -1041,7 +1036,7 @@ func TestDispatch_PDFPaddleOCR_BareModelUUID_UsesExactModel(t *testing.T) {
if got, want := mid, modelID; got != want {
t.Fatalf("modelID = %q, want %q", got, want)
}
- return &paddleocrTestDriver{}, "PaddleOCR-VL", &models.APIConfig{ApiKey: &apiKey, BaseURL: &baseURL}, nil
+ return models.NewPaddleOCRLocalModel(nil, models.URLSuffix{OCR: "layout-parsing"}), "PaddleOCR-VL", &models.APIConfig{ApiKey: &apiKey, BaseURL: &baseURL}, nil
}
setups := defaultSetups()
@@ -1105,7 +1100,7 @@ func TestDispatch_PDFPaddleOCR_BareModelUUID_InParseMethod(t *testing.T) {
if got, want := mid, modelID; got != want {
t.Fatalf("modelID = %q, want %q", got, want)
}
- return &paddleocrTestDriver{}, "PaddleOCR-VL", &models.APIConfig{ApiKey: &apiKey, BaseURL: &baseURL}, nil
+ return models.NewPaddleOCRLocalModel(nil, models.URLSuffix{OCR: "layout-parsing"}), "PaddleOCR-VL", &models.APIConfig{ApiKey: &apiKey, BaseURL: &baseURL}, nil
}
setups := defaultSetups()
@@ -1128,124 +1123,13 @@ func TestDispatch_PDFPaddleOCR_BareModelUUID_InParseMethod(t *testing.T) {
requireJSONText(t, out, "Cloud Paddle Title")
}
-// paddleocrTestDriver is a minimal ModelDriver mock whose Name() returns "paddleocr".
-type paddleocrTestDriver struct{}
-
-func (d *paddleocrTestDriver) NewInstance(baseURL map[string]string) models.ModelDriver { return d }
-func (d *paddleocrTestDriver) Name() string { return "paddleocr" }
-func (d *paddleocrTestDriver) ChatWithMessages(ctx context.Context, modelName string, messages []models.Message, apiConfig *models.APIConfig, chatModelConfig *models.ChatConfig, usage *common.ModelUsage) (*models.ChatResponse, error) {
- return nil, fmt.Errorf("not implemented")
-}
-func (d *paddleocrTestDriver) ChatStreamlyWithSender(ctx context.Context, modelName string, messages []models.Message, apiConfig *models.APIConfig, modelConfig *models.ChatConfig, usage *common.ModelUsage, sender func(*string, *string) error) error {
- return fmt.Errorf("not implemented")
-}
-func (d *paddleocrTestDriver) Embed(ctx context.Context, modelName *string, request models.EmbedRequest, apiConfig *models.APIConfig, embeddingConfig *models.EmbeddingConfig, usage *common.ModelUsage) ([]models.EmbeddingData, error) {
- return nil, fmt.Errorf("not implemented")
-}
-
-func (d *paddleocrTestDriver) Rerank(ctx context.Context, modelName *string, request models.RerankRequest, apiConfig *models.APIConfig, rerankConfig *models.RerankConfig, usage *common.ModelUsage) (*models.RerankResponse, error) {
- return nil, fmt.Errorf("not implemented")
-}
-func (d *paddleocrTestDriver) TranscribeAudio(ctx context.Context, modelName *string, file *string, apiConfig *models.APIConfig, asrConfig *models.ASRConfig, usage *common.ModelUsage) (*models.ASRResponse, error) {
- return nil, fmt.Errorf("not implemented")
-}
-func (d *paddleocrTestDriver) TranscribeAudioWithSender(ctx context.Context, modelName *string, file *string, apiConfig *models.APIConfig, asrConfig *models.ASRConfig, usage *common.ModelUsage, sender func(*string, *string) error) error {
- return fmt.Errorf("not implemented")
-}
-func (d *paddleocrTestDriver) AudioSpeech(ctx context.Context, modelName *string, audioContent *string, apiConfig *models.APIConfig, ttsConfig *models.TTSConfig, usage *common.ModelUsage) (*models.TTSResponse, error) {
- return nil, fmt.Errorf("not implemented")
-}
-func (d *paddleocrTestDriver) AudioSpeechWithSender(ctx context.Context, modelName *string, audioContent *string, apiConfig *models.APIConfig, ttsConfig *models.TTSConfig, usage *common.ModelUsage, sender func(*string, *string) error) error {
- return fmt.Errorf("not implemented")
-}
-
-// OCRFile mimics the local PaddleOCRLocalModel protocol: a synchronous
-// JSON POST to {baseURL}/layout-parsing carrying the file as base64, with
-// Bearer auth when the API config provides a key.
-func (d *paddleocrTestDriver) OCRFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *models.APIConfig, ocrConfig *models.OCRConfig, usage *common.ModelUsage) (*models.OCRFileResponse, error) {
- if apiConfig == nil || apiConfig.BaseURL == nil || *apiConfig.BaseURL == "" {
- return nil, fmt.Errorf("missing base url")
- }
- endpoint := strings.TrimRight(*apiConfig.BaseURL, "/") + "/layout-parsing"
- reqData := map[string]any{
- "file": base64.StdEncoding.EncodeToString(content),
- "fileType": 0,
- }
- if ocrConfig != nil && strings.TrimSpace(ocrConfig.Algorithm) != "" {
- reqData["algorithm"] = ocrConfig.Algorithm
- }
- jsonData, err := json.Marshal(reqData)
- if err != nil {
- return nil, err
- }
- req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(jsonData))
- if err != nil {
- return nil, err
- }
- req.Header.Set("Content-Type", "application/json")
- if auth := models.BearerAuth(apiConfig); auth != "" {
- req.Header.Set("Authorization", auth)
- }
- resp, err := http.DefaultClient.Do(req)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, err
- }
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("status %d: %s", resp.StatusCode, string(body))
- }
- var ocrResp struct {
- Result struct {
- LayoutParsingResults []struct {
- Markdown struct {
- Text string `json:"text"`
- } `json:"markdown"`
- } `json:"layoutParsingResults"`
- } `json:"result"`
- }
- if err := json.Unmarshal(body, &ocrResp); err != nil {
- return nil, err
- }
- var md strings.Builder
- for _, lr := range ocrResp.Result.LayoutParsingResults {
- if lr.Markdown.Text != "" {
- md.WriteString(lr.Markdown.Text)
- md.WriteString("\n\n")
- }
- }
- text := strings.TrimSpace(md.String())
- return &models.OCRFileResponse{Text: &text}, nil
-}
-func (d *paddleocrTestDriver) ParseFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *models.APIConfig, parseFileConfig *models.ParseFileConfig, usage *common.ModelUsage) (*models.ParseFileResponse, error) {
- return nil, fmt.Errorf("not implemented")
-}
-func (d *paddleocrTestDriver) ListModels(ctx context.Context, apiConfig *models.APIConfig) ([]models.ListModelResponse, error) {
- return nil, fmt.Errorf("not implemented")
-}
-func (d *paddleocrTestDriver) Balance(ctx context.Context, apiConfig *models.APIConfig) (map[string]interface{}, error) {
- return nil, fmt.Errorf("not implemented")
-}
-func (d *paddleocrTestDriver) CheckConnection(ctx context.Context, apiConfig *models.APIConfig) error {
- return fmt.Errorf("not implemented")
-}
-func (d *paddleocrTestDriver) ListTasks(ctx context.Context, apiConfig *models.APIConfig) ([]models.ListTaskStatus, error) {
- return nil, fmt.Errorf("not implemented")
-}
-func (d *paddleocrTestDriver) ShowTask(ctx context.Context, taskID string, apiConfig *models.APIConfig) (*models.TaskResponse, error) {
- return nil, fmt.Errorf("not implemented")
-}
-
func TestIsPaddleOCRDriver(t *testing.T) {
for _, tc := range []struct {
name string
d models.ModelDriver
want bool
}{
- {"local", &paddleocrTestDriver{}, true},
+ {"local", &models.PaddleOCRLocalModel{}, true},
{"remote", &models.PaddleOCRModel{}, true},
{"dummy", &models.DummyModel{}, false},
} {
diff --git a/internal/ingestion/component/pdf_vision_dispatch.go b/internal/ingestion/component/pdf_vision_dispatch.go
index b18a5e2d15..7276db99bf 100644
--- a/internal/ingestion/component/pdf_vision_dispatch.go
+++ b/internal/ingestion/component/pdf_vision_dispatch.go
@@ -579,59 +579,8 @@ func dispatchPaddleOCRPdf(
"parser: PaddleOCR requires a PaddleOCR OCR model; found %q. Please add a PaddleOCR OCR model to your tenant", driver.Name())
}
- // Align with Python's PaddleOCROcrModel: the tenant api_key for the cloud
- // PaddleOCR provider is a JSON payload carrying paddleocr_base_url /
- // paddleocr_api_url, paddleocr_access_token and paddleocr_algorithm, while
- // the instance base_url field stays empty. PaddleOCR.local keeps a
- // plain-text bearer token in api_key and its base url in the instance
- // extra, so a non-JSON api_key passes through untouched.
- keyBaseURL, keyAccessToken, keyAlgorithm := "", "", ""
- if apiConfig.ApiKey != nil {
- keyBaseURL, keyAccessToken, keyAlgorithm = modelModule.PaddleOCRConfigFromAPIKey(*apiConfig.ApiKey)
- }
-
- baseURL := ""
- if apiConfig.BaseURL != nil {
- baseURL = *apiConfig.BaseURL
- }
- if baseURL == "" {
- baseURL = keyBaseURL
- }
- if baseURL == "" {
- baseURL = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRBaseUrl))
- }
- if baseURL == "" {
- baseURL = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRAPIURL))
- }
- if baseURL == "" {
- return parser.ParseResult{}, fmt.Errorf(
- "parser: PaddleOCR requires a base url from the tenant PaddleOCR OCR model or PADDLEOCR_BASE_URL")
- }
-
- apiKey := ""
- if apiConfig.ApiKey != nil {
- apiKey = *apiConfig.ApiKey
- }
- if keyAccessToken != "" {
- apiKey = keyAccessToken
- }
- algorithm := strings.TrimSpace(getStringOr(setup, "paddleocr_algorithm", ""))
- if algorithm == "" {
- algorithm = keyAlgorithm
- }
- if algorithm == "" {
- algorithm = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRAlgorithm))
- }
- if algorithm == "" {
- algorithm = "PaddleOCR-VL"
- }
- ocrAPIConfig := &modelModule.APIConfig{BaseURL: &baseURL}
- if apiKey != "" {
- ocrAPIConfig.ApiKey = &apiKey
- }
-
- resp, err := driver.OCRFile(ctx, &modelName, binary, &filename, ocrAPIConfig, &modelModule.OCRConfig{
- Algorithm: algorithm,
+ resp, err := driver.OCRFile(ctx, &modelName, binary, &filename, apiConfig, &modelModule.OCRConfig{
+ Algorithm: strings.TrimSpace(getStringOr(setup, "paddleocr_algorithm", "")),
}, nil)
if err != nil {
return parser.ParseResult{}, fmt.Errorf("parser: PaddleOCR OCRFile: %w", err)
diff --git a/internal/service/model_service.go b/internal/service/model_service.go
index baa4bf5d00..cfcea2ebe1 100644
--- a/internal/service/model_service.go
+++ b/internal/service/model_service.go
@@ -1031,20 +1031,6 @@ func (m *ModelProviderService) CheckConnection(ctx context.Context, providerName
BaseURL: &baseURL,
}
- // The cloud PaddleOCR provider carries its config inside the api_key JSON
- // payload (paddleocr_api_url / paddleocr_access_token / paddleocr_algorithm),
- // mirroring Python's PaddleOCROcrModel. Unwrap the access token and base url
- // so the OCR driver authenticates with a plain bearer token. Non-JSON
- // api_keys (e.g. PaddleOCR.local) pass through untouched.
- if keyBaseURL, keyAccessToken, _ := modelModule.PaddleOCRConfigFromAPIKey(apiKey); keyAccessToken != "" {
- apiKey = keyAccessToken
- if baseURL == "" {
- baseURL = keyBaseURL
- }
- apiConfig.ApiKey = &apiKey
- apiConfig.BaseURL = &baseURL
- }
-
// Mirror Python verify_api_key: verify each model by making a real
// lightweight API request. Returns per-model verify results.
modelVerifyResult, verifyErr := verifyProviderModel(ctx, driver, providerInfo.Models, apiConfig, modelInfo)