feat: Revamp conversation configuration management and enhance localization
- Removed outdated DEVELOPMENT.md, consolidating development instructions into relevant documentation. - Updated conversation configuration to include new parameters such as max rounds, embedding thresholds, and rewrite prompts. - Enhanced UI components to support new conversation settings, including AgentSettings.vue and related views. - Improved localization in English, Russian, and Chinese for new conversation configuration fields and UI elements. - Implemented logic to apply tenant-specific conversation settings, ensuring defaults are respected.
This commit is contained in:
@@ -444,7 +444,7 @@ func (h *AgentStreamHandler) handleComplete(ctx context.Context, evt event.Event
|
||||
"total_duration_ms": data.TotalDurationMs,
|
||||
},
|
||||
}); err != nil {
|
||||
logger.GetLogger(h.ctx).Error("Append complete event to stream failed", "error", err)
|
||||
logger.GetLogger(h.ctx).Errorf("Append complete event to stream failed: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/config"
|
||||
@@ -103,18 +104,8 @@ func (h *Handler) CreateSession(c *gin.Context) {
|
||||
|
||||
logger.Debug(ctx, "Custom session strategy set")
|
||||
} else {
|
||||
// Use default configuration from global config
|
||||
createdSession.MaxRounds = h.config.Conversation.MaxRounds
|
||||
createdSession.EnableRewrite = h.config.Conversation.EnableRewrite
|
||||
createdSession.FallbackStrategy = types.FallbackStrategy(h.config.Conversation.FallbackStrategy)
|
||||
createdSession.FallbackResponse = h.config.Conversation.FallbackResponse
|
||||
createdSession.EmbeddingTopK = h.config.Conversation.EmbeddingTopK
|
||||
createdSession.KeywordThreshold = h.config.Conversation.KeywordThreshold
|
||||
createdSession.VectorThreshold = h.config.Conversation.VectorThreshold
|
||||
createdSession.RerankThreshold = h.config.Conversation.RerankThreshold
|
||||
createdSession.RerankTopK = h.config.Conversation.RerankTopK
|
||||
createdSession.SummaryParameters = h.createDefaultSummaryConfig(ctx)
|
||||
|
||||
tenantInfo, _ := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
|
||||
h.applyConversationDefaults(ctx, createdSession, tenantInfo)
|
||||
logger.Debug(ctx, "Using default session strategy")
|
||||
}
|
||||
|
||||
@@ -159,6 +150,45 @@ func (h *Handler) CreateSession(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) applyConversationDefaults(ctx context.Context, session *types.Session, tenant *types.Tenant) {
|
||||
session.MaxRounds = h.config.Conversation.MaxRounds
|
||||
session.EnableRewrite = h.config.Conversation.EnableRewrite
|
||||
session.FallbackStrategy = types.FallbackStrategy(h.config.Conversation.FallbackStrategy)
|
||||
session.FallbackResponse = h.config.Conversation.FallbackResponse
|
||||
session.EmbeddingTopK = h.config.Conversation.EmbeddingTopK
|
||||
session.KeywordThreshold = h.config.Conversation.KeywordThreshold
|
||||
session.VectorThreshold = h.config.Conversation.VectorThreshold
|
||||
session.RerankThreshold = h.config.Conversation.RerankThreshold
|
||||
session.RerankTopK = h.config.Conversation.RerankTopK
|
||||
session.RerankModelID = ""
|
||||
session.SummaryModelID = ""
|
||||
|
||||
if tenant != nil && tenant.ConversationConfig != nil {
|
||||
tc := tenant.ConversationConfig
|
||||
session.MaxRounds = tc.MaxRounds
|
||||
session.EnableRewrite = tc.EnableRewrite
|
||||
if tc.FallbackStrategy != "" {
|
||||
session.FallbackStrategy = types.FallbackStrategy(tc.FallbackStrategy)
|
||||
}
|
||||
if tc.FallbackResponse != "" {
|
||||
session.FallbackResponse = tc.FallbackResponse
|
||||
}
|
||||
session.EmbeddingTopK = tc.EmbeddingTopK
|
||||
session.KeywordThreshold = tc.KeywordThreshold
|
||||
session.VectorThreshold = tc.VectorThreshold
|
||||
session.RerankThreshold = tc.RerankThreshold
|
||||
session.RerankTopK = tc.RerankTopK
|
||||
if tc.RerankModelID != "" {
|
||||
session.RerankModelID = tc.RerankModelID
|
||||
}
|
||||
if tc.SummaryModelID != "" {
|
||||
session.SummaryModelID = tc.SummaryModelID
|
||||
}
|
||||
}
|
||||
|
||||
session.SummaryParameters = h.createDefaultSummaryConfig(ctx)
|
||||
}
|
||||
|
||||
// GetSession retrieves a session by its ID
|
||||
func (h *Handler) GetSession(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
@@ -185,10 +185,20 @@ func (h *Handler) createDefaultSummaryConfig(ctx context.Context) *types.Summary
|
||||
|
||||
// Override with tenant-level conversation config if available
|
||||
if tenant != nil && tenant.ConversationConfig != nil {
|
||||
if tenant.ConversationConfig.Prompt != "" {
|
||||
useSystemPrompt := tenant.ConversationConfig.UseCustomSystemPrompt
|
||||
if !useSystemPrompt && tenant.ConversationConfig.Prompt != "" {
|
||||
// Backward compatibility: treat legacy configs without flag as custom
|
||||
useSystemPrompt = true
|
||||
}
|
||||
if useSystemPrompt && tenant.ConversationConfig.Prompt != "" {
|
||||
cfg.Prompt = tenant.ConversationConfig.Prompt
|
||||
}
|
||||
if tenant.ConversationConfig.ContextTemplate != "" {
|
||||
|
||||
useContextTemplate := tenant.ConversationConfig.UseCustomContextTemplate
|
||||
if !useContextTemplate && tenant.ConversationConfig.ContextTemplate != "" {
|
||||
useContextTemplate = true
|
||||
}
|
||||
if useContextTemplate && tenant.ConversationConfig.ContextTemplate != "" {
|
||||
cfg.ContextTemplate = tenant.ConversationConfig.ContextTemplate
|
||||
}
|
||||
if tenant.ConversationConfig.Temperature > 0 {
|
||||
@@ -214,9 +224,15 @@ func (h *Handler) fillSummaryConfigDefaults(ctx context.Context, config *types.S
|
||||
var defaultMaxTokens int
|
||||
|
||||
if tenant != nil && tenant.ConversationConfig != nil {
|
||||
// Use tenant-level config as defaults
|
||||
defaultPrompt = tenant.ConversationConfig.Prompt
|
||||
defaultContextTemplate = tenant.ConversationConfig.ContextTemplate
|
||||
useSystemPrompt := tenant.ConversationConfig.UseCustomSystemPrompt
|
||||
if useSystemPrompt && tenant.ConversationConfig.Prompt != "" {
|
||||
defaultPrompt = tenant.ConversationConfig.Prompt
|
||||
}
|
||||
|
||||
useContextTemplate := tenant.ConversationConfig.UseCustomContextTemplate
|
||||
if useContextTemplate && tenant.ConversationConfig.ContextTemplate != "" {
|
||||
defaultContextTemplate = tenant.ConversationConfig.ContextTemplate
|
||||
}
|
||||
defaultTemperature = tenant.ConversationConfig.Temperature
|
||||
defaultMaxTokens = tenant.ConversationConfig.MaxTokens
|
||||
}
|
||||
|
||||
@@ -443,6 +443,16 @@ func (h *Handler) handleKnowledgeQARequest(
|
||||
if data.Done {
|
||||
logger.Infof(asyncCtx, "Knowledge QA service completed for session: %s", sessionID)
|
||||
h.completeAssistantMessage(asyncCtx, assistantMessage)
|
||||
// Emit completion event when stream finishes
|
||||
if err := eventBus.Emit(asyncCtx, event.Event{
|
||||
Type: event.EventAgentComplete,
|
||||
SessionID: sessionID,
|
||||
Data: event.AgentCompleteData{
|
||||
FinalAnswer: assistantMessage.Content,
|
||||
},
|
||||
}); err != nil {
|
||||
logger.Errorf(asyncCtx, "Failed to emit completion event: %v", err)
|
||||
}
|
||||
cancel() // Clean up context
|
||||
return nil
|
||||
}
|
||||
|
||||
+141
-29
@@ -510,6 +510,62 @@ func (h *TenantHandler) GetTenantWebSearchConfig(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *TenantHandler) buildDefaultConversationConfig() *types.ConversationConfig {
|
||||
return &types.ConversationConfig{
|
||||
Prompt: h.config.Conversation.Summary.Prompt,
|
||||
ContextTemplate: h.config.Conversation.Summary.ContextTemplate,
|
||||
UseCustomContextTemplate: true,
|
||||
UseCustomSystemPrompt: true,
|
||||
Temperature: h.config.Conversation.Summary.Temperature,
|
||||
MaxTokens: h.config.Conversation.Summary.MaxTokens,
|
||||
MaxRounds: h.config.Conversation.MaxRounds,
|
||||
EmbeddingTopK: h.config.Conversation.EmbeddingTopK,
|
||||
KeywordThreshold: h.config.Conversation.KeywordThreshold,
|
||||
VectorThreshold: h.config.Conversation.VectorThreshold,
|
||||
RerankTopK: h.config.Conversation.RerankTopK,
|
||||
RerankThreshold: h.config.Conversation.RerankThreshold,
|
||||
EnableRewrite: h.config.Conversation.EnableRewrite,
|
||||
FallbackStrategy: h.config.Conversation.FallbackStrategy,
|
||||
FallbackResponse: h.config.Conversation.FallbackResponse,
|
||||
FallbackPrompt: h.config.Conversation.FallbackPrompt,
|
||||
RewritePromptUser: h.config.Conversation.RewritePromptUser,
|
||||
RewritePromptSystem: h.config.Conversation.RewritePromptSystem,
|
||||
}
|
||||
}
|
||||
|
||||
func validateConversationConfig(req *types.ConversationConfig) error {
|
||||
if req.MaxRounds <= 0 {
|
||||
return errors.NewBadRequestError("max_rounds must be greater than 0")
|
||||
}
|
||||
if req.EmbeddingTopK <= 0 {
|
||||
return errors.NewBadRequestError("embedding_top_k must be greater than 0")
|
||||
}
|
||||
if req.KeywordThreshold < 0 || req.KeywordThreshold > 1 {
|
||||
return errors.NewBadRequestError("keyword_threshold must be between 0 and 1")
|
||||
}
|
||||
if req.VectorThreshold < 0 || req.VectorThreshold > 1 {
|
||||
return errors.NewBadRequestError("vector_threshold must be between 0 and 1")
|
||||
}
|
||||
if req.RerankTopK <= 0 {
|
||||
return errors.NewBadRequestError("rerank_top_k must be greater than 0")
|
||||
}
|
||||
if req.RerankThreshold < 0 || req.RerankThreshold > 1 {
|
||||
return errors.NewBadRequestError("rerank_threshold must be between 0 and 1")
|
||||
}
|
||||
if req.Temperature < 0 || req.Temperature > 2 {
|
||||
return errors.NewBadRequestError("temperature must be between 0 and 2")
|
||||
}
|
||||
if req.MaxTokens <= 0 || req.MaxTokens > 100000 {
|
||||
return errors.NewBadRequestError("max_tokens must be between 1 and 100000")
|
||||
}
|
||||
if req.FallbackStrategy != "" &&
|
||||
req.FallbackStrategy != string(types.FallbackStrategyFixed) &&
|
||||
req.FallbackStrategy != string(types.FallbackStrategyModel) {
|
||||
return errors.NewBadRequestError("fallback_strategy is invalid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTenantConversationConfig retrieves the conversation configuration for a tenant
|
||||
// This is the global conversation configuration that applies to normal mode sessions by default
|
||||
func (h *TenantHandler) GetTenantConversationConfig(c *gin.Context) {
|
||||
@@ -524,29 +580,94 @@ func (h *TenantHandler) GetTenantConversationConfig(c *gin.Context) {
|
||||
}
|
||||
|
||||
// If tenant has no conversation config, return defaults from config.yaml
|
||||
var response *types.ConversationConfig
|
||||
if tenant.ConversationConfig == nil {
|
||||
logger.Info(ctx, "Tenant has no conversation config, returning defaults")
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": gin.H{
|
||||
"prompt": h.config.Conversation.Summary.Prompt,
|
||||
"context_template": h.config.Conversation.Summary.ContextTemplate,
|
||||
"temperature": h.config.Conversation.Summary.Temperature,
|
||||
"max_tokens": h.config.Conversation.Summary.MaxTokens,
|
||||
},
|
||||
})
|
||||
return
|
||||
response = h.buildDefaultConversationConfig()
|
||||
} else {
|
||||
logger.Infof(ctx, "Tenant has conversation config, merging with defaults, Tenant ID: %d", tenant.ID)
|
||||
// Merge tenant config with defaults, so that newly added fields always have valid values
|
||||
defaultCfg := h.buildDefaultConversationConfig()
|
||||
tc := tenant.ConversationConfig
|
||||
|
||||
// Prompt related
|
||||
defaultCfg.UseCustomSystemPrompt = tc.UseCustomSystemPrompt
|
||||
if !defaultCfg.UseCustomSystemPrompt && tc.Prompt != "" {
|
||||
// Legacy configs without explicit flag
|
||||
defaultCfg.UseCustomSystemPrompt = true
|
||||
}
|
||||
defaultCfg.UseCustomContextTemplate = tc.UseCustomContextTemplate
|
||||
if !defaultCfg.UseCustomContextTemplate && tc.ContextTemplate != "" {
|
||||
defaultCfg.UseCustomContextTemplate = true
|
||||
}
|
||||
if tc.Prompt != "" {
|
||||
defaultCfg.Prompt = tc.Prompt
|
||||
}
|
||||
if tc.ContextTemplate != "" {
|
||||
defaultCfg.ContextTemplate = tc.ContextTemplate
|
||||
}
|
||||
if tc.Temperature > 0 {
|
||||
defaultCfg.Temperature = tc.Temperature
|
||||
}
|
||||
if tc.MaxTokens > 0 {
|
||||
defaultCfg.MaxTokens = tc.MaxTokens
|
||||
}
|
||||
|
||||
// Retrieval parameters
|
||||
if tc.MaxRounds > 0 {
|
||||
defaultCfg.MaxRounds = tc.MaxRounds
|
||||
}
|
||||
if tc.EmbeddingTopK > 0 {
|
||||
defaultCfg.EmbeddingTopK = tc.EmbeddingTopK
|
||||
}
|
||||
if tc.KeywordThreshold > 0 {
|
||||
defaultCfg.KeywordThreshold = tc.KeywordThreshold
|
||||
}
|
||||
if tc.VectorThreshold > 0 {
|
||||
defaultCfg.VectorThreshold = tc.VectorThreshold
|
||||
}
|
||||
if tc.RerankTopK > 0 {
|
||||
defaultCfg.RerankTopK = tc.RerankTopK
|
||||
}
|
||||
if tc.RerankThreshold > 0 {
|
||||
defaultCfg.RerankThreshold = tc.RerankThreshold
|
||||
}
|
||||
// EnableRewrite 需要允许显式关闭,因此直接覆盖
|
||||
defaultCfg.EnableRewrite = tc.EnableRewrite
|
||||
|
||||
// Model IDs
|
||||
if tc.SummaryModelID != "" {
|
||||
defaultCfg.SummaryModelID = tc.SummaryModelID
|
||||
}
|
||||
if tc.RerankModelID != "" {
|
||||
defaultCfg.RerankModelID = tc.RerankModelID
|
||||
}
|
||||
|
||||
// Fallback settings
|
||||
if tc.FallbackStrategy != "" {
|
||||
defaultCfg.FallbackStrategy = tc.FallbackStrategy
|
||||
}
|
||||
if tc.FallbackResponse != "" {
|
||||
defaultCfg.FallbackResponse = tc.FallbackResponse
|
||||
}
|
||||
if tc.FallbackPrompt != "" {
|
||||
defaultCfg.FallbackPrompt = tc.FallbackPrompt
|
||||
}
|
||||
|
||||
// Rewrite prompts
|
||||
if tc.RewritePromptSystem != "" {
|
||||
defaultCfg.RewritePromptSystem = tc.RewritePromptSystem
|
||||
}
|
||||
if tc.RewritePromptUser != "" {
|
||||
defaultCfg.RewritePromptUser = tc.RewritePromptUser
|
||||
}
|
||||
|
||||
response = defaultCfg
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Retrieved tenant conversation config successfully, Tenant ID: %d", tenant.ID)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": gin.H{
|
||||
"prompt": tenant.ConversationConfig.Prompt,
|
||||
"context_template": tenant.ConversationConfig.ContextTemplate,
|
||||
"temperature": tenant.ConversationConfig.Temperature,
|
||||
"max_tokens": tenant.ConversationConfig.MaxTokens,
|
||||
},
|
||||
"data": response,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -564,12 +685,8 @@ func (h *TenantHandler) updateTenantConversationConfigInternal(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Validate configuration
|
||||
if req.Temperature < 0 || req.Temperature > 2 {
|
||||
c.Error(errors.NewBadRequestError("temperature must be between 0 and 2"))
|
||||
return
|
||||
}
|
||||
if req.MaxTokens <= 0 || req.MaxTokens > 100000 {
|
||||
c.Error(errors.NewBadRequestError("max_tokens must be between 1 and 100000"))
|
||||
if err := validateConversationConfig(&req); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -599,12 +716,7 @@ func (h *TenantHandler) updateTenantConversationConfigInternal(c *gin.Context) {
|
||||
logger.Infof(ctx, "Tenant conversation config updated successfully, Tenant ID: %d", tenant.ID)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": gin.H{
|
||||
"prompt": updatedTenant.ConversationConfig.Prompt,
|
||||
"context_template": updatedTenant.ConversationConfig.ContextTemplate,
|
||||
"temperature": updatedTenant.ConversationConfig.Temperature,
|
||||
"max_tokens": updatedTenant.ConversationConfig.MaxTokens,
|
||||
},
|
||||
"data": updatedTenant.ConversationConfig,
|
||||
"message": "Conversation configuration updated successfully",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user