feat: Introduce query expansion feature and enhance related configurations

- Added support for query expansion in conversation settings, allowing the system to generate additional queries when recall is low.
- Updated localization files in English, Russian, and Chinese to include new strings for the query expansion feature.
- Enhanced UI in AgentSettings.vue to allow users to toggle query expansion settings.
- Refactored related API and service logic to accommodate the new query expansion functionality in session management and chat processing.
This commit is contained in:
wizardchen
2025-11-19 20:23:30 +08:00
parent 5434768530
commit b9c1191dd2
15 changed files with 104 additions and 60 deletions
+1 -1
View File
@@ -746,7 +746,7 @@ func (e *AgentEngine) streamFinalAnswerToEventBus(
})
// Build messages with all context
systemPrompt := BuildReActSystemPromptWithStatus(e.knowledgeBasesInfo, e.config.WebSearchEnabled, e.systemPromptTemplate)
systemPrompt := BuildProgressiveRAGSystemPrompt(e.knowledgeBasesInfo, e.config.WebSearchEnabled, e.systemPromptTemplate)
messages := []chat.Message{
{Role: "system", Content: systemPrompt},
-13
View File
@@ -239,19 +239,6 @@ func BuildProgressiveRAGSystemPrompt(knowledgeBases []*KnowledgeBaseInfo, webSea
return BuildProgressiveRAGSystemPromptWithoutWeb(knowledgeBases, systemPromptTemplate...)
}
// BuildReActSystemPromptWithStatus builds the system prompt, allowing caller to pass tool status
// Deprecated: Use BuildProgressiveRAGSystemPrompt instead for better tool calling capabilities
func BuildReActSystemPromptWithStatus(knowledgeBases []*KnowledgeBaseInfo, webSearchEnabled bool, systemPromptTemplate ...string) string {
var template string
if len(systemPromptTemplate) > 0 && systemPromptTemplate[0] != "" {
template = systemPromptTemplate[0]
} else {
template = DefaultSystemPromptTemplate
}
currentTime := time.Now().Format(time.RFC3339)
return renderPromptPlaceholdersWithStatus(template, knowledgeBases, webSearchEnabled, currentTime)
}
// ProgressiveRAGSystemPromptWithWeb is the progressive RAG system prompt template with web search enabled
// This version emphasizes hybrid retrieval strategy: KB-first with web supplementation
var ProgressiveRAGSystemPromptWithWeb = `# Role & Mission
@@ -123,7 +123,7 @@ func (p *PluginSearch) OnEvent(ctx context.Context,
chatManage.SearchResult = allResults
// If recall is low, attempt query expansion with keyword-focused search
if len(chatManage.SearchResult) < max(1, chatManage.EmbeddingTopK/2) {
if chatManage.EnableQueryExpansion && len(chatManage.SearchResult) < max(1, chatManage.EmbeddingTopK/2) {
pipelineInfo(ctx, "Search", "recall_low", map[string]interface{}{
"current": len(chatManage.SearchResult),
"threshold": chatManage.EmbeddingTopK / 2,
+25 -22
View File
@@ -453,6 +453,7 @@ func (s *sessionService) KnowledgeQA(ctx context.Context, session *types.Session
maxRounds := session.MaxRounds
fallbackResponse := session.FallbackResponse
enableRewrite := session.EnableRewrite
enableQueryExpansion := true
summaryParams := session.SummaryParameters
if summaryParams == nil {
@@ -483,6 +484,7 @@ func (s *sessionService) KnowledgeQA(ctx context.Context, session *types.Session
maxRounds = tenantConv.MaxRounds
fallbackResponse = tenantConv.FallbackResponse
enableRewrite = tenantConv.EnableRewrite
enableQueryExpansion = tenantConv.EnableQueryExpansion
if tenantConv.MaxCompletionTokens != 0 {
summaryConfig.MaxCompletionTokens = tenantConv.MaxCompletionTokens
@@ -507,28 +509,29 @@ func (s *sessionService) KnowledgeQA(ctx context.Context, session *types.Session
// Create chat management object with session settings
logger.Infof(ctx, "Creating chat manage object, knowledge base IDs: %v, chat model ID: %s", knowledgeBaseIDs, chatModelID)
chatManage := &types.ChatManage{
Query: query,
RewriteQuery: query,
SessionID: session.ID,
MessageID: assistantMessageID, // NEW: For event emission in pipeline
KnowledgeBaseID: knowledgeBaseIDs[0], // For backward compatibility, use first KB ID
KnowledgeBaseIDs: knowledgeBaseIDs, // Multi-KB support
VectorThreshold: vectorThreshold,
KeywordThreshold: keywordThreshold,
EmbeddingTopK: embeddingTopK,
RerankModelID: rerankModelID,
RerankTopK: rerankTopK,
RerankThreshold: rerankThreshold,
MaxRounds: maxRounds,
ChatModelID: chatModelID,
SummaryConfig: summaryConfig,
FallbackResponse: fallbackResponse,
EventBus: eventBus.AsEventBusInterface(), // NEW: For pipeline to emit events directly
WebSearchEnabled: webSearchEnabled,
TenantID: session.TenantID,
RewritePromptSystem: rewritePromptSystem,
RewritePromptUser: rewritePromptUser,
EnableRewrite: enableRewrite,
Query: query,
RewriteQuery: query,
SessionID: session.ID,
MessageID: assistantMessageID, // NEW: For event emission in pipeline
KnowledgeBaseID: knowledgeBaseIDs[0], // For backward compatibility, use first KB ID
KnowledgeBaseIDs: knowledgeBaseIDs, // Multi-KB support
VectorThreshold: vectorThreshold,
KeywordThreshold: keywordThreshold,
EmbeddingTopK: embeddingTopK,
RerankModelID: rerankModelID,
RerankTopK: rerankTopK,
RerankThreshold: rerankThreshold,
MaxRounds: maxRounds,
ChatModelID: chatModelID,
SummaryConfig: summaryConfig,
FallbackResponse: fallbackResponse,
EventBus: eventBus.AsEventBusInterface(), // NEW: For pipeline to emit events directly
WebSearchEnabled: webSearchEnabled,
TenantID: session.TenantID,
RewritePromptSystem: rewritePromptSystem,
RewritePromptUser: rewritePromptUser,
EnableRewrite: enableRewrite,
EnableQueryExpansion: enableQueryExpansion,
}
// Start knowledge QA event processing
+1
View File
@@ -46,6 +46,7 @@ type ConversationConfig struct {
FallbackResponse string `yaml:"fallback_response" json:"fallback_response"`
FallbackPrompt string `yaml:"fallback_prompt" json:"fallback_prompt"`
EnableRewrite bool `yaml:"enable_rewrite" json:"enable_rewrite"`
EnableQueryExpansion bool `yaml:"enable_query_expansion" json:"enable_query_expansion"`
EnableRerank bool `yaml:"enable_rerank" json:"enable_rerank"`
Summary *SummaryConfig `yaml:"summary" json:"summary"`
GenerateSessionTitlePrompt string `yaml:"generate_session_title_prompt" json:"generate_session_title_prompt"`
+4 -2
View File
@@ -353,8 +353,6 @@ func (h *TenantHandler) updateTenantAgentConfigInternal(c *gin.Context) {
c.Error(errors.NewAgentInvalidTemperatureError())
return
}
// thinking_model_id 不再强制要求,允许先启用 Agent 再设置模型
// 实际使用时会在 AgentQA 中进行验证
if len(req.AllowedTools) == 0 {
c.Error(errors.NewAgentMissingAllowedToolsError())
return
@@ -531,6 +529,7 @@ func (h *TenantHandler) buildDefaultConversationConfig() *types.ConversationConf
RerankTopK: h.config.Conversation.RerankTopK,
RerankThreshold: h.config.Conversation.RerankThreshold,
EnableRewrite: h.config.Conversation.EnableRewrite,
EnableQueryExpansion: h.config.Conversation.EnableQueryExpansion,
FallbackStrategy: h.config.Conversation.FallbackStrategy,
FallbackResponse: h.config.Conversation.FallbackResponse,
FallbackPrompt: h.config.Conversation.FallbackPrompt,
@@ -641,6 +640,9 @@ func (h *TenantHandler) GetTenantConversationConfig(c *gin.Context) {
// EnableRewrite 需要允许显式关闭,因此直接覆盖
defaultCfg.EnableRewrite = tc.EnableRewrite
// Query expansion toggle
defaultCfg.EnableQueryExpansion = tc.EnableQueryExpansion
// Model IDs
if tc.SummaryModelID != "" {
defaultCfg.SummaryModelID = tc.SummaryModelID
+14 -12
View File
@@ -6,8 +6,8 @@ type ChatManage struct {
SessionID string `json:"session_id"` // Unique identifier for the chat session
Query string `json:"query,omitempty"` // Original user query
ProcessedQuery string `json:"processed_query,omitempty"` // Query after preprocessing
RewriteQuery string `json:"rewrite_query,omitempty"` // Query after rewriting for better retrieval
QueryIntent string `json:"query_intent,omitempty"` // Parsed intent: definition/howto/compare/qa/general
RewriteQuery string `json:"rewrite_query,omitempty"` // Query after rewriting for better retrieval
QueryIntent string `json:"query_intent,omitempty"` // Parsed intent: definition/howto/compare/qa/general
History []*History `json:"history,omitempty"` // Chat history for context
KnowledgeBaseID string `json:"knowledge_base_id"` // ID of the knowledge base to search against (deprecated, use KnowledgeBaseIDs)
@@ -28,9 +28,10 @@ type ChatManage struct {
FallbackStrategy FallbackStrategy `json:"fallback_strategy"` // Strategy when no relevant results are found
FallbackResponse string `json:"fallback_response"` // Default response when fallback occurs
EnableRewrite bool `json:"enable_rewrite"` // Whether to enable rewrite
RewritePromptSystem string `json:"rewrite_prompt_system"` // Custom system prompt for rewrite stage
RewritePromptUser string `json:"rewrite_prompt_user"` // Custom user prompt for rewrite stage
EnableRewrite bool `json:"enable_rewrite"` // Whether to enable rewrite
EnableQueryExpansion bool `json:"enable_query_expansion"` // Whether to enable query expansion with LLM
RewritePromptSystem string `json:"rewrite_prompt_system"` // Custom system prompt for rewrite stage
RewritePromptUser string `json:"rewrite_prompt_user"` // Custom user prompt for rewrite stage
// Internal fields for pipeline data processing
SearchResult []*SearchResult `json:"-"` // Results from search phase
@@ -59,8 +60,8 @@ func (c *ChatManage) Clone() *ChatManage {
return &ChatManage{
Query: c.Query,
ProcessedQuery: c.ProcessedQuery,
RewriteQuery: c.RewriteQuery,
QueryIntent: c.QueryIntent,
RewriteQuery: c.RewriteQuery,
QueryIntent: c.QueryIntent,
SessionID: c.SessionID,
KnowledgeBaseID: c.KnowledgeBaseID,
KnowledgeBaseIDs: knowledgeBaseIDs,
@@ -87,11 +88,12 @@ func (c *ChatManage) Clone() *ChatManage {
Seed: c.SummaryConfig.Seed,
MaxCompletionTokens: c.SummaryConfig.MaxCompletionTokens,
},
FallbackStrategy: c.FallbackStrategy,
FallbackResponse: c.FallbackResponse,
RewritePromptSystem: c.RewritePromptSystem,
RewritePromptUser: c.RewritePromptUser,
EnableRewrite: c.EnableRewrite,
FallbackStrategy: c.FallbackStrategy,
FallbackResponse: c.FallbackResponse,
RewritePromptSystem: c.RewritePromptSystem,
RewritePromptUser: c.RewritePromptUser,
EnableRewrite: c.EnableRewrite,
EnableQueryExpansion: c.EnableQueryExpansion,
}
}
+8 -7
View File
@@ -86,13 +86,14 @@ type ConversationConfig struct {
MaxCompletionTokens int `json:"max_completion_tokens"`
// Retrieval & strategy parameters
MaxRounds int `json:"max_rounds"`
EmbeddingTopK int `json:"embedding_top_k"`
KeywordThreshold float64 `json:"keyword_threshold"`
VectorThreshold float64 `json:"vector_threshold"`
RerankTopK int `json:"rerank_top_k"`
RerankThreshold float64 `json:"rerank_threshold"`
EnableRewrite bool `json:"enable_rewrite"`
MaxRounds int `json:"max_rounds"`
EmbeddingTopK int `json:"embedding_top_k"`
KeywordThreshold float64 `json:"keyword_threshold"`
VectorThreshold float64 `json:"vector_threshold"`
RerankTopK int `json:"rerank_top_k"`
RerankThreshold float64 `json:"rerank_threshold"`
EnableRewrite bool `json:"enable_rewrite"`
EnableQueryExpansion bool `json:"enable_query_expansion"`
// Model configuration
SummaryModelID string `json:"summary_model_id"`