72dfb9ce75
- Updated context template to include runtime metadata such as current time and week, improving contextual awareness in user queries. - Enhanced rewrite template with critical instructions for intent classification, ensuring that rewritten questions preserve essential entities and keywords. - Refined intent classification logic to prioritize user intents more effectively, improving the accuracy of responses based on user queries. - Added examples to clarify expected input and output formats for intent classification, enhancing usability for developers.
225 lines
6.9 KiB
Go
225 lines
6.9 KiB
Go
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 := types.RenderPromptPlaceholders(base, types.PlaceholderValues{
|
|
"query": chatManage.Query,
|
|
"language": chatManage.Language,
|
|
"contexts": chatManage.RenderedContexts,
|
|
})
|
|
|
|
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" {
|
|
if message.RenderedContent != "" {
|
|
h.Query = message.RenderedContent
|
|
} else {
|
|
h.Query = message.Content
|
|
}
|
|
h.CreateAt = message.CreatedAt
|
|
if desc := extractImageCaptions(message.Images); desc != "" && message.RenderedContent == "" {
|
|
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")
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 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
|
|
}
|