feat: Update web search providers and enhance knowledge base management

- Added DuckDuckGo as a new web search provider in the configuration.
- Removed deprecated web search providers (Baidu, Bing, Bocha, Exa, Google, Kuaisou, Searxng, Tavily, Zhipu) to streamline the codebase.
- Enhanced knowledge base management by introducing temporary knowledge base handling in the session service.
- Updated the web search service to support RAG-based compression for improved search result processing.
- Refactored session and knowledge service interfaces to accommodate new functionalities.
This commit is contained in:
wizardchen
2025-11-10 22:45:27 +08:00
parent ac8c8e9af2
commit 244aaab328
40 changed files with 968 additions and 1027 deletions
+228 -59
View File
@@ -7,23 +7,238 @@ import (
"strings"
"time"
"github.com/Tencent/WeKnora/internal/application/service/web_search"
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
// WebSearchProvider defines the interface for web search providers
type WebSearchProvider interface {
Search(ctx context.Context, query string, maxResults int, includeDate bool) ([]*types.WebSearchResult, error)
Name() string
}
// WebSearchService provides web search functionality
type WebSearchService struct {
providers map[string]WebSearchProvider
providers map[string]interfaces.WebSearchProvider
config *config.WebSearchConfig
}
// CompressWithRAG performs RAG-based compression using a temporary, hidden knowledge base.
// The temporary knowledge base is deleted after use. The UI will not list it due to repo filtering.
func (s *WebSearchService) CompressWithRAG(
ctx context.Context, sessionID string, tempKBID string, questions []string,
webSearchResults []*types.WebSearchResult, cfg *types.WebSearchConfig,
kbSvc interfaces.KnowledgeBaseService, knowSvc interfaces.KnowledgeService,
seenURLs map[string]bool, knowledgeIDs []string,
) (compressed []*types.WebSearchResult, kbID string, newSeen map[string]bool, newIDs []string, err error) {
if len(webSearchResults) == 0 || len(questions) == 0 {
return
}
if cfg == nil {
return nil, tempKBID, seenURLs, knowledgeIDs, fmt.Errorf("web search config is required for RAG compression")
}
if cfg.EmbeddingModelID == "" {
return nil, tempKBID, seenURLs, knowledgeIDs, fmt.Errorf("embedding_model_id is required for RAG compression")
}
var createdKB *types.KnowledgeBase
// reuse or create temp KB
if strings.TrimSpace(tempKBID) != "" {
createdKB, err = kbSvc.GetKnowledgeBaseByID(ctx, tempKBID)
if err != nil {
logger.Warnf(ctx, "Temp KB %s not available, recreating: %v", tempKBID, err)
createdKB = nil
}
}
if createdKB == nil {
kb := &types.KnowledgeBase{
Name: fmt.Sprintf("tmp-websearch-%d", time.Now().UnixNano()),
Description: "Ephemeral search compression KB",
IsTemporary: true,
EmbeddingModelID: cfg.EmbeddingModelID,
RerankModelID: cfg.RerankModelID,
}
createdKB, err = kbSvc.CreateKnowledgeBase(ctx, kb)
if err != nil {
return nil, tempKBID, seenURLs, knowledgeIDs, fmt.Errorf("failed to create temporary knowledge base: %w", err)
}
tempKBID = createdKB.ID
}
// Ingest all web results as passages synchronously
// dedupe by URL across queries within the same temp KB for this request/session
if seenURLs == nil {
seenURLs = map[string]bool{}
}
for _, r := range webSearchResults {
sourceURL := r.URL
title := strings.TrimSpace(r.Title)
snippet := strings.TrimSpace(r.Snippet)
body := strings.TrimSpace(r.Content)
// skip if already ingested for this KB
if sourceURL != "" && seenURLs[sourceURL] {
continue
}
contentLines := make([]string, 0, 4)
contentLines = append(contentLines, fmt.Sprintf("[sourceUrl]: %s", sourceURL))
if title != "" {
contentLines = append(contentLines, title)
}
if snippet != "" {
contentLines = append(contentLines, snippet)
}
if body != "" {
contentLines = append(contentLines, body)
}
knowledge, err := knowSvc.CreateKnowledgeFromPassageSync(ctx, createdKB.ID, contentLines)
if err != nil {
logger.Warnf(ctx, "failed to ingest passage into temp KB: %v", err)
continue
}
if sourceURL != "" {
seenURLs[sourceURL] = true
}
knowledgeIDs = append(knowledgeIDs, knowledge.ID)
}
// Retrieve references for questions
matchCount := cfg.DocumentFragments
if matchCount <= 0 {
matchCount = 3
}
var allRefs []*types.SearchResult
for _, q := range questions {
params := types.SearchParams{
QueryText: q,
VectorThreshold: 0.5,
KeywordThreshold: 0.5,
MatchCount: matchCount,
}
results, err := kbSvc.HybridSearch(ctx, tempKBID, params)
if err != nil {
logger.Warnf(ctx, "hybrid search failed for temp KB: %v", err)
continue
}
allRefs = append(allRefs, results...)
}
// Round-robin select references across the original results by source URL
selected := s.selectReferencesRoundRobin(webSearchResults, allRefs, matchCount*len(webSearchResults))
// Consolidate by URL back into the web results
compressedResults := s.consolidateReferencesByURL(webSearchResults, selected)
return compressedResults, tempKBID, seenURLs, knowledgeIDs, nil
}
// selectReferencesRoundRobin selects up to limit references, distributing fairly across source URLs.
func (s *WebSearchService) selectReferencesRoundRobin(
raw []*types.WebSearchResult,
refs []*types.SearchResult,
limit int,
) []*types.SearchResult {
if limit <= 0 || len(refs) == 0 {
return nil
}
// group refs by url marker in content
urlToRefs := map[string][]*types.SearchResult{}
for _, r := range refs {
url := extractSourceURLFromContent(r.Content)
if url == "" {
continue
}
urlToRefs[url] = append(urlToRefs[url], r)
}
// preserve order based on raw results
order := make([]string, 0, len(raw))
seen := map[string]bool{}
for _, r := range raw {
if r.URL != "" && !seen[r.URL] {
order = append(order, r.URL)
seen[r.URL] = true
}
}
var out []*types.SearchResult
for len(out) < limit {
progress := false
for _, url := range order {
if len(out) >= limit {
break
}
list := urlToRefs[url]
if len(list) == 0 {
continue
}
out = append(out, list[0])
urlToRefs[url] = list[1:]
progress = true
}
if !progress {
break
}
}
return out
}
// consolidateReferencesByURL merges selected references back into the original results grouped by URL.
func (s *WebSearchService) consolidateReferencesByURL(
raw []*types.WebSearchResult,
selected []*types.SearchResult,
) []*types.WebSearchResult {
if len(selected) == 0 {
return raw
}
agg := map[string][]string{}
for _, ref := range selected {
url := extractSourceURLFromContent(ref.Content)
if url == "" {
continue
}
// strip the first marker line to avoid duplication
agg[url] = append(agg[url], stripMarker(ref.Content))
}
// build outputs, preserving raw ordering and metadata
out := make([]*types.WebSearchResult, 0, len(raw))
for _, r := range raw {
parts := agg[r.URL]
if len(parts) == 0 {
out = append(out, r)
continue
}
merged := strings.Join(parts, "\n---\n")
out = append(out, &types.WebSearchResult{
Title: r.Title,
URL: r.URL,
Snippet: r.Snippet,
Content: merged,
Source: r.Source,
PublishedAt: r.PublishedAt,
})
}
return out
}
func extractSourceURLFromContent(content string) string {
if content == "" {
return ""
}
lines := strings.Split(content, "\n")
if len(lines) == 0 {
return ""
}
first := strings.TrimSpace(lines[0])
const prefix = "[sourceUrl]: "
if strings.HasPrefix(first, prefix) {
return strings.TrimSpace(strings.TrimPrefix(first, prefix))
}
return ""
}
func stripMarker(content string) string {
lines := strings.Split(content, "\n")
if len(lines) == 0 {
return content
}
if strings.HasPrefix(strings.TrimSpace(lines[0]), "[sourceUrl]: ") {
return strings.Join(lines[1:], "\n")
}
return content
}
// Search performs web search using the specified provider
// This method implements the interface expected by PluginSearch
func (s *WebSearchService) Search(ctx context.Context, config *types.WebSearchConfig, query string) ([]*types.WebSearchResult, error) {
@@ -36,11 +251,6 @@ func (s *WebSearchService) Search(ctx context.Context, config *types.WebSearchCo
return nil, fmt.Errorf("web search provider %s is not available", config.Provider)
}
// Set API key for providers that need it
if config.APIKey != "" {
s.setProviderAPIKey(config.Provider, provider, config)
}
// Set timeout
timeout := time.Duration(s.config.Timeout) * time.Second
if timeout == 0 {
@@ -75,44 +285,24 @@ func NewWebSearchService(cfg *config.Config) (*WebSearchService, error) {
}
service := &WebSearchService{
providers: make(map[string]WebSearchProvider),
providers: make(map[string]interfaces.WebSearchProvider),
config: cfg.WebSearch,
}
// Initialize providers based on config
for _, providerConfig := range cfg.WebSearch.Providers {
var provider WebSearchProvider
var provider interfaces.WebSearchProvider
var err error
switch providerConfig.ID {
case "kuaisou":
provider, err = NewKuaisouProvider(providerConfig)
case "baidu":
provider, err = NewBaiduProvider(providerConfig)
case "google":
provider, err = NewGoogleProvider(providerConfig)
case "bing":
provider, err = NewBingProvider(providerConfig)
case "bocha":
provider, err = NewBochaProvider(providerConfig)
case "zhipu":
provider, err = NewZhipuProvider(providerConfig)
case "tavily":
provider, err = NewTavilyProvider(providerConfig)
case "searxng":
provider, err = NewSearxngProvider(providerConfig)
case "exa":
provider, err = NewExaProvider(providerConfig)
case "duckduckgo":
provider, err = web_search.NewDuckDuckGoProvider(providerConfig)
default:
logger.Warnf(context.Background(), "Unknown web search provider: %s", providerConfig.ID)
continue
return nil, fmt.Errorf("unknown web search provider: %s", providerConfig.ID)
}
if err != nil {
logger.Warnf(context.Background(), "Failed to initialize provider %s: %v", providerConfig.ID, err)
continue
return nil, fmt.Errorf("failed to initialize provider %s: %v", providerConfig.ID, err)
}
service.providers[providerConfig.ID] = provider
logger.Infof(context.Background(), "Initialized web search provider: %s", providerConfig.ID)
}
@@ -120,27 +310,6 @@ func NewWebSearchService(cfg *config.Config) (*WebSearchService, error) {
return service, nil
}
// setProviderAPIKey sets the API key for a provider based on its type
func (s *WebSearchService) setProviderAPIKey(providerID string, provider WebSearchProvider, config *types.WebSearchConfig) {
switch p := provider.(type) {
case *KuaisouProvider:
p.SetAPIKey(config.APIKey)
case *BochaProvider:
p.SetAPIKey(config.APIKey)
case *ZhipuProvider:
p.SetAPIKey(config.APIKey)
case *TavilyProvider:
p.SetAPIKey(config.APIKey)
case *ExaProvider:
p.SetAPIKey(config.APIKey)
case *GoogleProvider:
// Google needs both API key and search engine ID
// For now, we'll use API key as search engine ID if not provided separately
// This can be extended later to support separate fields
p.SetAPIKey(config.APIKey, config.APIKey) // TODO: Add search engine ID to config
}
}
// filterBlacklist filters results based on blacklist rules
func (s *WebSearchService) filterBlacklist(results []*types.WebSearchResult, blacklist []string) []*types.WebSearchResult {
if len(blacklist) == 0 {