feat(llm): add prompt-cache observability and wiki cache-friendly prompts

Normalize provider-specific cache counters into TokenUsage, propagate call
purpose and prefix fingerprints through context for logging and Langfuse,
and restructure wiki page-modify prompts with warmup/singleflight to improve
prefix cache hit rates during reduce batches.
This commit is contained in:
wizardchen
2026-07-23 18:00:35 +08:00
committed by lyingbug
parent 6c794f64cf
commit 6fb858105a
36 changed files with 877 additions and 175 deletions
@@ -55,6 +55,7 @@ func (p *PluginChatCompletion) OnEvent(
resourceRefs := llmresource.NewRegistry()
chatMessages = sourceRefs.EncodeMessages(chatMessages)
chatMessages = resourceRefs.EncodeMessages(chatMessages)
ctx = withPromptCacheMetadata(ctx, chatModel, chatMessages, opt, "knowledge_qa")
// Call the chat model to generate response
pipelineInfo(ctx, "Completion", "model_call", map[string]interface{}{
@@ -60,6 +60,7 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
resourceRefs := llmresource.NewRegistry()
chatMessages = sourceRefs.EncodeMessages(chatMessages)
chatMessages = resourceRefs.EncodeMessages(chatMessages)
ctx = withPromptCacheMetadata(ctx, chatModel, chatMessages, opt, "knowledge_qa")
pipelineInfo(ctx, "Stream", "messages_ready", map[string]interface{}{
"message_count": len(chatMessages),
"system_prompt": chatMessages[0].Content,
@@ -113,6 +113,18 @@ func prepareMessagesWithHistory(chatManage *types.ChatManage) []chat.Message {
return chatMessages
}
func withPromptCacheMetadata(
ctx context.Context,
chatModel chat.Chat,
messages []chat.Message,
opts *chat.ChatOptions,
purpose string,
) context.Context {
prefixFingerprint := chat.PromptPrefixFingerprint(messages, opts)
_ = chatModel // model identity is already captured by the usage sink
return types.WithLLMCallMetadata(ctx, purpose, prefixFingerprint)
}
// AppendHistoryMessages appends prior Q&A rounds in chronological order.
// History is already filtered and truncated upstream by the load_history plugin.
func AppendHistoryMessages(messages []chat.Message, history []*types.History) []chat.Message {
@@ -119,7 +119,8 @@ If NO, leave the sql field empty.
Return your response in the specified JSON format.`, chatManage.Query, knowledge.ID, schema.Description())
response, err := chatModel.Chat(ctx, []chat.Message{
modelCtx := types.WithLLMCallMetadata(ctx, "data_analysis_plan", "")
response, err := chatModel.Chat(modelCtx, []chat.Message{
{Role: "user", Content: analysisPrompt},
}, &chat.ChatOptions{
Temperature: 0.1,
@@ -184,7 +184,8 @@ func (e *Extractor) Extract(ctx context.Context, content string) (*types.GraphDa
// logger.Debugf(ctx, "chat system: %s", generator.System(ctx))
// logger.Debugf(ctx, "chat user: %s", generator.User(ctx, content))
chatResponse, err := e.chat.Chat(ctx, generator.Render(ctx, content), e.chatOpt)
modelCtx := types.WithLLMCallMetadata(ctx, "entity_extraction", "")
chatResponse, err := e.chat.Chat(modelCtx, generator.Render(ctx, content), e.chatOpt)
if err != nil {
logger.Errorf(ctx, "failed to chat: %v", err)
return nil, err
@@ -114,7 +114,8 @@ func (p *PluginQueryUnderstand) OnEvent(ctx context.Context,
// --- Call model ---
thinking := false
response, err := rewriteModel.Chat(ctx, []chat.Message{
modelCtx := types.WithLLMCallMetadata(ctx, "query_rewrite", "")
response, err := rewriteModel.Chat(modelCtx, []chat.Message{
{Role: "system", Content: systemContent},
userMsg,
}, &chat.ChatOptions{