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:
@@ -51,8 +51,6 @@ type SummaryConfig struct {
|
||||
|
||||
// AgentConfig defines agent configuration for agent mode
|
||||
type AgentConfig struct {
|
||||
Enabled bool `json:"enabled"` // Whether agent mode is enabled
|
||||
EnablePlanning bool `json:"enable_planning"` // Whether to plan before executing
|
||||
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
|
||||
|
||||
@@ -37,6 +37,7 @@ conversation:
|
||||
## 用户当前的问题是:
|
||||
{{.Query}}
|
||||
enable_rewrite: true
|
||||
enable_query_expansion: true
|
||||
enable_rerank: true
|
||||
rewrite_prompt_system: |
|
||||
你是一个专注于指代消解和省略补全的智能助手,你的任务是根据历史对话上下文,清晰识别用户问题中的代词并替换为明确的主语,同时补全省略的关键信息。
|
||||
|
||||
@@ -60,6 +60,7 @@ export interface ConversationConfig {
|
||||
rerank_model_id?: string
|
||||
rewrite_prompt_system?: string
|
||||
rewrite_prompt_user?: string
|
||||
enable_query_expansion?: boolean
|
||||
}
|
||||
|
||||
export function getSystemInfo(): Promise<{ data: SystemInfo }> {
|
||||
|
||||
@@ -1431,6 +1431,10 @@ export default {
|
||||
label: 'Enable Query Rewrite',
|
||||
desc: 'Automatically rewrite multi-turn queries for better recall'
|
||||
},
|
||||
enableQueryExpansion: {
|
||||
label: 'Enable LLM Query Expansion',
|
||||
desc: 'When recall is low, call a reasoning model to generate expansion queries (adds latency & cost)'
|
||||
},
|
||||
fallbackStrategy: {
|
||||
label: 'Fallback Strategy',
|
||||
desc: 'How to respond when no relevant documents are found',
|
||||
@@ -1506,6 +1510,7 @@ export default {
|
||||
rerankTopKSaved: 'ReRank TopK saved',
|
||||
rerankThresholdSaved: 'ReRank threshold saved',
|
||||
enableRewriteSaved: 'Query rewrite preference saved',
|
||||
enableQueryExpansionSaved: 'Query expansion preference saved',
|
||||
fallbackStrategySaved: 'Fallback strategy saved',
|
||||
fallbackResponseSaved: 'Fallback response saved',
|
||||
fallbackPromptSaved: 'Fallback prompt saved',
|
||||
|
||||
@@ -1237,6 +1237,15 @@ export default {
|
||||
autoDisabled: 'Конфигурация Agent неполная. Режим Agent автоматически выключен'
|
||||
}
|
||||
},
|
||||
conversationSettings: {
|
||||
enableQueryExpansion: {
|
||||
label: 'Включить расширение запросов',
|
||||
desc: 'При низкой выдаче обращаться к LLM для генерации дополнительных запросов (дороже и медленнее)'
|
||||
},
|
||||
toasts: {
|
||||
enableQueryExpansionSaved: 'Настройка расширения запросов сохранена'
|
||||
}
|
||||
},
|
||||
mcpSettings: {
|
||||
title: 'Сервисы MCP',
|
||||
description: 'Управление внешними сервисами MCP (Model Context Protocol) для использования инструментов и ресурсов в режиме Agent',
|
||||
|
||||
@@ -1435,6 +1435,10 @@ export default {
|
||||
label: "开启问题改写",
|
||||
desc: "多轮对话自动改写问题以获得更优召回",
|
||||
},
|
||||
enableQueryExpansion: {
|
||||
label: "启用查询扩展",
|
||||
desc: "召回不足时调用大模型生成扩展查询(增加时延与成本)",
|
||||
},
|
||||
fallbackStrategy: {
|
||||
label: "兜底策略",
|
||||
desc: "检索无结果时采用的处理方式",
|
||||
@@ -1510,6 +1514,7 @@ export default {
|
||||
rerankTopKSaved: "ReRank TopK 已保存",
|
||||
rerankThresholdSaved: "ReRank 阈值已保存",
|
||||
enableRewriteSaved: "问题改写开关已保存",
|
||||
enableQueryExpansionSaved: "查询扩展策略已保存",
|
||||
fallbackStrategySaved: "兜底策略已保存",
|
||||
fallbackResponseSaved: "兜底回复已保存",
|
||||
fallbackPromptSaved: "兜底 Prompt 已保存",
|
||||
|
||||
@@ -506,6 +506,20 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="setting-row">
|
||||
<div class="setting-info">
|
||||
<label>{{ $t('conversationSettings.enableQueryExpansion.label') }}</label>
|
||||
<p class="desc">{{ $t('conversationSettings.enableQueryExpansion.desc') }}</p>
|
||||
</div>
|
||||
<div class="setting-control">
|
||||
<t-switch
|
||||
v-model="localEnableQueryExpansion"
|
||||
:label="[$t('common.off'), $t('common.on')]"
|
||||
@change="handleEnableQueryExpansionChange"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 改写 Prompt:仅在开启改写时展示 -->
|
||||
<div v-if="localEnableRewrite" class="setting-row vertical">
|
||||
<div class="setting-info">
|
||||
@@ -662,6 +676,7 @@ const getDefaultConversationConfig = (): ConversationConfig => ({
|
||||
rerank_top_k: 5,
|
||||
rerank_threshold: 0.5,
|
||||
enable_rewrite: true,
|
||||
enable_query_expansion: true,
|
||||
fallback_strategy: 'fixed',
|
||||
fallback_response: '',
|
||||
fallback_prompt: '',
|
||||
@@ -720,6 +735,7 @@ const localVectorThreshold = ref(0.5)
|
||||
const localRerankTopK = ref(5)
|
||||
const localRerankThreshold = ref(0.5)
|
||||
const localEnableRewrite = ref(true)
|
||||
const localEnableQueryExpansion = ref(true)
|
||||
const localFallbackStrategy = ref<'fixed' | 'model'>('fixed')
|
||||
const localFallbackResponse = ref('')
|
||||
const localFallbackPrompt = ref('')
|
||||
@@ -750,6 +766,7 @@ const syncConversationLocals = () => {
|
||||
localRerankTopK.value = cfg.rerank_top_k ?? 5
|
||||
localRerankThreshold.value = cfg.rerank_threshold ?? 0.5
|
||||
localEnableRewrite.value = cfg.enable_rewrite ?? true
|
||||
localEnableQueryExpansion.value = cfg.enable_query_expansion ?? true
|
||||
localFallbackStrategy.value = (cfg.fallback_strategy as 'fixed' | 'model') || 'fixed'
|
||||
localFallbackResponse.value = cfg.fallback_response ?? ''
|
||||
localFallbackPrompt.value = cfg.fallback_prompt ?? ''
|
||||
@@ -1798,6 +1815,18 @@ const handleEnableRewriteChange = async (value: boolean) => {
|
||||
}
|
||||
}
|
||||
|
||||
const handleEnableQueryExpansionChange = async (value: boolean) => {
|
||||
try {
|
||||
await saveConversationConfig(
|
||||
{ enable_query_expansion: value },
|
||||
t('conversationSettings.toasts.enableQueryExpansionSaved')
|
||||
)
|
||||
} catch (error) {
|
||||
console.error('保存 enable_query_expansion 失败:', error)
|
||||
localEnableQueryExpansion.value = conversationConfig.value.enable_query_expansion ?? true
|
||||
}
|
||||
}
|
||||
|
||||
const handleFallbackStrategyChange = async (value: 'fixed' | 'model') => {
|
||||
try {
|
||||
await saveConversationConfig({ fallback_strategy: value }, t('conversationSettings.toasts.fallbackStrategySaved'))
|
||||
|
||||
@@ -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},
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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"`
|
||||
|
||||
Reference in New Issue
Block a user