Skip to content

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

Go: implement PaddleOCR provider and implement ASR for CoHere (#14954)

Haruko386bf41d3572987
diff --git a/conf/models/baidu.json b/conf/models/baidu.json
index 4313b6a6d1..1c584d27ad 100644
--- a/conf/models/baidu.json
+++ b/conf/models/baidu.json
@@ -7,7 +7,8 @@
"chat": "chat/completions",
"models": "models",
"embedding": "embeddings",
- "rerank": "rerank"
+ "rerank": "rerank",
+ "ocr": "ocr/paddleocr"
},
"class": "baidu",
"models": [
@@ -74,6 +75,13 @@
"model_types": [
"rerank"
]
+ },
+ {
+ "name": "paddleocr-vl-0.9b",
+ "max_tokens": 8192,
+ "model_types": [
+ "ocr"
+ ]
}
]
}
\ No newline at end of file
diff --git a/conf/models/cohere.json b/conf/models/cohere.json
index 8b5ef93ff7..98242d49a9 100644
--- a/conf/models/cohere.json
+++ b/conf/models/cohere.json
@@ -7,7 +7,8 @@
"chat": "v2/chat",
"models": "v1/models",
"embeddings": "v2/embed",
- "rerank": "v2/rerank"
+ "rerank": "v2/rerank",
+ "asr": "audio/transcriptions"
},
"class": "cohere",
"models": [
@@ -38,6 +39,13 @@
"model_types": [
"embedding"
]
+ },
+ {
+ "name": "cohere-transcribe-03-2026",
+ "max_tokens": 8192,
+ "model_types": [
+ "asr"
+ ]
}
]
}
\ No newline at end of file
diff --git a/conf/models/mistral.json b/conf/models/mistral.json
index fefc4833a6..be9cbb1861 100644
--- a/conf/models/mistral.json
+++ b/conf/models/mistral.json
@@ -1,12 +1,13 @@
{
"name": "Mistral",
"url": {
- "default": "https://api.mistral.ai/v1"
+ "default": "https://api.mistral.ai"
},
"url_suffix": {
- "chat": "chat/completions",
- "models": "models",
- "embedding": "embeddings"
+ "chat": "v1/chat/completions",
+ "models": "v1/models",
+ "embedding": "v1/embeddings",
+ "ocr": "v1/ocr"
},
"class": "mistral",
"models": [
@@ -94,6 +95,13 @@
"model_types": [
"embedding"
]
+ },
+ {
+ "name": "mistral-ocr-2512",
+ "max_tokens": 8192,
+ "model_types": [
+ "ocr"
+ ]
}
]
}
diff --git a/conf/models/paddleocr.json b/conf/models/paddleocr.json
new file mode 100644
index 0000000000..043921b803
--- /dev/null
+++ b/conf/models/paddleocr.json
@@ -0,0 +1,33 @@
+{
+ "name": "PaddleOCR",
+ "url": {
+ "default": "https://paddleocr.aistudio-app.com/api"
+ },
+ "url_suffix": {
+ "ocr": "v2/ocr/jobs"
+ },
+ "class": "paddleocr",
+ "models": [
+ {
+ "name": "PaddleOCR-VL-1.5",
+ "max_tokens": 16384,
+ "model_types": [
+ "ocr"
+ ]
+ },
+ {
+ "name": "PP-OCRv5",
+ "max_tokens": 16384,
+ "model_types": [
+ "ocr"
+ ]
+ },
+ {
+ "name": "PP-StructureV3",
+ "max_tokens": 16384,
+ "model_types": [
+ "ocr"
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/conf/models/stepfun.json b/conf/models/stepfun.json
index 34898c5a1b..06ef52d673 100644
--- a/conf/models/stepfun.json
+++ b/conf/models/stepfun.json
@@ -91,7 +91,7 @@
]
},
{
- "name": "step-tts-2 ",
+ "name": "step-tts-2",
"max_tokens": 8192,
"model_types": [
"tts"
diff --git a/internal/development.md b/internal/development.md
index 7e31a8df3e..ae2758cb51 100644
--- a/internal/development.md
+++ b/internal/development.md
@@ -372,4 +372,14 @@ RAGFlow(user)> asr with 'FunAudioLLM/SenseVoiceSmall@test@siliconflow' audio './
+----------------------------------------------------------------------------------------------------------------------+
| The examination and testimony of the experts enabled the commission to conclude that five shots may have been fired. |
+----------------------------------------------------------------------------------------------------------------------+
+```
+
+### 6.25 Optical Character Recognition\
+```
+RAGFlow(user)> ocr with 'paddleocr-vl-0.9b@test@baidu' file './internal/text.jpg'
++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
+| text |
++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
+| Parallel to these organizational innovations there were significant complementary technical innovations (e.g., improved methods of manufacturing cast-iron pipe and of coating interiors for pressure maintenance, and newer paving and construction material... |
++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
```
\ No newline at end of file
diff --git a/internal/entity/models/aliyun.go b/internal/entity/models/aliyun.go
index 12ee525ca0..79a01b02f7 100644
--- a/internal/entity/models/aliyun.go
+++ b/internal/entity/models/aliyun.go
@@ -564,8 +564,8 @@ func (z *AliyunModel) TranscribeAudioWithSender(modelName *string, file *string,
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (z *AliyunModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (z *AliyunModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", z.Name())
}
diff --git a/internal/entity/models/baichuan.go b/internal/entity/models/baichuan.go
index 98c8f6752f..d139b8a672 100644
--- a/internal/entity/models/baichuan.go
+++ b/internal/entity/models/baichuan.go
@@ -389,8 +389,8 @@ func (z *BaichuanModel) TranscribeAudioWithSender(modelName *string, file *strin
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (z *BaichuanModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (z *BaichuanModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", z.Name())
}
diff --git a/internal/entity/models/baidu.go b/internal/entity/models/baidu.go
index 4b99ec7221..b2bcf84393 100644
--- a/internal/entity/models/baidu.go
+++ b/internal/entity/models/baidu.go
@@ -3,6 +3,8 @@ package models
import (
"bufio"
"bytes"
+ "context"
+ "encoding/base64"
"encoding/json"
"fmt"
"io"
@@ -611,8 +613,8 @@ func (z *BaiduModel) TranscribeAudioWithSender(modelName *string, file *string,
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (b *BaiduModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (b *BaiduModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", b.Name())
}
@@ -621,13 +623,99 @@ func (z *BaiduModel) AudioSpeechWithSender(modelName *string, audioContent *stri
}
// OCRFile OCR file
-func (b *BaiduModel) OCRFile(modelName *string, content []byte, url *string, apiConfig *APIConfig, ocrConfig *OCRConfig) (*OCRFileResponse, error) {
- return nil, fmt.Errorf("%s, no such method", b.Name())
+type qianfanOCRResponse struct {
+ Id string `json:"id"`
+ Result struct {
+ LayoutParsingResults []struct {
+ Markdown struct {
+ Text string `json:"text"`
+ } `json:"markdown"`
+ } `json:"layoutParsingResults"`
+ } `json:"result"`
}
-// ParseFile parse file
-func (z *BaiduModel) ParseFile(modelName *string, content []byte, url *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig) (*ParseFileResponse, error) {
- return nil, fmt.Errorf("%s, no such method", z.Name())
+func (b *BaiduModel) OCRFile(modelName *string, content []byte, fileURL *string, apiConfig *APIConfig, ocrConfig *OCRConfig) (*OCRFileResponse, error) {
+ if (fileURL == nil || *fileURL == "") && (content == nil || len(content) == 0) {
+ return nil, fmt.Errorf("image url or content is required")
+ }
+
+ region := "default"
+ if apiConfig.Region != nil && *apiConfig.Region != "" {
+ region = *apiConfig.Region
+ }
+
+ url := fmt.Sprintf("%s/%s", b.BaseURL[region], b.URLSuffix.OCR)
+
+ reqData := map[string]interface{}{
+ "model": *modelName,
+ }
+
+ if fileURL != nil && *fileURL != "" {
+ reqData["file"] = *fileURL
+ if strings.HasSuffix(strings.ToLower(*fileURL), ".pdf") {
+ reqData["fileType"] = 0 // PDF
+ } else {
+ reqData["fileType"] = 1 // img
+ }
+ } else if content != nil && len(content) > 0 {
+ reqData["file"] = base64.StdEncoding.EncodeToString(content)
+
+ mimeType := http.DetectContentType(content)
+ if strings.Contains(mimeType, "pdf") {
+ reqData["fileType"] = 0 // PDF
+ } else {
+ reqData["fileType"] = 1 // img
+ }
+ }
+
+ jsonData, err := json.Marshal(reqData)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal json payload: %w", err)
+ }
+
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ defer cancel()
+
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
+ if err != nil {
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
+
+ resp, err := b.httpClient.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("failed to send request: %w", err)
+ }
+ defer resp.Body.Close()
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read response: %w", err)
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("API error: %s, body: %s", resp.Status, string(body))
+ }
+
+ var apiResponse qianfanOCRResponse
+ if err = json.Unmarshal(body, &apiResponse); err != nil {
+ return nil, fmt.Errorf("failed to parse response json: %w", err)
+ }
+
+ var extractedText string
+ if len(apiResponse.Result.LayoutParsingResults) > 0 {
+ extractedText = apiResponse.Result.LayoutParsingResults[0].Markdown.Text
+ } else {
+ return nil, fmt.Errorf("no parsing results returned from API")
+ }
+
+ var ocrResponse = OCRFileResponse{
+ Text: &extractedText,
+ }
+
+ return &ocrResponse, nil
}
func (b *BaiduModel) ListModels(apiConfig *APIConfig) ([]string, error) {
@@ -694,10 +782,14 @@ func (b *BaiduModel) CheckConnection(apiConfig *APIConfig) error {
return err
}
+func (z *BaiduModel) ParseFile(modelName *string, content []byte, url *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig) (*ParseFileResponse, error) {
+ return nil, fmt.Errorf("no such method", z.Name())
+}
+
func (z *BaiduModel) ListTasks(apiConfig *APIConfig) ([]ListTaskStatus, error) {
- return nil, fmt.Errorf("%s, no such method", z.Name())
+ return nil, fmt.Errorf("no such method", z.Name())
}
func (z *BaiduModel) ShowTask(taskID string, apiConfig *APIConfig) (*TaskResponse, error) {
- return nil, fmt.Errorf("%s, no such method", z.Name())
+ return nil, fmt.Errorf("no such method", z.Name())
}
diff --git a/internal/entity/models/cohere.go b/internal/entity/models/cohere.go
index f078737ecf..1d16ece24d 100644
--- a/internal/entity/models/cohere.go
+++ b/internal/entity/models/cohere.go
@@ -6,7 +6,11 @@ import (
"encoding/json"
"fmt"
"io"
+ "mime/multipart"
"net/http"
+ "os"
+ "path/filepath"
+ "strconv"
"strings"
"time"
)
@@ -482,15 +486,118 @@ func (c *CoHereModel) Rerank(modelName *string, query string, documents []string
// TranscribeAudio transcribe audio
func (c *CoHereModel) TranscribeAudio(modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig) (*ASRResponse, error) {
- return nil, fmt.Errorf("%s, no such method", c.Name())
+ if file == nil || *file == "" {
+ return nil, fmt.Errorf("file is missing")
+ }
+
+ region := "default"
+ if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
+ region = *apiConfig.Region
+ }
+
+ url := fmt.Sprintf("%s/%s", c.BaseURL[region], c.URLSuffix.ASR)
+
+ // multipart body
+
+ var body bytes.Buffer
+ writer := multipart.NewWriter(&body)
+
+ // open audio file
+ audioFile, err := os.Open(*file)
+ if err != nil {
+ return nil, fmt.Errorf("failed to open audio file: %w", err)
+ }
+ defer audioFile.Close()
+
+ // create multipart file field
+
+ if err = writer.WriteField("model", *modelName); err != nil {
+ return nil, fmt.Errorf("failed to write model name: %w", err)
+ }
+ // extra params
+ if asrConfig != nil && asrConfig.Params != nil {
+ for key, value := range asrConfig.Params {
+
+ var val string
+
+ switch v := value.(type) {
+ case string:
+ val = v
+ case bool:
+ val = strconv.FormatBool(v)
+ case int:
+ val = strconv.Itoa(v)
+ case int64:
+ val = strconv.FormatInt(v, 10)
+ case float32:
+ val = strconv.FormatFloat(float64(v), 'f', -1, 32)
+ case float64:
+ val = strconv.FormatFloat(v, 'f', -1, 64)
+ default:
+ val = fmt.Sprintf("%v", v)
+ }
+
+ if err = writer.WriteField(key, val); err != nil {
+ return nil, fmt.Errorf("failed to write field %s: %w", key, err)
+ }
+ }
+ }
+
+ // all form fields (model, language) must appear before the file part in the multipart body
+ part, err := writer.CreateFormFile("file", filepath.Base(*file))
+ if err != nil {
+ return nil, fmt.Errorf("failed to create form file: %w", err)
+ }
+
+ if _, err := io.Copy(part, audioFile); err != nil {
+ return nil, fmt.Errorf("failed to copy audio file: %w", err)
+ }
+
+ if err = writer.Close(); err != nil {
+ return nil, fmt.Errorf("failed to close writer: %w", err)
+ }
+
+ // build request
+ req, err := http.NewRequest("POST", url, &body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+
+ req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
+ req.Header.Set("Content-Type", writer.FormDataContentType())
+
+ resp, err := c.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 {
+ return nil, fmt.Errorf("Cohere ASR API error: status %d, body: %s", resp.StatusCode, string(respBody))
+ }
+
+ var result struct {
+ Text string `json:"text"`
+ }
+
+ if err = json.Unmarshal(respBody, &result); err != nil {
+ return nil, fmt.Errorf("failed to unmarshal response: %w", err)
+ }
+
+ return &ASRResponse{Text: result.Text}, nil
}
func (z *CoHereModel) TranscribeAudioWithSender(modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig, sender func(*string, *string) error) error {
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (c *CoHereModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (c *CoHereModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", c.Name())
}
@@ -514,15 +621,7 @@ func (c *CoHereModel) ListModels(apiConfig *APIConfig) ([]string, error) {
region = *apiConfig.Region
}
- baseURL := c.BaseURL[region]
- if baseURL == "" {
- baseURL = c.BaseURL["default"]
- }
- if baseURL == "" {
- baseURL = "https://api.cohere.com"
- }
- suffix := c.URLSuffix.Models
- url := fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), strings.TrimPrefix(suffix, "/"))
+ url := fmt.Sprintf("%s/%s", c.BaseURL[region], c.URLSuffix.Models)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
diff --git a/internal/entity/models/deepseek.go b/internal/entity/models/deepseek.go
index 7c809c71cf..78f5607c42 100644
--- a/internal/entity/models/deepseek.go
+++ b/internal/entity/models/deepseek.go
@@ -594,8 +594,8 @@ func (z *DeepSeekModel) TranscribeAudioWithSender(modelName *string, file *strin
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (d *DeepSeekModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (d *DeepSeekModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", d.Name())
}
diff --git a/internal/entity/models/dummy.go b/internal/entity/models/dummy.go
index d80616bd04..718dd4bca1 100644
--- a/internal/entity/models/dummy.go
+++ b/internal/entity/models/dummy.go
@@ -83,8 +83,8 @@ func (z *DummyModel) TranscribeAudioWithSender(modelName *string, file *string,
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (d *DummyModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (d *DummyModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", d.Name())
}
diff --git a/internal/entity/models/factory.go b/internal/entity/models/factory.go
index 581baa5133..2df11aec48 100644
--- a/internal/entity/models/factory.go
+++ b/internal/entity/models/factory.go
@@ -91,6 +91,8 @@ func (f *ModelFactory) CreateModelDriver(providerName string, baseURL map[string
return NewNovitaModel(baseURL, urlSuffix), nil
case "voyage":
return NewVoyageModel(baseURL, urlSuffix), nil
+ case "paddleocr":
+ return NewPaddleOCRModel(baseURL, urlSuffix), nil
default:
return NewDummyModel(baseURL, urlSuffix), nil
}
diff --git a/internal/entity/models/fishaudio.go b/internal/entity/models/fishaudio.go
index dc185e8975..70f6707621 100644
--- a/internal/entity/models/fishaudio.go
+++ b/internal/entity/models/fishaudio.go
@@ -176,8 +176,8 @@ func (f *FishAudioModel) TranscribeAudioWithSender(modelName *string, file *stri
return fmt.Errorf("%s, no such method", f.Name())
}
-// AudioSpeech convert audio to text
-func (f *FishAudioModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (f *FishAudioModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
return nil, fmt.Errorf("FishAudio API key is missing")
}
@@ -197,13 +197,13 @@ func (f *FishAudioModel) AudioSpeech(modelName *string, audioContent *string, ap
"text": *audioContent,
}
- if asrConfig != nil && asrConfig.Params != nil {
- for key, value := range asrConfig.Params {
+ if ttsConfig != nil && ttsConfig.Params != nil {
+ for key, value := range ttsConfig.Params {
reqBody[key] = value
}
}
- if asrConfig != nil && asrConfig.Format != "" {
- reqBody["format"] = asrConfig.Format
+ if ttsConfig != nil && ttsConfig.Format != "" {
+ reqBody["format"] = ttsConfig.Format
}
jsonData, err := json.Marshal(reqBody)
@@ -238,7 +238,7 @@ func (f *FishAudioModel) AudioSpeech(modelName *string, audioContent *string, ap
return &TTSResponse{Audio: body}, nil
}
-func (f *FishAudioModel) AudioSpeechWithSender(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig, sender func(*string, *string) error) error {
+func (f *FishAudioModel) AudioSpeechWithSender(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, sender func(*string, *string) error) error {
if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
return fmt.Errorf("FishAudio API key is missing")
}
@@ -258,13 +258,13 @@ func (f *FishAudioModel) AudioSpeechWithSender(modelName *string, audioContent *
"text": *audioContent,
}
- if asrConfig != nil && asrConfig.Params != nil {
- for key, value := range asrConfig.Params {
+ if ttsConfig != nil && ttsConfig.Params != nil {
+ for key, value := range ttsConfig.Params {
reqBody[key] = value
}
}
- if asrConfig != nil && asrConfig.Format != "" {
- reqBody["format"] = asrConfig.Format
+ if ttsConfig != nil && ttsConfig.Format != "" {
+ reqBody["format"] = ttsConfig.Format
}
jsonData, err := json.Marshal(reqBody)
diff --git a/internal/entity/models/gitee.go b/internal/entity/models/gitee.go
index 70854f78b2..6a493906bd 100644
--- a/internal/entity/models/gitee.go
+++ b/internal/entity/models/gitee.go
@@ -603,8 +603,8 @@ func (z *GiteeModel) TranscribeAudioWithSender(modelName *string, file *string,
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (g *GiteeModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (g *GiteeModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", g.Name())
}
diff --git a/internal/entity/models/google.go b/internal/entity/models/google.go
index 5578cdadd7..2702a04384 100644
--- a/internal/entity/models/google.go
+++ b/internal/entity/models/google.go
@@ -349,8 +349,8 @@ func (z *GoogleModel) TranscribeAudioWithSender(modelName *string, file *string,
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (g *GoogleModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (g *GoogleModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", g.Name())
}
diff --git a/internal/entity/models/huggingface.go b/internal/entity/models/huggingface.go
index a730e55635..87e41242bf 100644
--- a/internal/entity/models/huggingface.go
+++ b/internal/entity/models/huggingface.go
@@ -420,8 +420,8 @@ func (z *HuggingFaceModel) TranscribeAudioWithSender(modelName *string, file *st
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (h *HuggingFaceModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (h *HuggingFaceModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", h.Name())
}
diff --git a/internal/entity/models/jina.go b/internal/entity/models/jina.go
index 86377fa330..fffca66128 100644
--- a/internal/entity/models/jina.go
+++ b/internal/entity/models/jina.go
@@ -260,8 +260,8 @@ func (z *JinaModel) TranscribeAudioWithSender(modelName *string, file *string, a
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (z *JinaModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (z *JinaModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", z.Name())
}
diff --git a/internal/entity/models/lmstudio.go b/internal/entity/models/lmstudio.go
index e9767a511d..d5d60efb37 100644
--- a/internal/entity/models/lmstudio.go
+++ b/internal/entity/models/lmstudio.go
@@ -456,8 +456,8 @@ func (z *LmStudioModel) TranscribeAudioWithSender(modelName *string, file *strin
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (z *LmStudioModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (z *LmStudioModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", z.Name())
}
diff --git a/internal/entity/models/localai.go b/internal/entity/models/localai.go
index bc2b1b0300..b218709ba1 100644
--- a/internal/entity/models/localai.go
+++ b/internal/entity/models/localai.go
@@ -808,8 +808,8 @@ func (l *LocalAIModel) TranscribeAudioWithSender(modelName *string, file *string
return fmt.Errorf("%s, no such method", l.Name())
}
-// AudioSpeech (TTS): same story as TranscribeAudio above.
-func (l *LocalAIModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (l *LocalAIModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", l.Name())
}
diff --git a/internal/entity/models/longcat.go b/internal/entity/models/longcat.go
index b5f194e5a2..e35dea1966 100644
--- a/internal/entity/models/longcat.go
+++ b/internal/entity/models/longcat.go
@@ -450,7 +450,7 @@ func (l *LongCatModel) TranscribeAudioWithSender(modelName *string, file *string
}
// AudioSpeech (TTS) is not exposed by the LongCat API.
-func (l *LongCatModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+func (l *LongCatModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", l.Name())
}
diff --git a/internal/entity/models/minimax.go b/internal/entity/models/minimax.go
index 749d0d52e8..9ec953b906 100644
--- a/internal/entity/models/minimax.go
+++ b/internal/entity/models/minimax.go
@@ -458,8 +458,8 @@ func (z *MinimaxModel) TranscribeAudioWithSender(modelName *string, file *string
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (z *MinimaxModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (z *MinimaxModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
return nil, fmt.Errorf("MiniMax API key is missing")
}
@@ -478,14 +478,14 @@ func (z *MinimaxModel) AudioSpeech(modelName *string, audioContent *string, apiC
"model": modelName,
"text": audioContent,
}
- if asrConfig != nil && asrConfig.Params != nil {
- for key, value := range asrConfig.Params {
+ if ttsConfig != nil && ttsConfig.Params != nil {
+ for key, value := range ttsConfig.Params {
reqBody[key] = value
}
}
- if asrConfig != nil && asrConfig.Format != "" {
+ if ttsConfig != nil && ttsConfig.Format != "" {
reqBody["audio_setting"] = map[string]interface{}{
- "format": asrConfig.Format,
+ "format": ttsConfig.Format,
}
}
reqBody["stream"] = false
diff --git a/internal/entity/models/mistral.go b/internal/entity/models/mistral.go
index e1303346fa..1b526a8776 100644
--- a/internal/entity/models/mistral.go
+++ b/internal/entity/models/mistral.go
@@ -20,6 +20,7 @@ import (
"bufio"
"bytes"
"context"
+ "encoding/base64"
"encoding/json"
"fmt"
"io"
@@ -573,8 +574,8 @@ func (z *MistralModel) TranscribeAudioWithSender(modelName *string, file *string
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (z *MistralModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (z *MistralModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", z.Name())
}
@@ -583,19 +584,99 @@ func (z *MistralModel) AudioSpeechWithSender(modelName *string, audioContent *st
}
// OCRFile OCR file
-func (z *MistralModel) OCRFile(modelName *string, content []byte, url *string, apiConfig *APIConfig, ocrConfig *OCRConfig) (*OCRFileResponse, error) {
- return nil, fmt.Errorf("%s, no such method", z.Name())
+func (z *MistralModel) OCRFile(modelName *string, content []byte, urls *string, apiConfig *APIConfig, ocrConfig *OCRConfig) (*OCRFileResponse, error) {
+ if (urls == nil || *urls == "") && (content == nil || len(content) == 0) {
+ return nil, fmt.Errorf("file url or content is required")
+ }
+
+ region := "default"
+ if apiConfig.Region != nil && *apiConfig.Region != "" {
+ region = *apiConfig.Region
+ }
+
+ url := fmt.Sprintf("%s/%s", z.BaseURL[region], z.URLSuffix.OCR)
+
+ var docURL string
+ if urls != nil && *urls != "" {
+ docURL = *urls
+ } else {
+ mimeType := http.DetectContentType(content)
+ base64Str := base64.StdEncoding.EncodeToString(content)
+ docURL = fmt.Sprintf("data:%s;base64,%s", mimeType, base64Str)
+ }
+
+ reqData := map[string]interface{}{
+ "model": *modelName,
+ "document": map[string]interface{}{
+ "type": "document_url",
+ "document_url": docURL,
+ },
+ }
+
+ jsonData, err := json.Marshal(reqData)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal json payload: %w", err)
+ }
+
+ ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
+ defer cancel()
+
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
+ if err != nil {
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
+
+ resp, err := z.httpClient.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("failed to send request: %w", err)
+ }
+ defer resp.Body.Close()
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read response: %w", err)
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("Mistral OCR API error: %s, body: %s", resp.Status, string(body))
+ }
+
+ var mistralResp struct {
+ Pages []struct {
+ Index int `json:"index"`
+ Markdown string `json:"markdown"`
+ } `json:"pages"`
+ }
+
+ if err = json.Unmarshal(body, &mistralResp); err != nil {
+ return nil, fmt.Errorf("failed to parse response json: %w", err)
+ }
+
+ var fullMarkdown strings.Builder
+ for _, page := range mistralResp.Pages {
+ fullMarkdown.WriteString(page.Markdown)
+ fullMarkdown.WriteString("\n\n")
+ }
+
+ resultText := strings.TrimSpace(fullMarkdown.String())
+
+ return &OCRFileResponse{
+ Text: &resultText,
+ }, nil
}
-// ParseFile parse file
func (z *MistralModel) ParseFile(modelName *string, content []byte, url *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig) (*ParseFileResponse, error) {
- return nil, fmt.Errorf("%s, no such method", z.Name())
+ //TODO implement me
+ panic("implement me")
}
func (z *MistralModel) ListTasks(apiConfig *APIConfig) ([]ListTaskStatus, error) {
- return nil, fmt.Errorf("%s, no such method", z.Name())
+ return nil, fmt.Errorf("no such method", z.Name())
}
func (z *MistralModel) ShowTask(taskID string, apiConfig *APIConfig) (*TaskResponse, error) {
- return nil, fmt.Errorf("%s, no such method", z.Name())
+ return nil, fmt.Errorf("no such method", z.Name())
}
diff --git a/internal/entity/models/moonshot.go b/internal/entity/models/moonshot.go
index 3156b49cb9..fa1ad76ec4 100644
--- a/internal/entity/models/moonshot.go
+++ b/internal/entity/models/moonshot.go
@@ -497,8 +497,8 @@ func (z *MoonshotModel) TranscribeAudioWithSender(modelName *string, file *strin
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (z *MoonshotModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (z *MoonshotModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", z.Name())
}
diff --git a/internal/entity/models/novita.go b/internal/entity/models/novita.go
index 5eb58efd7c..9eb10ee987 100644
--- a/internal/entity/models/novita.go
+++ b/internal/entity/models/novita.go
@@ -649,7 +649,7 @@ func (n *NovitaModel) TranscribeAudioWithSender(modelName *string, file *string,
return fmt.Errorf("%s, no such method", n.Name())
}
-func (n *NovitaModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+func (n *NovitaModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", n.Name())
}
diff --git a/internal/entity/models/nvidia.go b/internal/entity/models/nvidia.go
index bb1e6785c7..998196253c 100644
--- a/internal/entity/models/nvidia.go
+++ b/internal/entity/models/nvidia.go
@@ -561,8 +561,8 @@ func (z *NvidiaModel) TranscribeAudioWithSender(modelName *string, file *string,
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (n *NvidiaModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (n *NvidiaModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", n.Name())
}
diff --git a/internal/entity/models/ollama.go b/internal/entity/models/ollama.go
index b4eb1b35d9..d95e9e8c73 100644
--- a/internal/entity/models/ollama.go
+++ b/internal/entity/models/ollama.go
@@ -454,8 +454,8 @@ func (z *OllamaModel) TranscribeAudioWithSender(modelName *string, file *string,
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (o *OllamaModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (o *OllamaModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", o.Name())
}
diff --git a/internal/entity/models/openai.go b/internal/entity/models/openai.go
index e46b734bb1..c19751231e 100644
--- a/internal/entity/models/openai.go
+++ b/internal/entity/models/openai.go
@@ -603,8 +603,8 @@ func (z *OpenAIModel) TranscribeAudioWithSender(modelName *string, file *string,
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (o *OpenAIModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (o *OpenAIModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", o.Name())
}
diff --git a/internal/entity/models/openrouter.go b/internal/entity/models/openrouter.go
index 3b885d40f3..4e99ef5bea 100644
--- a/internal/entity/models/openrouter.go
+++ b/internal/entity/models/openrouter.go
@@ -538,8 +538,8 @@ func (z *OpenRouterModel) TranscribeAudioWithSender(modelName *string, file *str
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (o *OpenRouterModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (o *OpenRouterModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
return nil, fmt.Errorf("OpenRouter API key is missing")
}
@@ -560,13 +560,13 @@ func (o *OpenRouterModel) AudioSpeech(modelName *string, audioContent *string, a
"input": audioContent,
}
- if asrConfig != nil && asrConfig.Params != nil {
- for key, value := range asrConfig.Params {
+ if ttsConfig != nil && ttsConfig.Params != nil {
+ for key, value := range ttsConfig.Params {
reqBody[key] = value
}
}
- if asrConfig != nil && asrConfig.Format != "" {
- reqBody["response_format"] = asrConfig.Format
+ if ttsConfig != nil && ttsConfig.Format != "" {
+ reqBody["response_format"] = ttsConfig.Format
}
jsonData, err := json.Marshal(reqBody)
diff --git a/internal/entity/models/paddleocr.go b/internal/entity/models/paddleocr.go
new file mode 100644
index 0000000000..25445ca38d
--- /dev/null
+++ b/internal/entity/models/paddleocr.go
@@ -0,0 +1,300 @@
+package models
+
+import (
+ "bufio"
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "mime/multipart"
+ "net/http"
+ "strings"
+ "time"
+)
+
+type PaddleOCRModel struct {
+ BaseURL map[string]string
+ URLSuffix URLSuffix
+ httpClient *http.Client
+}
+
+func NewPaddleOCRModel(baseURL map[string]string, urlSuffix URLSuffix) *PaddleOCRModel {
+ return &PaddleOCRModel{
+ BaseURL: baseURL,
+ URLSuffix: urlSuffix,
+ httpClient: &http.Client{
+ Timeout: 120 * time.Second,
+ Transport: &http.Transport{
+ MaxIdleConns: 100,
+ MaxIdleConnsPerHost: 10,
+ IdleConnTimeout: 90 * time.Second,
+ DisableCompression: false,
+ },
+ },
+ }
+}
+
+func (p PaddleOCRModel) NewInstance(baseURL map[string]string) ModelDriver {
+ return &PaddleOCRModel{
+ BaseURL: baseURL,
+ URLSuffix: p.URLSuffix,
+ httpClient: &http.Client{
+ Timeout: 120 * time.Second,
+ Transport: &http.Transport{
+ MaxIdleConns: 100,
+ MaxIdleConnsPerHost: 10,
+ IdleConnTimeout: 90 * time.Second,
+ DisableCompression: false,
+ },
+ },
+ }
+}
+
+func (p *PaddleOCRModel) Name() string {
+ return "paddle_ocr"
+}
+
+func (p *PaddleOCRModel) ChatWithMessages(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig) (*ChatResponse, error) {
+ return nil, fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) ChatStreamlyWithSender(modelName string, messages []Message, apiConfig *APIConfig, modelConfig *ChatConfig, sender func(*string, *string) error) error {
+ return fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) Embed(modelName *string, texts []string, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig) ([]EmbeddingData, error) {
+ return nil, fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) Rerank(modelName *string, query string, documents []string, apiConfig *APIConfig, rerankConfig *RerankConfig) (*RerankResponse, error) {
+ return nil, fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) TranscribeAudio(modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig) (*ASRResponse, error) {
+ return nil, fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) TranscribeAudioWithSender(modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig, sender func(*string, *string) error) error {
+ return fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
+ return nil, fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) AudioSpeechWithSender(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, sender func(*string, *string) error) error {
+ return fmt.Errorf("no such method", p.Name())
+}
+
+type paddleSubmitResponse struct {
+ Data struct {
+ JobId string `json:"jobId"`
+ } `json:"data"`
+}
+
+type paddlePollResponse struct {
+ Data struct {
+ State string `json:"state"`
+ ErrorMsg string `json:"errorMsg"`
+ ResultUrl struct {
+ JsonUrl string `json:"jsonUrl"`
+ } `json:"resultUrl"`
+ } `json:"data"`
+}
+
+type paddleJsonlLine struct {
+ Result struct {
+ LayoutParsingResults []struct {
+ Markdown struct {
+ Text string `json:"text"`
+ } `json:"markdown"`
+ } `json:"layoutParsingResults"`
+ } `json:"result"`
+}
+
+func (p *PaddleOCRModel) OCRFile(modelName *string, content []byte, fileURL *string, apiConfig *APIConfig, ocrConfig *OCRConfig) (*OCRFileResponse, error) {
+ if (content == nil || len(content) == 0) && (fileURL == nil || *fileURL == "") {
+ return nil, fmt.Errorf("content and fileURL cannot be both empty")
+ }
+
+ if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
+ return nil, fmt.Errorf("api key is required")
+ }
+
+ var region = "default"
+ if apiConfig.Region != nil && *apiConfig.Region != "" {
+ region = *apiConfig.Region
+ }
+
+ url := fmt.Sprintf("%s/%s", p.BaseURL[region], p.URLSuffix.OCR)
+
+ optionalPayload := map[string]bool{
+ "useDocOrientationClassify": false,
+ "useDocUnwarping": false,
+ "useChartRecognition": false,
+ }
+ optBytes, _ := json.Marshal(optionalPayload)
+
+ var req *http.Request
+ var err error
+
+ if fileURL != nil && strings.HasPrefix(*fileURL, "http") {
+ reqData := map[string]interface{}{
+ "fileUrl": *fileURL,
+ "model": *modelName,
+ "optionalPayload": optionalPayload,
+ }
+ jsonData, err := json.Marshal(reqData)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal json: %w", err)
+ }
+ req, err = http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req.Header.Set("Content-Type", "application/json")
+ } else {
+ body := &bytes.Buffer{}
+ writer := multipart.NewWriter(body)
+
+ _ = writer.WriteField("model", *modelName)
+ _ = writer.WriteField("optionalPayload", string(optBytes))
+
+ part, err := writer.CreateFormFile("file", "document.pdf")
+ if err != nil {
+ return nil, fmt.Errorf("failed to create form file: %w", err)
+ }
+ part.Write(content)
+ writer.Close()
+
+ req, err = http.NewRequest("POST", url, body)
+ req.Header.Set("Content-Type", writer.FormDataContentType())
+ }
+
+ req.Header.Set("Authorization", fmt.Sprintf("bearer %s", *apiConfig.ApiKey))
+
+ resp, err := p.httpClient.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("failed to submit job: %w", err)
+ }
+ defer resp.Body.Close()
+
+ respBody, _ := io.ReadAll(resp.Body)
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("submit job failed: %s", string(respBody))
+ }
+
+ var submitResp paddleSubmitResponse
+ if err := json.Unmarshal(respBody, &submitResp); err != nil {
+ return nil, fmt.Errorf("failed to parse submit response: %w", err)
+ }
+
+ jobId := submitResp.Data.JobId
+ if jobId == "" {
+ return nil, fmt.Errorf("failed to get jobId from response")
+ }
+
+ pollUrl := fmt.Sprintf("%s/%s", url, jobId)
+ var jsonlUrl string
+
+ for {
+ time.Sleep(3 * time.Second)
+
+ pollReq, _ := http.NewRequest("GET", pollUrl, nil)
+ pollReq.Header.Set("Authorization", fmt.Sprintf("bearer %s", *apiConfig.ApiKey))
+
+ pollResp, err := p.httpClient.Do(pollReq)
+ if err != nil {
+ return nil, fmt.Errorf("failed to poll job status: %w", err)
+ }
+
+ pollBody, _ := io.ReadAll(pollResp.Body)
+ pollResp.Body.Close()
+
+ if pollResp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("poll job failed: %s", string(pollBody))
+ }
+
+ var pollData paddlePollResponse
+ if err = json.Unmarshal(pollBody, &pollData); err != nil {
+ return nil, fmt.Errorf("failed to parse poll response: %w", err)
+ }
+
+ // end if 'done' or 'failed'
+ state := pollData.Data.State
+ if state == "done" {
+ jsonlUrl = pollData.Data.ResultUrl.JsonUrl
+ break
+ } else if state == "failed" {
+ return nil, fmt.Errorf("ocr job failed on server: %s", pollData.Data.ErrorMsg)
+ }
+ }
+
+ if jsonlUrl == "" {
+ return nil, fmt.Errorf("job done but jsonl url is empty")
+ }
+
+ resReq, err := http.NewRequest("GET", jsonlUrl, nil)
+ if err != nil {
+ return nil, fmt.Errorf("failed to create request for jsonl: %w", err)
+ }
+
+ resResp, err := p.httpClient.Do(resReq)
+ if err != nil {
+ return nil, fmt.Errorf("failed to download jsonl result: %w", err)
+ }
+ defer resResp.Body.Close()
+
+ if resResp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("failed to download jsonl, status: %d", resResp.StatusCode)
+ }
+
+ var fullMarkdown strings.Builder
+ scanner := bufio.NewScanner(resResp.Body)
+
+ for scanner.Scan() {
+ line := strings.TrimSpace(scanner.Text())
+ if line == "" {
+ continue
+ }
+
+ var lineData paddleJsonlLine
+ if err := json.Unmarshal([]byte(line), &lineData); err != nil {
+ continue
+ }
+
+ for _, layoutRes := range lineData.Result.LayoutParsingResults {
+ fullMarkdown.WriteString(layoutRes.Markdown.Text)
+ fullMarkdown.WriteString("\n\n")
+ }
+ }
+
+ if err = scanner.Err(); err != nil {
+ return nil, fmt.Errorf("error reading jsonl: %w", err)
+ }
+
+ extractedText := strings.TrimSpace(fullMarkdown.String())
+
+ return &OCRFileResponse{Text: &extractedText}, nil
+}
+
+func (p *PaddleOCRModel) ParseFile(modelName *string, content []byte, url *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig) (*ParseFileResponse, error) {
+ return nil, fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) ListModels(apiConfig *APIConfig) ([]string, error) {
+ return nil, fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) Balance(apiConfig *APIConfig) (map[string]interface{}, error) {
+ return nil, fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) CheckConnection(apiConfig *APIConfig) error {
+ return fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) ListTasks(apiConfig *APIConfig) ([]ListTaskStatus, error) {
+ return nil, fmt.Errorf("no such method", p.Name())
+}
+
+func (p *PaddleOCRModel) ShowTask(taskID string, apiConfig *APIConfig) (*TaskResponse, error) {
+ return nil, fmt.Errorf("no such method", p.Name())
+}
diff --git a/internal/entity/models/siliconflow.go b/internal/entity/models/siliconflow.go
index 552ef90361..11ba909365 100644
--- a/internal/entity/models/siliconflow.go
+++ b/internal/entity/models/siliconflow.go
@@ -731,7 +731,7 @@ func (o *SiliconflowModel) TranscribeAudio(modelName *string, file *string, apiC
}
region := "default"
- if apiConfig.Region != nil && *apiConfig.Region != "" {
+ if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
region = *apiConfig.Region
}
@@ -835,9 +835,6 @@ func (o *SiliconflowModel) TranscribeAudio(modelName *string, file *string, apiC
return nil, fmt.Errorf("failed to unmarshal response: %w, body=%s", err, string(respBody))
}
- var res ASRResponse
- res.Text = result.Text
-
return &ASRResponse{Text: result.Text}, nil
}
@@ -845,8 +842,8 @@ func (z *SiliconflowModel) TranscribeAudioWithSender(modelName *string, file *st
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (o *SiliconflowModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (o *SiliconflowModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
if audioContent == nil || *audioContent == "" {
return nil, fmt.Errorf("audio content is empty")
}
@@ -864,13 +861,13 @@ func (o *SiliconflowModel) AudioSpeech(modelName *string, audioContent *string,
"stream": false,
}
- if asrConfig != nil && asrConfig.Params != nil {
- for key, value := range asrConfig.Params {
+ if ttsConfig != nil && ttsConfig.Params != nil {
+ for key, value := range ttsConfig.Params {
reqBody[key] = value
}
}
- if asrConfig != nil && asrConfig.Format != "" {
- reqBody["response_format"] = asrConfig.Format
+ if ttsConfig != nil && ttsConfig.Format != "" {
+ reqBody["response_format"] = ttsConfig.Format
}
jsonData, err := json.Marshal(reqBody)
diff --git a/internal/entity/models/stepfun.go b/internal/entity/models/stepfun.go
index 941f815fd2..a4cce4bb9e 100644
--- a/internal/entity/models/stepfun.go
+++ b/internal/entity/models/stepfun.go
@@ -477,8 +477,8 @@ func (s *StepFunModel) TranscribeAudioWithSender(modelName *string, file *string
return fmt.Errorf("%s, no such method", s.Name())
}
-// AudioSpeech convert audio to text
-func (s *StepFunModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (s *StepFunModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
// TODO Test it
if audioContent == nil || *audioContent == "" {
return nil, fmt.Errorf("audio content is empty")
@@ -496,13 +496,13 @@ func (s *StepFunModel) AudioSpeech(modelName *string, audioContent *string, apiC
"input": *audioContent,
}
- if asrConfig != nil && asrConfig.Params != nil {
- for key, value := range asrConfig.Params {
+ if ttsConfig != nil && ttsConfig.Params != nil {
+ for key, value := range ttsConfig.Params {
reqBody[key] = value
}
}
- if asrConfig != nil && asrConfig.Format != "" {
- reqBody["response_format"] = asrConfig.Format
+ if ttsConfig != nil && ttsConfig.Format != "" {
+ reqBody["response_format"] = ttsConfig.Format
}
jsonData, err := json.Marshal(reqBody)
diff --git a/internal/entity/models/types.go b/internal/entity/models/types.go
index e4123e064d..d791ac04cb 100644
--- a/internal/entity/models/types.go
+++ b/internal/entity/models/types.go
@@ -28,8 +28,8 @@ type ModelDriver interface {
// TranscribeAudio transcribe audio
TranscribeAudio(modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig) (*ASRResponse, error)
TranscribeAudioWithSender(modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig, sender func(*string, *string) error) error
- // AudioSpeech convert audio to text
- AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error)
+ // AudioSpeech convert text to audio
+ AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error)
AudioSpeechWithSender(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, sender func(*string, *string) error) error
// OCRFile OCR file
OCRFile(modelName *string, content []byte, url *string, apiConfig *APIConfig, ocrConfig *OCRConfig) (*OCRFileResponse, error)
diff --git a/internal/entity/models/upstage.go b/internal/entity/models/upstage.go
index 7e5596a902..1b492b60e2 100644
--- a/internal/entity/models/upstage.go
+++ b/internal/entity/models/upstage.go
@@ -594,8 +594,8 @@ func (z *UpstageModel) TranscribeAudioWithSender(modelName *string, file *string
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (z *UpstageModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (z *UpstageModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", z.Name())
}
diff --git a/internal/entity/models/vllm.go b/internal/entity/models/vllm.go
index 61452c9bca..c8dbcdab7c 100644
--- a/internal/entity/models/vllm.go
+++ b/internal/entity/models/vllm.go
@@ -672,8 +672,8 @@ func (z *VllmModel) TranscribeAudioWithSender(modelName *string, file *string, a
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (o *VllmModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (o *VllmModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", o.Name())
}
diff --git a/internal/entity/models/volcengine.go b/internal/entity/models/volcengine.go
index 4a806883ec..8f3133aa41 100644
--- a/internal/entity/models/volcengine.go
+++ b/internal/entity/models/volcengine.go
@@ -519,8 +519,8 @@ func (z *VolcEngine) TranscribeAudioWithSender(modelName *string, file *string,
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (o *VolcEngine) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (o *VolcEngine) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", o.Name())
}
diff --git a/internal/entity/models/voyage.go b/internal/entity/models/voyage.go
index 14f67dda75..c17a0e8081 100644
--- a/internal/entity/models/voyage.go
+++ b/internal/entity/models/voyage.go
@@ -363,7 +363,7 @@ func (v *VoyageModel) TranscribeAudioWithSender(modelName *string, file *string,
return fmt.Errorf("%s, no such method", v.Name())
}
-func (v *VoyageModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+func (v *VoyageModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", v.Name())
}
diff --git a/internal/entity/models/xai.go b/internal/entity/models/xai.go
index 88b465fecf..b19f93ca7d 100644
--- a/internal/entity/models/xai.go
+++ b/internal/entity/models/xai.go
@@ -502,8 +502,8 @@ func (z *XAIModel) TranscribeAudioWithSender(modelName *string, file *string, ap
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (o *XAIModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (o *XAIModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", o.Name())
}
diff --git a/internal/entity/models/zhipu-ai.go b/internal/entity/models/zhipu-ai.go
index fa86c15467..d90d63559b 100644
--- a/internal/entity/models/zhipu-ai.go
+++ b/internal/entity/models/zhipu-ai.go
@@ -676,8 +676,8 @@ func (z *ZhipuAIModel) TranscribeAudioWithSender(modelName *string, file *string
return fmt.Errorf("%s, no such method", z.Name())
}
-// AudioSpeech convert audio to text
-func (o *ZhipuAIModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, asrConfig *TTSConfig) (*TTSResponse, error) {
+// AudioSpeech convert text to audio
+func (o *ZhipuAIModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
return nil, fmt.Errorf("%s, no such method", o.Name())
}