feat: Introduce conversation configuration management and localization updates

- Added new ConversationConfig interface and API endpoints for retrieving and updating conversation settings.
- Enhanced AgentSettings.vue to include UI for configuring conversation parameters such as prompt, context template, temperature, and max tokens.
- Updated localization files in English and Chinese to include new strings for conversation settings.
- Implemented logic to load and save conversation configuration for tenants, ensuring defaults are applied when no specific configuration exists.
This commit is contained in:
wizardchen
2025-11-17 15:33:43 +08:00
parent a886106917
commit 1f5e445eaa
14 changed files with 913 additions and 175 deletions
+3 -3
View File
@@ -97,9 +97,9 @@ func (h *Handler) CreateSession(c *gin.Context) {
if request.SessionStrategy.SummaryParameters != nil {
createdSession.SummaryParameters = request.SessionStrategy.SummaryParameters
} else {
createdSession.SummaryParameters = h.createDefaultSummaryConfig()
createdSession.SummaryParameters = h.createDefaultSummaryConfig(ctx)
}
h.fillSummaryConfigDefaults(createdSession.SummaryParameters)
h.fillSummaryConfigDefaults(ctx, createdSession.SummaryParameters)
logger.Debug(ctx, "Custom session strategy set")
} else {
@@ -113,7 +113,7 @@ func (h *Handler) CreateSession(c *gin.Context) {
createdSession.VectorThreshold = h.config.Conversation.VectorThreshold
createdSession.RerankThreshold = h.config.Conversation.RerankThreshold
createdSession.RerankTopK = h.config.Conversation.RerankTopK
createdSession.SummaryParameters = h.createDefaultSummaryConfig()
createdSession.SummaryParameters = h.createDefaultSummaryConfig(ctx)
logger.Debug(ctx, "Using default session strategy")
}
+68 -6
View File
@@ -162,8 +162,13 @@ func getFloat64(m map[string]interface{}, key string) float64 {
}
// createDefaultSummaryConfig creates a default summary configuration from config
func (h *Handler) createDefaultSummaryConfig() *types.SummaryConfig {
return &types.SummaryConfig{
// It prioritizes tenant-level ConversationConfig, then falls back to config.yaml defaults
func (h *Handler) createDefaultSummaryConfig(ctx context.Context) *types.SummaryConfig {
// Try to get tenant from context
tenant, _ := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
// Initialize with config.yaml defaults
cfg := &types.SummaryConfig{
MaxTokens: h.config.Conversation.Summary.MaxTokens,
TopP: h.config.Conversation.Summary.TopP,
TopK: h.config.Conversation.Summary.TopK,
@@ -177,17 +182,74 @@ func (h *Handler) createDefaultSummaryConfig() *types.SummaryConfig {
Seed: h.config.Conversation.Summary.Seed,
MaxCompletionTokens: h.config.Conversation.Summary.MaxCompletionTokens,
}
// Override with tenant-level conversation config if available
if tenant != nil && tenant.ConversationConfig != nil {
if tenant.ConversationConfig.Prompt != "" {
cfg.Prompt = tenant.ConversationConfig.Prompt
}
if tenant.ConversationConfig.ContextTemplate != "" {
cfg.ContextTemplate = tenant.ConversationConfig.ContextTemplate
}
if tenant.ConversationConfig.Temperature > 0 {
cfg.Temperature = tenant.ConversationConfig.Temperature
}
if tenant.ConversationConfig.MaxTokens > 0 {
cfg.MaxTokens = tenant.ConversationConfig.MaxTokens
}
}
return cfg
}
// fillSummaryConfigDefaults fills missing fields in summary config with defaults
func (h *Handler) fillSummaryConfigDefaults(config *types.SummaryConfig) {
// It prioritizes tenant-level ConversationConfig, then falls back to config.yaml defaults
func (h *Handler) fillSummaryConfigDefaults(ctx context.Context, config *types.SummaryConfig) {
// Try to get tenant from context
tenant, _ := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
// Determine default values: tenant config first, then config.yaml
var defaultPrompt, defaultContextTemplate, defaultNoMatchPrefix string
var defaultTemperature float64
var defaultMaxTokens int
if tenant != nil && tenant.ConversationConfig != nil {
// Use tenant-level config as defaults
defaultPrompt = tenant.ConversationConfig.Prompt
defaultContextTemplate = tenant.ConversationConfig.ContextTemplate
defaultTemperature = tenant.ConversationConfig.Temperature
defaultMaxTokens = tenant.ConversationConfig.MaxTokens
}
// Fall back to config.yaml if tenant config is empty
if defaultPrompt == "" {
defaultPrompt = h.config.Conversation.Summary.Prompt
}
if defaultContextTemplate == "" {
defaultContextTemplate = h.config.Conversation.Summary.ContextTemplate
}
if defaultTemperature == 0 {
defaultTemperature = h.config.Conversation.Summary.Temperature
}
if defaultMaxTokens == 0 {
defaultMaxTokens = h.config.Conversation.Summary.MaxTokens
}
defaultNoMatchPrefix = h.config.Conversation.Summary.NoMatchPrefix
// Fill missing fields
if config.Prompt == "" {
config.Prompt = h.config.Conversation.Summary.Prompt
config.Prompt = defaultPrompt
}
if config.ContextTemplate == "" {
config.ContextTemplate = h.config.Conversation.Summary.ContextTemplate
config.ContextTemplate = defaultContextTemplate
}
if config.Temperature == 0 {
config.Temperature = defaultTemperature
}
if config.MaxTokens == 0 {
config.MaxTokens = defaultMaxTokens
}
if config.NoMatchPrefix == "" {
config.NoMatchPrefix = h.config.Conversation.Summary.NoMatchPrefix
config.NoMatchPrefix = defaultNoMatchPrefix
}
}
+110 -1
View File
@@ -8,6 +8,7 @@ import (
"github.com/Tencent/WeKnora/internal/agent"
agenttools "github.com/Tencent/WeKnora/internal/agent/tools"
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/errors"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
@@ -19,16 +20,19 @@ import (
// through the REST API endpoints
type TenantHandler struct {
service interfaces.TenantService
config *config.Config
}
// NewTenantHandler creates a new tenant handler instance with the provided service
// Parameters:
// - service: An implementation of the TenantService interface for business logic
// - config: Application configuration
//
// Returns a pointer to the newly created TenantHandler
func NewTenantHandler(service interfaces.TenantService) *TenantHandler {
func NewTenantHandler(service interfaces.TenantService, config *config.Config) *TenantHandler {
return &TenantHandler{
service: service,
config: config,
}
}
@@ -410,6 +414,9 @@ func (h *TenantHandler) GetTenantKV(c *gin.Context) {
case "web-search-config":
h.GetTenantWebSearchConfig(c)
return
case "conversation-config":
h.GetTenantConversationConfig(c)
return
default:
logger.Info(ctx, "KV key not supported", "key", key)
c.Error(errors.NewBadRequestError("unsupported key"))
@@ -430,6 +437,9 @@ func (h *TenantHandler) UpdateTenantKV(c *gin.Context) {
case "web-search-config":
h.updateTenantWebSearchConfigInternal(c)
return
case "conversation-config":
h.updateTenantConversationConfigInternal(c)
return
default:
logger.Info(ctx, "KV key not supported", "key", key)
c.Error(errors.NewBadRequestError("unsupported key"))
@@ -499,3 +509,102 @@ func (h *TenantHandler) GetTenantWebSearchConfig(c *gin.Context) {
"data": tenant.WebSearchConfig,
})
}
// 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) {
ctx := c.Request.Context()
logger.Info(ctx, "Start retrieving tenant conversation config")
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
return
}
// If tenant has no conversation config, return defaults from config.yaml
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
}
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,
},
})
}
// updateTenantConversationConfigInternal updates the conversation configuration for a tenant
// This sets the global conversation configuration for normal mode sessions in this tenant
func (h *TenantHandler) updateTenantConversationConfigInternal(c *gin.Context) {
ctx := c.Request.Context()
logger.Info(ctx, "Start updating tenant conversation config")
var req types.ConversationConfig
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(ctx, "Failed to parse request parameters", err)
c.Error(errors.NewValidationError("Invalid request data").WithDetails(err.Error()))
return
}
// 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"))
return
}
// Get existing tenant
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
return
}
// Update conversation configuration
tenant.ConversationConfig = &req
updatedTenant, err := h.service.UpdateTenant(ctx, tenant)
if err != nil {
if appErr, ok := errors.IsAppError(err); ok {
logger.Error(ctx, "Failed to update tenant: application error", appErr)
c.Error(appErr)
} else {
logger.ErrorWithFields(ctx, err, nil)
c.Error(errors.NewInternalServerError("Failed to update tenant conversation config").WithDetails(err.Error()))
}
return
}
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,
},
"message": "Conversation configuration updated successfully",
})
}