feat(prompts): update assistant descriptions and add intent-specific templates
- Updated the assistant descriptions in various prompt templates to specify that they are developed by Tencent. - Added new intent-specific system prompt templates for greeting, chitchat, follow-up, image analysis, summarization, and web search unavailability scenarios. - Enhanced citation rules in the system prompts to ensure proper inline citation formatting. - Refactored the rewrite prompt to classify user intent more accurately and output structured JSON responses.
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
package chatpipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/common"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
|
||||
var regThinkTags = regexp.MustCompile(`(?s)<think>.*?</think>`)
|
||||
|
||||
// pipelineInfo logs pipeline info level entries.
|
||||
func pipelineInfo(ctx context.Context, stage, action string, fields map[string]interface{}) {
|
||||
common.PipelineInfo(ctx, stage, action, fields)
|
||||
}
|
||||
|
||||
// pipelineWarn logs pipeline warning level entries.
|
||||
func pipelineWarn(ctx context.Context, stage, action string, fields map[string]interface{}) {
|
||||
common.PipelineWarn(ctx, stage, action, fields)
|
||||
}
|
||||
|
||||
// pipelineError logs pipeline error level entries.
|
||||
func pipelineError(ctx context.Context, stage, action string, fields map[string]interface{}) {
|
||||
common.PipelineError(ctx, stage, action, fields)
|
||||
}
|
||||
|
||||
// prepareChatModel shared logic to prepare chat model and options
|
||||
// it gets the chat model and sets up the chat options based on the chat manage.
|
||||
func prepareChatModel(ctx context.Context, modelService interfaces.ModelService,
|
||||
chatManage *types.ChatManage,
|
||||
) (chat.Chat, *chat.ChatOptions, error) {
|
||||
chatModel, err := modelService.GetChatModel(ctx, chatManage.ChatModelID)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to get chat model: %v", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
opt := &chat.ChatOptions{
|
||||
Temperature: chatManage.SummaryConfig.Temperature,
|
||||
TopP: chatManage.SummaryConfig.TopP,
|
||||
Seed: chatManage.SummaryConfig.Seed,
|
||||
MaxTokens: chatManage.SummaryConfig.MaxTokens,
|
||||
MaxCompletionTokens: chatManage.SummaryConfig.MaxCompletionTokens,
|
||||
FrequencyPenalty: chatManage.SummaryConfig.FrequencyPenalty,
|
||||
PresencePenalty: chatManage.SummaryConfig.PresencePenalty,
|
||||
Thinking: chatManage.SummaryConfig.Thinking,
|
||||
}
|
||||
|
||||
return chatModel, opt, nil
|
||||
}
|
||||
|
||||
// prepareMessagesWithHistory prepare complete messages including history.
|
||||
// When SystemPromptOverride is set (e.g. by intent-specific prompt logic),
|
||||
// it takes precedence over the default SummaryConfig.Prompt.
|
||||
func prepareMessagesWithHistory(chatManage *types.ChatManage) []chat.Message {
|
||||
base := chatManage.SummaryConfig.Prompt
|
||||
if chatManage.SystemPromptOverride != "" {
|
||||
base = chatManage.SystemPromptOverride
|
||||
}
|
||||
systemPrompt := renderSystemPromptPlaceholders(base, chatManage.Language)
|
||||
|
||||
chatMessages := []chat.Message{
|
||||
{Role: "system", Content: systemPrompt},
|
||||
}
|
||||
|
||||
// Add conversation history (already limited by maxRounds in load_history/rewrite plugins)
|
||||
for _, history := range chatManage.History {
|
||||
chatMessages = append(chatMessages, chat.Message{Role: "user", Content: history.Query})
|
||||
chatMessages = append(chatMessages, chat.Message{Role: "assistant", Content: history.Answer})
|
||||
}
|
||||
|
||||
// Add current user message. Only include images when the chat model supports
|
||||
// vision; non-vision models rely on the text description in UserContent.
|
||||
userMsg := chat.Message{Role: "user", Content: chatManage.UserContent}
|
||||
if chatManage.ChatModelSupportsVision && len(chatManage.Images) > 0 {
|
||||
userMsg.Images = chatManage.Images
|
||||
}
|
||||
chatMessages = append(chatMessages, userMsg)
|
||||
|
||||
return chatMessages
|
||||
}
|
||||
|
||||
// loadAndProcessHistory fetches recent messages, groups them into Q&A pairs,
|
||||
// strips <think> tags from assistant answers, sorts by recency, and limits to maxRounds.
|
||||
// fetchCount controls how many raw messages to fetch (typically maxRounds*2+10).
|
||||
func loadAndProcessHistory(
|
||||
ctx context.Context,
|
||||
messageService interfaces.MessageService,
|
||||
sessionID string,
|
||||
maxRounds int,
|
||||
fetchCount int,
|
||||
) ([]*types.History, error) {
|
||||
history, err := messageService.GetRecentMessagesBySession(ctx, sessionID, fetchCount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
historyMap := make(map[string]*types.History)
|
||||
for _, message := range history {
|
||||
h, ok := historyMap[message.RequestID]
|
||||
if !ok {
|
||||
h = &types.History{}
|
||||
}
|
||||
if message.Role == "user" {
|
||||
h.Query = message.Content
|
||||
h.CreateAt = message.CreatedAt
|
||||
if desc := extractImageCaptions(message.Images); desc != "" {
|
||||
h.Query += "\n\n[用户上传图片内容]\n" + desc
|
||||
}
|
||||
} else {
|
||||
h.Answer = regThinkTags.ReplaceAllString(message.Content, "")
|
||||
h.KnowledgeReferences = message.KnowledgeReferences
|
||||
}
|
||||
historyMap[message.RequestID] = h
|
||||
}
|
||||
|
||||
historyList := make([]*types.History, 0, len(historyMap))
|
||||
for _, h := range historyMap {
|
||||
if h.Answer != "" && h.Query != "" {
|
||||
historyList = append(historyList, h)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(historyList, func(i, j int) bool {
|
||||
return historyList[i].CreateAt.After(historyList[j].CreateAt)
|
||||
})
|
||||
|
||||
if len(historyList) > maxRounds {
|
||||
historyList = historyList[:maxRounds]
|
||||
}
|
||||
|
||||
slices.Reverse(historyList)
|
||||
return historyList, nil
|
||||
}
|
||||
|
||||
// extractImageCaptions concatenates non-empty Caption fields from stored
|
||||
// message images. Used when loading history so that previous turns' image
|
||||
// descriptions are visible to the model.
|
||||
func extractImageCaptions(images types.MessageImages) string {
|
||||
var parts []string
|
||||
for _, img := range images {
|
||||
if img.Caption != "" {
|
||||
parts = append(parts, img.Caption)
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, "\n")
|
||||
}
|
||||
|
||||
// renderSystemPromptPlaceholders replaces placeholders in system prompt
|
||||
// Supported placeholders:
|
||||
// - {{current_time}} -> current time in RFC3339 format
|
||||
// - {{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 types.RenderPromptPlaceholders(prompt, vals)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Concurrency utilities
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// ParallelTask represents a named unit of concurrent work.
|
||||
type ParallelTask struct {
|
||||
Name string
|
||||
Run func() *PluginError
|
||||
}
|
||||
|
||||
// RunParallel executes tasks concurrently.
|
||||
// Returns a map of task name → error for tasks that returned non-nil errors.
|
||||
func RunParallel(tasks ...ParallelTask) map[string]*PluginError {
|
||||
errs := make(map[string]*PluginError)
|
||||
var mu sync.Mutex
|
||||
var wg sync.WaitGroup
|
||||
|
||||
wg.Add(len(tasks))
|
||||
for _, task := range tasks {
|
||||
go func(t ParallelTask) {
|
||||
defer wg.Done()
|
||||
if err := t.Run(); err != nil {
|
||||
mu.Lock()
|
||||
errs[t.Name] = err
|
||||
mu.Unlock()
|
||||
}
|
||||
}(task)
|
||||
}
|
||||
wg.Wait()
|
||||
return errs
|
||||
}
|
||||
|
||||
// ParallelMap applies fn to each element of items concurrently (up to
|
||||
// maxWorkers goroutines) and returns results in the same order as items.
|
||||
// If maxWorkers <= 0, concurrency is unbounded (one goroutine per item).
|
||||
func ParallelMap[T, R any](items []T, maxWorkers int, fn func(int, T) R) []R {
|
||||
n := len(items)
|
||||
if n == 0 {
|
||||
return nil
|
||||
}
|
||||
results := make([]R, n)
|
||||
|
||||
if maxWorkers <= 0 || maxWorkers > n {
|
||||
maxWorkers = n
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
sem := make(chan struct{}, maxWorkers)
|
||||
|
||||
for i, item := range items {
|
||||
wg.Add(1)
|
||||
sem <- struct{}{}
|
||||
go func(idx int, it T) {
|
||||
defer func() { <-sem; wg.Done() }()
|
||||
results[idx] = fn(idx, it)
|
||||
}(i, item)
|
||||
}
|
||||
wg.Wait()
|
||||
return results
|
||||
}
|
||||
Reference in New Issue
Block a user