d4d005422d
Added functionality to enable updates for the `wiki_config` field in the `KnowledgeBaseConfig` struct. This includes: 1. **Type Definition Update**: Added `WikiConfig` field to `KnowledgeBaseConfig` to allow JSON unmarshaling during update requests. 2. **Service Layer Enhancement**: Updated the `UpdateKnowledgeBase` method to persist changes to `wiki_config` when provided in the request. 3. **Frontend TypeScript Adjustments**: Enhanced type definitions in the frontend to support `wiki_config`, improving IDE autocomplete and type checking. This fix addresses the critical issue where `wiki_config` was not being updated due to its absence in the `KnowledgeBaseConfig` struct, ensuring proper handling of wiki configurations during knowledge base updates.
90 lines
2.7 KiB
Go
90 lines
2.7 KiB
Go
package chatpipeline
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/Tencent/WeKnora/internal/logger"
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
)
|
|
|
|
// wikiBoostFactor is the score multiplier applied to wiki page chunks.
|
|
// Wiki pages contain LLM-synthesized, cross-referenced knowledge and should
|
|
// be preferred over raw document chunks when both are available.
|
|
const wikiBoostFactor = 1.3
|
|
|
|
// PluginWikiBoost boosts the relevance score of wiki page chunks in search results.
|
|
// Wiki pages contain pre-synthesized knowledge that is more coherent and
|
|
// cross-referenced than raw document chunks, so they should rank higher.
|
|
//
|
|
// This plugin runs in the CHUNK_RERANK phase, after initial retrieval and reranking.
|
|
// It identifies chunks with ChunkType == "wiki_page" and multiplies their score.
|
|
type PluginWikiBoost struct {
|
|
kbService interfaces.KnowledgeBaseService
|
|
}
|
|
|
|
// NewPluginWikiBoost creates and registers the wiki boost plugin
|
|
func NewPluginWikiBoost(eventManager *EventManager, kbService interfaces.KnowledgeBaseService) *PluginWikiBoost {
|
|
p := &PluginWikiBoost{
|
|
kbService: kbService,
|
|
}
|
|
eventManager.Register(p)
|
|
return p
|
|
}
|
|
|
|
// ActivationEvents returns the event types this plugin handles
|
|
func (p *PluginWikiBoost) ActivationEvents() []types.EventType {
|
|
return []types.EventType{types.CHUNK_RERANK}
|
|
}
|
|
|
|
// OnEvent boosts wiki page chunk scores after reranking
|
|
func (p *PluginWikiBoost) OnEvent(
|
|
ctx context.Context,
|
|
eventType types.EventType,
|
|
chatManage *types.ChatManage,
|
|
next func() *PluginError,
|
|
) *PluginError {
|
|
// Run the normal reranking first
|
|
if err := next(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Check if any search target is a wiki KB
|
|
hasWikiKB := false
|
|
for _, target := range chatManage.SearchTargets {
|
|
kb, err := p.kbService.GetKnowledgeBaseByIDOnly(ctx, target.KnowledgeBaseID)
|
|
if err == nil && kb.IsWikiEnabled() {
|
|
hasWikiKB = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !hasWikiKB {
|
|
return nil
|
|
}
|
|
|
|
// Boost wiki page chunks in RerankResult
|
|
boostedCount := 0
|
|
for i := range chatManage.RerankResult {
|
|
if chatManage.RerankResult[i].ChunkType == types.ChunkTypeWikiPage {
|
|
chatManage.RerankResult[i].Score *= wikiBoostFactor
|
|
boostedCount++
|
|
}
|
|
}
|
|
|
|
if boostedCount > 0 {
|
|
logger.Infof(ctx, "WikiBoost: boosted %d wiki page chunks by %.1fx", boostedCount, wikiBoostFactor)
|
|
|
|
// Re-sort by score after boosting
|
|
for i := 0; i < len(chatManage.RerankResult); i++ {
|
|
for j := i + 1; j < len(chatManage.RerankResult); j++ {
|
|
if chatManage.RerankResult[j].Score > chatManage.RerankResult[i].Score {
|
|
chatManage.RerankResult[i], chatManage.RerankResult[j] = chatManage.RerankResult[j], chatManage.RerankResult[i]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|