Skip to content

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

14.0 KB
Raw Blame
//
//  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"
	"fmt"
	"io"
	"mime/multipart"
	"net/http"
	"net/url"
	"ragflow/internal/common"
	"strings"
)

type MinerULocalModel struct {
	baseModel BaseModel
}

func NewMinerLocalUModel(baseURL map[string]string, urlSuffix URLSuffix) *MinerULocalModel {
	return &MinerULocalModel{
		baseModel: BaseModel{
			BaseURL:          baseURL,
			URLSuffix:        urlSuffix,
			AllowEmptyAPIKey: true,
			httpClient:       common.GetSchemeSafeHTTPClient(),
		},
	}
}

func (m *MinerULocalModel) NewInstance(baseURL map[string]string) ModelDriver {
	return NewMinerLocalUModel(baseURL, m.baseModel.URLSuffix)
}

func (m *MinerULocalModel) Name() string {
	return "mineru"
}

// GetBaseURL prefers the instance URL, then mineru_apiserver from the
// provider api_key JSON, then MINERU_APISERVER, then the factory default.
func (m *MinerULocalModel) GetBaseURL(apiConfig *APIConfig) (string, error) {
	if u := ResolveMinerUAPIServer(apiConfig); u != "" {
		return u, nil
	}
	return m.baseModel.GetBaseURL(apiConfig)
}

func (m *MinerULocalModel) 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 *MinerULocalModel) 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 *MinerULocalModel) 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 *MinerULocalModel) 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 *MinerULocalModel) 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 *MinerULocalModel) 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 *MinerULocalModel) 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 *MinerULocalModel) 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 is exercised during model verification (verifyOCRModel). Local MinerU
// is a document-parsing API and has no image-OCR endpoint, so delegate to
// CheckConnection — same pattern as the hosted mineru.net driver (mineru.go).
func (m *MinerULocalModel) OCRFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *APIConfig, ocrConfig *OCRConfig, modelUsage *common.ModelUsage) (*OCRFileResponse, error) {
	if err := m.CheckConnection(ctx, apiConfig); err != nil {
		return nil, err
	}
	return &OCRFileResponse{}, nil
}

func (m *MinerULocalModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]ListModelResponse, error) {
	if err := m.CheckConnection(ctx, apiConfig); err != nil {
		return nil, err
	}
	return []ListModelResponse{{
		Name:       "MinerU-model",
		ModelTypes: []string{"ocr"},
	}}, nil
}

func (m *MinerULocalModel) Balance(ctx context.Context, apiConfig *APIConfig) (map[string]interface{}, error) {
	return nil, fmt.Errorf("%s: no such method: Balance", m.Name())
}

func (m *MinerULocalModel) CheckConnection(ctx context.Context, apiConfig *APIConfig) error {
	if err := m.baseModel.APIConfigCheck(apiConfig); err != nil {
		return err
	}
	resolvedBaseURL, err := m.GetBaseURL(apiConfig)
	if err != nil {
		return err
	}
	if !strings.HasPrefix(resolvedBaseURL, "http://") && !strings.HasPrefix(resolvedBaseURL, "https://") {
		resolvedBaseURL = "http://" + resolvedBaseURL
	}

	u, err := url.Parse(resolvedBaseURL)
	if err != nil {
		return fmt.Errorf("invalid base URL: %w", err)
	}
	token := ""
	if apiConfig != nil && apiConfig.ApiKey != nil {
		token = MinerUBearerTokenFromAPIKey(*apiConfig.ApiKey)
	}
	ctx, cancel := context.WithTimeout(ctx, nonStreamCallTimeout)
	defer cancel()

	// mineru.net is the hosted product. It has no /v1/health; reachability of
	// the site is the check. A self-hosted URL must answer with MinerU's
	// health JSON. A reachable server that is not MinerU, including one that
	// answers 404, is a failure.
	if IsMinerUCloudBase(u.String()) {
		return minerUCloudReachable(ctx, m.baseModel.httpClient, u)
	}
	v1URL := *u
	v1URL.Path = "/v1/health"
	v1Code, v1Body, v1Err := minerUGET(ctx, m.baseModel.httpClient, v1URL.String(), token)
	if v1Err == nil && (v1Code == http.StatusUnauthorized || v1Code == http.StatusForbidden) {
		return fmt.Errorf("authentication failed (HTTP %d)", v1Code)
	}
	if v1Err == nil && minerUHealthOK(v1Code, v1Body) {
		return nil
	}

	legacy := *u
	legacy.Path = "/health"
	code, body, err := minerUGET(ctx, m.baseModel.httpClient, legacy.String(), token)
	if err != nil {
		if v1Err != nil {
			return fmt.Errorf("connection failed: %w", v1Err)
		}
		return fmt.Errorf("connection failed: %w", err)
	}
	if code == http.StatusUnauthorized || code == http.StatusForbidden {
		return fmt.Errorf("authentication failed (HTTP %d)", code)
	}
	if minerUHealthOK(code, body) {
		return nil
	}
	if v1Err != nil {
		return fmt.Errorf("not a MinerU service: %s: %w", v1URL.String(), v1Err)
	}
	return fmt.Errorf("not a MinerU service: %s HTTP %d, %s HTTP %d", v1URL.String(), v1Code, legacy.String(), code)
}

