Skip to content

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

Go: implement provider: Jina (#14838)

Haruko38645ee5ca9cd0d
diff --git a/conf/models/jina.json b/conf/models/jina.json
new file mode 100644
index 0000000000..07463b6edf
--- /dev/null
+++ b/conf/models/jina.json
@@ -0,0 +1,100 @@
+{
+ "name": "Jina",
+ "url": {
+ "default": "https://api.jina.ai/v1",
+ "deepsearch": "https://deepsearch.jina.ai/v1"
+ },
+ "url_suffix": {
+ "chat": "chat/completions",
+ "models": "models",
+ "embedding": "embeddings",
+ "rerank": "rerank"
+ },
+ "class": "jina",
+ "models": [
+ {
+ "name": "jina-reranker-v3",
+ "max_tokens": 134144,
+ "model_types": [
+ "rerank"
+ ]
+ },
+ {
+ "name": "jina-reranker-m0",
+ "max_tokens": 134144,
+ "model_types": [
+ "rerank"
+ ]
+ },
+ {
+ "name": "jina-colbert-v2",
+ "max_tokens": 134144,
+ "model_types": [
+ "rerank"
+ ]
+ },
+ {
+ "name": "jina-reranker-v2-base-multilingual",
+ "max_tokens": 134144,
+ "model_types": [
+ "rerank"
+ ]
+ },
+ {
+ "name": "jina-embeddings-v3",
+ "max_tokens": 8192,
+ "model_types": [
+ "embedding"
+ ]
+ },
+ {
+ "name": "jina-embeddings-v4",
+ "max_tokens": 32768,
+ "model_types": [
+ "embedding"
+ ]
+ },
+ {
+ "name": "jina-embeddings-v5-text-small",
+ "max_tokens": 32768,
+ "model_types": [
+ "embedding"
+ ]
+ },
+ {
+ "name": "jina-embeddings-v5-text-nano",
+ "max_tokens": 8192,
+ "model_types": [
+ "embedding"
+ ]
+ },
+ {
+ "name": "jina-embeddings-v5-omni-small",
+ "max_tokens": 32768,
+ "model_types": [
+ "embedding"
+ ]
+ },
+ {
+ "name": "jina-embeddings-v5-omni-nano",
+ "max_tokens": 8192,
+ "model_types": [
+ "embedding"
+ ]
+ },
+ {
+ "name": "jina-clip-v2",
+ "max_tokens": 8192,
+ "model_types": [
+ "embedding"
+ ]
+ },
+ {
+ "name": "jina-embeddings-v2-base-en",
+ "max_tokens": 8192,
+ "model_types": [
+ "embedding"
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/internal/entity/models/factory.go b/internal/entity/models/factory.go
index c11e479642..7540605d34 100644
--- a/internal/entity/models/factory.go
+++ b/internal/entity/models/factory.go
@@ -81,6 +81,8 @@ func (f *ModelFactory) CreateModelDriver(providerName string, baseURL map[string
return NewStepFunModel(baseURL, urlSuffix), nil
case "baichuan":
return NewBaichuanModel(baseURL, urlSuffix), nil
+ case "jina":
+ return NewJinaModel(baseURL, urlSuffix), nil
default:
return NewDummyModel(baseURL, urlSuffix), nil
}
diff --git a/internal/entity/models/jina.go b/internal/entity/models/jina.go
new file mode 100644
index 0000000000..1a3d2ff9f7
--- /dev/null
+++ b/internal/entity/models/jina.go
@@ -0,0 +1,252 @@
+package models
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "time"
+)
+
+type JinaModel struct {
+ BaseURL map[string]string
+ URLSuffix URLSuffix
+ httpClient *http.Client
+}
+
+func NewJinaModel(baseURL map[string]string, urlSuffix URLSuffix) *JinaModel {
+ return &JinaModel{
+ BaseURL: baseURL,
+ URLSuffix: urlSuffix,
+ httpClient: &http.Client{
+ Timeout: time.Second * 90,
+ },
+ }
+}
+
+func (j *JinaModel) NewInstance(baseURL map[string]string) ModelDriver {
+ return &JinaModel{
+ BaseURL: baseURL,
+ URLSuffix: j.URLSuffix,
+ httpClient: &http.Client{
+ Timeout: time.Second * 90,
+ },
+ }
+}
+
+func (j *JinaModel) Name() string {
+ return "jina"
+}
+
+func (j *JinaModel) ChatWithMessages(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig) (*ChatResponse, error) {
+ //TODO implement me: https://api.jina.ai/docs#/Search%20Foundation%20Models/chat_completions_v1_chat_completions_post
+ return nil, fmt.Errorf("jina does not implement ChatWithMessages(not available for now)")
+}
+
+func (j *JinaModel) ChatStreamlyWithSender(modelName string, messages []Message, apiConfig *APIConfig, modelConfig *ChatConfig, sender func(*string, *string) error) error {
+ //TODO implement me: https://api.jina.ai/docs#/Search%20Foundation%20Models/chat_completions_v1_chat_completions_post
+ return fmt.Errorf("jina does not implement ChatStreamlyWithSender(not available for now)")
+}
+
+func (j *JinaModel) Embed(modelName *string, texts []string, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig) ([]EmbeddingData, error) {
+ if len(texts) == 0 {
+ return []EmbeddingData{}, nil
+ }
+
+ var region = "default"
+ if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
+ region = *apiConfig.Region
+ }
+
+ url := fmt.Sprintf("%s/%s", j.BaseURL[region], j.URLSuffix.Embedding)
+
+ reqBody := map[string]interface{}{
+ "model": *modelName,
+ "input": texts,
+ }
+
+ jsonData, err := json.Marshal(reqBody)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ req, err := http.NewRequest("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 := j.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("Jina embedding API error: status %d, body: %s", resp.StatusCode, string(body))
+ }
+
+ var parsedResponse struct {
+ Data []struct {
+ Embedding []float64 `json:"embedding"`
+ Index int `json:"index"`
+ } `json:"data"`
+ }
+
+ if err = json.Unmarshal(body, &parsedResponse); err != nil {
+ return nil, fmt.Errorf("failed to decode response: %w", err)
+ }
+
+ if len(parsedResponse.Data) == 0 {
+ return nil, fmt.Errorf("Jina embedding response contains no data: %s", string(body))
+ }
+
+ var embeddings []EmbeddingData
+ for _, dataElem := range parsedResponse.Data {
+ embeddings = append(embeddings, EmbeddingData{
+ Embedding: dataElem.Embedding,
+ Index: dataElem.Index,
+ })
+ }
+
+ return embeddings, nil
+}
+
+func (j *JinaModel) Rerank(modelName *string, query string, documents []string, apiConfig *APIConfig, rerankConfig *RerankConfig) (*RerankResponse, error) {
+ if len(documents) == 0 {
+ return &RerankResponse{}, nil
+ }
+
+ var region = "default"
+ if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
+ region = *apiConfig.Region
+ }
+
+ url := fmt.Sprintf("%s/%s", j.BaseURL[region], j.URLSuffix.Rerank)
+
+ var topN = rerankConfig.TopN
+ if rerankConfig.TopN != 0 {
+ topN = rerankConfig.TopN
+ }
+
+ reqBody := map[string]interface{}{
+ "model": *modelName,
+ "query": query,
+ "documents": documents,
+ "top_n": topN,
+ }
+
+ jsonData, err := json.Marshal(reqBody)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ req, err := http.NewRequest("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 := j.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("Jina Rerank API error: status %d, body: %s", resp.StatusCode, string(body))
+ }
+
+ var rerankResp struct {
+ Results []struct {
+ Index int `json:"index"`
+ RelevanceScore float64 `json:"relevance_score"`
+ } `json:"results"`
+ }
+
+ if err = json.Unmarshal(body, &rerankResp); err != nil {
+ return nil, fmt.Errorf("failed to decode response: %w", err)
+ }
+
+ var rerankResponse RerankResponse
+ for _, result := range rerankResp.Results {
+ rerankResult := RerankResult{
+ Index: result.Index,
+ RelevanceScore: result.RelevanceScore,
+ }
+ rerankResponse.Data = append(rerankResponse.Data, rerankResult)
+ }
+
+ return &rerankResponse, nil
+}
+
+func (j *JinaModel) ListModels(apiConfig *APIConfig) ([]string, error) {
+ var region = "default"
+ if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
+ region = *apiConfig.Region
+ }
+
+ url := fmt.Sprintf("%s/%s", j.BaseURL[region], j.URLSuffix.Models)
+
+ req, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+
+ req.Header.Set("Content-Type", "application/json")
+
+ resp, err := j.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 body: %w", err)
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
+ }
+
+ // Parse response
+ var result map[string]interface{}
+ if err = json.Unmarshal(body, &result); err != nil {
+ return nil, fmt.Errorf("failed to parse response: %w", err)
+ }
+
+ // convert result["data"] to []map[string]interface{}
+ models := make([]string, 0)
+ for _, model := range result["data"].([]interface{}) {
+ modelMap := model.(map[string]interface{})
+ modelName := modelMap["name"].(string)
+ models = append(models, modelName)
+ }
+
+ return models, nil
+}
+
+func (j *JinaModel) Balance(apiConfig *APIConfig) (map[string]interface{}, error) {
+ return nil, fmt.Errorf("no such method")
+}
+
+func (j *JinaModel) CheckConnection(apiConfig *APIConfig) error {
+ _, err := j.ListModels(apiConfig)
+ return err
+}