Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(agent): let the WeKnora assistant search without its own rerank model (#4315)

lyingbug1b241325669d
diff --git a/internal/application/service/agent_rerank_requirement_test.go b/internal/application/service/agent_rerank_requirement_test.go
index 06d096156..3ebea8ad6 100644
--- a/internal/application/service/agent_rerank_requirement_test.go
+++ b/internal/application/service/agent_rerank_requirement_test.go
@@ -1,11 +1,13 @@
package service
import (
+ "path/filepath"
"testing"
"github.com/Tencent/WeKnora/internal/agent/tools"
"github.com/Tencent/WeKnora/internal/types"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func TestAgentRequiresRerankModel(t *testing.T) {
@@ -66,3 +68,64 @@ func TestAgentRequiresRerankModel(t *testing.T) {
})
}
}
+
+// The assistant searches knowledge bases but cannot be given a rerank model,
+// so it must take the workspace's instead of failing every run.
+func TestWeKnoraAssistantTakesWorkspaceRerankModel(t *testing.T) {
+ require.NoError(t, types.LoadBuiltinAgentsConfig(filepath.Join("..", "..", "..", "config")))
+ agent := types.GetBuiltinAgent(types.BuiltinWeKnoraAssistantID, 1)
+ require.NotNil(t, agent)
+ assert.True(t, agentRequiresRerankModel(agent))
+ assert.Empty(t, agent.Config.RerankModelID)
+ assert.True(t, types.UsesWorkspaceModelFallback(agent))
+}
+
+func TestPickWorkspaceRerankModelID(t *testing.T) {
+ model := func(id string, typ types.ModelType, status types.ModelStatus, isDefault bool) *types.Model {
+ return &types.Model{ID: id, Type: typ, Status: status, IsDefault: isDefault}
+ }
+ active := types.ModelStatusActive
+ tests := []struct {
+ name string
+ models []*types.Model
+ want string
+ }{
+ {name: "no models", want: ""},
+ {
+ name: "default rerank model wins over an earlier one",
+ models: []*types.Model{
+ model("first", types.ModelTypeRerank, active, false),
+ model("default", types.ModelTypeRerank, active, true),
+ },
+ want: "default",
+ },
+ {
+ name: "first active rerank model without a default",
+ models: []*types.Model{
+ nil,
+ model("chat", types.ModelTypeKnowledgeQA, active, true),
+ model("first", types.ModelTypeRerank, active, false),
+ model("second", types.ModelTypeRerank, active, false),
+ },
+ want: "first",
+ },
+ {
+ name: "inactive rerank models are skipped, even the default",
+ models: []*types.Model{
+ model("broken-default", types.ModelTypeRerank, types.ModelStatusDownloadFailed, true),
+ model("usable", types.ModelTypeRerank, active, false),
+ },
+ want: "usable",
+ },
+ {
+ name: "no rerank model",
+ models: []*types.Model{model("chat", types.ModelTypeKnowledgeQA, active, true)},
+ want: "",
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ assert.Equal(t, tt.want, pickWorkspaceRerankModelID(tt.models))
+ })
+ }
+}
diff --git a/internal/application/service/agent_service_test.go b/internal/application/service/agent_service_test.go
index f04ed70d5..2a70b588f 100644
--- a/internal/application/service/agent_service_test.go
+++ b/internal/application/service/agent_service_test.go
@@ -516,7 +516,7 @@ func TestWeKnoraAssistantIsAHiddenBuiltinWithOperations(t *testing.T) {
require.NotNil(t, agent)
require.NotContains(t, types.GetBuiltinAgentIDs(), types.BuiltinWeKnoraAssistantID)
require.True(t, types.GrantsOperationCatalog(agent))
- require.True(t, types.UsesFallbackChatModel(agent))
+ require.True(t, types.UsesWorkspaceModelFallback(agent))
require.Equal(t, "weknora_assistant", agent.Config.SystemPromptID)
require.NotContains(t, agent.Config.AllowedTools, tools.ToolRunOperation,
"operations are granted by ID, never through the editable allowlist")
diff --git a/internal/application/service/agent_share.go b/internal/application/service/agent_share.go
index a7fd83039..e44612f3a 100644
--- a/internal/application/service/agent_share.go
+++ b/internal/application/service/agent_share.go
@@ -59,6 +59,25 @@ func agentRequiresRerankModel(agent *types.CustomAgent) bool {
return false
}
+// pickWorkspaceRerankModelID chooses the rerank model for an agent that
+// cannot be configured with one: the workspace's default active rerank
+// model, else its first active one, else none.
+func pickWorkspaceRerankModelID(models []*types.Model) string {
+ first := ""
+ for _, model := range models {
+ if model == nil || model.Type != types.ModelTypeRerank || model.Status != types.ModelStatusActive {
+ continue
+ }
+ if model.IsDefault {
+ return model.ID
+ }
+ if first == "" {
+ first = model.ID
+ }
+ }
+ return first
+}
+
// isBuiltinAgent guards sharing. Shares of built-in agents created before
// ErrBuiltinAgentNotShareable are hidden from receivers and never resolved,
// since the ID collides with the receiver's own built-in agent; the source
diff --git a/internal/application/service/session_agent_qa.go b/internal/application/service/session_agent_qa.go
index 738e56aa5..431337bac 100644
--- a/internal/application/service/session_agent_qa.go
+++ b/internal/application/service/session_agent_qa.go
@@ -129,15 +129,30 @@ func (s *sessionService) AgentQA(
// the agent. If a Wiki-only agent doesn't need reranking,
// agentRequiresRerankModel() below already lets it pass.
rerankModelID := req.CustomAgent.Config.RerankModelID
- if rerankModelID == "" {
+ if rerankModelID == "" && types.UsesWorkspaceModelFallback(req.CustomAgent) {
+ // Nobody can set a rerank model on an internal agent, so it takes
+ // the workspace's; search_knowledge keeps retrieval order without one.
+ models, listErr := s.modelService.ListModels(ctx)
+ if listErr != nil {
+ logger.Warnf(ctx, "Failed to list models for internal agent %s rerank fallback: %v",
+ req.CustomAgent.ID, listErr)
+ }
+ rerankModelID = pickWorkspaceRerankModelID(models)
+ if rerankModelID == "" {
+ logger.Infof(ctx, "No rerank model in the workspace, internal agent %s searches without reranking",
+ req.CustomAgent.ID)
+ }
+ } else if rerankModelID == "" {
logger.Warnf(ctx, "No rerank model configured for custom agent %s, but knowledge_search tool is enabled", req.CustomAgent.ID)
return errors.New("rerank model is not configured: please set rerank_model_id on the agent")
}
- rerankModel, err = s.modelService.GetRerankModel(ctx, rerankModelID)
- if err != nil {
- logger.Warnf(ctx, "Failed to get rerank model: %v", err)
- return fmt.Errorf("failed to get rerank model: %w", err)
+ if rerankModelID != "" {
+ rerankModel, err = s.modelService.GetRerankModel(ctx, rerankModelID)
+ if err != nil {
+ logger.Warnf(ctx, "Failed to get rerank model: %v", err)
+ return fmt.Errorf("failed to get rerank model: %w", err)
+ }
}
} else {
logger.Infof(ctx, "knowledge_search is unavailable for the effective agent scope, skipping rerank model initialization")
diff --git a/internal/application/service/session_qa_helpers.go b/internal/application/service/session_qa_helpers.go
index 7f9cadfae..61c435e05 100644
--- a/internal/application/service/session_qa_helpers.go
+++ b/internal/application/service/session_qa_helpers.go
@@ -154,7 +154,7 @@ func (s *sessionService) resolveChatModelID(
if customAgent != nil {
configuredAgentModelID = strings.TrimSpace(customAgent.Config.ModelID)
- if configuredAgentModelID == "" && !types.UsesFallbackChatModel(customAgent) {
+ if configuredAgentModelID == "" && !types.UsesWorkspaceModelFallback(customAgent) {
return "", fmt.Errorf("chat model is not configured: please set model_id on agent %s", customAgent.ID)
}
if configuredAgentModelID != "" {
diff --git a/internal/types/custom_agent.go b/internal/types/custom_agent.go
index ac5f4cb76..8725b647a 100644
--- a/internal/types/custom_agent.go
+++ b/internal/types/custom_agent.go
@@ -49,11 +49,12 @@ func GrantsProductDocs(agent *CustomAgent) bool {
return agent != nil && agent.IsBuiltin && agent.ID == BuiltinWeKnoraAssistantID
}
-// UsesFallbackChatModel reports whether a built-in agent runs without its
-// own model_id. These agents are hidden from agent management, so a user
-// cannot configure one; the run resolves the model from its knowledge base
-// or the system KnowledgeQA default.
-func UsesFallbackChatModel(agent *CustomAgent) bool {
+// UsesWorkspaceModelFallback reports whether a built-in agent runs without
+// its own model_id and rerank_model_id. These agents are hidden from agent
+// management, so a user cannot configure them; the run resolves the chat
+// model from its knowledge base or the system KnowledgeQA default, and the
+// rerank model from the workspace.
+func UsesWorkspaceModelFallback(agent *CustomAgent) bool {
return agent != nil && agent.IsBuiltin &&
(agent.ID == BuiltinWikiFixerID || agent.ID == BuiltinWeKnoraAssistantID)
}