feat(llm): introduce request-local source reference aliases
Replace raw chunk/document/KB IDs in LLM context with short cN/dN/bN/wN aliases, decode tool arguments on the way in, and expand compact <ref/> citations to the public <kb>/<web> format before user-facing output.
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/Tencent/WeKnora/internal/llmreference"
|
||||
"github.com/Tencent/WeKnora/internal/llmresource"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
@@ -55,8 +56,9 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
||||
|
||||
// Prepare base messages without history
|
||||
|
||||
chatMessages := prepareMessagesWithHistory(chatManage)
|
||||
chatMessages, sourceRefs := prepareMessagesWithReferences(ctx, chatManage)
|
||||
resourceRefs := llmresource.NewRegistry()
|
||||
chatMessages = sourceRefs.EncodeMessages(chatMessages)
|
||||
chatMessages = resourceRefs.EncodeMessages(chatMessages)
|
||||
pipelineInfo(ctx, "Stream", "messages_ready", map[string]interface{}{
|
||||
"message_count": len(chatMessages),
|
||||
@@ -110,6 +112,8 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
||||
go func() {
|
||||
answerDecoder := llmresource.NewStreamDecoder(resourceRefs)
|
||||
thinkingDecoder := llmresource.NewStreamDecoder(resourceRefs)
|
||||
answerRefExpander := llmreference.NewStreamExpander(sourceRefs)
|
||||
thinkingRefExpander := llmreference.NewStreamExpander(sourceRefs)
|
||||
thinkingID := fmt.Sprintf("%s-thinking", uuid.New().String()[:8])
|
||||
answerID := fmt.Sprintf("%s-answer", uuid.New().String()[:8])
|
||||
thinkingOpen := false
|
||||
@@ -135,20 +139,22 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
||||
// reference in flight at teardown is silently dropped (and never
|
||||
// persisted, since the assistant message is saved from these events).
|
||||
flushDecoders := func() {
|
||||
if tail := thinkingDecoder.Flush(); tail != "" {
|
||||
thinkingTail := thinkingRefExpander.Feed(thinkingDecoder.Flush()) + thinkingRefExpander.Flush()
|
||||
if thinkingTail != "" {
|
||||
_ = eventBus.Emit(ctx, types.Event{
|
||||
ID: thinkingID,
|
||||
Type: types.EventType(event.EventAgentThought),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentThoughtData{Content: tail},
|
||||
Data: event.AgentThoughtData{Content: thinkingTail},
|
||||
})
|
||||
}
|
||||
if tail := answerDecoder.Flush(); tail != "" {
|
||||
answerTail := answerRefExpander.Feed(answerDecoder.Flush()) + answerRefExpander.Flush()
|
||||
if answerTail != "" {
|
||||
_ = eventBus.Emit(ctx, types.Event{
|
||||
ID: answerID,
|
||||
Type: types.EventType(event.EventAgentFinalAnswer),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentFinalAnswerData{Content: tail},
|
||||
Data: event.AgentFinalAnswerData{Content: answerTail},
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -192,7 +198,7 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
||||
}
|
||||
|
||||
if response.ResponseType == types.ResponseTypeThinking {
|
||||
response.Content = thinkingDecoder.Feed(response.Content)
|
||||
response.Content = thinkingRefExpander.Feed(thinkingDecoder.Feed(response.Content))
|
||||
if response.Content != "" {
|
||||
thinkingOpen = true
|
||||
eventBus.Emit(ctx, types.Event{
|
||||
@@ -212,7 +218,7 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
||||
}
|
||||
|
||||
if response.ResponseType == types.ResponseTypeAnswer {
|
||||
response.Content = answerDecoder.Feed(response.Content)
|
||||
response.Content = answerRefExpander.Feed(answerDecoder.Feed(response.Content))
|
||||
closeThinking()
|
||||
eventBus.Emit(ctx, types.Event{
|
||||
ID: answerID,
|
||||
|
||||
Reference in New Issue
Block a user