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 {
|
if chatManage.ImageDescription != "" && !chatManage.ChatModelSupportsVision {
|
||||||
userContent += "\n\n[用户上传图片内容]\n" + chatManage.ImageDescription
|
userContent += "\n\n[用户上传图片内容]\n" + chatManage.ImageDescription
|
||||||
}
|
}
|
||||||
|
if chatManage.QuotedContext != "" {
|
||||||
|
userContent += "\n\n" + chatManage.QuotedContext
|
||||||
|
}
|
||||||
|
|
||||||
if tpl := chatManage.SummaryConfig.ContextTemplate; tpl != "" {
|
if tpl := chatManage.SummaryConfig.ContextTemplate; tpl != "" {
|
||||||
chatManage.UserContent = types.RenderPromptPlaceholders(tpl, types.PlaceholderValues{
|
chatManage.UserContent = types.RenderPromptPlaceholders(tpl, types.PlaceholderValues{
|
||||||
@@ -174,6 +177,9 @@ func (p *PluginIntoChatMessage) OnEvent(ctx context.Context,
|
|||||||
if chatManage.ImageDescription != "" && !chatManage.ChatModelSupportsVision {
|
if chatManage.ImageDescription != "" && !chatManage.ChatModelSupportsVision {
|
||||||
userContent += "\n\n[用户上传图片内容]\n" + chatManage.ImageDescription
|
userContent += "\n\n[用户上传图片内容]\n" + chatManage.ImageDescription
|
||||||
}
|
}
|
||||||
|
if chatManage.QuotedContext != "" {
|
||||||
|
userContent += "\n\n" + chatManage.QuotedContext
|
||||||
|
}
|
||||||
|
|
||||||
// Set formatted content back to chat management
|
// Set formatted content back to chat management
|
||||||
chatManage.UserContent = userContent
|
chatManage.UserContent = userContent
|
||||||
|
|||||||
@@ -174,6 +174,9 @@ func (s *sessionService) AgentQA(
|
|||||||
agentQuery = req.Query + "\n\n[用户上传图片内容]\n" + req.ImageDescription
|
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))
|
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)
|
// Execute agent with streaming (asynchronously)
|
||||||
// Events will be emitted to EventBus and handled by the Handler layer
|
// Events will be emitted to EventBus and handled by the Handler layer
|
||||||
|
|||||||
@@ -126,6 +126,7 @@ func (s *sessionService) KnowledgeQA(
|
|||||||
PipelineState: types.PipelineState{
|
PipelineState: types.PipelineState{
|
||||||
RewriteQuery: req.Query,
|
RewriteQuery: req.Query,
|
||||||
ImageDescription: req.ImageDescription,
|
ImageDescription: req.ImageDescription,
|
||||||
|
QuotedContext: req.QuotedContext,
|
||||||
},
|
},
|
||||||
PipelineContext: types.PipelineContext{
|
PipelineContext: types.PipelineContext{
|
||||||
EventBus: eventBus.AsEventBusInterface(),
|
EventBus: eventBus.AsEventBusInterface(),
|
||||||
@@ -150,6 +151,9 @@ func (s *sessionService) KnowledgeQA(
|
|||||||
if req.ImageDescription != "" && !chatModelSupportsVision {
|
if req.ImageDescription != "" && !chatModelSupportsVision {
|
||||||
userContent += "\n\n[用户上传图片内容]\n" + req.ImageDescription
|
userContent += "\n\n[用户上传图片内容]\n" + req.ImageDescription
|
||||||
}
|
}
|
||||||
|
if req.QuotedContext != "" {
|
||||||
|
userContent += "\n\n" + req.QuotedContext
|
||||||
|
}
|
||||||
chatManage.UserContent = userContent
|
chatManage.UserContent = userContent
|
||||||
|
|
||||||
pipeline = types.NewPipelineBuilder().
|
pipeline = types.NewPipelineBuilder().
|
||||||
@@ -732,6 +736,9 @@ func (s *sessionService) renderFallbackPrompt(ctx context.Context, chatManage *t
|
|||||||
if chatManage.ImageDescription != "" && !chatManage.ChatModelSupportsVision {
|
if chatManage.ImageDescription != "" && !chatManage.ChatModelSupportsVision {
|
||||||
result += "\n\n[用户上传图片内容]\n" + chatManage.ImageDescription
|
result += "\n\n[用户上传图片内容]\n" + chatManage.ImageDescription
|
||||||
}
|
}
|
||||||
|
if chatManage.QuotedContext != "" {
|
||||||
|
result += "\n\n" + chatManage.QuotedContext
|
||||||
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -193,6 +193,12 @@ func buildIMQARequest(
|
|||||||
// frontend toggle; for IM channels the user has no per-message toggle,
|
// 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).
|
// so we derive it from the agent config (the single source of truth).
|
||||||
webSearchEnabled := customAgent != nil && customAgent.Config.WebSearchEnabled
|
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{
|
return &types.QARequest{
|
||||||
Session: session,
|
Session: session,
|
||||||
Query: query,
|
Query: query,
|
||||||
@@ -201,7 +207,7 @@ func buildIMQARequest(
|
|||||||
KnowledgeBaseIDs: kbIDs,
|
KnowledgeBaseIDs: kbIDs,
|
||||||
UserMessageID: userMessageID,
|
UserMessageID: userMessageID,
|
||||||
WebSearchEnabled: webSearchEnabled,
|
WebSearchEnabled: webSearchEnabled,
|
||||||
QuotedContext: formatQuotedContext(quote),
|
QuotedContext: quotedContext,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package im
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFormatQuotedContext(t *testing.T) {
|
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