func minerUGET(ctx context.Context, client *http.Client, rawURL, token string) (int, []byte, error) {
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
	if err != nil {
		return 0, nil, err
	}
	if token != "" {
		req.Header.Set("Authorization", "Bearer "+token)
	}
	resp, err := client.Do(req)
	if err != nil {
		return 0, nil, err
	}
	defer resp.Body.Close()
	body, err := io.ReadAll(io.LimitReader(resp.Body, 4096))
	if err != nil {
		return resp.StatusCode, nil, err
	}
	return resp.StatusCode, body, nil
}

func minerUHealthOK(code int, body []byte) bool {
	if code < http.StatusOK || code >= http.StatusMultipleChoices {
		return false
	}
	var payload struct {
		Status  string `json:"status"`
		Version string `json:"version"`
	}
	if err := json.Unmarshal(body, &payload); err != nil {
		return false
	}
	switch strings.ToLower(strings.TrimSpace(payload.Status)) {
	case "ok", "healthy":
		return true
	}
	return strings.TrimSpace(payload.Version) != ""
}

func minerUCloudReachable(ctx context.Context, client *http.Client, u *url.URL) error {
	origin := u.Scheme + "://" + u.Host + "/"
	code, _, err := minerUGET(ctx, client, origin, "")
	if err != nil {
		return fmt.Errorf("connection failed: %w", err)
	}
	if code < http.StatusOK || code >= http.StatusMultipleChoices {
		return fmt.Errorf("not a MinerU service: %s HTTP %d", origin, code)
	}
	return nil
}

