feat: enhance localization and configuration support

- Added timezone and language settings to the environment configuration.
- Introduced built-in agent configurations with multilingual support for various agents.
- Updated Docker Compose to utilize new environment variables for timezone and language.
- Created new prompt templates for question generation, summary generation, and keywords extraction.
This commit is contained in:
wizardchen
2026-03-17 15:33:19 +08:00
committed by lyingbug
parent 09d9fe5ddb
commit 139a9c40ff
54 changed files with 2524 additions and 1482 deletions
@@ -203,6 +203,7 @@ func (s *agentService) CreateAgentEngine(
sessionID,
systemPromptTemplate,
)
engine.SetAppConfig(s.cfg)
// Initialize skills manager if skills are enabled
if config.SkillsEnabled && len(config.SkillDirs) > 0 {
@@ -3,7 +3,6 @@ package chatpipline
import (
"context"
"strings"
"time"
"github.com/Tencent/WeKnora/internal/common"
"github.com/Tencent/WeKnora/internal/logger"
@@ -55,7 +54,7 @@ func prepareChatModel(ctx context.Context, modelService interfaces.ModelService,
// prepareMessagesWithHistory prepare complete messages including history
func prepareMessagesWithHistory(chatManage *types.ChatManage) []chat.Message {
// Replace placeholders in system prompt
systemPrompt := renderSystemPromptPlaceholders(chatManage.SummaryConfig.Prompt)
systemPrompt := renderSystemPromptPlaceholders(chatManage.SummaryConfig.Prompt, chatManage.Language)
chatMessages := []chat.Message{
{Role: "system", Content: systemPrompt},
@@ -94,14 +93,11 @@ func extractImageCaptions(images types.MessageImages) string {
// renderSystemPromptPlaceholders replaces placeholders in system prompt
// Supported placeholders:
// - {{current_time}} -> current time in RFC3339 format
func renderSystemPromptPlaceholders(prompt string) string {
result := prompt
// Replace {{current_time}} placeholder
if strings.Contains(result, "{{current_time}}") {
currentTime := time.Now().Format(time.RFC3339)
result = strings.ReplaceAll(result, "{{current_time}}", currentTime)
// - {{language}} -> user language name (replaced if present; empty string if not set in ChatManage)
func renderSystemPromptPlaceholders(prompt string, language ...string) string {
vals := types.PlaceholderValues{}
if len(language) > 0 {
vals["language"] = language[0]
}
return result
return types.RenderPromptPlaceholders(prompt, vals)
}
@@ -6,7 +6,6 @@ import (
"fmt"
"regexp"
"strings"
"time"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/utils"
@@ -74,9 +73,6 @@ func (p *PluginIntoChatMessage) OnEvent(ctx context.Context,
return ErrTemplateExecute.WithError(fmt.Errorf("user query contains invalid content"))
}
// Prepare weekday names
weekdayName := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
// Intent-based no-search path: bypass "reference materials" template entirely.
if chatManage.SkipKBSearch {
// Prefer rewritten query in no-search mode; fallback to original query.
@@ -140,11 +136,11 @@ func (p *PluginIntoChatMessage) OnEvent(ctx context.Context,
}
// Replace placeholders in context template
userContent := chatManage.SummaryConfig.ContextTemplate
userContent = strings.ReplaceAll(userContent, "{{query}}", safeQuery)
userContent = strings.ReplaceAll(userContent, "{{contexts}}", contextsBuilder.String())
userContent = strings.ReplaceAll(userContent, "{{current_time}}", time.Now().Format("2006-01-02 15:04:05"))
userContent = strings.ReplaceAll(userContent, "{{current_week}}", weekdayName[time.Now().Weekday()])
userContent := types.RenderPromptPlaceholders(chatManage.SummaryConfig.ContextTemplate, types.PlaceholderValues{
"query": safeQuery,
"contexts": contextsBuilder.String(),
"language": chatManage.Language,
})
// Append image description as text fallback only when the chat model cannot
// process images directly. Vision-capable models see images via MultiContent.
@@ -354,18 +354,15 @@ func (p *PluginRewrite) buildPrompts(chatManage *types.ChatManage, historyList [
}
conversationText := formatConversationHistory(historyList)
currentTime := time.Now().Format("2006-01-02 15:04:05")
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
replacePlaceholders := func(s string) string {
s = strings.ReplaceAll(s, "{{conversation}}", conversationText)
s = strings.ReplaceAll(s, "{{query}}", chatManage.Query)
s = strings.ReplaceAll(s, "{{current_time}}", currentTime)
s = strings.ReplaceAll(s, "{{yesterday}}", yesterday)
return s
vals := types.PlaceholderValues{
"conversation": conversationText,
"query": chatManage.Query,
"language": chatManage.Language,
}
return replacePlaceholders(systemPrompt), replacePlaceholders(userPrompt)
return types.RenderPromptPlaceholders(systemPrompt, vals),
types.RenderPromptPlaceholders(userPrompt, vals)
}
// parseRewriteOutput extracts intent classification, rewritten query, and
+6 -6
View File
@@ -103,8 +103,8 @@ func (s *customAgentService) GetAgentByID(ctx context.Context, id string) (*type
// Found in database, return with customized config
return agent, nil
}
// Not in database, return default built-in agent from registry
if builtinAgent := types.GetBuiltinAgent(id, tenantID); builtinAgent != nil {
// Not in database, return default built-in agent from registry (i18n-aware)
if builtinAgent := types.GetBuiltinAgentWithContext(ctx, id, tenantID); builtinAgent != nil {
return builtinAgent, nil
}
}
@@ -179,8 +179,8 @@ func (s *customAgentService) ListAgents(ctx context.Context) ([]*types.CustomAge
}
}
} else {
// Use default built-in agent
if agent := types.GetBuiltinAgent(builtinID, tenantID); agent != nil {
// Use default built-in agent (i18n-aware)
if agent := types.GetBuiltinAgentWithContext(ctx, builtinID, tenantID); agent != nil {
result = append(result, agent)
}
}
@@ -258,8 +258,8 @@ func (s *customAgentService) UpdateAgent(ctx context.Context, agent *types.Custo
// updateBuiltinAgent updates a built-in agent's configuration (but not basic info)
func (s *customAgentService) updateBuiltinAgent(ctx context.Context, agent *types.CustomAgent, tenantID uint64) (*types.CustomAgent, error) {
// Get the default built-in agent from registry
defaultAgent := types.GetBuiltinAgent(agent.ID, tenantID)
// Get the default built-in agent from registry (i18n-aware)
defaultAgent := types.GetBuiltinAgentWithContext(ctx, agent.ID, tenantID)
if defaultAgent == nil {
return nil, ErrAgentNotFound
}
+4 -1
View File
@@ -1873,11 +1873,14 @@ func (s *knowledgeService) getSummary(ctx context.Context,
}
// Generate summary using AI model
summaryPrompt := types.RenderPromptPlaceholders(s.config.Conversation.GenerateSummaryPrompt, types.PlaceholderValues{
"language": types.LanguageNameFromContext(ctx),
})
thinking := false
summary, err := summaryModel.Chat(ctx, []chat.Message{
{
Role: "system",
Content: s.config.Conversation.GenerateSummaryPrompt,
Content: summaryPrompt,
},
{
Role: "user",
+4 -1
View File
@@ -404,9 +404,12 @@ func (s *sessionService) GenerateTitle(ctx context.Context,
}
// Prepare messages for title generation
titlePrompt := types.RenderPromptPlaceholders(s.cfg.Conversation.GenerateSessionTitlePrompt, types.PlaceholderValues{
"language": types.LanguageNameFromContext(ctx),
})
var chatMessages []chat.Message
chatMessages = append(chatMessages,
chat.Message{Role: "system", Content: s.cfg.Conversation.GenerateSessionTitlePrompt},
chat.Message{Role: "system", Content: titlePrompt},
)
chatMessages = append(chatMessages,
chat.Message{Role: "user", Content: message.Content},
@@ -124,6 +124,7 @@ func (s *sessionService) KnowledgeQA(
ImageDescription: req.ImageDescription,
VLMModelID: vlmModelID,
ChatModelSupportsVision: chatModelSupportsVision,
Language: types.LanguageNameFromContext(ctx),
}
// Apply custom agent overrides (system prompt, temperature, retrieval params,
@@ -729,7 +730,10 @@ func (s *sessionService) renderFallbackPrompt(ctx context.Context, chatManage *t
if rq := strings.TrimSpace(chatManage.RewriteQuery); rq != "" {
query = rq
}
result := strings.ReplaceAll(chatManage.FallbackPrompt, "{{query}}", query)
result := types.RenderPromptPlaceholders(chatManage.FallbackPrompt, types.PlaceholderValues{
"query": query,
"language": chatManage.Language,
})
if chatManage.ImageDescription != "" && !chatManage.ChatModelSupportsVision {
result += "\n\n[用户上传图片内容]\n" + chatManage.ImageDescription