feat: Add web search functionality and enhance agent mode integration
- Introduced web search configuration in config.yaml, allowing multiple providers with specific settings. - Updated InputField component to manage agent mode and web search toggles, improving user interaction. - Enhanced chat API to include web search status in requests, enabling dynamic query handling. - Implemented backend support for web search integration, merging results with knowledge base queries. - Added tenant-level web search configuration management, allowing customization per tenant.
This commit is contained in:
@@ -37,6 +37,10 @@ type ChatManage struct {
|
||||
// Event system for streaming responses
|
||||
EventBus EventBusInterface `json:"-"` // EventBus for emitting streaming events
|
||||
MessageID string `json:"-"` // Assistant message ID for event emission
|
||||
|
||||
// Web search configuration (internal use)
|
||||
TenantID uint `json:"-"` // Tenant ID for retrieving web search config
|
||||
WebSearchEnabled bool `json:"-"` // Whether web search is enabled for this request
|
||||
}
|
||||
|
||||
// Clone creates a deep copy of the ChatManage object
|
||||
|
||||
@@ -20,6 +20,7 @@ const (
|
||||
MatchTypeParentChunk // 父Chunk匹配类型
|
||||
MatchTypeRelationChunk // 关系Chunk匹配类型
|
||||
MatchTypeGraph
|
||||
MatchTypeWebSearch // 网络搜索匹配类型
|
||||
)
|
||||
|
||||
// IndexInfo contains information about indexed content
|
||||
|
||||
@@ -29,9 +29,10 @@ type SessionService interface {
|
||||
// KnowledgeQA performs knowledge-based question answering
|
||||
// knowledgeBaseIDs: list of knowledge base IDs to search (supports multi-KB)
|
||||
// summaryModelID: optional summary model ID override (if empty, uses session/KB default)
|
||||
// webSearchEnabled: whether to enable web search to supplement knowledge base results
|
||||
// Events are emitted through eventBus (references, answer chunks, completion)
|
||||
KnowledgeQA(ctx context.Context,
|
||||
session *types.Session, query string, knowledgeBaseIDs []string, assistantMessageID string, summaryModelID string, eventBus *event.EventBus,
|
||||
session *types.Session, query string, knowledgeBaseIDs []string, assistantMessageID string, summaryModelID string, webSearchEnabled bool, eventBus *event.EventBus,
|
||||
) error
|
||||
// KnowledgeQAByEvent performs knowledge-based question answering by event
|
||||
KnowledgeQAByEvent(ctx context.Context, chatManage *types.ChatManage, eventList []types.EventType) error
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// WebSearchService defines the interface for web search service
|
||||
type WebSearchService interface {
|
||||
Search(ctx context.Context, config *types.WebSearchConfig, query string) ([]*types.WebSearchResult, error)
|
||||
}
|
||||
@@ -32,6 +32,8 @@ type Tenant struct {
|
||||
AgentConfig *AgentConfig `yaml:"agent_config" json:"agent_config" gorm:"type:jsonb"`
|
||||
// Global Context configuration for this tenant (default for all sessions)
|
||||
ContextConfig *ContextConfig `yaml:"context_config" json:"context_config" gorm:"type:jsonb"`
|
||||
// Global WebSearch configuration for this tenant
|
||||
WebSearchConfig *WebSearchConfig `yaml:"web_search_config" json:"web_search_config" gorm:"type:jsonb"`
|
||||
// Creation time
|
||||
CreatedAt time.Time `yaml:"created_at" json:"created_at"`
|
||||
// Last updated time
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
// WebSearchConfig represents the web search configuration for a tenant
|
||||
type WebSearchConfig struct {
|
||||
Provider string `json:"provider"` // 搜索引擎提供商ID
|
||||
APIKey string `json:"api_key"` // API密钥(如果需要)
|
||||
MaxResults int `json:"max_results"` // 最大搜索结果数
|
||||
IncludeDate bool `json:"include_date"` // 是否包含日期
|
||||
CompressionMethod string `json:"compression_method"` // 压缩方法:none, summary, extract, rag
|
||||
Blacklist []string `json:"blacklist"` // 黑名单规则列表
|
||||
// RAG压缩相关配置
|
||||
EmbeddingModelID string `json:"embedding_model_id,omitempty"` // 嵌入模型ID(用于RAG压缩)
|
||||
EmbeddingDimension int `json:"embedding_dimension,omitempty"` // 嵌入维度(用于RAG压缩)
|
||||
RerankModelID string `json:"rerank_model_id,omitempty"` // 重排模型ID(用于RAG压缩)
|
||||
DocumentFragments int `json:"document_fragments,omitempty"` // 文档片段数量(用于RAG压缩)
|
||||
}
|
||||
|
||||
// Value implements driver.Valuer interface for WebSearchConfig
|
||||
func (c WebSearchConfig) Value() (driver.Value, error) {
|
||||
return json.Marshal(c)
|
||||
}
|
||||
|
||||
// Scan implements sql.Scanner interface for WebSearchConfig
|
||||
func (c *WebSearchConfig) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
b, ok := value.([]byte)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(b, c)
|
||||
}
|
||||
|
||||
// WebSearchResult represents a single web search result
|
||||
type WebSearchResult struct {
|
||||
Title string `json:"title"` // 搜索结果标题
|
||||
URL string `json:"url"` // 结果URL
|
||||
Snippet string `json:"snippet"` // 摘要片段
|
||||
Content string `json:"content"` // 完整内容(可选,需要额外抓取)
|
||||
Source string `json:"source"` // 来源(如:kuaisou, baidu等)
|
||||
PublishedAt *time.Time `json:"published_at,omitempty"` // 发布时间(如果有)
|
||||
}
|
||||
|
||||
// WebSearchProviderInfo represents information about a web search provider
|
||||
type WebSearchProviderInfo struct {
|
||||
ID string `json:"id"` // 提供商ID
|
||||
Name string `json:"name"` // 提供商名称
|
||||
Free bool `json:"free"` // 是否免费
|
||||
RequiresAPIKey bool `json:"requires_api_key"` // 是否需要API密钥
|
||||
Description string `json:"description"` // 描述
|
||||
APIURL string `json:"api_url,omitempty"` // API地址(可选)
|
||||
}
|
||||
Reference in New Issue
Block a user