feat: 统一系统提示词并优化智能体选择器界面

- 将系统提示词从分离的网络搜索开/关状态合并为统一提示词,使用 {{web_search_status}} 占位符动态控制行为
- 重构智能体选择器,从简单tooltip升级为内容丰富的popup,显示智能体详细信息、类型和能力
- 新增FAQ优先策略配置,支持FAQ答案优先于普通文档
- 优化知识库选择逻辑,智能体切换时自动同步配置的知识库
- 更新相关接口类型定义和国际化文本
This commit is contained in:
wizardchen
2025-12-28 21:46:42 +08:00
parent c9e482eb79
commit 6bc19ab082
21 changed files with 1198 additions and 609 deletions
+23 -14
View File
@@ -10,20 +10,22 @@ import (
// AgentConfig represents the full agent configuration (used at tenant level and runtime)
// This includes all configuration parameters for agent execution
type AgentConfig struct {
MaxIterations int `json:"max_iterations"` // Maximum number of ReAct iterations
ReflectionEnabled bool `json:"reflection_enabled"` // Whether to enable reflection
AllowedTools []string `json:"allowed_tools"` // List of allowed tool names
Temperature float64 `json:"temperature"` // LLM temperature for agent
KnowledgeBases []string `json:"knowledge_bases"` // Accessible knowledge base IDs
KnowledgeIDs []string `json:"knowledge_ids"` // Accessible knowledge IDs (individual documents)
SystemPromptWebEnabled string `json:"system_prompt_web_enabled,omitempty"` // Custom prompt when web search is enabled
SystemPromptWebDisabled string `json:"system_prompt_web_disabled,omitempty"` // Custom prompt when web search is disabled
UseCustomSystemPrompt bool `json:"use_custom_system_prompt"` // Whether to use custom system prompt instead of default
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search tool is enabled
WebSearchMaxResults int `json:"web_search_max_results"` // Maximum number of web search results (default: 5)
MultiTurnEnabled bool `json:"multi_turn_enabled"` // Whether multi-turn conversation is enabled
HistoryTurns int `json:"history_turns"` // Number of history turns to keep in context
SearchTargets SearchTargets `json:"-"` // Pre-computed unified search targets (runtime only)
MaxIterations int `json:"max_iterations"` // Maximum number of ReAct iterations
ReflectionEnabled bool `json:"reflection_enabled"` // Whether to enable reflection
AllowedTools []string `json:"allowed_tools"` // List of allowed tool names
Temperature float64 `json:"temperature"` // LLM temperature for agent
KnowledgeBases []string `json:"knowledge_bases"` // Accessible knowledge base IDs
KnowledgeIDs []string `json:"knowledge_ids"` // Accessible knowledge IDs (individual documents)
SystemPrompt string `json:"system_prompt,omitempty"` // Unified system prompt (uses {{web_search_status}} placeholder for dynamic behavior)
// Deprecated: Use SystemPrompt instead. Kept for backward compatibility during migration.
SystemPromptWebEnabled string `json:"system_prompt_web_enabled,omitempty"` // Deprecated: Custom prompt when web search is enabled
SystemPromptWebDisabled string `json:"system_prompt_web_disabled,omitempty"` // Deprecated: Custom prompt when web search is disabled
UseCustomSystemPrompt bool `json:"use_custom_system_prompt"` // Whether to use custom system prompt instead of default
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search tool is enabled
WebSearchMaxResults int `json:"web_search_max_results"` // Maximum number of web search results (default: 5)
MultiTurnEnabled bool `json:"multi_turn_enabled"` // Whether multi-turn conversation is enabled
HistoryTurns int `json:"history_turns"` // Number of history turns to keep in context
SearchTargets SearchTargets `json:"-"` // Pre-computed unified search targets (runtime only)
// MCP service selection
MCPSelectionMode string `json:"mcp_selection_mode"` // MCP selection mode: "all", "selected", "none"
MCPServices []string `json:"mcp_services"` // Selected MCP service IDs (when mode is "selected")
@@ -73,11 +75,18 @@ func (c *SessionAgentConfig) Scan(value interface{}) error {
}
// ResolveSystemPrompt returns the prompt template for the given web search state.
// It uses the unified SystemPrompt field, falling back to deprecated fields for backward compatibility.
func (c *AgentConfig) ResolveSystemPrompt(webSearchEnabled bool) string {
if c == nil {
return ""
}
// First, try the new unified SystemPrompt field
if c.SystemPrompt != "" {
return c.SystemPrompt
}
// Fallback to deprecated fields for backward compatibility
if webSearchEnabled {
if c.SystemPromptWebEnabled != "" {
return c.SystemPromptWebEnabled
+9
View File
@@ -53,6 +53,11 @@ type ChatManage struct {
// Web search configuration (internal use)
TenantID uint64 `json:"-"` // Tenant ID for retrieving web search config
WebSearchEnabled bool `json:"-"` // Whether web search is enabled for this request
// FAQ Strategy Settings
FAQPriorityEnabled bool `json:"-"` // Whether FAQ priority strategy is enabled
FAQDirectAnswerThreshold float64 `json:"-"` // Threshold for direct FAQ answer (similarity > this value)
FAQScoreBoost float64 `json:"-"` // Score multiplier for FAQ results
}
// Clone creates a deep copy of the ChatManage object
@@ -117,6 +122,10 @@ func (c *ChatManage) Clone() *ChatManage {
EnableRewrite: c.EnableRewrite,
EnableQueryExpansion: c.EnableQueryExpansion,
TenantID: c.TenantID,
// FAQ Strategy Settings
FAQPriorityEnabled: c.FAQPriorityEnabled,
FAQDirectAnswerThreshold: c.FAQDirectAnswerThreshold,
FAQScoreBoost: c.FAQScoreBoost,
}
}
+288 -5
View File
@@ -14,6 +14,14 @@ const (
BuiltinQuickAnswerID = "builtin-quick-answer"
// BuiltinSmartReasoningID is the ID for the built-in smart reasoning (ReAct) agent
BuiltinSmartReasoningID = "builtin-smart-reasoning"
// BuiltinDeepResearcherID is the ID for the built-in deep researcher agent
BuiltinDeepResearcherID = "builtin-deep-researcher"
// BuiltinDataAnalystID is the ID for the built-in data analyst agent
BuiltinDataAnalystID = "builtin-data-analyst"
// BuiltinKnowledgeGraphExpertID is the ID for the built-in knowledge graph expert agent
BuiltinKnowledgeGraphExpertID = "builtin-knowledge-graph-expert"
// BuiltinDocumentAssistantID is the ID for the built-in document assistant agent
BuiltinDocumentAssistantID = "builtin-document-assistant"
)
// AgentMode constants for agent running mode
@@ -57,10 +65,8 @@ type CustomAgentConfig struct {
// ===== Basic Settings =====
// Agent mode: "quick-answer" for RAG mode, "smart-reasoning" for ReAct agent mode
AgentMode string `yaml:"agent_mode" json:"agent_mode"`
// System prompt for the agent (for normal mode, or agent mode when web search is disabled)
// System prompt for the agent (unified prompt, uses {{web_search_status}} placeholder for dynamic behavior)
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"`
@@ -92,6 +98,14 @@ type CustomAgentConfig struct {
// Associated knowledge base IDs (only used when KBSelectionMode is "selected")
KnowledgeBases []string `yaml:"knowledge_bases" json:"knowledge_bases"`
// ===== FAQ Strategy Settings =====
// Whether FAQ priority strategy is enabled (FAQ answers prioritized over document chunks)
FAQPriorityEnabled bool `yaml:"faq_priority_enabled" json:"faq_priority_enabled"`
// FAQ direct answer threshold - if similarity > this value, use FAQ answer directly
FAQDirectAnswerThreshold float64 `yaml:"faq_direct_answer_threshold" json:"faq_direct_answer_threshold"`
// FAQ score boost multiplier - FAQ results score multiplied by this factor
FAQScoreBoost float64 `yaml:"faq_score_boost" json:"faq_score_boost"`
// ===== Web Search Settings =====
// Whether web search is enabled
WebSearchEnabled bool `yaml:"web_search_enabled" json:"web_search_enabled"`
@@ -224,6 +238,10 @@ func GetBuiltinQuickAnswerAgent(tenantID uint64) *CustomAgent {
MultiTurnEnabled: true,
HistoryTurns: 5,
KBSelectionMode: "all",
// FAQ strategy
FAQPriorityEnabled: true,
FAQDirectAnswerThreshold: 0.9,
FAQScoreBoost: 1.2,
// Retrieval strategy
EmbeddingTopK: 10,
KeywordThreshold: 0.3,
@@ -259,6 +277,10 @@ func GetBuiltinSmartReasoningAgent(tenantID uint64) *CustomAgent {
ReflectionEnabled: false,
MultiTurnEnabled: true,
HistoryTurns: 5,
// FAQ strategy
FAQPriorityEnabled: true,
FAQDirectAnswerThreshold: 0.9,
FAQScoreBoost: 1.2,
// Retrieval strategy
EmbeddingTopK: 10,
KeywordThreshold: 0.3,
@@ -269,6 +291,259 @@ func GetBuiltinSmartReasoningAgent(tenantID uint64) *CustomAgent {
}
}
// GetBuiltinDeepResearcherAgent returns the built-in deep researcher agent
// This agent is optimized for in-depth research and comprehensive analysis
func GetBuiltinDeepResearcherAgent(tenantID uint64) *CustomAgent {
return &CustomAgent{
ID: BuiltinDeepResearcherID,
Name: "深度研究员",
Description: "专注于深度研究和综合分析,能够制定研究计划、多维度检索信息、深入思考并给出全面的分析报告",
Avatar: "🔬",
IsBuiltin: true,
TenantID: tenantID,
Config: CustomAgentConfig{
AgentMode: AgentModeSmartReasoning,
SystemPrompt: `你是一位专业的深度研究员,擅长进行系统性的研究和综合分析。
## 核心能力
- 制定结构化的研究计划
- 多维度信息检索和交叉验证
- 深入思考和逻辑推理
- 综合分析和报告撰写
## 工作流程
1. **理解问题**:深入分析用户的研究需求,明确研究目标和范围
2. **制定计划**:使用 todo_write 工具制定详细的研究计划
3. **信息收集**:
- 使用 knowledge_search 进行语义搜索获取相关文档
- 使用 grep_chunks 进行关键词精确搜索
- 使用 query_knowledge_graph 探索实体关系
- 必要时使用网络搜索获取最新信息
4. **深度分析**:使用 thinking 工具进行深入思考和推理
5. **综合报告**:整合所有信息,给出结构化的研究报告
## 输出要求
- 研究报告应包含:背景、方法、发现、分析、结论
- 引用具体的信息来源
- 指出信息的可靠性和局限性
- 提供进一步研究的建议`,
Temperature: 0.5,
MaxCompletionTokens: 4096,
MaxIterations: 30,
KBSelectionMode: "all",
AllowedTools: []string{"thinking", "todo_write", "knowledge_search", "grep_chunks", "list_knowledge_chunks", "query_knowledge_graph", "get_document_info"},
WebSearchEnabled: true,
WebSearchMaxResults: 10,
ReflectionEnabled: true,
MultiTurnEnabled: true,
HistoryTurns: 10,
// FAQ strategy
FAQPriorityEnabled: true,
FAQDirectAnswerThreshold: 0.9,
FAQScoreBoost: 1.2,
// Retrieval strategy - more comprehensive
EmbeddingTopK: 20,
KeywordThreshold: 0.2,
VectorThreshold: 0.4,
RerankTopK: 15,
RerankThreshold: 0.25,
},
}
}
// GetBuiltinDataAnalystAgent returns the built-in data analyst agent
// This agent is optimized for database queries and data analysis
func GetBuiltinDataAnalystAgent(tenantID uint64) *CustomAgent {
return &CustomAgent{
ID: BuiltinDataAnalystID,
Name: "数据分析师",
Description: "专注于数据库查询和数据分析,能够理解业务需求、构建SQL查询、分析数据并提供洞察",
Avatar: "📊",
IsBuiltin: true,
TenantID: tenantID,
Config: CustomAgentConfig{
AgentMode: AgentModeSmartReasoning,
SystemPrompt: `你是一位专业的数据分析师,擅长数据库查询和数据分析。
## 核心能力
- 理解业务需求并转化为数据查询
- 构建高效的 SQL 查询语句
- 数据分析和可视化建议
- 提供数据驱动的业务洞察
## 工作流程
1. **需求理解**:
- 明确用户的数据分析目标
- 确定需要查询的数据范围和维度
2. **数据探索**:
- 使用 database_query 查询相关数据
- 使用 get_document_info 了解数据结构和元数据
3. **数据分析**:
- 使用 thinking 工具进行数据分析和推理
- 识别数据模式和趋势
4. **结果呈现**:
- 清晰展示查询结果
- 提供数据解读和业务建议
## 输出要求
- 解释 SQL 查询的逻辑
- 以表格或结构化方式展示数据
- 提供数据的业务含义解读
- 指出数据的局限性和注意事项
- 建议后续分析方向`,
Temperature: 0.3,
MaxCompletionTokens: 2048,
MaxIterations: 20,
KBSelectionMode: "all",
AllowedTools: []string{"thinking", "todo_write", "database_query", "knowledge_search", "get_document_info"},
WebSearchEnabled: false,
WebSearchMaxResults: 5,
ReflectionEnabled: false,
MultiTurnEnabled: true,
HistoryTurns: 5,
// FAQ strategy
FAQPriorityEnabled: true,
FAQDirectAnswerThreshold: 0.9,
FAQScoreBoost: 1.2,
// Retrieval strategy
EmbeddingTopK: 10,
KeywordThreshold: 0.3,
VectorThreshold: 0.5,
RerankTopK: 5,
RerankThreshold: 0.3,
},
}
}
// GetBuiltinKnowledgeGraphExpertAgent returns the built-in knowledge graph expert agent
// This agent is optimized for knowledge graph exploration and relationship analysis
func GetBuiltinKnowledgeGraphExpertAgent(tenantID uint64) *CustomAgent {
return &CustomAgent{
ID: BuiltinKnowledgeGraphExpertID,
Name: "知识图谱专家",
Description: "专注于知识图谱查询和关系分析,能够探索实体关系、发现隐藏联系并构建知识网络",
Avatar: "🕸️",
IsBuiltin: true,
TenantID: tenantID,
Config: CustomAgentConfig{
AgentMode: AgentModeSmartReasoning,
SystemPrompt: `你是一位知识图谱专家,擅长探索实体关系和构建知识网络。
## 核心能力
- 实体识别和关系发现
- 知识图谱查询和遍历
- 关系链分析和推理
- 知识网络可视化建议
## 工作流程
1. **实体识别**:
- 从用户问题中识别关键实体
- 确定需要探索的关系类型
2. **图谱查询**:
- 使用 query_knowledge_graph 查询实体关系
- 探索多跳关系和间接联系
3. **关系分析**:
- 使用 thinking 工具分析关系模式
- 发现隐藏的关联和规律
4. **知识整合**:
- 结合 knowledge_search 获取更多上下文
- 构建完整的知识图景
## 输出要求
- 清晰展示实体和关系
- 使用图形化描述(如 A -> 关系 -> B)
- 解释关系的含义和重要性
- 指出可能的推理和假设
- 建议进一步探索的方向`,
Temperature: 0.5,
MaxCompletionTokens: 2048,
MaxIterations: 25,
KBSelectionMode: "all",
AllowedTools: []string{"thinking", "todo_write", "query_knowledge_graph", "knowledge_search", "grep_chunks", "get_document_info"},
WebSearchEnabled: false,
WebSearchMaxResults: 5,
ReflectionEnabled: true,
MultiTurnEnabled: true,
HistoryTurns: 5,
// FAQ strategy
FAQPriorityEnabled: true,
FAQDirectAnswerThreshold: 0.9,
FAQScoreBoost: 1.2,
// Retrieval strategy
EmbeddingTopK: 15,
KeywordThreshold: 0.3,
VectorThreshold: 0.4,
RerankTopK: 10,
RerankThreshold: 0.3,
},
}
}
// GetBuiltinDocumentAssistantAgent returns the built-in document assistant agent
// This agent is optimized for document retrieval, organization and summarization
func GetBuiltinDocumentAssistantAgent(tenantID uint64) *CustomAgent {
return &CustomAgent{
ID: BuiltinDocumentAssistantID,
Name: "文档助手",
Description: "专注于文档检索和内容整理,能够快速定位文档、提取关键信息并生成摘要",
Avatar: "📚",
IsBuiltin: true,
TenantID: tenantID,
Config: CustomAgentConfig{
AgentMode: AgentModeSmartReasoning,
SystemPrompt: `你是一位专业的文档助手,擅长文档检索、信息提取和内容整理。
## 核心能力
- 快速定位相关文档和内容
- 提取文档关键信息
- 生成结构化摘要
- 文档对比和整合
## 工作流程
1. **需求分析**:
- 理解用户的文档检索需求
- 确定搜索关键词和范围
2. **文档检索**:
- 使用 knowledge_search 语义搜索相关内容
- 使用 grep_chunks 精确匹配关键词
- 使用 get_document_info 获取文档元信息
3. **内容处理**:
- 使用 list_knowledge_chunks 查看完整内容
- 使用 thinking 工具整理和分析信息
4. **结果输出**:
- 提供结构化的信息摘要
- 标注信息来源和位置
## 输出要求
- 清晰标注信息来源(文档名、位置)
- 使用结构化格式展示信息
- 区分直接引用和总结内容
- 指出信息的完整性和可能遗漏
- 提供相关文档的导航建议`,
Temperature: 0.3,
MaxCompletionTokens: 2048,
MaxIterations: 20,
KBSelectionMode: "all",
AllowedTools: []string{"thinking", "knowledge_search", "grep_chunks", "list_knowledge_chunks", "get_document_info"},
WebSearchEnabled: false,
WebSearchMaxResults: 5,
ReflectionEnabled: false,
MultiTurnEnabled: true,
HistoryTurns: 5,
// FAQ strategy
FAQPriorityEnabled: true,
FAQDirectAnswerThreshold: 0.9,
FAQScoreBoost: 1.2,
// Retrieval strategy - focused on precision
EmbeddingTopK: 15,
KeywordThreshold: 0.25,
VectorThreshold: 0.45,
RerankTopK: 10,
RerankThreshold: 0.3,
},
}
}
// Deprecated: Use GetBuiltinQuickAnswerAgent instead
func GetBuiltinNormalAgent(tenantID uint64) *CustomAgent {
return GetBuiltinQuickAnswerAgent(tenantID)
@@ -281,14 +556,22 @@ func GetBuiltinAgentAgent(tenantID uint64) *CustomAgent {
// BuiltinAgentRegistry provides a registry of all built-in agents for easy extension
var BuiltinAgentRegistry = map[string]func(uint64) *CustomAgent{
BuiltinQuickAnswerID: GetBuiltinQuickAnswerAgent,
BuiltinSmartReasoningID: GetBuiltinSmartReasoningAgent,
BuiltinQuickAnswerID: GetBuiltinQuickAnswerAgent,
BuiltinSmartReasoningID: GetBuiltinSmartReasoningAgent,
BuiltinDeepResearcherID: GetBuiltinDeepResearcherAgent,
BuiltinDataAnalystID: GetBuiltinDataAnalystAgent,
BuiltinKnowledgeGraphExpertID: GetBuiltinKnowledgeGraphExpertAgent,
BuiltinDocumentAssistantID: GetBuiltinDocumentAssistantAgent,
}
// builtinAgentIDsOrdered defines the fixed display order of built-in agents
var builtinAgentIDsOrdered = []string{
BuiltinQuickAnswerID,
BuiltinSmartReasoningID,
BuiltinDeepResearcherID,
BuiltinDataAnalystID,
BuiltinKnowledgeGraphExpertID,
BuiltinDocumentAssistantID,
}
// GetBuiltinAgentIDs returns all built-in agent IDs in fixed order
+2
View File
@@ -54,6 +54,8 @@ type SessionService interface {
assistantMessageID string,
eventBus *event.EventBus,
customAgent *types.CustomAgent,
knowledgeBaseIDs []string,
knowledgeIDs []string,
) error
// ClearContext clears the LLM context for a session
ClearContext(ctx context.Context, sessionID string) error