Skip to content

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

Go: implement Encode (embeddings) in Gitee AI driver (#14698)

BitTobybfb4a0eea2d9
diff --git a/conf/models/gitee.json b/conf/models/gitee.json
index 630106592f..a6d1869a74 100644
--- a/conf/models/gitee.json
+++ b/conf/models/gitee.json
@@ -39,6 +39,13 @@
"model_types": [
"rerank"
]
+ },
+ {
+ "name": "BAAI/bge-m3",
+ "max_tokens": 8192,
+ "model_types": [
+ "embedding"
+ ]
}
]
}
\ No newline at end of file
diff --git a/internal/entity/models/gitee.go b/internal/entity/models/gitee.go
index 34d0425102..417b7e2ddf 100644
--- a/internal/entity/models/gitee.go
+++ b/internal/entity/models/gitee.go
@@ -29,6 +29,13 @@ import (
"time"
)
+type giteeEmbeddingResponse struct {
+ Data []struct {
+ Index int `json:"index"`
+ Embedding []interface{} `json:"embedding"`
+ } `json:"data"`
+}
+
// GiteeModel implements ModelDriver for Gitee
type GiteeModel struct {
BaseURL map[string]string
@@ -400,7 +407,105 @@ func (z *GiteeModel) ChatStreamlyWithSender(modelName string, messages []Message
// Encode encodes a list of texts into embeddings
func (z *GiteeModel) Encode(modelName *string, texts []string, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig) ([][]float64, error) {
- return nil, fmt.Errorf("%s, no such method", 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.Embedding)
+
+ reqBody := map[string]interface{}{
+ "model": *modelName,
+ "input": texts,
+ }
+ if embeddingConfig != nil && embeddingConfig.Dimension > 0 {
+ reqBody["dimensions"] = embeddingConfig.Dimension
+ }
+
+ 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 embeddings API error: %s, body: %s", resp.Status, string(body))
+ }
+
+ var parsed giteeEmbeddingResponse
+ if err = json.Unmarshal(body, &parsed); err != nil {
+ return nil, fmt.Errorf("failed to parse response: %w", err)
+ }
+
+ embeddings := make([][]float64, len(texts))
+ for _, item := range parsed.Data {
+ if item.Index < 0 || item.Index >= len(texts) {
+ return nil, fmt.Errorf("unexpected embedding index %d for %d inputs", item.Index, len(texts))
+ }
+ vec := make([]float64, len(item.Embedding))
+ for j, v := range item.Embedding {
+ switch val := v.(type) {
+ case float64:
+ vec[j] = val
+ case float32:
+ vec[j] = float64(val)
+ default:
+ return nil, fmt.Errorf("unexpected embedding value type at item %d index %d", item.Index, j)
+ }
+ }
+ embeddings[item.Index] = vec
+ }
+
+ for i, vec := range embeddings {
+ if vec == nil {
+ return nil, fmt.Errorf("missing embedding for input at index %d", i)
+ }
+ }
+
+ return embeddings, nil
}
type giteeRerankRequest struct {