Skip to content

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

Feat/xiaomi list models refresh (#19855)

adws7029a0973c70
diff --git a/conf/models/xiaomi.json b/conf/models/xiaomi.json
index f92dd63b38..538c9b3590 100644
--- a/conf/models/xiaomi.json
+++ b/conf/models/xiaomi.json
@@ -4,7 +4,8 @@
"default": "https://api.xiaomimimo.com/v1"
},
"url_suffix": {
- "chat": "chat/completions"
+ "chat": "chat/completions",
+ "models": "models"
},
"models": [
{
diff --git a/internal/entity/models/xiaomi.go b/internal/entity/models/xiaomi.go
index 9121a5807b..3b2e6c4ecd 100644
--- a/internal/entity/models/xiaomi.go
+++ b/internal/entity/models/xiaomi.go
@@ -608,7 +608,34 @@ func (x *XiaomiModel) ParseFile(ctx context.Context, modelName *string, content
}
func (x *XiaomiModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]ListModelResponse, error) {
- return nil, fmt.Errorf("no such method %s", x.Name())
+ if err := x.baseModel.APIConfigCheck(apiConfig); err != nil {
+ return nil, err
+ }
+
+ baseURL, err := x.baseModel.GetBaseURL(apiConfig)
+ if err != nil {
+ return nil, err
+ }
+ modelsSuffix := strings.Trim(strings.TrimSpace(x.baseModel.URLSuffix.Models), "/")
+ if modelsSuffix == "" {
+ return nil, fmt.Errorf("xiaomi: models URL suffix is not configured")
+ }
+ url := fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), modelsSuffix)
+
+ body, err := x.baseModel.doGetRequest(ctx, url, apiConfig, nonStreamCallTimeout)
+ if err != nil {
+ return nil, err
+ }
+
+ var modelList ModelList
+ if err = json.Unmarshal(body, &modelList); err != nil {
+ return nil, fmt.Errorf("failed to parse response: %w", err)
+ }
+ if modelList.Models == nil {
+ return nil, fmt.Errorf("invalid models list format")
+ }
+
+ return ParseListModel(modelList), nil
}
func (x *XiaomiModel) Balance(ctx context.Context, apiConfig *APIConfig) (map[string]interface{}, error) {
@@ -616,10 +643,7 @@ func (x *XiaomiModel) Balance(ctx context.Context, apiConfig *APIConfig) (map[st
}
func (x *XiaomiModel) CheckConnection(ctx context.Context, apiConfig *APIConfig) error {
- if err := x.baseModel.APIConfigCheck(apiConfig); err != nil {
- return err
- }
- _, err := x.baseModel.GetBaseURL(apiConfig)
+ _, err := x.ListModels(ctx, apiConfig)
return err
}
diff --git a/internal/entity/models/xiaomi_test.go b/internal/entity/models/xiaomi_test.go
index 8e795cb4be..ddbe1a2a6a 100644
--- a/internal/entity/models/xiaomi_test.go
+++ b/internal/entity/models/xiaomi_test.go
@@ -33,10 +33,13 @@ func newXiaomiServer(t *testing.T, expectedPath string, handler func(t *testing.
t.Errorf("read body: %v", err)
return
}
+ // GET requests (e.g. ListModels) carry no body.
var body map[string]interface{}
- if err := json.Unmarshal(raw, &body); err != nil {
- t.Errorf("unmarshal: %v\nraw=%s", err, string(raw))
- return
+ if len(raw) > 0 {
+ if err := json.Unmarshal(raw, &body); err != nil {
+ t.Errorf("unmarshal: %v\nraw=%s", err, string(raw))
+ return
+ }
}
handler(t, r, body, w)
}))
@@ -367,10 +370,6 @@ func TestXiaomiUnsupportedMethods(t *testing.T) {
if _, err := m.Rerank(ctx, &model, RerankRequest{Query: "q", Documents: []string{"d"}}, cfg, nil, nil); err == nil || !strings.Contains(err.Error(), "no such method") {
t.Errorf("Rerank: %v", err)
}
- // CheckConnection IS implemented — verifies API config and base URL are reachable.
- if err := m.CheckConnection(ctx, cfg); err != nil {
- t.Errorf("CheckConnection: %v", err)
- }
// TranscribeAudio IS implemented; with nil file it returns input validation error.
if _, err := m.TranscribeAudio(ctx, &model, nil, cfg, nil, nil); err == nil || !strings.Contains(err.Error(), "file is missing") {
t.Errorf("TranscribeAudio: %v", err)
@@ -383,3 +382,141 @@ func TestXiaomiUnsupportedMethods(t *testing.T) {
t.Errorf("OCRFile: %v", err)
}
}
+
+func newXiaomiListModelsForTest(baseURL string) *XiaomiModel {
+ return NewXiaomiModel(
+ map[string]string{"default": baseURL},
+ URLSuffix{Chat: "v1/chat/completions", Models: "models"},
+ )
+}
+
+func TestXiaomiListModelsHappyPath(t *testing.T) {
+ withSSRFBypass(t)
+ ctx := t.Context()
+ srv := newXiaomiServer(t, "/v1/models", func(t *testing.T, r *http.Request, body map[string]interface{}, w http.ResponseWriter) {
+ if r.Method != http.MethodGet {
+ t.Errorf("expected method GET, got %s", r.Method)
+ }
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write([]byte(`{"object":"list","data":[{"id":"mimo-v2.5","object":"model","owned_by":"xiaomi"},{"id":"mimo-v2.5-pro","object":"model","owned_by":"xiaomi"}]}`))
+ })
+ defer srv.Close()
+
+ m := newXiaomiListModelsForTest(srv.URL + "/v1")
+ apiKey := "test-key"
+ models, err := m.ListModels(ctx, &APIConfig{ApiKey: &apiKey})
+ if err != nil {
+ t.Fatalf("ListModels: %v", err)
+ }
+
+ names := make([]string, 0, len(models))
+ for _, model := range models {
+ names = append(names, model.Name)
+ }
+ if got := strings.Join(names, ","); got != "mimo-v2.5,mimo-v2.5-pro" {
+ t.Fatalf("models=%v, want [mimo-v2.5 mimo-v2.5-pro]", names)
+ }
+
+ // The remote payload only carries ids, so the catalog must backfill types.
+ wantTypes := []string{"chat", "vision"}
+ for _, want := range wantTypes {
+ found := false
+ for _, got := range models[0].ModelTypes {
+ if got == want {
+ found = true
+ break
+ }
+ }
+ if !found {
+ t.Errorf("mimo-v2.5 model_types=%v, want to contain %q", models[0].ModelTypes, want)
+ }
+ }
+}
+
+// The upstream payload only carries ids, and it advertises models the local
+// catalog does not list (the tts variants in Xiaomi's docs). Those entries
+// must still come back typed so the picker does not show them as chat-only.
+func TestXiaomiListModelsTypesModelsMissingFromCatalog(t *testing.T) {
+ withSSRFBypass(t)
+ ctx := t.Context()
+ srv := newXiaomiServer(t, "/v1/models", func(t *testing.T, r *http.Request, body map[string]interface{}, w http.ResponseWriter) {
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write([]byte(`{"object":"list","data":[{"id":"mimo-v2.5","object":"model","owned_by":"xiaomi"},{"id":"mimo-v2.5-asr","object":"model","owned_by":"xiaomi"},{"id":"mimo-v2.5-pro","object":"model","owned_by":"xiaomi"},{"id":"mimo-v2.5-tts","object":"model","owned_by":"xiaomi"},{"id":"mimo-v2.5-tts-voiceclone","object":"model","owned_by":"xiaomi"},{"id":"mimo-v2.5-tts-voicedesign","object":"model","owned_by":"xiaomi"}]}`))
+ })
+ defer srv.Close()
+
+ m := newXiaomiListModelsForTest(srv.URL + "/v1")
+ apiKey := "test-key"
+ models, err := m.ListModels(ctx, &APIConfig{ApiKey: &apiKey})
+ if err != nil {
+ t.Fatalf("ListModels: %v", err)
+ }
+
+ byName := make(map[string][]string, len(models))
+ for _, model := range models {
+ byName[model.Name] = model.ModelTypes
+ }
+ if len(byName) != 6 {
+ t.Fatalf("models=%v, want 6 entries", byName)
+ }
+ for _, name := range []string{"mimo-v2.5-tts-voiceclone", "mimo-v2.5-tts-voicedesign"} {
+ types, ok := byName[name]
+ if !ok {
+ t.Fatalf("model %q missing from %v", name, byName)
+ }
+ if len(types) != 1 || types[0] != "tts" {
+ t.Errorf("%s model_types=%v, want [tts]", name, types)
+ }
+ }
+}
+
+func TestXiaomiListModelsRejectsProviderError(t *testing.T) {
+ withSSRFBypass(t)
+ ctx := t.Context()
+ srv := newXiaomiServer(t, "/v1/models", func(t *testing.T, r *http.Request, body map[string]interface{}, w http.ResponseWriter) {
+ http.Error(w, "bad key", http.StatusUnauthorized)
+ })
+ defer srv.Close()
+
+ m := newXiaomiListModelsForTest(srv.URL + "/v1")
+ apiKey := "test-key"
+ if _, err := m.ListModels(ctx, &APIConfig{ApiKey: &apiKey}); err == nil || !strings.Contains(err.Error(), "401") {
+ t.Errorf("ListModels error=%v, want status 401", err)
+ }
+}
+
+func TestXiaomiListModelsRequiresURLSuffix(t *testing.T) {
+ withSSRFBypass(t)
+ ctx := t.Context()
+ // No server: the driver must fail before issuing a request.
+ m := newXiaomiForTest("http://unused")
+ apiKey := "test-key"
+ if _, err := m.ListModels(ctx, &APIConfig{ApiKey: &apiKey}); err == nil || !strings.Contains(err.Error(), "models URL suffix is not configured") {
+ t.Errorf("ListModels error=%v, want missing URL suffix", err)
+ }
+}
+
+func TestXiaomiCheckConnectionUsesListModels(t *testing.T) {
+ withSSRFBypass(t)
+ ctx := t.Context()
+ apiKey := "test-key"
+
+ srv := newXiaomiServer(t, "/v1/models", func(t *testing.T, r *http.Request, body map[string]interface{}, w http.ResponseWriter) {
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write([]byte(`{"object":"list","data":[{"id":"mimo-v2.5-pro"}]}`))
+ })
+ defer srv.Close()
+
+ if err := newXiaomiListModelsForTest(srv.URL+"/v1").CheckConnection(ctx, &APIConfig{ApiKey: &apiKey}); err != nil {
+ t.Errorf("CheckConnection: %v", err)
+ }
+
+ rejected := newXiaomiServer(t, "/v1/models", func(t *testing.T, r *http.Request, body map[string]interface{}, w http.ResponseWriter) {
+ http.Error(w, "bad key", http.StatusUnauthorized)
+ })
+ defer rejected.Close()
+
+ if err := newXiaomiListModelsForTest(rejected.URL+"/v1").CheckConnection(ctx, &APIConfig{ApiKey: &apiKey}); err == nil {
+ t.Error("CheckConnection: expected an error for a rejected key")
+ }
+}