Skip to content

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

Go: implement Rerank in Gitee AI driver (#14656)

Panda Devd8d49df35e81
diff --git a/conf/models/gitee.json b/conf/models/gitee.json
index 9ac683bc93..630106592f 100644
--- a/conf/models/gitee.json
+++ b/conf/models/gitee.json
@@ -32,6 +32,13 @@
"model_types": [
"chat"
]
+ },
+ {
+ "name": "BAAI/bge-reranker-v2-m3",
+ "max_tokens": 8192,
+ "model_types": [
+ "rerank"
+ ]
}
]
}
\ No newline at end of file
diff --git a/internal/entity/models/gitee.go b/internal/entity/models/gitee.go
index 7f7050bfb9..85d4635611 100644
--- a/internal/entity/models/gitee.go
+++ b/internal/entity/models/gitee.go
@@ -19,6 +19,7 @@ package models
import (
"bufio"
"bytes"
+ "context"
"encoding/json"
"fmt"
"io"
@@ -402,9 +403,105 @@ func (z *GiteeModel) Encode(modelName *string, texts []string, apiConfig *APICon
return nil, fmt.Errorf("%s, no such method", z.Name())
}
+type giteeRerankRequest struct {
+ Model string `json:"model"`
+ Query string `json:"query"`
+ Documents []string `json:"documents"`
+ TopN int `json:"top_n"`
+ ReturnDocuments bool `json:"return_documents"`
+}
+
+type giteeRerankResponse struct {
+ Results []struct {
+ Index int `json:"index"`
+ RelevanceScore float64 `json:"relevance_score"`
+ } `json:"results"`
+}
+
// Rerank calculates similarity scores between query and texts
func (z *GiteeModel) Rerank(modelName *string, query string, texts []string, apiConfig *APIConfig) ([]float64, error) {
- return nil, fmt.Errorf("%s, Rerank not implemented", z.Name())
+ if len(texts) == 0 {
+ return []float64{}, nil
+ }
+
+ if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
+ return nil, fmt.Errorf("api key is required")
+ }
+
+ if modelName == nil || *modelName == "" {
+ return nil, fmt.Errorf("model name is required")
+ }
+
+ region := "default"
+ if apiConfig.Region != nil && *apiConfig.Region != "" {
+ region = *apiConfig.Region
+ }
+
+ baseURL := z.BaseURL["default"]
+ if region != "default" {
+ if regional, ok := z.BaseURL[region]; ok && regional != "" {
+ baseURL = regional
+ }
+ }
+ if baseURL == "" {
+ return nil, fmt.Errorf("gitee: no base URL configured for default region")
+ }
+
+ url := fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), z.URLSuffix.Rerank)
+
+ reqBody := giteeRerankRequest{
+ Model: *modelName,
+ Query: query,
+ Documents: texts,
+ TopN: len(texts),
+ ReturnDocuments: false,
+ }
+
+ jsonData, err := json.Marshal(reqBody)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal request: %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 := 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("Gitee rerank API error: %s, body: %s", resp.Status, string(body))
+ }
+
+ var parsed giteeRerankResponse
+ if err = json.Unmarshal(body, &parsed); err != nil {
+ return nil, fmt.Errorf("failed to parse response: %w", err)
+ }
+
+ scores := make([]float64, len(texts))
+ for _, r := range parsed.Results {
+ if r.Index < 0 || r.Index >= len(texts) {
+ return nil, fmt.Errorf("unexpected rerank index %d for %d inputs", r.Index, len(texts))
+ }
+ scores[r.Index] = r.RelevanceScore
+ }
+
+ return scores, nil
}
func (z *GiteeModel) ListModels(apiConfig *APIConfig) ([]string, error) {