RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Fix: remove table tenant_llm (#20204)
Lynn89f119ab1b24
diff --git a/cmd/ragflow_server.go b/cmd/ragflow_server.goindex b89ee93a49..9460a16099 100644--- a/cmd/ragflow_server.go+++ b/cmd/ragflow_server.go@@ -1107,7 +1107,6 @@ func startServer(ctx context.Context, serverName string, arguments *serverArgs)datasetsService := dataset.NewDatasetService()metadataService := service.NewMetadataService()chunkService := chunk.NewChunkService()- llmService := service.NewLLMService()tenantService := service.NewTenantService()chatService := service.NewChatService()chatChannelService := service.NewChatChannelService()@@ -1180,7 +1179,6 @@ func startServer(ctx context.Context, serverName string, arguments *serverArgs)systemHandler := handler.NewSystemHandler(systemService)statsHandler := handler.NewStatsHandler(statsService)chunkHandler := handler.NewChunkHandler(chunkService, userService)- llmHandler := handler.NewLLMHandler(llmService, userService)chatHandler := handler.NewChatHandler(chatService, userService)chatChannelHandler := handler.NewChatChannelHandler(chatChannelService)langfuseHandler := handler.NewLangfuseHandler(langfuseService)@@ -1217,10 +1215,8 @@ func startServer(ctx context.Context, serverName string, arguments *serverArgs)// Public chatbot/agentbot endpoints (api/v1/chatbots/...,// api/v1/agentbots/...) and the agent attachment download.// BotService delegates the agentBot completion to agentService so- // both paths share the same canvas runner. Reuse the llmService- // already constructed above (line 222) — do NOT redeclare with- // `:=` since the variable is in scope.- botService := service.NewBotService(agentService, llmService)+ // both paths share the same canvas runner.+ botService := service.NewBotService(agentService)botHandler := handler.NewBotHandler(botService)// Wire the TTS synthesizer to the per-tenant model-provider@@ -1294,7 +1290,6 @@ func startServer(ctx context.Context, serverName string, arguments *serverArgs)systemHandler,statsHandler,chunkHandler,- llmHandler,chatHandler,chatChannelHandler,langfuseHandler,diff --git a/internal/admin/service.go b/internal/admin/service.goindex 1f41c5fc48..a535a68dcd 100644--- a/internal/admin/service.go+++ b/internal/admin/service.go@@ -36,7 +36,6 @@ import ("ragflow/internal/entity"modelModule "ragflow/internal/entity/models""ragflow/internal/server"- "ragflow/internal/server/config"servicepkg "ragflow/internal/service""ragflow/internal/storage""ragflow/internal/utility"@@ -55,7 +54,6 @@ type Service struct {systemSettingsDAO *dao.SystemSettingsDAOtenantDAO *dao.TenantDAOuserTenantDAO *dao.UserTenantDAO- tenantLLMDAO *dao.TenantLLMDAOfileDAO *dao.FileDAOdocumentDAO *dao.DocumentDAOtaskDAO *dao.TaskDAO@@ -87,7 +85,6 @@ func NewService() *Service {systemSettingsDAO: dao.NewSystemSettingsDAO(),tenantDAO: dao.NewTenantDAO(),userTenantDAO: dao.NewUserTenantDAO(),- tenantLLMDAO: dao.NewTenantLLMDAO(),fileDAO: dao.NewFileDAO(),documentDAO: dao.NewDocumentDAO(),taskDAO: dao.NewTaskDAO(),@@ -308,19 +305,7 @@ func (s *Service) CreateUser(ctx context.Context, username, password, role strinreturn nil, fmt.Errorf("failed to create user-tenant relation: %w", err)}- // 4. Create tenant LLM configurations- tenantLLMs, err := s.getInitTenantLLM(ctx, userID)- if err != nil {- common.Warn("failed to get init tenant LLM configs", zap.Error(err))- // Continue without LLM configs - not a critical error- } else if len(tenantLLMs) > 0 {- if err = tx.Create(&tenantLLMs).Error; err != nil {- common.Warn("failed to create tenant LLM configs", zap.Error(err))- // Continue without LLM configs - not a critical error- }- }-- // 5. Create root file folder+ // 4. Create root file folderfileID := utility.GenerateToken()fileLocation := ""file := &entity.File{@@ -355,132 +340,6 @@ func (s *Service) CreateUser(ctx context.Context, username, password, role strin}, nil}-// getInitTenantLLM gets initial tenant LLM configurations-// This matches Python's get_init_tenant_llm function-func (s *Service) getInitTenantLLM(ctx context.Context, userID string) ([]*entity.TenantLLM, error) {- cfg := server.GetConfig()- if cfg == nil {- return nil, fmt.Errorf("config not initialized")- }-- var tenantLLMs []*entity.TenantLLM-- // Get model configs from configuration- modelConfigs := []config.ModelConfig{- cfg.GetDefaultChatModel(),- cfg.GetDefaultEmbeddingModel(),- cfg.GetDefaultRerankModel(),- cfg.GetDefaultASRModel(),- cfg.GetDefaultVisionModel(),- cfg.GetDefaultTTSModel(),- cfg.GetDefaultOCRModel(),- }-- // Track seen factories to avoid duplicates- seenFactories := make(map[string]bool)- var uniqueFactories []config.ModelConfig-- for _, mc := range modelConfigs {- if mc.Factory == "" {- continue- }- if !seenFactories[mc.Factory] {- seenFactories[mc.Factory] = true- uniqueFactories = append(uniqueFactories, mc)- }- }-- // Get LLMs for each unique factory- for _, factoryConfig := range uniqueFactories {- models, err := s.llmDAO.GetByFactory(ctx, dao.DB, factoryConfig.Factory)- if err != nil {- common.Warn("failed to get LLMs for factory", zap.String("factory", factoryConfig.Factory), zap.Error(err))- continue- }-- for _, model := range models {- // Determine API key and base URL based on model type- var apiKey, apiBase string- switch model.ModelType {- case entity.ModelTypeChat.String():- apiKey = factoryConfig.APIKey- apiBase = factoryConfig.BaseURL- case entity.ModelTypeEmbedding.String():- apiKey = cfg.GetDefaultEmbeddingModel().APIKey- apiBase = cfg.GetDefaultEmbeddingModel().BaseURL- if apiKey == "" {- apiKey = factoryConfig.APIKey- }- if apiBase == "" {- apiBase = factoryConfig.BaseURL- }- case entity.ModelTypeRerank.String():- apiKey = cfg.GetDefaultRerankModel().APIKey- apiBase = cfg.GetDefaultRerankModel().BaseURL- if apiKey == "" {- apiKey = factoryConfig.APIKey- }- if apiBase == "" {- apiBase = factoryConfig.BaseURL- }- case entity.ModelTypeSpeech2Text.String():- apiKey = cfg.GetDefaultASRModel().APIKey- apiBase = cfg.GetDefaultASRModel().BaseURL- if apiKey == "" {- apiKey = factoryConfig.APIKey- }- if apiBase == "" {- apiBase = factoryConfig.BaseURL- }- case entity.ModelTypeImage2Text.String():- apiKey = cfg.GetDefaultVisionModel().APIKey- apiBase = cfg.GetDefaultVisionModel().BaseURL- if apiKey == "" {- apiKey = factoryConfig.APIKey- }- if apiBase == "" {- apiBase = factoryConfig.BaseURL- }- default:- apiKey = factoryConfig.APIKey- apiBase = factoryConfig.BaseURL- }-- maxTokens := int64(8192)- if model.MaxTokens > 0 {- maxTokens = model.MaxTokens- }-- llmName := model.LLMName- modelType := model.ModelType- tenantLLM := &entity.TenantLLM{- TenantID: userID,- LLMFactory: factoryConfig.Factory,- LLMName: &llmName,- ModelType: &modelType,- APIKey: &apiKey,- APIBase: &apiBase,- MaxTokens: maxTokens,- Status: "1",- }- tenantLLMs = append(tenantLLMs, tenantLLM)- }- }-- // Remove duplicates based on (tenant_id, llm_factory, llm_name)- seen := make(map[string]bool)- var uniqueLLMs []*entity.TenantLLM- for _, tenantLLM := range tenantLLMs {- key := fmt.Sprintf("%s|%s|%s", tenantLLM.TenantID, tenantLLM.LLMFactory, *tenantLLM.LLMName)- if !seen[key] {- seen[key] = true- uniqueLLMs = append(uniqueLLMs, tenantLLM)- }- }-- return uniqueLLMs, nil-}-// GetUserDetails get user detailsfunc (s *Service) GetUserDetails(ctx context.Context, username string) (map[string]interface{}, error) {user, err := s.userDAO.GetByEmail(ctx, dao.DB, username)@@ -509,7 +368,6 @@ func (s *Service) GetUserDetails(ctx context.Context, username string) (map[stri// DeleteUserResult result of delete user operationtype DeleteUserResult struct {Username string `json:"username"`- TenantLLMCount int `json:"tenant_llm_count"`LangfuseCount int `json:"langfuse_count"`MetadataTable string `json:"metadata_table"`TenantCount int `json:"tenant_count"`diff --git a/internal/admin/user_delete.go b/internal/admin/user_delete.goindex dbe4bc510e..d1169ee426 100644--- a/internal/admin/user_delete.go+++ b/internal/admin/user_delete.go@@ -745,11 +745,6 @@ func (data *userDeletionData) deleteDatabaseRows(ctx context.Context, tx *gorm.Dreturn err}}- tenantLLMCount, err := remove("tenant LLMs", &entity.TenantLLM{}, "tenant_id = ?", owner)- if err != nil {- return err- }- result.TenantLLMCount = int(tenantLLMCount)langfuseCount, err := remove("Langfuse credentials", &entity.TenantLangfuse{}, "tenant_id = ?", owner)if err != nil {return errdiff --git a/internal/admin/user_delete_test.go b/internal/admin/user_delete_test.goindex fa83f2e7ba..79c2c0b6e4 100644--- a/internal/admin/user_delete_test.go+++ b/internal/admin/user_delete_test.go@@ -132,7 +132,7 @@ func setupUserDeletionDB(t *testing.T) *gorm.DB {&entity.IngestionTask{}, &entity.IngestionTaskLog{}, &entity.FileCommit{}, &entity.FileCommitItem{},&entity.TenantModelProvider{}, &entity.TenantModelInstance{}, &entity.TenantModel{},&entity.TenantModelGroup{}, &entity.TenantModelGroupMapping{},- &entity.TenantLLM{}, &entity.TenantLangfuse{}, &entity.APIToken{}, &entity.InvitationCode{},+ &entity.TenantLangfuse{}, &entity.APIToken{}, &entity.InvitationCode{},)t.Cleanup(testutil.ReplaceDBForTest(t, db))return dbdiff --git a/internal/agent/component/agent.go b/internal/agent/component/agent.goindex 1f0848ea3e..bfb4f0c3e1 100644--- a/internal/agent/component/agent.go+++ b/internal/agent/component/agent.go@@ -56,8 +56,7 @@ const defaultAgentDeferredTimeout = 10 * time.Minute// provider is `parts[1]` for the 2-part shape and `parts[2]` for// the 3+ shape. Any middle `@<seg>` segments (the "instance" in// Python's split_model_name) are intentionally dropped — the Go-// drivers and the tenant_llm lookup both key on the bare model-// name + factory, not on the instance.+// drivers key on the bare model name, not on the instance.//// Mirrors Python's split_model_name at// api/db/joint_services/tenant_model_service.py:163-178:@@ -1130,9 +1129,7 @@ func buildAgentChatModel(ctx context.Context, p AgentParam) (*models.EinoChatMod// llm_id format. The RAGFlow DSL stores the model identifier as// "<model>@<instance>@<provider>" (mirrors Python's// split_model_name at- // api/db/joint_services/tenant_model_service.py:163-178 and the- // Go-side SplitModelNameAndFactory at- // internal/service/tenant.go:168). Two-part+ // api/db/joint_services/tenant_model_service.py:163-178. Two-part// "<model>@<provider>" and bare "<model>" are also accepted —// bare means no driver known, which falls through to the dummy// driver below. The trailing "@<provider>" suffix must also bediff --git a/internal/agent/component/llm_credentials.go b/internal/agent/component/llm_credentials.goindex 83c8794110..1f99b7e41e 100644--- a/internal/agent/component/llm_credentials.go+++ b/internal/agent/component/llm_credentials.go@@ -17,9 +17,8 @@ import ()// resolveTenantLLMConfig fills tenant-scoped API credentials for the supplied-// driver/model pair when the canvas DSL omitted them. It first checks the old-// tenant_llm table, then falls back to tenant_model_provider +-// tenant_model_instance when the composite llm_id carries an instance name.+// driver/model pair when the canvas DSL omitted them. It resolves them through+// tenant_model_provider + tenant_model_instance using the composite llm_id.func resolveTenantLLMConfig(ctx context.Context, db *gorm.DB, driver, modelID, apiKey, baseURL, originalModelID string) (string, string) {if apiKey != "" || driver == "" || modelID == "" {return apiKey, baseURL@@ -35,9 +34,6 @@ func resolveTenantLLMConfig(ctx context.Context, db *gorm.DB, driver, modelID, areturn apiKey, baseURL}- if resolvedKey, resolvedBaseURL, ok := resolveTenantLLMCredentials(ctx, db, tid, driver, modelID, baseURL); ok {- return resolvedKey, resolvedBaseURL- }if originalModelID == "" {return apiKey, baseURL}@@ -107,33 +103,6 @@ func rejectUnsupportedImages(ctx context.Context, db *gorm.DB, state *runtime.Careturn nil}-// resolveTenantLLMCredentials looks up the old tenant_llm table for the given-// tenant / factory / model. Returns true when credentials were found.-func resolveTenantLLMCredentials(ctx context.Context, db *gorm.DB, tid, driver, modelID, baseURL string) (string, string, bool) {- common.Debug("llm credentials: tenant_llm lookup", zap.String("tid", tid), zap.String("factory", driver), zap.String("model", modelID))- row, err := dao.NewTenantLLMDAO().GetByTenantFactoryAndModelName(ctx, db, tid, driver, modelID)- if err != nil {- common.Debug("llm credentials: tenant_llm lookup", zap.Error(err))- return "", baseURL, false- }- if row == nil {- common.Debug("llm credentials: tenant_llm lookup: no row")- return "", baseURL, false- }-- apiKey := ""- if row.APIKey != nil {- apiKey = *row.APIKey- }- if baseURL == "" && row.APIBase != nil {- baseURL = *row.APIBase- }- common.Debug("llm credentials: tenant_llm OK",- zap.Bool("api_key_present", apiKey != ""),- zap.Bool("base_url_present", baseURL != ""))- return apiKey, baseURL, apiKey != ""-}-// resolveTenantModelInstanceCredentials attempts to resolve llm credentials// through tenant_model_provider + tenant_model_instance using the original// composite llm_id (which still carries the instance name).diff --git a/internal/dao/database.go b/internal/dao/database.goindex a7ce337a78..905014cc46 100644--- a/internal/dao/database.go+++ b/internal/dao/database.go@@ -114,30 +114,6 @@ func (m namedIndexMigrator) MigrateColumnUnique(dst interface{}, field *schema.Freturn m.Migrator.MigrateColumnUnique(dst, field, columnType)}-// LLMFactoryConfig represents a single LLM factory configuration-type LLMFactoryConfig struct {- Name string `json:"name"`- Logo string `json:"logo"`- Tags string `json:"tags"`- Status string `json:"status"`- Rank string `json:"rank"`- LLM []LLMConfig `json:"llm"`-}--// LLMConfig represents a single LLM model configuration-type LLMConfig struct {- LLMName string `json:"llm_name"`- Tags string `json:"tags"`- MaxTokens int64 `json:"max_tokens"`- ModelType string `json:"model_type"`- IsTools bool `json:"is_tools"`-}--// LLMFactoriesFile represents the structure of llm_factories.json-type LLMFactoriesFile struct {- FactoryLLMInfos []LLMFactoryConfig `json:"factory_llm_infos"`-}-// InitDB initialize database connectionfunc InitDB(ctx context.Context, migrateDB bool) error {globalConfig := server.GetConfig()diff --git a/internal/dao/tenant_llm.go b/internal/dao/tenant_llm.godeleted file mode 100644index aaeba1dc4a..0000000000--- a/internal/dao/tenant_llm.go+++ /dev/null@@ -1,344 +0,0 @@-//-// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.-//--package dao--import (- "context"- "fmt"- "ragflow/internal/entity"-- "gorm.io/gorm"-)--// TenantLLMDAO tenant LLM data access object-type TenantLLMDAO struct{}--// NewTenantLLMDAO create tenant LLM DAO-func NewTenantLLMDAO() *TenantLLMDAO {- return &TenantLLMDAO{}-}--// GetByID get tenant LLM by primary key ID-func (dao *TenantLLMDAO) GetByID(ctx context.Context, db *gorm.DB, id int64) (*entity.TenantLLM, error) {- var tenantLLM entity.TenantLLM- err := db.WithContext(ctx).Where("id = ?", id).First(&tenantLLM).Error- if err != nil {- return nil, err- }- return &tenantLLM, nil-}--// GetByTenantAndModelName get tenant LLM by tenant ID and model name-func (dao *TenantLLMDAO) GetByTenantAndModelName(ctx context.Context, db *gorm.DB, tenantID, providerName string, modelName string) (*entity.TenantLLM, error) {- var tenantLLM entity.TenantLLM- err := db.WithContext(ctx).Where("tenant_id = ? AND llm_factory = ? AND llm_name = ?", tenantID, providerName, modelName).First(&tenantLLM).Error- if err != nil {- return nil, err- }- return &tenantLLM, nil-}--// GetByTenantNameAndType get tenant LLM by tenant ID, model name, and model type-func (dao *TenantLLMDAO) GetByTenantNameAndType(ctx context.Context, db *gorm.DB, tenantID, modelName string, modelType entity.ModelType) (*entity.TenantLLM, error) {- var tenantLLM entity.TenantLLM- // tenant_llm.model_type is a VARCHAR column, so convert to string.- err := db.WithContext(ctx).Where("tenant_id = ? AND llm_name = ? AND model_type = ?", tenantID, modelName, modelType.String()).First(&tenantLLM).Error- if err != nil {- return nil, err- }- return &tenantLLM, nil-}--// GetByTenantAndType get tenant LLM by tenant ID and model type-func (dao *TenantLLMDAO) GetByTenantAndType(ctx context.Context, db *gorm.DB, tenantID string, modelType entity.ModelType) (*entity.TenantLLM, error) {- var tenantLLM entity.TenantLLM- // tenant_llm.model_type is a VARCHAR column, so convert to string.- err := db.WithContext(ctx).Where("tenant_id = ? AND model_type = ?", tenantID, modelType.String()).First(&tenantLLM).Error- if err != nil {- return nil, err- }- return &tenantLLM, nil-}--// GetByTenantAndFactory get tenant LLM by tenant ID, model type and factory-func (dao *TenantLLMDAO) GetByTenantAndFactory(ctx context.Context, db *gorm.DB, tenantID string, modelType entity.ModelType, factory string) (*entity.TenantLLM, error) {- var tenantLLM entity.TenantLLM- // tenant_llm.model_type is a VARCHAR column, so convert to string.- err := db.WithContext(ctx).Where("tenant_id = ? AND model_type = ? AND llm_factory = ?", tenantID, modelType.String(), factory).First(&tenantLLM).Error- if err != nil {- return nil, err- }- return &tenantLLM, nil-}--// ListByTenant list all tenant LLMs for a tenant-func (dao *TenantLLMDAO) ListByTenant(ctx context.Context, db *gorm.DB, tenantID string) ([]entity.TenantLLM, error) {- var tenantLLMs []entity.TenantLLM- err := db.WithContext(ctx).Where("tenant_id = ?", tenantID).Find(&tenantLLMs).Error- if err != nil {- return nil, err- }- return tenantLLMs, nil-}--// GetByTenantFactoryAndModelName get tenant LLM by tenant ID, factory and model name-func (dao *TenantLLMDAO) GetByTenantFactoryAndModelName(ctx context.Context, db *gorm.DB, tenantID, factory, modelName string) (*entity.TenantLLM, error) {- var tenantLLM entity.TenantLLM- err := db.WithContext(ctx).Where("tenant_id = ? AND llm_factory = ? AND llm_name = ?", tenantID, factory, modelName).First(&tenantLLM).Error- if err != nil {- return nil, err- }- return &tenantLLM, nil-}--// Create a new tenant LLM record-func (dao *TenantLLMDAO) Create(ctx context.Context, db *gorm.DB, tenantLLM *entity.TenantLLM) error {- return db.WithContext(ctx).Create(tenantLLM).Error-}--// Update an existing tenant LLM record-func (dao *TenantLLMDAO) Update(ctx context.Context, db *gorm.DB, tenantLLM *entity.TenantLLM) error {- return db.WithContext(ctx).Save(tenantLLM).Error-}--// Delete a tenant LLM record by tenant ID, factory and model name-func (dao *TenantLLMDAO) Delete(ctx context.Context, db *gorm.DB, tenantID, factory, modelName string) error {- return db.WithContext(ctx).Where("tenant_id = ? AND llm_factory = ? AND llm_name = ?", tenantID, factory, modelName).Delete(&entity.TenantLLM{}).Error-}--// GetMyLLMs get tenant LLMs with factory details-func (dao *TenantLLMDAO) GetMyLLMs(ctx context.Context, db *gorm.DB, tenantID string) ([]entity.MyLLM, error) {- var myLLMs []entity.MyLLM-- err := db.WithContext(ctx).Table("tenant_llm tl").- Select("tl.id, tl.llm_factory, lf.logo, lf.tags, tl.model_type, tl.llm_name, tl.used_tokens, tl.status").- Joins("JOIN llm_factories lf ON tl.llm_factory = lf.name").- Where("tl.tenant_id = ? AND tl.api_key IS NOT NULL", tenantID).- Find(&myLLMs).Error- if err != nil {- return nil, err- }- return myLLMs, nil-}--// ListValidByTenant lists valid tenant LLMs for a tenant-func (dao *TenantLLMDAO) ListValidByTenant(ctx context.Context, db *gorm.DB, tenantID string) ([]*entity.TenantLLM, error) {- var tenantLLMs []*entity.TenantLLM- err := db.WithContext(ctx).Where("tenant_id = ? AND api_key IS NOT NULL AND api_key != ? AND status = ?", tenantID, "", "1").Find(&tenantLLMs).Error- if err != nil {- return nil, err- }- return tenantLLMs, nil-}--// ListAllByTenant lists all tenant LLMs for a tenant-func (dao *TenantLLMDAO) ListAllByTenant(ctx context.Context, db *gorm.DB, tenantID string) ([]*entity.TenantLLM, error) {- var tenantLLMs []*entity.TenantLLM- err := db.WithContext(ctx).Where("tenant_id = ?", tenantID).Find(&tenantLLMs).Error- if err != nil {- return nil, err- }- return tenantLLMs, nil-}--// InsertMany inserts multiple tenant LLM records-func (dao *TenantLLMDAO) InsertMany(ctx context.Context, db *gorm.DB, tenantLLMs []*entity.TenantLLM) error {- if len(tenantLLMs) == 0 {- return nil- }- return db.WithContext(ctx).Create(&tenantLLMs).Error-}--// DeleteByTenantID deletes all tenant LLM records by tenant ID (hard delete)-func (dao *TenantLLMDAO) DeleteByTenantID(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {- result := db.WithContext(ctx).Unscoped().Where("tenant_id = ?", tenantID).Delete(&entity.TenantLLM{})- return result.RowsAffected, result.Error-}--// splitModelNameAndFactory splits model name and factory from combined format-// This matches Python's split_model_name_and_factory logic-//-// Parameters:-// - modelName: The model name which can be in format "ModelName" or "ModelName@Factory"-//-// Returns:-// - string: The model name without factory prefix-// - string: The factory name (empty string if not specified)-//-// Example:-//-// modelName, factory := splitModelNameAndFactory("gpt-4")-// // Returns: "gpt-4", ""-//-// modelName, factory := splitModelNameAndFactory("gpt-4@OpenAI")-// // Returns: "gpt-4", "OpenAI"-func splitModelNameAndFactory(ctx context.Context, db *gorm.DB, modelName string) (string, string) {- // Split by "@" separator- // Handle cases like "model@factory" or "model@sub@factory"- lastAtIndex := -1- for i := len(modelName) - 1; i >= 0; i-- {- if modelName[i] == '@' {- lastAtIndex = i- break- }- }-- // No "@" found, return original name- if lastAtIndex == -1 {- return modelName, ""- }-- // Split into model name and potential factory- modelNamePart := modelName[:lastAtIndex]- factory := modelName[lastAtIndex+1:]-- // Validate if factory exists in llm_factories table- // This matches Python's logic of checking against model providers- var factoryCount int64- db.WithContext(ctx).Model(&entity.LLMFactories{}).Where("name = ?", factory).Count(&factoryCount)-- // If factory doesn't exist in database, treat the whole string as model name- if factoryCount == 0 {- return modelName, ""- }-- return modelNamePart, factory-}--// GetByTenantIDAndLLMName gets tenant LLM by tenant ID and LLM name-// This is used to resolve tenant_llm_id from llm_id-// It supports both simple model names and factory-prefixed names (e.g., "gpt-4@OpenAI")-//-// Parameters:-// - tenantID: The tenant identifier-// - llmName: The LLM model name (can include factory prefix like "OpenAI@gpt-4")-//-// Returns:-// - *model.TenantLLM: The tenant LLM record-// - error: Error if not found-//-// Example:-//-// // Simple model name-// tenantLLM, err := dao.GetByTenantIDAndLLMName("tenant123", "gpt-4")-//-// // Model name with factory prefix-// tenantLLM, err := dao.GetByTenantIDAndLLMName("tenant123", "gpt-4@OpenAI")-func (dao *TenantLLMDAO) GetByTenantIDAndLLMName(ctx context.Context, db *gorm.DB, tenantID string, llmName string) (*entity.TenantLLM, error) {- var tenantLLM entity.TenantLLM-- // Split model name and factory from the combined format- modelName, factory := splitModelNameAndFactory(ctx, db, llmName)-- // First attempt: try to find with model name only- err := db.WithContext(ctx).Where("tenant_id = ? AND llm_name = ?", tenantID, modelName).First(&tenantLLM).Error- if err == nil {- return &tenantLLM, nil- }-- // Second attempt: if factory is specified, try with both model name and factory- if factory != "" {- err = db.WithContext(ctx).Where("tenant_id = ? AND llm_name = ? AND llm_factory = ?", tenantID, modelName, factory).First(&tenantLLM).Error- if err == nil {- return &tenantLLM, nil- }-- // Special handling for LocalAI and HuggingFace (matching Python logic)- // These factories append "___FactoryName" to the model name- if factory == "LocalAI" || factory == "HuggingFace" || factory == "OpenAI-API-Compatible" {- specialModelName := modelName + "___" + factory- err = db.WithContext(ctx).Where("tenant_id = ? AND llm_name = ?", tenantID, specialModelName).First(&tenantLLM).Error- if err == nil {- return &tenantLLM, nil- }- }- }-- // Return the last error (record not found)- return nil, err-}--// GetByTenantIDLLMNameAndFactory gets tenant LLM by tenant ID, LLM name and factory-// This is used when model name includes factory suffix (e.g., "model@factory")-//-// Parameters:-// - tenantID: The tenant identifier-// - llmName: The LLM model name-// - factory: The LLM factory name-//-// Returns:-// - *model.TenantLLM: The tenant LLM record-// - error: Error if not found-//-// Example:-//-// tenantLLM, err := dao.GetByTenantIDLLMNameAndFactory("tenant123", "gpt-4", "OpenAI")-func (dao *TenantLLMDAO) GetByTenantIDLLMNameAndFactory(ctx context.Context, db *gorm.DB, tenantID, llmName, factory string) (*entity.TenantLLM, error) {- var tenantLLM entity.TenantLLM- err := db.WithContext(ctx).Where("tenant_id = ? AND llm_name = ? AND llm_factory = ?", tenantID, llmName, factory).First(&tenantLLM).Error- if err != nil {- return nil, err- }- return &tenantLLM, nil-}--// LookupTenantLLMByID looks up a TenantLLM record by ID and returns the record plus composite model name.-func LookupTenantLLMByID(ctx context.Context, db *gorm.DB, tenantLLMDao *TenantLLMDAO, id int64) (*entity.TenantLLM, string, error) {- tenantLLM, err := tenantLLMDao.GetByID(ctx, db, id)- if err != nil {- return nil, "", fmt.Errorf("failed to get tenant_llm by id %d: %w", id, err)- }- if tenantLLM == nil || tenantLLM.LLMName == nil || *tenantLLM.LLMName == "" {- return nil, "", fmt.Errorf("tenant_llm record not found for id %d", id)- }- compositeName := fmt.Sprintf("%s@%s", *tenantLLM.LLMName, tenantLLM.LLMFactory)- return tenantLLM, compositeName, nil-}--// LookupTenantLLMByName looks up a TenantLLM record by tenant name and model type.-func LookupTenantLLMByName(ctx context.Context, db *gorm.DB, tenantLLMDao *TenantLLMDAO, tenantID, name string, modelType entity.ModelType) (*entity.TenantLLM, string, error) {- // Parse factory from name if present (e.g., "model@Factory")- modelName, factory := splitModelNameAndFactory(ctx, db, name)-- // If factory is found, use factory-based lookup- if factory != "" {- return LookupTenantLLMByFactory(ctx, db, tenantLLMDao, tenantID, factory, modelName, modelType)- }-- tenantLLM, err := tenantLLMDao.GetByTenantNameAndType(ctx, db, tenantID, modelName, modelType)- if err != nil {- return nil, "", fmt.Errorf("failed to get tenant_llm by name %s: %w", name, err)- }- if tenantLLM == nil || tenantLLM.LLMName == nil || *tenantLLM.LLMName == "" {- return nil, "", fmt.Errorf("tenant_llm record not found for name %s", name)- }- compositeName := fmt.Sprintf("%s@%s", *tenantLLM.LLMName, tenantLLM.LLMFactory)- return tenantLLM, compositeName, nil-}--// LookupTenantLLMByFactory looks up a TenantLLM record by tenant, factory, and model name.-func LookupTenantLLMByFactory(ctx context.Context, db *gorm.DB, tenantLLMDao *TenantLLMDAO, tenantID, factory, name string, modelType entity.ModelType) (*entity.TenantLLM, string, error) {- tenantLLM, err := tenantLLMDao.GetByTenantFactoryAndModelName(ctx, db, tenantID, factory, name)- if err != nil {- return nil, "", fmt.Errorf("failed to get tenant_llm by factory %s and name %s: %w", factory, name, err)- }- if tenantLLM == nil || tenantLLM.LLMName == nil || *tenantLLM.LLMName == "" {- return nil, "", fmt.Errorf("tenant_llm record not found for factory %s and name %s", factory, name)- }- compositeName := fmt.Sprintf("%s@%s", *tenantLLM.LLMName, tenantLLM.LLMFactory)- return tenantLLM, compositeName, nil-}diff --git a/internal/handler/llm.go b/internal/handler/llm.godeleted file mode 100644index 804c317224..0000000000--- a/internal/handler/llm.go+++ /dev/null@@ -1,150 +0,0 @@-//-// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.-//--package handler--import (- "github.com/gin-gonic/gin"-- "ragflow/internal/common"- "ragflow/internal/service"-)--// FactoryResponse represents a model provider factory-type FactoryResponse struct {- Name string `json:"name"`- Logo string `json:"logo"`- Tags string `json:"tags"`- Status string `json:"status"`- Rank string `json:"rank"`- ModelTypes []string `json:"model_types"`-}--// LLMHandler LLM handler-type LLMHandler struct {- llmService *service.LLMService- userService *service.UserService-}--// NewLLMHandler create LLM handler-func NewLLMHandler(llmService *service.LLMService, userService *service.UserService) *LLMHandler {- return &LLMHandler{- llmService: llmService,- userService: userService,- }-}--// GetMyLLMs get my LLMs-// @Summary Get My LLMs-// @Description Get LLM list for current tenant-// @Tags llm-// @Accept json-// @Produce json-// @Security ApiKeyAuth-// @Param include_details query string false "Include detailed fields" default(false)-// @Success 200 {object} map[string]interface{}-// @Router /v1/llm/my_llms [get]-func (h *LLMHandler) GetMyLLMs(c *gin.Context) {- user, errorCode, errorMessage := GetUser(c)- if errorCode != common.CodeSuccess {- common.ErrorWithCode(c, errorCode, errorMessage)- return- }-- tenantID := user.ID- includeDetailsStr := c.DefaultQuery("include_details", "false")- includeDetails := includeDetailsStr == "true"- ctx := c.Request.Context()-- llms, err := h.llmService.GetMyLLMs(ctx, tenantID, includeDetails)- if err != nil {- common.ResponseWithCodeData(c, common.CodeExceptionError, false, err.Error())- return- }-- common.SuccessWithData(c, llms, "success")-}--// SetAPIKey set API key for a LLM factory-// @Summary Set API Key-// @Description Set API key for a LLM factory and test connectivity-// @Tags llm-// @Accept json-// @Produce json-// @Security ApiKeyAuth-// @Param request body service.SetAPIKeyRequest true "API Key configuration"-// @Success 200 {object} map[string]interface{}-// @Router /v1/llm/set_api_key [post]-func (h *LLMHandler) SetAPIKey(c *gin.Context) {- user, errorCode, errorMessage := GetUser(c)- if errorCode != common.CodeSuccess {- common.ErrorWithCode(c, errorCode, errorMessage)- return- }-- var req service.SetAPIKeyRequest- if err := c.ShouldBindJSON(&req); err != nil {- common.ResponseWithCodeData(c, common.CodeArgumentError, false, "Invalid request: "+err.Error())- return- }-- ctx := c.Request.Context()-- tenantID := user.ID- result, err := h.llmService.SetAPIKey(ctx, tenantID, &req)- if err != nil {- common.ResponseWithCodeData(c, common.CodeDataError, false, err.Error())- return- }-- if req.Verify {- common.SuccessWithData(c, result, "success")- return- }-- common.SuccessWithData(c, true, "success")-}--// ListApp lists LLMs grouped by factory-// @Summary List LLMs-// @Description Get list of LLMs grouped by factory with availability info-// @Tags llm-// @Accept json-// @Produce json-// @Security ApiKeyAuth-// @Param model_type query string false "Filter by model type"-// @Success 200 {object} map[string][]service.LLMListItem-// @Router /v1/llm/list [get]-func (h *LLMHandler) ListApp(c *gin.Context) {- user, errorCode, errorMessage := GetUser(c)- if errorCode != common.CodeSuccess {- common.ErrorWithCode(c, errorCode, errorMessage)- return- }-- tenantID := user.ID- ctx := c.Request.Context()-- modelType := c.Query("model_type")-- llms, err := h.llmService.ListLLMs(ctx, tenantID, modelType)- if err != nil {- common.ResponseWithCodeData(c, common.CodeExceptionError, false, err.Error())- return- }-- common.SuccessWithData(c, llms, "success")-}diff --git a/internal/router/router.go b/internal/router/router.goindex 1ec04a98e1..cf5ed59652 100644--- a/internal/router/router.go+++ b/internal/router/router.go@@ -32,7 +32,6 @@ type Router struct {systemHandler *handler.SystemHandlerstatsHandler *handler.StatsHandlerchunkHandler *handler.ChunkHandler- llmHandler *handler.LLMHandlerchatHandler *handler.ChatHandlerchatChannelHandler *handler.ChatChannelHandlerlangfuseHandler *handler.LangfuseHandler@@ -71,7 +70,6 @@ func NewRouter(systemHandler *handler.SystemHandler,statsHandler *handler.StatsHandler,chunkHandler *handler.ChunkHandler,- llmHandler *handler.LLMHandler,chatHandler *handler.ChatHandler,chatChannelHandler *handler.ChatChannelHandler,langfuseHandler *handler.LangfuseHandler,@@ -107,7 +105,6 @@ func NewRouter(systemHandler: systemHandler,statsHandler: statsHandler,chunkHandler: chunkHandler,- llmHandler: llmHandler,chatHandler: chatHandler,chatChannelHandler: chatChannelHandler,langfuseHandler: langfuseHandler,diff --git a/internal/service/bot.go b/internal/service/bot.goindex 9b37b39321..b396f720db 100644--- a/internal/service/bot.go+++ b/internal/service/bot.go@@ -47,20 +47,17 @@ type BotService struct {canvasDAO *dao.UserCanvasDAOapi4ConversationDAO *dao.API4ConversationDAOagentService *AgentService- llmService *LLMServicepipeline *ChatPipelineService}// NewBotService wires a fresh BotService. agentSvc is required for-// AgentbotCompletion; llmSvc is required for ChatbotCompletion (in-// step 6). Both are nullable in unit tests.-func NewBotService(agentSvc *AgentService, llmSvc *LLMService) *BotService {+// AgentbotCompletion and is nullable in unit tests.+func NewBotService(agentSvc *AgentService) *BotService {return &BotService{chatDAO: dao.NewChatSessionDAO(),canvasDAO: dao.NewUserCanvasDAO(),api4ConversationDAO: dao.NewAPI4ConversationDAO(),agentService: agentSvc,- llmService: llmSvc,pipeline: NewChatPipelineService(),}}diff --git a/internal/service/bot_completion.go b/internal/service/bot_completion.goindex 316cdcabca..c61f4d1196 100644--- a/internal/service/bot_completion.go+++ b/internal/service/bot_completion.go@@ -396,11 +396,8 @@ func (s *BotService) ChatbotCompletion(// a sanitised error before any SSE byte is written when the// service is unwired (test boot path) or the dialog has no LLM// configured — see WriteChatbotFrame's sanitization contract.- // NewBotService wires both dependencies; the nil checks only- // guard a hand-rolled zero-value BotService against panicking.- if s.llmService == nil {- return nil, common.CodeServerError, errors.New("bot: llm service not wired")- }+ // NewBotService wires the pipeline; the nil check only guards a+ // hand-rolled zero-value BotService against panicking.if s.pipeline == nil {return nil, common.CodeServerError, errors.New("bot: chat pipeline not wired")}diff --git a/internal/service/bot_completion_history_test.go b/internal/service/bot_completion_history_test.goindex bc47faa03b..2873e0fd88 100644--- a/internal/service/bot_completion_history_test.go+++ b/internal/service/bot_completion_history_test.go@@ -171,7 +171,7 @@ func TestBotService_AgentbotInputs_CrossTenantDenied(t *testing.T) {t.Fatalf("seed tenant-B: %v", err)}- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)// Attacker (tenant-B) asks for victim (tenant-A's canvas).title, _, _, _, _, code, err := svc.AgentbotInputs(t.Context(),@@ -236,7 +236,7 @@ func TestBotService_AgentbotInputsReadsBeginParams(t *testing.T) {t.Fatalf("seed canvas: %v", err)}- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)gotTitle, gotAvatar, prologue, mode, inputs, code, err := svc.AgentbotInputs(t.Context(), "owner-1", "agent-1")if err != nil {@@ -407,7 +407,7 @@ func TestBotService_ChatbotCompletion_NewSessionSkipsLLM(t *testing.T) {// llmService is nil — any attempt to reach the LLM path would// fail loudly, so a successful run proves the LLM was skipped.- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)frames, code, err := svc.ChatbotCompletion(t.Context(),"tenant-1", "dlg-1", ChatbotCompletionRequest{Question: ""})if err != nil {@@ -509,7 +509,7 @@ func TestPersistChatbotTurn_AppendsPairAndReference(t *testing.T) {t.Fatalf("seed session: %v", err)}- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)ref := map[string]any{"chunks": []any{map[string]any{"chunk_id": "c1"}},"doc_aggs": []any{},@@ -574,7 +574,7 @@ func TestPersistChatbotTurn_NilReferenceDefaultsToEmpty(t *testing.T) {t.Fatalf("seed session: %v", err)}- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)if err := svc.persistChatbotQuestion(ctx, sess, "q", "msg-p2", 2); err != nil {t.Fatalf("persist question: %v", err)}@@ -623,7 +623,7 @@ func TestPersistChatbotTurn_ConcurrentSameSession(t *testing.T) {t.Fatalf("seed session: %v", err)}- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)// Both callers hold the same stale snapshot loaded before their// streams started — exactly the race the lock fixes.var wg sync.WaitGroup@@ -706,7 +706,7 @@ func TestStreamChatbotTurn_FinalFrameAndPersistenceUseRawStreamText(t *testing.TpushServiceDB(t, db)seedStreamTurnSession(t, "sess-s1", "q1", "msg-s1")- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)ref := map[string]any{"chunks": []any{map[string]any{"chunk_id": "c1"}, map[string]any{"chunk_id": "c2"}},@@ -764,7 +764,7 @@ func TestStreamChatbotTurn_ThinkMarkersPersistedAsTags(t *testing.T) {pushServiceDB(t, db)seedStreamTurnSession(t, "sess-s2", "q2", "msg-s2")- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)results := make(chan AsyncChatResult, 5)results <- AsyncChatResult{StartToThink: true}@@ -800,7 +800,7 @@ func TestStreamChatbotTurn_SingleShotFinalKeepsText(t *testing.T) {pushServiceDB(t, db)seedStreamTurnSession(t, "sess-s3", "q3", "msg-s3")- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)ref := map[string]any{"chunks": []any{}, "doc_aggs": []any{}}results := make(chan AsyncChatResult, 1)@@ -837,7 +837,7 @@ func TestStreamChatbotTurn_ErrorKeptOnWireAndNotPersisted(t *testing.T) {pushServiceDB(t, db)seedStreamTurnSession(t, "sess-s4", "q4", "msg-s4")- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)results := make(chan AsyncChatResult, 1)results <- AsyncChatResult{Answer: "**ERROR**: boom", Final: true}@@ -875,7 +875,7 @@ func TestStreamChatbotTurn_ErrorAfterDeltasKeptOnWire(t *testing.T) {pushServiceDB(t, db)seedStreamTurnSession(t, "sess-s5", "q5", "msg-s5")- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)results := make(chan AsyncChatResult, 2)results <- AsyncChatResult{Answer: "partial", Reference: map[string]any{}}@@ -919,7 +919,7 @@ func TestStreamChatbotTurn_ReasoningFieldForwardedAsStreamText(t *testing.T) {pushServiceDB(t, db)seedStreamTurnSession(t, "sess-s6", "q6", "msg-s6")- svc := NewBotService(nil, nil)+ svc := NewBotService(nil)results := make(chan AsyncChatResult, 5)results <- AsyncChatResult{StartToThink: true}diff --git a/internal/service/chunk/chunk.go b/internal/service/chunk/chunk.goindex efb59bac77..42d585b003 100644--- a/internal/service/chunk/chunk.go+++ b/internal/service/chunk/chunk.go@@ -383,14 +383,7 @@ func (s *ChunkService) RetrievalTest(ctx context.Context, req *service.RetrievalembdID = kbRecords[0].EmbdIDtarget, getErr = modelSolver.ResolveModelConfig(ctx, tenantIDs[0], entity.ModelTypeEmbedding, embdID)if getErr != nil {- _, embdID, err = dao.LookupTenantLLMByName(ctx, dao.DB, dao.NewTenantLLMDAO(), tenantIDs[0], embdID, entity.ModelTypeEmbedding)- if err != nil {- return nil, fmt.Errorf("failed to get embedding model by embd_id: %w", err)- }- target, getErr = modelSolver.ResolveModelConfig(ctx, tenantIDs[0], entity.ModelTypeEmbedding, embdID)- if getErr != nil {- return nil, fmt.Errorf("failed to get embedding model by embd_id: %w", getErr)- }+ return nil, fmt.Errorf("failed to get embedding model by embd_id: %w", getErr)}} else {target, getErr = modelSolver.ResolveDefaultModelConfig(ctx, tenantIDs[0], entity.ModelTypeEmbedding)diff --git a/internal/service/dataset/service.go b/internal/service/dataset/service.goindex e0116aa201..4cc6774b6d 100644--- a/internal/service/dataset/service.go+++ b/internal/service/dataset/service.go@@ -15,7 +15,6 @@ type DatasetService struct {documentDAO *dao.DocumentDAOconnectorDAO *dao.ConnectorDAOtenantDAO *dao.TenantDAO- tenantLLMDAO *dao.TenantLLMDAOpipelineLogDAO *dao.PipelineOperationLogDAOuserTenantDAO *dao.UserTenantDAOtaskDAO *dao.TaskDAO@@ -38,7 +37,6 @@ func NewDatasetService() *DatasetService {documentDAO: dao.NewDocumentDAO(),connectorDAO: dao.NewConnectorDAO(),tenantDAO: dao.NewTenantDAO(),- tenantLLMDAO: dao.NewTenantLLMDAO(),pipelineLogDAO: dao.NewPipelineOperationLogDAO(),userTenantDAO: dao.NewUserTenantDAO(),taskDAO: dao.NewTaskDAO(),diff --git a/internal/service/llm.go b/internal/service/llm.godeleted file mode 100644index d0bfe85d65..0000000000--- a/internal/service/llm.go+++ /dev/null@@ -1,405 +0,0 @@-//-// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.-//--package service--import (- "context"- "fmt"- "ragflow/internal/entity"- "strconv"- "strings"-- "ragflow/internal/dao"-)--// LLMService LLM service-type LLMService struct {- tenantLLMDAO *dao.TenantLLMDAO- llmDAO *dao.LLMDAO-}--// NewLLMService create LLM service-func NewLLMService() *LLMService {- return &LLMService{- tenantLLMDAO: dao.NewTenantLLMDAO(),- llmDAO: dao.NewLLMDAO(),- }-}--// MyLLMItem represents a single LLM item in the response-type MyLLMItem struct {- ID string `json:"id"`- Type string `json:"type"`- Name string `json:"name"`- UsedToken int64 `json:"used_token"`- Status string `json:"status"`- APIBase string `json:"api_base,omitempty"`- MaxTokens int64 `json:"max_tokens,omitempty"`-}--// MyLLMFactory represents the response structure for a factory in my LLMs-type MyLLMFactory struct {- Tags string `json:"tags"`- LLM []MyLLMItem `json:"llm"`-}--// GetMyLLMs get my LLMs for a tenant-func (s *LLMService) GetMyLLMs(ctx context.Context, tenantID string, includeDetails bool) (map[string]MyLLMFactory, error) {- result := make(map[string]MyLLMFactory)-- if includeDetails {- objs, err := s.tenantLLMDAO.ListAllByTenant(ctx, dao.DB, tenantID)- if err != nil {- return nil, err- }-- factoryDAO := dao.NewLLMFactoryDAO()- factories, err := factoryDAO.GetAllValid(ctx, dao.DB)- if err != nil {- return nil, err- }-- factoryTagsMap := make(map[string]string)- for _, f := range factories {- if f.Tags != "" {- factoryTagsMap[f.Name] = f.Tags- }- }-- for _, o := range objs {- llmFactory := o.LLMFactory- if _, exists := result[llmFactory]; !exists {- tags := factoryTagsMap[llmFactory]- result[llmFactory] = MyLLMFactory{- Tags: tags,- LLM: []MyLLMItem{},- }- }-- item := MyLLMItem{- ID: int64ToString(o.ID),- Type: getStringValue(o.ModelType),- Name: getStringValue(o.LLMName),- UsedToken: o.UsedTokens,- Status: getValidStatus(o.Status),- }-- if includeDetails {- item.APIBase = getStringValueDefault(o.APIBase, "")- item.MaxTokens = o.MaxTokens- }-- factory := result[llmFactory]- factory.LLM = append(factory.LLM, item)- result[llmFactory] = factory- }- } else {- objs, err := s.tenantLLMDAO.GetMyLLMs(ctx, dao.DB, tenantID)- if err != nil {- return nil, err- }-- for _, o := range objs {- llmFactory := o.LLMFactory- if _, exists := result[llmFactory]; !exists {- result[llmFactory] = MyLLMFactory{- Tags: getStringValue(o.Tags),- LLM: []MyLLMItem{},- }- }-- item := MyLLMItem{- ID: o.ID,- Type: getStringValue(o.ModelType),- Name: getStringValue(o.LLMName),- UsedToken: getInt64Value(o.UsedTokens),- Status: getStringValueDefault(o.Status, "1"),- }-- factory := result[llmFactory]- factory.LLM = append(factory.LLM, item)- result[llmFactory] = factory- }- }-- return result, nil-}--// LLMListItem represents a single LLM item in the list response-type LLMListItem struct {- ID string `json:"id"`- LLMName string `json:"llm_name"`- ModelType string `json:"model_type"`- FID string `json:"fid"`- Available bool `json:"available"`- Status string `json:"status"`- MaxTokens int64 `json:"max_tokens,omitempty"`- CreateDate *string `json:"create_date,omitempty"`- CreateTime *int64 `json:"create_time,omitempty"`- UpdateDate *string `json:"update_date,omitempty"`- UpdateTime *int64 `json:"update_time,omitempty"`- IsTools bool `json:"is_tools"`- Tags string `json:"tags,omitempty"`-}--// ListLLMsResponse represents the response for list LLMs-type ListLLMsResponse map[string][]LLMListItem--// ListLLMs lists LLMs for a tenant with availability info-func (s *LLMService) ListLLMs(ctx context.Context, tenantID string, modelType string) (ListLLMsResponse, error) {- selfDeployed := map[string]bool{- "FastEmbed": true,- "Ollama": true,- "Xinference": true,- "LocalAI": true,- "LM-Studio": true,- "GPUStack": true,- "ModelScope": true,- }-- objs, err := s.tenantLLMDAO.ListAllByTenant(ctx, dao.DB, tenantID)- if err != nil {- return nil, err- }-- facts := make(map[string]bool)- status := make(map[string]bool)- tenantLLMMapping := make(map[string]string)-- for _, o := range objs {- if o.APIKey != nil && *o.APIKey != "" && getValidStatus(o.Status) == "1" {- facts[o.LLMFactory] = true- }- llmName := getStringValue(o.LLMName)- key := llmName + "@" + o.LLMFactory- if getValidStatus(o.Status) == "1" {- status[key] = true- }- tenantLLMMapping[key] = int64ToString(o.ID)- }-- allLLMs, err := s.llmDAO.GetAllValid(ctx, dao.DB)- if err != nil {- return nil, err- }-- llmSet := make(map[string]bool)- result := make(ListLLMsResponse)-- for _, llm := range allLLMs {- if llm.Status == nil || *llm.Status != "1" {- continue- }-- key := llm.LLMName + "@" + llm.FID-- if llm.FID != "Builtin" && !status[key] {- continue- }-- if modelType != "" && !strings.Contains(llm.ModelType, modelType) {- continue- }-- available := facts[llm.FID] || selfDeployed[llm.FID] || strings.ToLower(llm.LLMName) == "flag-embedding"-- item := LLMListItem{- ID: tenantLLMMapping[key],- LLMName: llm.LLMName,- ModelType: llm.ModelType,- FID: llm.FID,- Available: available,- Status: "1",- MaxTokens: llm.MaxTokens,- IsTools: llm.IsTools,- Tags: llm.Tags,- }-- if llm.CreateDate != nil {- createDateStr := llm.CreateDate.Format("2006-01-02T15:04:05")- item.CreateDate = &createDateStr- }- item.CreateTime = llm.CreateTime- if llm.UpdateDate != nil {- updateDateStr := llm.UpdateDate.Format("2006-01-02T15:04:05")- item.UpdateDate = &updateDateStr- }- if llm.UpdateTime != nil {- item.UpdateTime = llm.UpdateTime- }-- result[llm.FID] = append(result[llm.FID], item)- llmSet[key] = true- }-- for _, o := range objs {- llmName := getStringValue(o.LLMName)- key := llmName + "@" + o.LLMFactory- if llmSet[key] {- continue- }-- modelTypeValue := getStringValue(o.ModelType)- if modelType != "" && !strings.Contains(modelTypeValue, modelType) {- continue- }-- item := LLMListItem{- ID: int64ToString(o.ID),- LLMName: llmName,- ModelType: modelTypeValue,- FID: o.LLMFactory,- Available: true,- Status: getValidStatus(o.Status),- }-- result[o.LLMFactory] = append(result[o.LLMFactory], item)- }-- return result, nil-}--func getStringValue(s *string) string {- if s == nil {- return ""- }- return *s-}--func getStringValueDefault(s *string, defaultVal string) string {- if s == nil || *s == "" {- return defaultVal- }- return *s-}--func getValidStatus(status string) string {- if status == "" {- return "1"- }- return status-}--func getInt64Value(i *int64) int64 {- if i == nil {- return 0- }- return *i-}--func getInt64ValueDefault(i *int64, defaultVal int64) int64 {- if i == nil || *i == 0 {- return defaultVal- }- return *i-}--func getBoolValue(b *bool) bool {- if b == nil {- return false- }- return *b-}--func int64ToString(n int64) string {- return strconv.FormatInt(n, 10)-}--// SetAPIKeyRequest represents the request for setting API key-type SetAPIKeyRequest struct {- LLMFactory string `json:"llm_factory"`- APIKey string `json:"api_key"`- BaseURL string `json:"base_url"`- SourceFID string `json:"source_fid"`- ModelType string `json:"model_type"`- LLMName string `json:"llm_name"`- Verify bool `json:"verify"`- MaxTokens int64 `json:"max_tokens"`-}--// SetAPIKeyResult represents the result of setting API key-type SetAPIKeyResult struct {- Message string `json:"message"`- Success bool `json:"success"`-}--// SetAPIKey sets API key for a LLM factory-func (s *LLMService) SetAPIKey(ctx context.Context, tenantID string, req *SetAPIKeyRequest) (*SetAPIKeyResult, error) {- factory := req.LLMFactory- baseURL := req.BaseURL- sourceFactory := req.SourceFID- if sourceFactory == "" {- sourceFactory = factory- }-- sourceLLMs, err := s.llmDAO.GetByFactory(ctx, dao.DB, sourceFactory)- if err != nil || len(sourceLLMs) == 0 {- msg := "No models configured for " + factory + " (source: " + sourceFactory + ")."- if req.Verify {- return &SetAPIKeyResult{Message: msg, Success: false}, nil- }- return nil, fmt.Errorf("%s", msg)- }-- llmConfig := map[string]interface{}{- "api_key": req.APIKey,- "api_base": baseURL,- }-- if req.ModelType != "" {- llmConfig["model_type"] = req.ModelType- }- if req.LLMName != "" {- llmConfig["llm_name"] = req.LLMName- }-- for _, llm := range sourceLLMs {- maxTokens := llm.MaxTokens- if maxTokens == 0 {- maxTokens = 8192- }- llmConfig["max_tokens"] = maxTokens-- existingLLM, _ := s.tenantLLMDAO.GetByTenantFactoryAndModelName(ctx, dao.DB, tenantID, factory, llm.LLMName)- if existingLLM != nil {- updates := map[string]interface{}{- "api_key": req.APIKey,- "api_base": baseURL,- "max_tokens": maxTokens,- }- dao.DB.WithContext(ctx).Model(&entity.TenantLLM{}).- Where("tenant_id = ? AND llm_factory = ? AND llm_name = ?", tenantID, factory, llm.LLMName).- Updates(updates)- } else {- modelType := llm.ModelType- llmName := llm.LLMName- tenantLLM := &entity.TenantLLM{- TenantID: tenantID,- LLMFactory: factory,- ModelType: &modelType,- LLMName: &llmName,- APIKey: &req.APIKey,- APIBase: &baseURL,- MaxTokens: maxTokens,- Status: "1",- }- s.tenantLLMDAO.Create(ctx, dao.DB, tenantLLM)- }- }-- return &SetAPIKeyResult{Message: "", Success: true}, nil-}diff --git a/internal/service/tenant.go b/internal/service/tenant.goindex a8d0ad8ac1..cdb247bfe6 100644--- a/internal/service/tenant.go+++ b/internal/service/tenant.go@@ -110,7 +110,6 @@ type TenantListItem struct {// TenantLLMService tenant LLM service// This service handles operations related to tenant-specific LLM configurationstype TenantLLMService struct {- tenantLLMDAO *dao.TenantLLMDAOmodelProviderDAO *dao.TenantModelProviderDAOmodelInstanceDAO *dao.TenantModelInstanceDAOmodelDAO *dao.TenantModelDAO@@ -119,70 +118,12 @@ type TenantLLMService struct {// NewTenantLLMService creates a new TenantLLMService instancefunc NewTenantLLMService() *TenantLLMService {return &TenantLLMService{- tenantLLMDAO: dao.NewTenantLLMDAO(),modelProviderDAO: dao.NewTenantModelProviderDAO(),modelInstanceDAO: dao.NewTenantModelInstanceDAO(),modelDAO: dao.NewTenantModelDAO(),}}-// GetAPIKey retrieves the tenant LLM record by tenant ID and model name-/**- * This method splits the model name into name and factory parts using the "@" separator,- * then queries the database for the matching tenant LLM configuration.- *- * Parameters:- * - tenantID: the unique identifier of the tenant- * - modelName: the model name, optionally including factory suffix (e.g., "gpt-4@OpenAI")- *- * Returns:- * - *model.TenantLLM: the tenant LLM record if found, nil otherwise- * - error: an error if the query fails, nil otherwise- *- * Example:- *- * service := NewTenantLLMService()- *- * // Get API key for model with factory- * tenantLLM, err := service.GetAPIKey("tenant-123", "gpt-4@OpenAI")- * if err != nil {- * log.Printf("Error: %v", err)- * }- *- * // Get API key for model without factory- * tenantLLM, err := service.GetAPIKey("tenant-123", "gpt-4")- */-func (s *TenantLLMService) GetAPIKey(ctx context.Context, tenantID, modelName string) (*entity.TenantLLM, error) {- modelName, factory := s.SplitModelNameAndFactory(modelName)-- var tenantLLM *entity.TenantLLM- var err error-- if factory == "" {- tenantLLM, err = s.tenantLLMDAO.GetByTenantIDAndLLMName(ctx, dao.DB, tenantID, modelName)- } else {- tenantLLM, err = s.tenantLLMDAO.GetByTenantIDLLMNameAndFactory(ctx, dao.DB, tenantID, modelName, factory)- }-- if err != nil {- return nil, err- }-- return tenantLLM, nil-}--// SplitModelNameAndFactory splits a model name into name and factory parts-func (s *TenantLLMService) SplitModelNameAndFactory(modelName string) (string, string) {- arr := strings.Split(modelName, "@")- if len(arr) < 2 {- return modelName, ""- }- if len(arr) > 2 {- return strings.Join(arr[0:len(arr)-1], "@"), arr[len(arr)-1]- }- return arr[0], arr[1]-}-// GetAPIKeyFromInstance returns the API key for the given composite model name// by looking it up in the tenant_model_instance table. compositeModelName is in// "model@instance@provider" or "model@provider" format.@@ -226,66 +167,6 @@ func (s *TenantLLMService) GetAPIKeyFromInstance(ctx context.Context, tenantID,return instance.APIKey, nil}-// EnsureTenantModelIDForParams ensures tenant model IDs are populated for LLM-related parameters-/**- * This method iterates through a predefined list of LLM-related parameter keys (llm_id, embd_id,- * asr_id, img2txt_id, rerank_id, tts_id) and automatically populates the corresponding tenant_*- * fields (tenant_llm_id, tenant_embd_id, etc.) with the tenant LLM record IDs.- *- * If a parameter key exists and its corresponding tenant_* key doesn't exist, this method will:- * 1. Query the tenant LLM record using GetAPIKey- * 2. If found, set the tenant_* key to the record's ID- * 3. If not found, set the tenant_* key to 0- *- * Parameters:- * - tenantID: the unique identifier of the tenant- * - params: a map of parameters to be updated (will be modified in place)- *- * Returns:- * - map[string]interface{}: the updated parameters map (same as input, modified in place)- *- * Example:- *- * service := NewTenantLLMService()- * params := map[string]interface{}{- * "llm_id": "gpt-4@OpenAI",- * "embd_id": "text-embedding-3-small@OpenAI",- * }- * result := service.EnsureTenantModelIDForParams("tenant-123", params)- * // result will contain:- * // {- * // "llm_id": "gpt-4@OpenAI",- * // "embd_id": "text-embedding-3-small@OpenAI",- * // "tenant_llm_id": 123, // ID from tenant_llm table- * // "tenant_embd_id": 456, // ID from tenant_llm table- * // }- */-func (s *TenantLLMService) EnsureTenantModelIDForParams(ctx context.Context, tenantID string, params map[string]interface{}) map[string]interface{} {- paramKeys := []string{"llm_id", "embd_id", "asr_id", "img2txt_id", "rerank_id", "tts_id"}-- for _, key := range paramKeys {- tenantKey := "tenant_" + key-- if value, exists := params[key]; exists && value != nil && value != "" {- if _, tenantExists := params[tenantKey]; !tenantExists {- modelName, ok := value.(string)- if !ok || modelName == "" {- continue- }-- tenantLLM, err := s.GetAPIKey(ctx, tenantID, modelName)- if err == nil && tenantLLM != nil {- params[tenantKey] = tenantLLM.ID- } else {- params[tenantKey] = int64(0)- }- }- }- }-- return params-}-// GetTenantList get tenant list for a userfunc (s *TenantService) GetTenantList(ctx context.Context, userID string) ([]*TenantListItem, error) {tenants, err := s.userTenantDAO.GetTenantsByUserID(ctx, dao.DB, userID)diff --git a/internal/service/user.go b/internal/service/user.goindex 646e51a943..21d08aff10 100644--- a/internal/service/user.go+++ b/internal/service/user.go@@ -29,7 +29,6 @@ import ("ragflow/internal/engine/kvrocks""ragflow/internal/entity""ragflow/internal/server"- "ragflow/internal/server/config""regexp""strconv""strings"@@ -228,11 +227,6 @@ func (s *UserService) Register(ctx context.Context, req *RegisterRequest) (*entiSize: 0,}- tenantLLMs, err := s.getInitTenantLLM(ctx, userID)- if err != nil {- return nil, common.CodeServerError, fmt.Errorf("failed to initialize tenant llm: %w", err)- }-db := dao.GetDB()if err = db.Transaction(func(tx *gorm.DB) error {if err = tx.Create(user).Error; err != nil {@@ -247,12 +241,6 @@ func (s *UserService) Register(ctx context.Context, req *RegisterRequest) (*entireturn fmt.Errorf("failed to create user tenant relation: %w", err)}- if len(tenantLLMs) > 0 {- if err = tx.Create(&tenantLLMs).Error; err != nil {- return fmt.Errorf("failed to create tenant llm: %w", err)- }- }-if err = tx.Create(rootFile).Error; err != nil {return fmt.Errorf("failed to create root folder: %w", err)}@@ -263,98 +251,6 @@ func (s *UserService) Register(ctx context.Context, req *RegisterRequest) (*entireturn user, common.CodeSuccess, nil}-// getInitTenantLLM builds the tenant_llm rows created for a new user's default tenant.-func (s *UserService) getInitTenantLLM(ctx context.Context, userID string) ([]*entity.TenantLLM, error) {- cfg := server.GetConfig()- if cfg == nil {- return nil, fmt.Errorf("config not initialized")- }-- modelConfigs := map[string]config.ModelConfig{- entity.ModelTypeChat.String(): cfg.GetDefaultChatModel(),- entity.ModelTypeEmbedding.String(): cfg.GetDefaultEmbeddingModel(),- entity.ModelTypeSpeech2Text.String(): cfg.GetDefaultASRModel(),- entity.ModelTypeImage2Text.String(): cfg.GetDefaultVisionModel(),- entity.ModelTypeRerank.String(): cfg.GetDefaultRerankModel(),- entity.ModelTypeTTS.String(): cfg.GetDefaultTTSModel(),- entity.ModelTypeOCR.String(): cfg.GetDefaultOCRModel(),- }-- seenFactories := make(map[string]bool)- factoryConfigs := make([]config.ModelConfig, 0, len(modelConfigs))- for _, modelConfig := range []config.ModelConfig{- cfg.GetDefaultChatModel(),- cfg.GetDefaultEmbeddingModel(),- cfg.GetDefaultASRModel(),- cfg.GetDefaultVisionModel(),- cfg.GetDefaultRerankModel(),- cfg.GetDefaultTTSModel(),- cfg.GetDefaultOCRModel(),- } {- if modelConfig.Factory == "" || seenFactories[modelConfig.Factory] {- continue- }- seenFactories[modelConfig.Factory] = true- factoryConfigs = append(factoryConfigs, modelConfig)- }-- llmDAO := dao.NewLLMDAO()- tenantLLMs := make([]*entity.TenantLLM, 0)- for _, factoryConfig := range factoryConfigs {- llms, err := llmDAO.GetByFactory(ctx, dao.DB, factoryConfig.Factory)- if err != nil {- return nil, fmt.Errorf("failed to get LLMs for factory %s: %w", factoryConfig.Factory, err)- }-- for _, llm := range llms {- apiKey := factoryConfig.APIKey- apiBase := factoryConfig.BaseURL- if modelConfig, ok := modelConfigs[llm.ModelType]; ok {- if modelConfig.APIKey != "" {- apiKey = modelConfig.APIKey- }- if modelConfig.BaseURL != "" {- apiBase = modelConfig.BaseURL- }- }-- maxTokens := int64(8192)- if llm.MaxTokens > 0 {- maxTokens = llm.MaxTokens- }-- llmName := llm.LLMName- modelType := llm.ModelType- tenantLLMs = append(tenantLLMs, &entity.TenantLLM{- TenantID: userID,- LLMFactory: factoryConfig.Factory,- LLMName: &llmName,- ModelType: &modelType,- APIKey: &apiKey,- APIBase: &apiBase,- MaxTokens: maxTokens,- Status: "1",- })- }- }-- seen := make(map[string]bool)- uniqueTenantLLMs := make([]*entity.TenantLLM, 0, len(tenantLLMs))- for _, tenantLLM := range tenantLLMs {- llmName := ""- if tenantLLM.LLMName != nil {- llmName = *tenantLLM.LLMName- }- key := strings.Join([]string{tenantLLM.TenantID, tenantLLM.LLMFactory, llmName}, "|")- if seen[key] {- continue- }- seen[key] = true- uniqueTenantLLMs = append(uniqueTenantLLMs, tenantLLM)- }- return uniqueTenantLLMs, nil-}-// Login user loginfunc (s *UserService) Login(ctx context.Context, req *LoginRequest) (*entity.User, common.ErrorCode, error) {// Get user by email (using username field as email)@@ -859,9 +755,6 @@ func (s *UserService) SetTenantInfo(ctx context.Context, userID string, req *SettenantID = *req.TenantID}- tenantLLMService := NewTenantLLMService()- updates = tenantLLMService.EnsureTenantModelIDForParams(ctx, tenantID, updates)-if len(updates) > 0 {if err := tenantDAO.Update(ctx, dao.DB, tenantID, updates); err != nil {return common.CodeExceptionError, errdiff --git a/test/playwright/conftest.py b/test/playwright/conftest.pyindex 31f52719cb..c2e3707127 100644--- a/test/playwright/conftest.py+++ b/test/playwright/conftest.py@@ -319,6 +319,15 @@ def _response_data(payload: dict | None) -> dict:return data if isinstance(data, dict) else {}+def _response_data_list(payload: dict | None) -> list:+ if not isinstance(payload, dict):+ return []+ if payload.get("code") not in (0, None):+ raise RuntimeError(f"API returned failure payload: {payload}")+ data = payload.get("data")+ return data if isinstance(data, list) else []++def _is_malformed_tenant_model_value(value: str | None) -> bool:text = str(value or "").strip()if not text:@@ -351,16 +360,20 @@ def _normalize_tenant_model_value(value: str | None) -> str:return text-def _provider_has_model(my_llms_data: dict, provider: str, model_name: str) -> bool:- if not isinstance(my_llms_data, dict):- return False- provider_data = my_llms_data.get(provider)- if not isinstance(provider_data, dict):- return False- llms = provider_data.get("llm")- if not isinstance(llms, list):+def _added_model_exists(added_models: list, provider: str, model_name: str) -> bool:+ """True when GET /api/v1/models lists `model_name` under `provider`.++ GET /api/v1/models returns the array form produced by+ ModelProviderService.ListTenantAddedModels: one row per+ (provider x instance x model) with `provider_name` and `name`.+ """+ if not isinstance(added_models, list):return False- for model in llms:+ for model in added_models:+ if not isinstance(model, dict):+ continue+ if str(model.get("provider_name") or "").strip() != provider:+ continueif str(model.get("name") or "").strip() == model_name:return Truereturn False@@ -631,7 +644,6 @@ def pytest_collection_modifyitems(session, config, items):"test/playwright/auth/test_sso_optional.py","test/playwright/auth/test_register_success_optional.py","test/playwright/auth/test_login_success_optional.py",- "test/playwright/e2e/test_model_providers_zhipu_ai_defaults.py","test/playwright/e2e/test_dataset_upload_parse.py","test/playwright/e2e/test_next_apps_chat.py","test/playwright/e2e/test_next_apps_search.py",@@ -988,28 +1000,45 @@ def ensure_auth_context(def _ensure_model_provider_ready_via_api(base_url: str, auth_header: str) -> dict:headers = {"Authorization": auth_header}- _, my_llms_payload = _api_request_json(_build_url(base_url, "/v1/llm/my_llms"), headers=headers)- my_llms_data = _response_data(my_llms_payload)- has_provider = bool(my_llms_data)+ _, providers_payload = _api_request_json(_build_url(base_url, "/api/v1/providers"), headers=headers)+ providers = _response_data_list(providers_payload)+ has_provider = any(isinstance(p, dict) and p.get("has_instance") for p in providers)created_provider = Falsezhipu_key = os.getenv("ZHIPU_AI_API_KEY")if not has_provider and zhipu_key:- _, set_key_payload = _api_request_json(- _build_url(base_url, "/v1/llm/set_api_key"),+ _, add_provider_payload = _api_request_json(+ _build_url(base_url, "/api/v1/providers"),+ method="PUT",+ payload={"provider_name": "ZHIPU-AI"},+ headers=headers,+ )+ _response_data(add_provider_payload)+ _, add_instance_payload = _api_request_json(+ _build_url(base_url, "/api/v1/providers/ZHIPU-AI/instances"),method="POST",- payload={"llm_factory": "ZHIPU-AI", "api_key": zhipu_key},+ payload={+ "instance_name": "e2e-default",+ "api_key": zhipu_key,+ "model_info": [+ {"model_name": "glm-4-flash", "model_type": ["chat"], "max_tokens": 8192},+ {"model_name": "embedding-2", "model_type": ["embedding"], "max_tokens": 8192},+ {"model_name": "glm-4.5v", "model_type": ["image2text"], "max_tokens": 8192},+ {"model_name": "glm-asr", "model_type": ["asr"], "max_tokens": 8192},+ ],+ },headers=headers,)- _response_data(set_key_payload)+ _response_data(add_instance_payload)has_provider = Truecreated_provider = True- _, my_llms_payload = _api_request_json(_build_url(base_url, "/v1/llm/my_llms"), headers=headers)- my_llms_data = _response_data(my_llms_payload)if not has_provider:pytest.skip("No model provider configured and ZHIPU_AI_API_KEY is not set.")+ _, models_payload = _api_request_json(_build_url(base_url, "/api/v1/models"), headers=headers)+ added_models = _response_data_list(models_payload)+_, tenant_payload = _api_request_json(_build_url(base_url, "/api/v1/users/me/models"), headers=headers)tenant_data = _response_data(tenant_payload)tenant_id = tenant_data.get("tenant_id")@@ -1026,7 +1055,7 @@ def _ensure_model_provider_ready_via_api(base_url: str, auth_header: str) -> dictarget_llm = current_llmif not target_llm or _is_malformed_tenant_model_value(target_llm):target_llm = _normalize_tenant_model_value(current_llm)- if not target_llm and _provider_has_model(my_llms_data, "ZHIPU-AI", "glm-4-flash"):+ if not target_llm and _added_model_exists(added_models, "ZHIPU-AI", "glm-4-flash"):target_llm = "glm-4-flash@ZHIPU-AI"if not target_llm:pytest.skip("Provider exists but no canonical default llm_id could be inferred for tenant setup.")@@ -1034,7 +1063,7 @@ def _ensure_model_provider_ready_via_api(base_url: str, auth_header: str) -> dictarget_embd = current_embdif not target_embd or _is_malformed_tenant_model_value(target_embd):target_embd = _normalize_tenant_model_value(current_embd)- if not target_embd and _provider_has_model(my_llms_data, "ZHIPU-AI", "embedding-2"):+ if not target_embd and _added_model_exists(added_models, "ZHIPU-AI", "embedding-2"):target_embd = "embedding-2@ZHIPU-AI"if not target_embd:target_embd = "BAAI/bge-small-en-v1.5@Builtin"@@ -1042,14 +1071,14 @@ def _ensure_model_provider_ready_via_api(base_url: str, auth_header: str) -> dictarget_img2txt = current_img2txtif _is_malformed_tenant_model_value(target_img2txt):target_img2txt = _normalize_tenant_model_value(current_img2txt)- if not target_img2txt and _provider_has_model(my_llms_data, "ZHIPU-AI", "glm-4.5v"):+ if not target_img2txt and _added_model_exists(added_models, "ZHIPU-AI", "glm-4.5v"):target_img2txt = "glm-4.5v@ZHIPU-AI"target_img2txt = target_img2txt or ""target_asr = current_asrif _is_malformed_tenant_model_value(target_asr):target_asr = _normalize_tenant_model_value(current_asr)- if not target_asr and _provider_has_model(my_llms_data, "ZHIPU-AI", "glm-asr"):+ if not target_asr and _added_model_exists(added_models, "ZHIPU-AI", "glm-asr"):target_asr = "glm-asr@ZHIPU-AI"target_asr = target_asr or ""@@ -1090,7 +1119,7 @@ def _ensure_model_provider_ready_via_api(base_url: str, auth_header: str) -> dic"has_provider": True,"created_provider": created_provider,"normalized_defaults": should_update_tenant_defaults,- "llm_factories": list(my_llms_data.keys()) if isinstance(my_llms_data, dict) else [],+ "llm_factories": sorted({str(m.get("provider_name")) for m in added_models if isinstance(m, dict) and m.get("provider_name")}),}diff --git a/test/playwright/e2e/test_model_providers_zhipu_ai_defaults.py b/test/playwright/e2e/test_model_providers_zhipu_ai_defaults.pydeleted file mode 100644index 09ec85b5fb..0000000000--- a/test/playwright/e2e/test_model_providers_zhipu_ai_defaults.py+++ /dev/null@@ -1,309 +0,0 @@-import re-import os-import pytest-from playwright.sync_api import expect--from test.playwright.helpers.flow_steps import flow_params, require-from test.playwright.helpers.auth_selectors import EMAIL_INPUT, PASSWORD_INPUT, SUBMIT_BUTTON-from test.playwright.helpers.auth_waits import wait_for_login_complete-from test.playwright.helpers.response_capture import capture_response-from test.playwright.helpers.model_providers import (- open_user_settings,- safe_close_modal,- select_default_model,-)--RESULT_TIMEOUT_MS = 15000---def step_01_open_login(- flow_page,- flow_state,- base_url,- login_url,- active_auth_context,- step,- snap,- auth_click,- seeded_user_credentials,-):- api_key = os.getenv("ZHIPU_AI_API_KEY")- if not api_key:- pytest.skip("ZHIPU_AI_API_KEY not set; skipping model providers test.")-- email, password = seeded_user_credentials-- flow_state["api_key"] = api_key- flow_state["email"] = email- flow_state["password"] = password-- with step("open login page"):- flow_page.goto(login_url, wait_until="domcontentloaded")- flow_state["login_opened"] = True- snap("login_opened")---def step_02_login(- flow_page,- flow_state,- base_url,- login_url,- active_auth_context,- step,- snap,- auth_click,- seeded_user_credentials,-):- require(flow_state, "login_opened", "email", "password")- page = flow_page- form, _ = active_auth_context()- email_input = form.locator(EMAIL_INPUT)- password_input = form.locator(PASSWORD_INPUT)- with step("fill credentials"):- expect(email_input).to_have_count(1)- expect(password_input).to_have_count(1)- email_input.fill(flow_state["email"])- password_input.fill(flow_state["password"])- password_input.blur()-- with step("submit login"):- submit_button = form.locator(SUBMIT_BUTTON)- expect(submit_button).to_have_count(1)- auth_click(submit_button, "submit_login")-- with step("wait for login"):- wait_for_login_complete(page, timeout_ms=RESULT_TIMEOUT_MS)-- flow_state["logged_in"] = True- snap("home_loaded")---def step_03_open_settings(- flow_page,- flow_state,- base_url,- login_url,- active_auth_context,- step,- snap,- auth_click,- seeded_user_credentials,-):- require(flow_state, "logged_in")- page = flow_page- with step("open settings"):- open_user_settings(page, base_url)- flow_state["settings_open"] = True- snap("settings_opened")---def step_04_open_model_providers(- flow_page,- flow_state,- base_url,- login_url,- active_auth_context,- step,- snap,- auth_click,- seeded_user_credentials,-):- require(flow_state, "settings_open")- page = flow_page- with step("open model providers"):- model_nav = page.locator("[data-testid='settings-nav-model-providers']")- expect(model_nav).to_have_count(1)- model_nav.first.click()- expect(page.locator("text=Set default models")).to_be_visible()- flow_state["model_providers_open"] = True- snap("model_providers_open")---def step_05_filter_zhipu(- flow_page,- flow_state,- base_url,- login_url,- active_auth_context,- step,- snap,- auth_click,- seeded_user_credentials,-):- require(flow_state, "model_providers_open")- page = flow_page- with step("filter providers"):- search_input = page.locator("[data-testid='model-providers-search']")- expect(search_input).to_have_count(1)- search_input.first.fill("zhipu")- available_section = page.locator("[data-testid='available-models-section']")- provider = available_section.locator("[data-testid='available-model-card'][data-provider='ZHIPU-AI']").first- if provider.count() == 0:- added_section = page.locator("[data-testid='added-models-section']")- if added_section.locator("[data-testid='added-model-card'][data-provider='ZHIPU-AI']").count() == 0:- raise AssertionError("ZHIPU-AI provider not found in available or added models.")- else:- expect(provider).to_be_visible()- flow_state["provider_filtered"] = True- snap("provider_filtered")---def step_06_add_api_key(- flow_page,- flow_state,- base_url,- login_url,- active_auth_context,- step,- snap,- auth_click,- seeded_user_credentials,-):- require(flow_state, "provider_filtered", "api_key")- page = flow_page- available_section = page.locator("[data-testid='available-models-section']")- provider = available_section.locator("[data-testid='available-model-card'][data-provider='ZHIPU-AI']").first-- with step("add ZHIPU-AI api key"):- if provider.count() > 0:- provider.click()- else:- added_section = page.locator("[data-testid='added-models-section']")- card = added_section.locator("[data-testid='added-model-card'][data-provider='ZHIPU-AI']").first- api_key_button = card.locator("button", has_text=re.compile("API-?Key", re.I)).first- expect(api_key_button).to_be_visible()- api_key_button.click()- modal = page.locator("[data-testid='apikey-modal']")- expect(modal).to_be_visible()- api_input = modal.locator("[data-testid='apikey-input']").first- save_button = modal.locator("[data-testid='apikey-save']").first- try:-- def trigger():- api_input.fill(flow_state["api_key"])- save_button.click()-- capture_response(- page,- trigger,- lambda resp: resp.request.method == "POST" and "/v1/llm/set_api_key" in resp.url,- )- expect(modal).not_to_be_visible(timeout=RESULT_TIMEOUT_MS)- except Exception:- safe_close_modal(modal)- raise-- with step("confirm added model"):- added_section = page.locator("[data-testid='added-models-section']")- expect(added_section).to_be_visible()- expect(added_section.locator("[data-testid='added-model-card'][data-provider='ZHIPU-AI']")).to_be_visible()- flow_state["provider_added"] = True- snap("provider_saved")---def step_07_set_defaults(- flow_page,- flow_state,- base_url,- login_url,- active_auth_context,- step,- snap,- auth_click,- seeded_user_credentials,-):- require(flow_state, "provider_added")- page = flow_page- with step("set default models"):- llm_combo = page.locator("[data-testid='default-llm-combobox']").first- emb_combo = page.locator("[data-testid='default-embedding-combobox']").first-- select_default_model(- page,- expect,- llm_combo,- "glm-4-flash@ZHIPU-AI",- "glm-4-flash",- list_testid="default-llm-options",- fallback_to_first=False,- timeout_ms=RESULT_TIMEOUT_MS,- )- selected_emb_text, _ = select_default_model(- page,- expect,- emb_combo,- "embedding-2@ZHIPU-AI",- "embedding-2",- list_testid="default-embedding-options",- fallback_to_first=True,- timeout_ms=RESULT_TIMEOUT_MS,- )- flow_state["selected_emb_text"] = selected_emb_text- flow_state["defaults_set"] = True- snap("defaults_selected")---def step_08_verify_persist(- flow_page,- flow_state,- base_url,- login_url,- active_auth_context,- step,- snap,- auth_click,- seeded_user_credentials,-):- require(flow_state, "defaults_set")- page = flow_page- with step("reload and verify defaults"):- page.reload(wait_until="domcontentloaded")- expect(page.locator("text=Set default models")).to_be_visible()- llm_combo = page.locator("[data-testid='default-llm-combobox']").first- emb_combo = page.locator("[data-testid='default-embedding-combobox']").first- expect(llm_combo).to_contain_text("glm-4-flash")- expect(emb_combo).to_contain_text(flow_state.get("selected_emb_text") or "embedding-2")- added_section = page.locator("[data-testid='added-models-section']")- expect(added_section.locator("[data-testid='added-model-card'][data-provider='ZHIPU-AI']")).to_be_visible()- snap("defaults_persisted")- snap("success")---STEPS = [- ("01_open_login", step_01_open_login),- ("02_login", step_02_login),- ("03_open_settings", step_03_open_settings),- ("04_open_model_providers", step_04_open_model_providers),- ("05_filter_zhipu", step_05_filter_zhipu),- ("06_add_api_key", step_06_add_api_key),- ("07_set_defaults", step_07_set_defaults),- ("08_verify_persist", step_08_verify_persist),-]--[email protected]("step_fn", flow_params(STEPS))-def test_add_zhipu_ai_set_defaults_persist_flow(- step_fn,- flow_page,- flow_state,- base_url,- login_url,- active_auth_context,- step,- snap,- auth_click,- seeded_user_credentials,-):- step_fn(- flow_page,- flow_state,- base_url,- login_url,- active_auth_context,- step,- snap,- auth_click,- seeded_user_credentials,- )