feat: 重构提示词模板系统,新增多场景模板配置支持;优化智能体知识库选择模式,支持全部/指定/禁用三种模式

This commit is contained in:
wizardchen
2025-12-25 18:22:36 +08:00
parent dc1ab5b81d
commit abfe87eaed
32 changed files with 2755 additions and 1102 deletions
+106 -13
View File
@@ -56,34 +56,83 @@ type CustomAgent struct {
// CustomAgentConfig represents the configuration of a custom agent
type CustomAgentConfig struct {
// ===== Basic Settings =====
// Agent mode: "normal" for RAG mode, "agent" for ReAct agent mode
AgentMode string `yaml:"agent_mode" json:"agent_mode"`
// System prompt for the agent
// System prompt for the agent (for normal mode, or agent mode when web search is disabled)
SystemPrompt string `yaml:"system_prompt" json:"system_prompt"`
// System prompt for agent mode when web search is enabled
SystemPromptWebEnabled string `yaml:"system_prompt_web_enabled" json:"system_prompt_web_enabled"`
// Context template for normal mode (how to format retrieved chunks)
ContextTemplate string `yaml:"context_template" json:"context_template"`
// ===== Model Settings =====
// Model ID to use for conversations
ModelID string `yaml:"model_id" json:"model_id"`
// ReRank model ID for retrieval
RerankModelID string `yaml:"rerank_model_id" json:"rerank_model_id"`
// Temperature for LLM (0-1)
Temperature float64 `yaml:"temperature" json:"temperature"`
// Maximum completion tokens (only for normal mode)
MaxCompletionTokens int `yaml:"max_completion_tokens" json:"max_completion_tokens"`
// ===== Agent Mode Settings =====
// Maximum iterations for ReAct loop (only for agent type)
MaxIterations int `yaml:"max_iterations" json:"max_iterations"`
// Allowed tools (only for agent type)
AllowedTools []string `yaml:"allowed_tools" json:"allowed_tools"`
// Associated knowledge base IDs
// Whether reflection is enabled (only for agent type)
ReflectionEnabled bool `yaml:"reflection_enabled" json:"reflection_enabled"`
// ===== Knowledge Base Settings =====
// Knowledge base selection mode: "all" = all KBs, "selected" = specific KBs, "none" = no KB
KBSelectionMode string `yaml:"kb_selection_mode" json:"kb_selection_mode"`
// Associated knowledge base IDs (only used when KBSelectionMode is "selected")
KnowledgeBases []string `yaml:"knowledge_bases" json:"knowledge_bases"`
// Whether to allow user to select knowledge bases when no KnowledgeBases are configured
// If true (default), user can freely select any knowledge base
// If false, knowledge base selection is disabled for this agent
// Whether to allow user to select knowledge bases during conversation
// If true, user can select from available KBs (all or selected based on KBSelectionMode)
// If false, the configured KBs are used automatically without user selection
AllowUserKBSelection *bool `yaml:"allow_user_kb_selection" json:"allow_user_kb_selection"`
// ===== Web Search Settings =====
// Whether web search is enabled
WebSearchEnabled bool `yaml:"web_search_enabled" json:"web_search_enabled"`
// Maximum web search results
WebSearchMaxResults int `yaml:"web_search_max_results" json:"web_search_max_results"`
// Whether reflection is enabled (only for agent type)
ReflectionEnabled bool `yaml:"reflection_enabled" json:"reflection_enabled"`
// Welcome message displayed when agent is selected
// ===== Multi-turn Conversation Settings =====
// Whether multi-turn conversation is enabled
MultiTurnEnabled bool `yaml:"multi_turn_enabled" json:"multi_turn_enabled"`
// Number of history turns to keep in context
HistoryTurns int `yaml:"history_turns" json:"history_turns"`
// ===== Retrieval Strategy Settings (for both modes) =====
// Embedding/Vector retrieval top K
EmbeddingTopK int `yaml:"embedding_top_k" json:"embedding_top_k"`
// Keyword retrieval threshold
KeywordThreshold float64 `yaml:"keyword_threshold" json:"keyword_threshold"`
// Vector retrieval threshold
VectorThreshold float64 `yaml:"vector_threshold" json:"vector_threshold"`
// Rerank top K
RerankTopK int `yaml:"rerank_top_k" json:"rerank_top_k"`
// Rerank threshold
RerankThreshold float64 `yaml:"rerank_threshold" json:"rerank_threshold"`
// ===== Advanced Settings (mainly for normal mode) =====
// Whether to enable query expansion
EnableQueryExpansion bool `yaml:"enable_query_expansion" json:"enable_query_expansion"`
// Whether to enable query rewrite for multi-turn conversations
EnableRewrite bool `yaml:"enable_rewrite" json:"enable_rewrite"`
// Rewrite prompt system message
RewritePromptSystem string `yaml:"rewrite_prompt_system" json:"rewrite_prompt_system"`
// Rewrite prompt user message template
RewritePromptUser string `yaml:"rewrite_prompt_user" json:"rewrite_prompt_user"`
// Fallback strategy: "fixed" for fixed response, "model" for model generation
FallbackStrategy string `yaml:"fallback_strategy" json:"fallback_strategy"`
// Fixed fallback response (when FallbackStrategy is "fixed")
FallbackResponse string `yaml:"fallback_response" json:"fallback_response"`
// Fallback prompt (when FallbackStrategy is "model")
FallbackPrompt string `yaml:"fallback_prompt" json:"fallback_prompt"`
}
// Value implements driver.Valuer interface for CustomAgentConfig
@@ -128,6 +177,29 @@ func (a *CustomAgent) EnsureDefaults() {
if a.Config.HistoryTurns == 0 {
a.Config.HistoryTurns = 5
}
// Retrieval strategy defaults
if a.Config.EmbeddingTopK == 0 {
a.Config.EmbeddingTopK = 10
}
if a.Config.KeywordThreshold == 0 {
a.Config.KeywordThreshold = 0.3
}
if a.Config.VectorThreshold == 0 {
a.Config.VectorThreshold = 0.5
}
if a.Config.RerankTopK == 0 {
a.Config.RerankTopK = 5
}
if a.Config.RerankThreshold == 0 {
a.Config.RerankThreshold = 0.5
}
// Advanced settings defaults
if a.Config.FallbackStrategy == "" {
a.Config.FallbackStrategy = "model"
}
if a.Config.MaxCompletionTokens == 0 {
a.Config.MaxCompletionTokens = 2048
}
}
// IsAgentMode returns true if this agent uses ReAct agent mode
@@ -146,10 +218,23 @@ func GetBuiltinNormalAgent(tenantID uint64) *CustomAgent {
Type: CustomAgentTypeNormal,
TenantID: tenantID,
Config: CustomAgentConfig{
AgentMode: "normal",
SystemPrompt: "",
Temperature: 0.7,
WebSearchEnabled: false,
AgentMode: "normal",
SystemPrompt: "",
Temperature: 0.7,
MaxCompletionTokens: 2048,
WebSearchEnabled: false,
MultiTurnEnabled: true,
HistoryTurns: 5,
// Retrieval strategy
EmbeddingTopK: 10,
KeywordThreshold: 0.3,
VectorThreshold: 0.5,
RerankTopK: 5,
RerankThreshold: 0.5,
// Advanced settings
EnableQueryExpansion: true,
EnableRewrite: true,
FallbackStrategy: "model",
},
}
}
@@ -169,10 +254,18 @@ func GetBuiltinAgentAgent(tenantID uint64) *CustomAgent {
SystemPrompt: "",
Temperature: 0.7,
MaxIterations: 10,
AllowedTools: []string{"knowledge_search", "web_search"},
AllowedTools: []string{"thinking", "todo_write", "knowledge_search", "grep_chunks", "list_knowledge_chunks", "query_knowledge_graph", "get_document_info"},
WebSearchEnabled: true,
WebSearchMaxResults: 5,
ReflectionEnabled: false,
MultiTurnEnabled: true,
HistoryTurns: 5,
// Retrieval strategy
EmbeddingTopK: 10,
KeywordThreshold: 0.3,
VectorThreshold: 0.5,
RerankTopK: 5,
RerankThreshold: 0.5,
},
}
}
+3 -2
View File
@@ -65,14 +65,15 @@ type CustomAgentRepository interface {
// - Possible errors such as database connection failure, unique constraint conflicts, etc.
CreateAgent(ctx context.Context, agent *types.CustomAgent) error
// GetAgentByID queries an agent by ID
// GetAgentByID queries an agent by ID and tenant
// Parameters:
// - ctx: Context information
// - id: Agent ID
// - tenantID: Tenant ID for isolation
// Returns:
// - Agent object, if found
// - Possible errors such as record not existing, database errors, etc.
GetAgentByID(ctx context.Context, id string) (*types.CustomAgent, error)
GetAgentByID(ctx context.Context, id string, tenantID uint64) (*types.CustomAgent, error)
// ListAgentsByTenantID lists all agents for a specific tenant
// Parameters:
+16 -16
View File
@@ -80,22 +80,22 @@ type Session struct {
// Tenant ID
TenantID uint64 `json:"tenant_id" gorm:"index"`
// Strategy configuration
KnowledgeBaseID string `json:"knowledge_base_id"` // 关联的知识库ID
MaxRounds int `json:"max_rounds"` // 多轮保持轮数
EnableRewrite bool `json:"enable_rewrite"` // 多轮改写开关
FallbackStrategy FallbackStrategy `json:"fallback_strategy"` // 兜底策略
FallbackResponse string `json:"fallback_response"` // 固定回复内容
EmbeddingTopK int `json:"embedding_top_k"` // 向量召回TopK
KeywordThreshold float64 `json:"keyword_threshold"` // 关键词召回阈值
VectorThreshold float64 `json:"vector_threshold"` // 向量召回阈值
RerankModelID string `json:"rerank_model_id"` // 排序模型ID
RerankTopK int `json:"rerank_top_k"` // 排序TopK
RerankThreshold float64 `json:"rerank_threshold"` // 排序阈值
SummaryModelID string `json:"summary_model_id"` // 总结模型ID
SummaryParameters *SummaryConfig `json:"summary_parameters" gorm:"type:json"` // 总结模型参数
AgentConfig *SessionAgentConfig `json:"agent_config" gorm:"type:jsonb"` // Agent 配置(会话级别,仅存储enabled和knowledge_bases)
ContextConfig *ContextConfig `json:"context_config" gorm:"type:jsonb"` // 上下文管理配置(可选)
// // Strategy configuration
// KnowledgeBaseID string `json:"knowledge_base_id"` // 关联的知识库ID
// MaxRounds int `json:"max_rounds"` // 多轮保持轮数
// EnableRewrite bool `json:"enable_rewrite"` // 多轮改写开关
// FallbackStrategy FallbackStrategy `json:"fallback_strategy"` // 兜底策略
// FallbackResponse string `json:"fallback_response"` // 固定回复内容
// EmbeddingTopK int `json:"embedding_top_k"` // 向量召回TopK
// KeywordThreshold float64 `json:"keyword_threshold"` // 关键词召回阈值
// VectorThreshold float64 `json:"vector_threshold"` // 向量召回阈值
// RerankModelID string `json:"rerank_model_id"` // 排序模型ID
// RerankTopK int `json:"rerank_top_k"` // 排序TopK
// RerankThreshold float64 `json:"rerank_threshold"` // 排序阈值
// SummaryModelID string `json:"summary_model_id"` // 总结模型ID
// SummaryParameters *SummaryConfig `json:"summary_parameters" gorm:"type:json"` // 总结模型参数
// AgentConfig *SessionAgentConfig `json:"agent_config" gorm:"type:jsonb"` // Agent 配置(会话级别,仅存储enabled和knowledge_bases)
// ContextConfig *ContextConfig `json:"context_config" gorm:"type:jsonb"` // 上下文管理配置(可选)
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
+1 -3
View File
@@ -126,9 +126,7 @@ func (c *RetrieverEngines) Scan(value interface{}) error {
// ConversationConfig represents the conversation configuration for normal mode
type ConversationConfig struct {
// Prompt is the system prompt for normal mode
UseCustomSystemPrompt bool `json:"use_custom_system_prompt"`
Prompt string `json:"prompt"`
UseCustomContextTemplate bool `json:"use_custom_context_template"`
Prompt string `json:"prompt"`
// ContextTemplate is the prompt template for summarizing retrieval results
ContextTemplate string `json:"context_template"`
// Temperature controls the randomness of the model output