func (m *MinerULocalModel) ParseFile(ctx context.Context, modelName *string, content []byte, documentURL *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig, modelUsage *common.ModelUsage) (*ParseFileResponse, error) {
	if err := m.baseModel.APIConfigCheck(apiConfig); err != nil {
		return nil, err
	}

	if len(content) == 0 {
		return nil, fmt.Errorf("local MinerU API requires file content byte array, but content is empty")
	}

	resolvedBaseURL, err := m.GetBaseURL(apiConfig)
	if err != nil {
		return nil, err
	}
	apiURL := joinURL(resolvedBaseURL, m.baseModel.URLSuffix.DocumentParse)

	var body bytes.Buffer
	writer := multipart.NewWriter(&body)

	// Get file
	part, err := writer.CreateFormFile("files", "upload_document.pdf")
	if err != nil {
		return nil, fmt.Errorf("failed to create multipart file field: %w", err)
	}
	if _, err = part.Write(content); err != nil {
		return nil, fmt.Errorf("failed to write file content: %w", err)
	}

	apiKeyRaw := ""
	if apiConfig != nil && apiConfig.ApiKey != nil {
		apiKeyRaw = *apiConfig.ApiKey
	}
	setupBackend := ""
	if modelName != nil && strings.TrimSpace(*modelName) != "" {
		setupBackend = strings.TrimSpace(*modelName)
	} else if parseFileConfig != nil {
		setupBackend = strings.TrimSpace(parseFileConfig.Backend)
	}
	setupServerURL := ""
	if parseFileConfig != nil {
		setupServerURL = parseFileConfig.ServerURL
	}
	backend := ResolveMinerUBackend(setupBackend, apiKeyRaw)
	serverURL := ResolveMinerUServerURL(setupServerURL, apiKeyRaw)
	if err := ValidateMinerUConfig(backend, serverURL); err != nil {
		return nil, err
	}
	_ = writer.WriteField("backend", backend)
	if serverURL != "" {
		_ = writer.WriteField("server_url", serverURL)
	}

	if err = writer.Close(); err != nil {
		return nil, fmt.Errorf("failed to close multipart writer: %w", err)
	}

	ctx, cancel := context.WithTimeout(ctx, longOpCallTimeout)
	defer cancel()

	req, err := http.NewRequestWithContext(ctx, "POST", apiURL, &body)
	if err != nil {
		return nil, fmt.Errorf("failed to create request: %w", err)
	}

	req.Header.Set("Content-Type", writer.FormDataContentType())

	if apiConfig != nil && apiConfig.ApiKey != nil {
		if token := MinerUBearerTokenFromAPIKey(*apiConfig.ApiKey); token != "" {
			req.Header.Set("Authorization", "Bearer "+token)
		}
	}

	resp, err := m.baseModel.httpClient.Do(req)
	if err != nil {
		return nil, fmt.Errorf("failed to send request: %w", err)
	}
	defer resp.Body.Close()

	respBody, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("failed to read response: %w", err)
	}

	if resp.StatusCode != http.StatusOK && resp.StatusCode != 202 {
		return nil, fmt.Errorf("local MinerU API failed with status %d: %s (URL: %s)", resp.StatusCode, string(respBody), apiURL)
	}

	var result map[string]interface{}
	if err := json.Unmarshal(respBody, &result); err != nil {
		return nil, fmt.Errorf("failed to parse response JSON: %w, body: %s", err, string(respBody))
	}
	// Get task ID
	var taskID string
	if dataMap, ok := result["data"].(map[string]interface{}); ok {
		if tid, ok := dataMap["task_id"].(string); ok {
			taskID = tid
		}
	} else if tid, ok := result["task_id"].(string); ok {
		taskID = tid
	}

	if taskID == "" {
		return nil, fmt.Errorf("failed to extract task_id from local MinerU response: %s", string(respBody))
	}

	return &ParseFileResponse{
		TaskID: taskID,
	}, nil
}

func (m *MinerULocalModel) ListTasks(ctx context.Context, apiConfig *APIConfig) ([]ListTaskStatus, error) {
	return nil, fmt.Errorf("%s: no such method: ListTasks", m.Name())
}

func (m *MinerULocalModel) ShowTask(ctx context.Context, taskID string, apiConfig *APIConfig) (*TaskResponse, error) {
	if err := m.baseModel.APIConfigCheck(apiConfig); err != nil {
		return nil, err
	}

	if taskID == "" {
		return nil, fmt.Errorf("taskID is empty")
	}

	taskURL, err := m.baseModel.EndpointURL(apiConfig, m.baseModel.URLSuffix.Task)
	if err != nil {
		return nil, err
	}
	url := taskURL + "/" + taskID + "/result"
	ctx, cancel := context.WithTimeout(ctx, nonStreamCallTimeout)
	defer cancel()

	req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to create status request: %w", err)
	}

	if apiConfig != nil && apiConfig.ApiKey != nil {
		if token := MinerUBearerTokenFromAPIKey(*apiConfig.ApiKey); token != "" {
			req.Header.Set("Authorization", "Bearer "+token)
		}
	}

	resp, err := m.baseModel.httpClient.Do(req)
	if err != nil {
		return nil, fmt.Errorf("failed to send status request: %w", err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("failed to read status response: %w", err)
	}

	if resp.StatusCode != http.StatusOK && resp.StatusCode != 202 {
		return nil, fmt.Errorf("MinerU local status API failed with status %d: %s", resp.StatusCode, string(body))
	}

	// parse JSON
	var result map[string]interface{}

	if err := json.Unmarshal(body, &result); err != nil {
		return nil, fmt.Errorf("failed to parse JSON: %w", err)
	}

	content := ""

	// results
	results, ok := result["results"].(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("missing results field")
	}

	// Get Markdown
	for _, fileObj := range results {

		fileMap, ok := fileObj.(map[string]interface{})
		if !ok {
			continue
		}

		md, ok := fileMap["md_content"].(string)
		if ok {
			content = md
			break
		}
	}

	if content == "" {
		return nil, fmt.Errorf("md_content not found")
	}

	return &TaskResponse{
		Segments: []TaskSegment{
			{
				Index:   1,
				Content: content,
			},
		},
	}, nil
}

func (m *MinerULocalModel) 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())
}