feat(pipeline): inject QuotedContext at LLM prompt stage
This commit is contained in:
@@ -92,6 +92,9 @@ func (p *PluginIntoChatMessage) OnEvent(ctx context.Context,
|
||||
if chatManage.ImageDescription != "" && !chatManage.ChatModelSupportsVision {
|
||||
userContent += "\n\n[用户上传图片内容]\n" + chatManage.ImageDescription
|
||||
}
|
||||
if chatManage.QuotedContext != "" {
|
||||
userContent += "\n\n" + chatManage.QuotedContext
|
||||
}
|
||||
|
||||
if tpl := chatManage.SummaryConfig.ContextTemplate; tpl != "" {
|
||||
chatManage.UserContent = types.RenderPromptPlaceholders(tpl, types.PlaceholderValues{
|
||||
@@ -174,6 +177,9 @@ func (p *PluginIntoChatMessage) OnEvent(ctx context.Context,
|
||||
if chatManage.ImageDescription != "" && !chatManage.ChatModelSupportsVision {
|
||||
userContent += "\n\n[用户上传图片内容]\n" + chatManage.ImageDescription
|
||||
}
|
||||
if chatManage.QuotedContext != "" {
|
||||
userContent += "\n\n" + chatManage.QuotedContext
|
||||
}
|
||||
|
||||
// Set formatted content back to chat management
|
||||
chatManage.UserContent = userContent
|
||||
|
||||
@@ -174,6 +174,9 @@ func (s *sessionService) AgentQA(
|
||||
agentQuery = req.Query + "\n\n[用户上传图片内容]\n" + req.ImageDescription
|
||||
logger.Infof(ctx, "Agent model does not support vision, appending image description (%d chars)", len(req.ImageDescription))
|
||||
}
|
||||
if req.QuotedContext != "" {
|
||||
agentQuery += "\n\n" + req.QuotedContext
|
||||
}
|
||||
|
||||
// Execute agent with streaming (asynchronously)
|
||||
// Events will be emitted to EventBus and handled by the Handler layer
|
||||
|
||||
@@ -126,6 +126,7 @@ func (s *sessionService) KnowledgeQA(
|
||||
PipelineState: types.PipelineState{
|
||||
RewriteQuery: req.Query,
|
||||
ImageDescription: req.ImageDescription,
|
||||
QuotedContext: req.QuotedContext,
|
||||
},
|
||||
PipelineContext: types.PipelineContext{
|
||||
EventBus: eventBus.AsEventBusInterface(),
|
||||
@@ -150,6 +151,9 @@ func (s *sessionService) KnowledgeQA(
|
||||
if req.ImageDescription != "" && !chatModelSupportsVision {
|
||||
userContent += "\n\n[用户上传图片内容]\n" + req.ImageDescription
|
||||
}
|
||||
if req.QuotedContext != "" {
|
||||
userContent += "\n\n" + req.QuotedContext
|
||||
}
|
||||
chatManage.UserContent = userContent
|
||||
|
||||
pipeline = types.NewPipelineBuilder().
|
||||
@@ -732,6 +736,9 @@ func (s *sessionService) renderFallbackPrompt(ctx context.Context, chatManage *t
|
||||
if chatManage.ImageDescription != "" && !chatManage.ChatModelSupportsVision {
|
||||
result += "\n\n[用户上传图片内容]\n" + chatManage.ImageDescription
|
||||
}
|
||||
if chatManage.QuotedContext != "" {
|
||||
result += "\n\n" + chatManage.QuotedContext
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -193,6 +193,12 @@ func buildIMQARequest(
|
||||
// frontend toggle; for IM channels the user has no per-message toggle,
|
||||
// so we derive it from the agent config (the single source of truth).
|
||||
webSearchEnabled := customAgent != nil && customAgent.Config.WebSearchEnabled
|
||||
quotedContext := formatQuotedContext(quote)
|
||||
if quotedContext != "" {
|
||||
// Note: using context.Background() because buildIMQARequest is a pure function without ctx.
|
||||
// This debug log won't carry trace/span IDs — acceptable for a debug-level log.
|
||||
logger.Debugf(context.Background(), "[IM] QuotedContext set: length=%d", len(quotedContext))
|
||||
}
|
||||
return &types.QARequest{
|
||||
Session: session,
|
||||
Query: query,
|
||||
@@ -201,7 +207,7 @@ func buildIMQARequest(
|
||||
KnowledgeBaseIDs: kbIDs,
|
||||
UserMessageID: userMessageID,
|
||||
WebSearchEnabled: webSearchEnabled,
|
||||
QuotedContext: formatQuotedContext(quote),
|
||||
QuotedContext: quotedContext,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package im
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
func TestFormatQuotedContext(t *testing.T) {
|
||||
@@ -52,3 +54,38 @@ func TestFormatQuotedContext(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildIMQARequest_QuotedContext(t *testing.T) {
|
||||
session := &types.Session{ID: "s1"}
|
||||
|
||||
t.Run("nil quote produces empty QuotedContext", func(t *testing.T) {
|
||||
req := buildIMQARequest(session, "hello", "a1", "u1", nil, nil, nil)
|
||||
if req.QuotedContext != "" {
|
||||
t.Errorf("QuotedContext = %q, want empty", req.QuotedContext)
|
||||
}
|
||||
if req.Query != "hello" {
|
||||
t.Errorf("Query = %q, want %q", req.Query, "hello")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("bot quote sets QuotedContext with bot label", func(t *testing.T) {
|
||||
quote := &QuotedMessage{Content: "bot reply", IsBotMessage: true}
|
||||
req := buildIMQARequest(session, "follow up", "a1", "u1", nil, nil, quote)
|
||||
if req.Query != "follow up" {
|
||||
t.Errorf("Query = %q, want %q", req.Query, "follow up")
|
||||
}
|
||||
want := "[引用的机器人回复]\nbot reply"
|
||||
if req.QuotedContext != want {
|
||||
t.Errorf("QuotedContext = %q, want %q", req.QuotedContext, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("user quote sets QuotedContext with user label", func(t *testing.T) {
|
||||
quote := &QuotedMessage{Content: "user msg", IsBotMessage: false}
|
||||
req := buildIMQARequest(session, "question", "a1", "u1", nil, nil, quote)
|
||||
want := "[被引用的消息]\nuser msg"
|
||||
if req.QuotedContext != want {
|
||||
t.Errorf("QuotedContext = %q, want %q", req.QuotedContext, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user