refactor: Remove agent_test command-line tool and associated files to streamline project structure and eliminate unused components
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package agent
|
||||
|
||||
const (
|
||||
DefaultAgentTemperature = 0.7
|
||||
DefaultAgentMaxIterations = 20
|
||||
// DefaultAgentTemperature is the default temperature for the agent
|
||||
DefaultAgentTemperature = 0.7
|
||||
// DefaultAgentMaxIterations is the default maximum number of iterations for the agent
|
||||
DefaultAgentMaxIterations = 20
|
||||
// DefaultAgentReflectionEnabled is the default whether to enable reflection for the agent
|
||||
DefaultAgentReflectionEnabled = false
|
||||
DefaultUseCustomSystemPrompt = false
|
||||
// DefaultUseCustomSystemPrompt is the default whether to use custom system prompt for the agent
|
||||
DefaultUseCustomSystemPrompt = false
|
||||
)
|
||||
|
||||
@@ -96,7 +96,7 @@ func (e *AgentEngine) Execute(
|
||||
}
|
||||
|
||||
// Build system prompt using progressive RAG prompt
|
||||
systemPrompt := BuildProgressiveRAGSystemPrompt(
|
||||
systemPrompt := BuildSystemPrompt(
|
||||
e.knowledgeBasesInfo,
|
||||
e.config.WebSearchEnabled,
|
||||
e.systemPromptTemplate,
|
||||
@@ -771,7 +771,7 @@ func (e *AgentEngine) streamFinalAnswerToEventBus(
|
||||
})
|
||||
|
||||
// Build messages with all context
|
||||
systemPrompt := BuildProgressiveRAGSystemPrompt(
|
||||
systemPrompt := BuildSystemPrompt(
|
||||
e.knowledgeBasesInfo,
|
||||
e.config.WebSearchEnabled,
|
||||
e.systemPromptTemplate,
|
||||
|
||||
@@ -209,8 +209,8 @@ func renderPromptPlaceholdersWithStatus(
|
||||
return result
|
||||
}
|
||||
|
||||
// BuildProgressiveRAGSystemPromptWithWeb builds the progressive RAG system prompt with web search enabled
|
||||
func BuildProgressiveRAGSystemPromptWithWeb(
|
||||
// BuildSystemPromptWithWeb builds the progressive RAG system prompt with web search enabled
|
||||
func BuildSystemPromptWithWeb(
|
||||
knowledgeBases []*KnowledgeBaseInfo,
|
||||
systemPromptTemplate ...string,
|
||||
) string {
|
||||
@@ -224,8 +224,8 @@ func BuildProgressiveRAGSystemPromptWithWeb(
|
||||
return renderPromptPlaceholdersWithStatus(template, knowledgeBases, true, currentTime)
|
||||
}
|
||||
|
||||
// BuildProgressiveRAGSystemPromptWithoutWeb builds the progressive RAG system prompt without web search
|
||||
func BuildProgressiveRAGSystemPromptWithoutWeb(
|
||||
// BuildSystemPromptWithoutWeb builds the progressive RAG system prompt without web search
|
||||
func BuildSystemPromptWithoutWeb(
|
||||
knowledgeBases []*KnowledgeBaseInfo,
|
||||
systemPromptTemplate ...string,
|
||||
) string {
|
||||
@@ -241,15 +241,15 @@ func BuildProgressiveRAGSystemPromptWithoutWeb(
|
||||
|
||||
// BuildProgressiveRAGSystemPrompt builds the progressive RAG system prompt based on web search status
|
||||
// This is the main function to use - it automatically selects the appropriate version
|
||||
func BuildProgressiveRAGSystemPrompt(
|
||||
func BuildSystemPrompt(
|
||||
knowledgeBases []*KnowledgeBaseInfo,
|
||||
webSearchEnabled bool,
|
||||
systemPromptTemplate ...string,
|
||||
) string {
|
||||
if webSearchEnabled {
|
||||
return BuildProgressiveRAGSystemPromptWithWeb(knowledgeBases, systemPromptTemplate...)
|
||||
return BuildSystemPromptWithWeb(knowledgeBases, systemPromptTemplate...)
|
||||
}
|
||||
return BuildProgressiveRAGSystemPromptWithoutWeb(knowledgeBases, systemPromptTemplate...)
|
||||
return BuildSystemPromptWithoutWeb(knowledgeBases, systemPromptTemplate...)
|
||||
}
|
||||
|
||||
// ProgressiveRAGSystemPromptWithWeb is the progressive RAG system prompt template with web search enabled
|
||||
|
||||
@@ -98,6 +98,7 @@ func (t *GetDocumentInfoTool) Execute(ctx context.Context, args map[string]inter
|
||||
}
|
||||
}
|
||||
|
||||
// Check if knowledge IDs are valid
|
||||
if len(knowledgeIDs) == 0 {
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
@@ -116,6 +117,7 @@ func (t *GetDocumentInfoTool) Execute(ctx context.Context, args map[string]inter
|
||||
var mu sync.Mutex
|
||||
results := make(map[string]*docInfo)
|
||||
|
||||
// Concurrently get info for each knowledge ID
|
||||
for _, knowledgeID := range knowledgeIDs {
|
||||
wg.Add(1)
|
||||
go func(id string) {
|
||||
@@ -195,10 +197,7 @@ func (t *GetDocumentInfoTool) Execute(ctx context.Context, args map[string]inter
|
||||
for i, doc := range successDocs {
|
||||
k := doc.knowledge
|
||||
|
||||
// Get file type icon
|
||||
typeIcon := getTypeIcon(k.Type, k.FileType)
|
||||
|
||||
output += fmt.Sprintf("【文档 #%d】 %s\n", i+1, typeIcon)
|
||||
output += fmt.Sprintf("【文档 #%d】\n", i+1)
|
||||
output += fmt.Sprintf(" ID: %s\n", k.ID)
|
||||
output += fmt.Sprintf(" 标题: %s\n", k.Title)
|
||||
|
||||
@@ -240,7 +239,6 @@ func (t *GetDocumentInfoTool) Execute(ctx context.Context, args map[string]inter
|
||||
"parse_status": k.ParseStatus,
|
||||
"chunk_count": doc.chunkCount,
|
||||
"metadata": k.GetMetadata(),
|
||||
"type_icon": typeIcon,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -264,36 +262,6 @@ func (t *GetDocumentInfoTool) Execute(ctx context.Context, args map[string]inter
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
|
||||
func getTypeIcon(knowledgeType, fileType string) string {
|
||||
switch knowledgeType {
|
||||
case "file":
|
||||
switch fileType {
|
||||
case "pdf", "PDF":
|
||||
return "📄 PDF"
|
||||
case "docx", "doc", "DOCX", "DOC":
|
||||
return "📝 Word"
|
||||
case "txt", "TXT":
|
||||
return "📃 Text"
|
||||
case "md", "MD", "markdown":
|
||||
return "📋 Markdown"
|
||||
case "xlsx", "xls", "XLSX", "XLS":
|
||||
return "📊 Excel"
|
||||
case "pptx", "ppt", "PPTX", "PPT":
|
||||
return "📊 PowerPoint"
|
||||
default:
|
||||
return "📄 File"
|
||||
}
|
||||
case "url":
|
||||
return "🌐 URL"
|
||||
case "passage":
|
||||
return "📝 Passage"
|
||||
default:
|
||||
return "📄 Document"
|
||||
}
|
||||
}
|
||||
|
||||
func formatSource(knowledgeType, source string) string {
|
||||
switch knowledgeType {
|
||||
case "file":
|
||||
|
||||
@@ -1173,44 +1173,6 @@ func (t *KnowledgeSearchTool) formatOutput(
|
||||
output += fmt.Sprintf(" 已召回: %d 个 (%.1f%%)\n", retrievedCount, percentage)
|
||||
output += fmt.Sprintf(" 未召回: %d 个\n", remaining)
|
||||
|
||||
if remaining > 0 {
|
||||
output += " 建议: 使用 list_knowledge_chunks 工具获取完整内容\n"
|
||||
|
||||
// Find missing chunk ranges (gaps in retrieved chunks)
|
||||
missingRanges := t.findMissingChunkRanges(retrievedChunks, int(totalChunks))
|
||||
|
||||
if len(missingRanges) == 0 {
|
||||
// No gaps found (shouldn't happen if remaining > 0, but handle it)
|
||||
output += fmt.Sprintf(
|
||||
" - 获取全部内容: list_knowledge_chunks(knowledge_id=\"%s\", offset=0, limit=%d)\n",
|
||||
knowledgeID,
|
||||
totalChunks,
|
||||
)
|
||||
} else if len(missingRanges) == 1 && missingRanges[0].start == 0 && missingRanges[0].end == int(totalChunks)-1 {
|
||||
// All chunks are missing (shouldn't happen, but handle it)
|
||||
output += fmt.Sprintf(" - 获取全部内容: list_knowledge_chunks(knowledge_id=\"%s\", offset=0, limit=%d)\n", knowledgeID, totalChunks)
|
||||
} else {
|
||||
// Suggest getting each missing range
|
||||
for idx, r := range missingRanges {
|
||||
rangeSize := r.end - r.start + 1
|
||||
if rangeSize <= 100 {
|
||||
// Small range, get all at once
|
||||
output += fmt.Sprintf(" - 区间 %d: chunk_index %d-%d (%d 个) → list_knowledge_chunks(knowledge_id=\"%s\", offset=%d, limit=%d)\n",
|
||||
idx+1, r.start, r.end, rangeSize, knowledgeID, r.start, rangeSize)
|
||||
} else {
|
||||
// Large range, suggest getting in batches
|
||||
output += fmt.Sprintf(" - 区间 %d: chunk_index %d-%d (%d 个,建议分批获取):\n",
|
||||
idx+1, r.start, r.end, rangeSize)
|
||||
output += fmt.Sprintf(" 首次: list_knowledge_chunks(knowledge_id=\"%s\", offset=%d, limit=100)\n",
|
||||
knowledgeID, r.start)
|
||||
if rangeSize > 100 {
|
||||
output += " 继续: 根据返回结果调整 offset 继续获取剩余内容\n"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
output += "\n"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -168,6 +168,7 @@ func (t *ListKnowledgeChunksTool) Execute(ctx context.Context, args map[string]i
|
||||
}, nil
|
||||
}
|
||||
|
||||
// lookupKnowledgeTitle looks up the title of a knowledge document
|
||||
func (t *ListKnowledgeChunksTool) lookupKnowledgeTitle(ctx context.Context, knowledgeID string) string {
|
||||
if t.knowledgeService == nil {
|
||||
return ""
|
||||
@@ -179,6 +180,7 @@ func (t *ListKnowledgeChunksTool) lookupKnowledgeTitle(ctx context.Context, know
|
||||
return strings.TrimSpace(knowledge.Title)
|
||||
}
|
||||
|
||||
// buildOutput builds the output for the list knowledge chunks tool
|
||||
func (t *ListKnowledgeChunksTool) buildOutput(
|
||||
knowledgeID string,
|
||||
knowledgeTitle string,
|
||||
@@ -226,6 +228,7 @@ func (t *ListKnowledgeChunksTool) buildOutput(
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// summarizeContent summarizes the content of a chunk
|
||||
func summarizeContent(content string) string {
|
||||
cleaned := strings.TrimSpace(content)
|
||||
if cleaned == "" {
|
||||
|
||||
@@ -85,7 +85,7 @@ func (t *QueryKnowledgeGraphTool) Parameters() map[string]interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// Execute queries the knowledge graph with concurrent KB processing
|
||||
// Execute performs the knowledge graph query with concurrent KB processing
|
||||
func (t *QueryKnowledgeGraphTool) Execute(ctx context.Context, args map[string]interface{}) (*types.ToolResult, error) {
|
||||
// Extract knowledge_base_ids array
|
||||
kbIDsRaw, ok := args["knowledge_base_ids"].([]interface{})
|
||||
@@ -394,14 +394,10 @@ func buildGraphVisualizationData(
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Extract actual entities and relations when graph extraction is fully implemented
|
||||
// For now, create placeholder structure
|
||||
|
||||
return map[string]interface{}{
|
||||
"nodes": nodes,
|
||||
"edges": edges,
|
||||
"total_nodes": len(nodes),
|
||||
"total_edges": len(edges),
|
||||
"visualization_note": "完整的图可视化功能开发中,当前显示文档节点",
|
||||
"nodes": nodes,
|
||||
"edges": edges,
|
||||
"total_nodes": len(nodes),
|
||||
"total_edges": len(edges),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// SequentialThinkingTool implements a dynamic and reflective problem-solving tool
|
||||
// SequentialThinkingTool is a dynamic and reflective problem-solving tool
|
||||
// This tool helps analyze problems through a flexible thinking process that can adapt and evolve
|
||||
type SequentialThinkingTool struct {
|
||||
BaseTool
|
||||
|
||||
@@ -19,29 +19,31 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
webFetchTimeout = 60 * time.Second
|
||||
webFetchMaxChars = 100000
|
||||
webFetchTimeout = 60 * time.Second // timeout for web fetch
|
||||
webFetchMaxChars = 100000 // maximum number of characters to fetch
|
||||
)
|
||||
|
||||
// webFetchParams is the parameters for the web fetch tool
|
||||
type webFetchParams struct {
|
||||
URL string
|
||||
Prompt string
|
||||
}
|
||||
|
||||
// webFetchItemResult is the result for a web fetch item
|
||||
type webFetchItemResult struct {
|
||||
output string
|
||||
data map[string]interface{}
|
||||
err error
|
||||
}
|
||||
|
||||
// WebFetchTool 拉取网页内容并根据提示执行总结
|
||||
// WebFetchTool fetches web page content and summarizes it using an LLM
|
||||
type WebFetchTool struct {
|
||||
BaseTool
|
||||
client *http.Client
|
||||
chatModel chat.Chat
|
||||
}
|
||||
|
||||
// NewWebFetchTool 创建 web_fetch 工具实例
|
||||
// NewWebFetchTool creates a new web_fetch tool instance
|
||||
func NewWebFetchTool(chatModel chat.Chat) *WebFetchTool {
|
||||
description := `Fetch detailed web content from previously discovered URLs and analyze it with an LLM.
|
||||
|
||||
@@ -226,6 +228,7 @@ func (t *WebFetchTool) Execute(ctx context.Context, args map[string]interface{})
|
||||
}, nil
|
||||
}
|
||||
|
||||
// parseParams parses the parameters for a web fetch item
|
||||
func (t *WebFetchTool) parseParams(item interface{}) webFetchParams {
|
||||
params := webFetchParams{}
|
||||
if m, ok := item.(map[string]interface{}); ok {
|
||||
@@ -239,6 +242,7 @@ func (t *WebFetchTool) parseParams(item interface{}) webFetchParams {
|
||||
return params
|
||||
}
|
||||
|
||||
// validateParams validates the parameters for a web fetch item
|
||||
func (t *WebFetchTool) validateParams(p webFetchParams) error {
|
||||
if p.URL == "" {
|
||||
return fmt.Errorf("url is required")
|
||||
@@ -252,6 +256,7 @@ func (t *WebFetchTool) validateParams(p webFetchParams) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// executeFetch executes a web fetch item
|
||||
func (t *WebFetchTool) executeFetch(
|
||||
ctx context.Context,
|
||||
params webFetchParams,
|
||||
@@ -294,6 +299,7 @@ func (t *WebFetchTool) executeFetch(
|
||||
return output, resultData, summaryErr
|
||||
}
|
||||
|
||||
// normalizeGitHubURL normalizes a GitHub URL
|
||||
func (t *WebFetchTool) normalizeGitHubURL(source string) string {
|
||||
if strings.Contains(source, "github.com") && strings.Contains(source, "/blob/") {
|
||||
source = strings.Replace(source, "github.com", "raw.githubusercontent.com", 1)
|
||||
@@ -302,6 +308,7 @@ func (t *WebFetchTool) normalizeGitHubURL(source string) string {
|
||||
return source
|
||||
}
|
||||
|
||||
// processWithLLM processes the content with an LLM
|
||||
func (t *WebFetchTool) processWithLLM(ctx context.Context, params webFetchParams, content string) (string, error) {
|
||||
if t.chatModel == nil {
|
||||
return "", fmt.Errorf("chat model not available for web_fetch")
|
||||
@@ -336,6 +343,7 @@ func (t *WebFetchTool) processWithLLM(ctx context.Context, params webFetchParams
|
||||
return strings.TrimSpace(response.Content), nil
|
||||
}
|
||||
|
||||
// buildOutputText builds the output text for a web fetch item
|
||||
func (t *WebFetchTool) buildOutputText(params webFetchParams, content string, summary string, summaryErr error) string {
|
||||
var builder strings.Builder
|
||||
builder.WriteString(fmt.Sprintf("URL: %s\n", params.URL))
|
||||
@@ -354,6 +362,7 @@ func (t *WebFetchTool) buildOutputText(params webFetchParams, content string, su
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// fetchHTMLContent fetches the HTML content for a web fetch item
|
||||
func (t *WebFetchTool) fetchHTMLContent(ctx context.Context, targetURL string) (string, string, error) {
|
||||
html, err := t.fetchWithChromedp(ctx, targetURL)
|
||||
if err == nil && strings.TrimSpace(html) != "" {
|
||||
@@ -375,6 +384,7 @@ func (t *WebFetchTool) fetchHTMLContent(ctx context.Context, targetURL string) (
|
||||
return html, "http", nil
|
||||
}
|
||||
|
||||
// fetchWithChromedp fetches the HTML content with Chromedp
|
||||
func (t *WebFetchTool) fetchWithChromedp(ctx context.Context, targetURL string) (string, error) {
|
||||
logger.Debugf(ctx, "[Tool][WebFetch] Chromedp 抓取开始 url=%s", targetURL)
|
||||
|
||||
@@ -414,6 +424,7 @@ func (t *WebFetchTool) fetchWithChromedp(ctx context.Context, targetURL string)
|
||||
return html, nil
|
||||
}
|
||||
|
||||
// fetchWithHTTP fetches the HTML content with HTTP
|
||||
func (t *WebFetchTool) fetchWithHTTP(ctx context.Context, targetURL string) (string, error) {
|
||||
resp, err := t.fetchWithTimeout(ctx, targetURL)
|
||||
if err != nil {
|
||||
@@ -434,6 +445,7 @@ func (t *WebFetchTool) fetchWithHTTP(ctx context.Context, targetURL string) (str
|
||||
return string(htmlBytes), nil
|
||||
}
|
||||
|
||||
// fetchWithTimeout fetches the HTML content with a timeout
|
||||
func (t *WebFetchTool) fetchWithTimeout(ctx context.Context, targetURL string) (*http.Response, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, targetURL, nil)
|
||||
if err != nil {
|
||||
@@ -451,6 +463,7 @@ func (t *WebFetchTool) fetchWithTimeout(ctx context.Context, targetURL string) (
|
||||
return t.client.Do(req)
|
||||
}
|
||||
|
||||
// convertHTMLToText converts the HTML content to text
|
||||
func (t *WebFetchTool) convertHTMLToText(html string) string {
|
||||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
|
||||
if err != nil {
|
||||
@@ -469,6 +482,7 @@ func (t *WebFetchTool) convertHTMLToText(html string) string {
|
||||
return strings.TrimSpace(result)
|
||||
}
|
||||
|
||||
// processNode processes a node in the HTML content
|
||||
func (t *WebFetchTool) processNode(s *goquery.Selection, markdown *strings.Builder) {
|
||||
s.Contents().Each(func(i int, node *goquery.Selection) {
|
||||
nodeName := goquery.NodeName(node)
|
||||
@@ -578,6 +592,7 @@ func (t *WebFetchTool) processNode(s *goquery.Selection, markdown *strings.Build
|
||||
})
|
||||
}
|
||||
|
||||
// basicTextExtraction extracts the text from the HTML content
|
||||
func (t *WebFetchTool) basicTextExtraction(html string) string {
|
||||
re := regexp.MustCompile(`<[^>]*>`)
|
||||
text := re.ReplaceAllString(html, " ")
|
||||
|
||||
@@ -11,19 +11,23 @@ import (
|
||||
"github.com/neo4j/neo4j-go-driver/v6/neo4j"
|
||||
)
|
||||
|
||||
// Neo4jRepository is a repository for Neo4j
|
||||
type Neo4jRepository struct {
|
||||
driver neo4j.Driver
|
||||
nodePrefix string
|
||||
}
|
||||
|
||||
// NewNeo4jRepository creates a new Neo4j repository
|
||||
func NewNeo4jRepository(driver neo4j.Driver) interfaces.RetrieveGraphRepository {
|
||||
return &Neo4jRepository{driver: driver, nodePrefix: "ENTITY"}
|
||||
}
|
||||
|
||||
// _remove_hyphen removes hyphens from a string
|
||||
func _remove_hyphen(s string) string {
|
||||
return strings.ReplaceAll(s, "-", "_")
|
||||
}
|
||||
|
||||
// Labels returns the labels for a namespace
|
||||
func (n *Neo4jRepository) Labels(namespace types.NameSpace) []string {
|
||||
res := make([]string, 0)
|
||||
for _, label := range namespace.Labels() {
|
||||
@@ -32,12 +36,13 @@ func (n *Neo4jRepository) Labels(namespace types.NameSpace) []string {
|
||||
return res
|
||||
}
|
||||
|
||||
// Label returns the label for a namespace
|
||||
func (n *Neo4jRepository) Label(namespace types.NameSpace) string {
|
||||
labels := n.Labels(namespace)
|
||||
return strings.Join(labels, ":")
|
||||
}
|
||||
|
||||
// AddGraph implements interfaces.RetrieveGraphRepository.
|
||||
// AddGraph adds a graph to the Neo4j repository
|
||||
func (n *Neo4jRepository) AddGraph(ctx context.Context, namespace types.NameSpace, graphs []*types.GraphData) error {
|
||||
if n.driver == nil {
|
||||
logger.Warnf(ctx, "NOT SUPPORT RETRIEVE GRAPH")
|
||||
@@ -51,11 +56,13 @@ func (n *Neo4jRepository) AddGraph(ctx context.Context, namespace types.NameSpac
|
||||
return nil
|
||||
}
|
||||
|
||||
// addGraph adds a graph to the Neo4j repository
|
||||
func (n *Neo4jRepository) addGraph(ctx context.Context, namespace types.NameSpace, graph *types.GraphData) error {
|
||||
session := n.driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite})
|
||||
defer session.Close(ctx)
|
||||
|
||||
_, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (interface{}, error) {
|
||||
// Node import query
|
||||
node_import_query := `
|
||||
UNWIND $data AS row
|
||||
CALL apoc.merge.node(row.labels, {name: row.name, kg: row.knowledge_id}, row.props, {}) YIELD node
|
||||
@@ -76,6 +83,7 @@ func (n *Neo4jRepository) addGraph(ctx context.Context, namespace types.NameSpac
|
||||
return nil, fmt.Errorf("failed to create nodes: %v", err)
|
||||
}
|
||||
|
||||
// Relationship import query
|
||||
rel_import_query := `
|
||||
UNWIND $data AS row
|
||||
CALL apoc.merge.node(row.source_labels, {name: row.source, kg: row.knowledge_id}, {}, {}) YIELD node as source
|
||||
@@ -106,7 +114,7 @@ func (n *Neo4jRepository) addGraph(ctx context.Context, namespace types.NameSpac
|
||||
return nil
|
||||
}
|
||||
|
||||
// DelGraph implements interfaces.RetrieveGraphRepository.
|
||||
// DelGraph deletes a graph from the Neo4j repository
|
||||
func (n *Neo4jRepository) DelGraph(ctx context.Context, namespaces []types.NameSpace) error {
|
||||
if n.driver == nil {
|
||||
logger.Warnf(ctx, "NOT SUPPORT RETRIEVE GRAPH")
|
||||
@@ -152,6 +160,7 @@ func (n *Neo4jRepository) DelGraph(ctx context.Context, namespaces []types.NameS
|
||||
return nil
|
||||
}
|
||||
|
||||
// SearchNode searches for nodes in the Neo4j repository
|
||||
func (n *Neo4jRepository) SearchNode(
|
||||
ctx context.Context,
|
||||
namespace types.NameSpace,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// knowledgeTagRepository is a repository for knowledge tags
|
||||
type knowledgeTagRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
@@ -17,14 +18,17 @@ func NewKnowledgeTagRepository(db *gorm.DB) interfaces.KnowledgeTagRepository {
|
||||
return &knowledgeTagRepository{db: db}
|
||||
}
|
||||
|
||||
// Create creates a new knowledge tag
|
||||
func (r *knowledgeTagRepository) Create(ctx context.Context, tag *types.KnowledgeTag) error {
|
||||
return r.db.WithContext(ctx).Create(tag).Error
|
||||
}
|
||||
|
||||
// Update updates a knowledge tag
|
||||
func (r *knowledgeTagRepository) Update(ctx context.Context, tag *types.KnowledgeTag) error {
|
||||
return r.db.WithContext(ctx).Save(tag).Error
|
||||
}
|
||||
|
||||
// GetByID gets a knowledge tag by ID
|
||||
func (r *knowledgeTagRepository) GetByID(ctx context.Context, tenantID uint64, id string) (*types.KnowledgeTag, error) {
|
||||
var tag types.KnowledgeTag
|
||||
if err := r.db.WithContext(ctx).
|
||||
@@ -35,6 +39,7 @@ func (r *knowledgeTagRepository) GetByID(ctx context.Context, tenantID uint64, i
|
||||
return &tag, nil
|
||||
}
|
||||
|
||||
// ListByKB lists knowledge tags by knowledge base ID
|
||||
func (r *knowledgeTagRepository) ListByKB(
|
||||
ctx context.Context,
|
||||
tenantID uint64,
|
||||
@@ -50,13 +55,14 @@ func (r *knowledgeTagRepository) ListByKB(
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
// Delete deletes a knowledge tag
|
||||
func (r *knowledgeTagRepository) Delete(ctx context.Context, tenantID uint64, id string) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND id = ?", tenantID, id).
|
||||
Delete(&types.KnowledgeTag{}).Error
|
||||
}
|
||||
|
||||
// CountReferences returns how many knowledges and chunks reference this tag.
|
||||
// CountReferences returns the number of knowledges and chunks that reference this tag
|
||||
func (r *knowledgeTagRepository) CountReferences(
|
||||
ctx context.Context,
|
||||
tenantID uint64,
|
||||
|
||||
@@ -10,31 +10,32 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
|
||||
// pipelineInfo logs pipeline info level entries.
|
||||
func pipelineInfo(ctx context.Context, stage, action string, fields map[string]interface{}) {
|
||||
common.PipelineInfo(ctx, stage, action, fields)
|
||||
}
|
||||
|
||||
// pipelineWarn logs pipeline warning level entries.
|
||||
func pipelineWarn(ctx context.Context, stage, action string, fields map[string]interface{}) {
|
||||
common.PipelineWarn(ctx, stage, action, fields)
|
||||
}
|
||||
|
||||
// pipelineError logs pipeline error level entries.
|
||||
func pipelineError(ctx context.Context, stage, action string, fields map[string]interface{}) {
|
||||
common.PipelineError(ctx, stage, action, fields)
|
||||
}
|
||||
|
||||
// prepareChatModel shared logic to prepare chat model and options
|
||||
// it gets the chat model and sets up the chat options based on the chat manage.
|
||||
func prepareChatModel(ctx context.Context, modelService interfaces.ModelService,
|
||||
chatManage *types.ChatManage,
|
||||
) (chat.Chat, *chat.ChatOptions, error) {
|
||||
logger.Infof(ctx, "Getting chat model, model ID: %s", chatManage.ChatModelID)
|
||||
|
||||
chatModel, err := modelService.GetChatModel(ctx, chatManage.ChatModelID)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to get chat model: %v", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
logger.Info(ctx, "Setting up chat options")
|
||||
opt := &chat.ChatOptions{
|
||||
Temperature: chatManage.SummaryConfig.Temperature,
|
||||
TopP: chatManage.SummaryConfig.TopP,
|
||||
|
||||
@@ -94,6 +94,7 @@ func (p *PluginExtractEntity) OnEvent(ctx context.Context,
|
||||
return next()
|
||||
}
|
||||
|
||||
// Extractor is a struct for extracting entities
|
||||
type Extractor struct {
|
||||
chat chat.Chat
|
||||
formater *Formater
|
||||
@@ -101,6 +102,7 @@ type Extractor struct {
|
||||
chatOpt *chat.ChatOptions
|
||||
}
|
||||
|
||||
// NewExtractor creates a new extractor
|
||||
func NewExtractor(
|
||||
chatModel chat.Chat,
|
||||
template *types.PromptTemplateStructured,
|
||||
@@ -118,6 +120,7 @@ func NewExtractor(
|
||||
}
|
||||
}
|
||||
|
||||
// Extract extracts entities from content
|
||||
func (e *Extractor) Extract(ctx context.Context, content string) (*types.GraphData, error) {
|
||||
generator := NewQAPromptGenerator(e.formater, e.template)
|
||||
|
||||
@@ -139,6 +142,7 @@ func (e *Extractor) Extract(ctx context.Context, content string) (*types.GraphDa
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
// RemoveUnknownRelation removes unknown relations from graph
|
||||
func (e *Extractor) RemoveUnknownRelation(ctx context.Context, graph *types.GraphData) {
|
||||
relationType := make(map[string]bool)
|
||||
for _, tag := range e.template.Tags {
|
||||
@@ -156,6 +160,7 @@ func (e *Extractor) RemoveUnknownRelation(ctx context.Context, graph *types.Grap
|
||||
graph.Relation = relationNew
|
||||
}
|
||||
|
||||
// QAPromptGenerator is a struct for generating QA prompts
|
||||
type QAPromptGenerator struct {
|
||||
Formater *Formater
|
||||
Template *types.PromptTemplateStructured
|
||||
@@ -165,6 +170,7 @@ type QAPromptGenerator struct {
|
||||
AnswerPrefix string
|
||||
}
|
||||
|
||||
// NewQAPromptGenerator creates a new QA prompt generator
|
||||
func NewQAPromptGenerator(formater *Formater, template *types.PromptTemplateStructured) *QAPromptGenerator {
|
||||
return &QAPromptGenerator{
|
||||
Formater: formater,
|
||||
@@ -176,6 +182,7 @@ func NewQAPromptGenerator(formater *Formater, template *types.PromptTemplateStru
|
||||
}
|
||||
}
|
||||
|
||||
// System generates a system prompt
|
||||
func (qa *QAPromptGenerator) System(ctx context.Context) string {
|
||||
promptLines := []string{}
|
||||
|
||||
@@ -205,6 +212,7 @@ func (qa *QAPromptGenerator) System(ctx context.Context) string {
|
||||
return strings.Join(promptLines, "\n")
|
||||
}
|
||||
|
||||
// User generates a user prompt
|
||||
func (qa *QAPromptGenerator) User(ctx context.Context, question string) string {
|
||||
promptLines := []string{}
|
||||
promptLines = append(promptLines, qa.QuestionHeading)
|
||||
@@ -213,6 +221,7 @@ func (qa *QAPromptGenerator) User(ctx context.Context, question string) string {
|
||||
return strings.Join(promptLines, "\n")
|
||||
}
|
||||
|
||||
// Render renders a prompt
|
||||
func (qa *QAPromptGenerator) Render(ctx context.Context, question string) []chat.Message {
|
||||
return []chat.Message{
|
||||
{
|
||||
@@ -226,10 +235,13 @@ func (qa *QAPromptGenerator) Render(ctx context.Context, question string) []chat
|
||||
}
|
||||
}
|
||||
|
||||
// FormatType is a type for format types
|
||||
type FormatType string
|
||||
|
||||
const (
|
||||
// FormatTypeJSON is a format type for JSON
|
||||
FormatTypeJSON FormatType = "json"
|
||||
// FormatTypeYAML is a format type for YAML
|
||||
FormatTypeYAML FormatType = "yaml"
|
||||
)
|
||||
|
||||
@@ -245,6 +257,7 @@ var _FENCE_RE = regexp.MustCompile(
|
||||
_FENCE_START + _LANGUAGE_TAG + _FENCE_NEWLINE + _FENCE_BODY + _FENCE_END,
|
||||
)
|
||||
|
||||
// Formater is a struct for formatting entities
|
||||
type Formater struct {
|
||||
attributeSuffix string
|
||||
formatType FormatType
|
||||
@@ -256,6 +269,7 @@ type Formater struct {
|
||||
relationPrefix string
|
||||
}
|
||||
|
||||
// NewFormater creates a new formater
|
||||
func NewFormater() *Formater {
|
||||
return &Formater{
|
||||
attributeSuffix: "_attributes",
|
||||
@@ -268,6 +282,7 @@ func NewFormater() *Formater {
|
||||
}
|
||||
}
|
||||
|
||||
// formatExtraction formats extraction
|
||||
func (f *Formater) formatExtraction(nodes []*types.GraphNode, relations []*types.GraphRelation) (string, error) {
|
||||
items := make([]map[string]interface{}, 0)
|
||||
for _, node := range nodes {
|
||||
|
||||
@@ -51,6 +51,7 @@ func (p *PluginMerge) OnEvent(ctx context.Context,
|
||||
"chunk_cnt": len(searchResult),
|
||||
})
|
||||
|
||||
// If there are no search results, return early
|
||||
if len(searchResult) == 0 {
|
||||
pipelineWarn(ctx, "Merge", "output", map[string]interface{}{
|
||||
"chunk_cnt": 0,
|
||||
@@ -212,6 +213,7 @@ func mergeImageInfo(ctx context.Context, target *types.SearchResult, source *typ
|
||||
return nil
|
||||
}
|
||||
|
||||
// populateFAQAnswers populates FAQ answers for the search results
|
||||
func (p *PluginMerge) populateFAQAnswers(
|
||||
ctx context.Context,
|
||||
chatManage *types.ChatManage,
|
||||
@@ -300,6 +302,7 @@ func (p *PluginMerge) populateFAQAnswers(
|
||||
return results
|
||||
}
|
||||
|
||||
// buildFAQAnswerContent builds the content of a FAQ answer
|
||||
func buildFAQAnswerContent(meta *types.FAQChunkMetadata) string {
|
||||
if meta == nil {
|
||||
return ""
|
||||
@@ -336,6 +339,7 @@ func buildFAQAnswerContent(meta *types.FAQChunkMetadata) string {
|
||||
return strings.TrimSpace(builder.String())
|
||||
}
|
||||
|
||||
// expandShortContextWithNeighbors expands the short context with neighbors
|
||||
func (p *PluginMerge) expandShortContextWithNeighbors(
|
||||
ctx context.Context,
|
||||
chatManage *types.ChatManage,
|
||||
@@ -580,10 +584,12 @@ func (p *PluginMerge) expandShortContextWithNeighbors(
|
||||
return results
|
||||
}
|
||||
|
||||
// runeLen returns the length of a string in runes
|
||||
func runeLen(s string) int {
|
||||
return len([]rune(s))
|
||||
}
|
||||
|
||||
// mergeOrderedContent merges ordered content
|
||||
func mergeOrderedContent(prev, base, next string, maxLen int) string {
|
||||
content := base
|
||||
if prev != "" {
|
||||
@@ -599,7 +605,7 @@ func mergeOrderedContent(prev, base, next string, maxLen int) string {
|
||||
return content
|
||||
}
|
||||
|
||||
// concatNoOverlap concatenates a and b, removing potential overlapping prefix/suffix
|
||||
// concatNoOverlap concatenates two strings, removing potential overlapping prefix/suffix
|
||||
func concatNoOverlap(a, b string) string {
|
||||
if a == "" {
|
||||
return b
|
||||
|
||||
@@ -239,6 +239,7 @@ func isInformativeToken(token string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// containsChineseCharacters checks if a string contains Chinese characters
|
||||
func containsChineseCharacters(text string) bool {
|
||||
for _, r := range text {
|
||||
if unicode.Is(unicode.Han, r) {
|
||||
@@ -248,6 +249,7 @@ func containsChineseCharacters(text string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// containsLatinLetters checks if a string contains Latin letters
|
||||
func containsLatinLetters(text string) bool {
|
||||
for _, r := range text {
|
||||
if r <= unicode.MaxASCII && unicode.IsLetter(r) {
|
||||
@@ -257,22 +259,26 @@ func containsLatinLetters(text string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// normalizeWhitespace normalizes whitespace in a string
|
||||
func normalizeWhitespace(text string) string {
|
||||
text = strings.TrimSpace(text)
|
||||
return multiSpaceRegex.ReplaceAllString(text, " ")
|
||||
}
|
||||
|
||||
// normalizeLatinQuery normalizes a Latin query
|
||||
func normalizeLatinQuery(text string) string {
|
||||
text = strings.ToLower(text)
|
||||
text = multiSpaceRegex.ReplaceAllString(text, " ")
|
||||
return strings.TrimSpace(text)
|
||||
}
|
||||
|
||||
// intentResp is a response for intent detection
|
||||
type intentResp struct {
|
||||
Intent string `json:"intent"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
}
|
||||
|
||||
// detectIntentLLM detects the intent of a query using an LLM
|
||||
func (p *PluginPreprocess) detectIntentLLM(ctx context.Context, chatManage *types.ChatManage, text string) string {
|
||||
if p.modelService == nil || chatManage.ChatModelID == "" {
|
||||
pipelineWarn(
|
||||
@@ -330,6 +336,7 @@ func (p *PluginPreprocess) detectIntentLLM(ctx context.Context, chatManage *type
|
||||
}
|
||||
}
|
||||
|
||||
// extractJSONBody extracts a JSON body from a string
|
||||
func extractJSONBody(text string) string {
|
||||
t := strings.TrimSpace(text)
|
||||
// Try fenced block first
|
||||
|
||||
@@ -176,6 +176,7 @@ func (p *PluginRerank) rerank(ctx context.Context,
|
||||
return rankFilter
|
||||
}
|
||||
|
||||
// ensureMetadata ensures the metadata is not nil
|
||||
func ensureMetadata(m map[string]string) map[string]string {
|
||||
if m == nil {
|
||||
return make(map[string]string)
|
||||
@@ -183,6 +184,7 @@ func ensureMetadata(m map[string]string) map[string]string {
|
||||
return m
|
||||
}
|
||||
|
||||
// compositeScore calculates the composite score for a search result
|
||||
func compositeScore(sr *types.SearchResult, modelScore, baseScore float64, chatManage *types.ChatManage) float64 {
|
||||
sourceWeight := 1.0
|
||||
switch strings.ToLower(sr.KnowledgeSource) {
|
||||
@@ -222,6 +224,7 @@ func compositeScore(sr *types.SearchResult, modelScore, baseScore float64, chatM
|
||||
return composite
|
||||
}
|
||||
|
||||
// applyMMR applies the MMR algorithm to the search results
|
||||
func applyMMR(
|
||||
ctx context.Context,
|
||||
results []*types.SearchResult,
|
||||
@@ -285,6 +288,7 @@ func applyMMR(
|
||||
return selected
|
||||
}
|
||||
|
||||
// tokenizeSimple tokenizes a text into a set of tokens
|
||||
func tokenizeSimple(text string) map[string]struct{} {
|
||||
text = strings.ToLower(text)
|
||||
fields := strings.Fields(text)
|
||||
@@ -297,6 +301,7 @@ func tokenizeSimple(text string) map[string]struct{} {
|
||||
return set
|
||||
}
|
||||
|
||||
// jaccard calculates the Jaccard similarity between two sets of tokens
|
||||
func jaccard(a, b map[string]struct{}) float64 {
|
||||
if len(a) == 0 && len(b) == 0 {
|
||||
return 0
|
||||
@@ -314,6 +319,7 @@ func jaccard(a, b map[string]struct{}) float64 {
|
||||
return float64(inter) / float64(union)
|
||||
}
|
||||
|
||||
// clampFloat clamps a float value between a minimum and maximum value
|
||||
func clampFloat(v, minV, maxV float64) float64 {
|
||||
if v < minV {
|
||||
return minV
|
||||
|
||||
@@ -16,6 +16,7 @@ type PluginSearchEntity struct {
|
||||
knowledgeRepo interfaces.KnowledgeRepository
|
||||
}
|
||||
|
||||
// NewPluginSearchEntity creates a new plugin search entity
|
||||
func NewPluginSearchEntity(
|
||||
eventManager *EventManager,
|
||||
graphRepository interfaces.RetrieveGraphRepository,
|
||||
@@ -31,12 +32,12 @@ func NewPluginSearchEntity(
|
||||
return res
|
||||
}
|
||||
|
||||
// ActivationEvents returns the event types this plugin handles
|
||||
// ActivationEvents returns the list of event types this plugin responds to
|
||||
func (p *PluginSearchEntity) ActivationEvents() []types.EventType {
|
||||
return []types.EventType{types.ENTITY_SEARCH}
|
||||
}
|
||||
|
||||
// OnEvent handles search events in the chat pipeline
|
||||
// OnEvent processes triggered events
|
||||
func (p *PluginSearchEntity) OnEvent(ctx context.Context,
|
||||
eventType types.EventType, chatManage *types.ChatManage, next func() *PluginError,
|
||||
) *PluginError {
|
||||
@@ -148,6 +149,7 @@ func (p *PluginSearchEntity) OnEvent(ctx context.Context,
|
||||
return next()
|
||||
}
|
||||
|
||||
// filterSeenChunk filters seen chunks from the graph
|
||||
func filterSeenChunk(ctx context.Context, graph *types.GraphData, searchResult []*types.SearchResult) []string {
|
||||
seen := map[string]bool{}
|
||||
for _, chunk := range searchResult {
|
||||
@@ -169,6 +171,7 @@ func filterSeenChunk(ctx context.Context, graph *types.GraphData, searchResult [
|
||||
return chunkIDs
|
||||
}
|
||||
|
||||
// chunk2SearchResult converts a chunk to a search result
|
||||
func chunk2SearchResult(chunk *types.Chunk, knowledge *types.Knowledge) *types.SearchResult {
|
||||
return &types.SearchResult{
|
||||
ID: chunk.ID,
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
// NewChunkExtractTask creates a new chunk extract task
|
||||
func NewChunkExtractTask(
|
||||
ctx context.Context,
|
||||
client *asynq.Client,
|
||||
@@ -45,6 +46,7 @@ func NewChunkExtractTask(
|
||||
return nil
|
||||
}
|
||||
|
||||
// ChunkExtractService is a service for extracting chunks
|
||||
type ChunkExtractService struct {
|
||||
template *types.PromptTemplateStructured
|
||||
modelService interfaces.ModelService
|
||||
@@ -53,6 +55,7 @@ type ChunkExtractService struct {
|
||||
graphEngine interfaces.RetrieveGraphRepository
|
||||
}
|
||||
|
||||
// NewChunkExtractService creates a new chunk extract service
|
||||
func NewChunkExtractService(
|
||||
config *config.Config,
|
||||
modelService interfaces.ModelService,
|
||||
@@ -73,6 +76,7 @@ func NewChunkExtractService(
|
||||
}
|
||||
}
|
||||
|
||||
// Extract extracts a chunk
|
||||
func (s *ChunkExtractService) Extract(ctx context.Context, t *asynq.Task) error {
|
||||
var p types.ExtractChunkPayload
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
@@ -137,7 +141,5 @@ func (s *ChunkExtractService) Extract(ctx context.Context, t *asynq.Task) error
|
||||
logger.Errorf(ctx, "failed to add graph: %v", err)
|
||||
return err
|
||||
}
|
||||
// gg, _ := json.Marshal(graph)
|
||||
// logger.Infof(ctx, "extracted graph: %s", string(gg))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -193,12 +193,12 @@ func (s *mcpServiceService) UpdateMCPService(ctx context.Context, service *types
|
||||
logger.GetLogger(ctx).Infof("MCP service disabled, connection closed: %s (ID: %s)", name, service.ID)
|
||||
} else if configChanged {
|
||||
s.mcpManager.CloseClient(service.ID)
|
||||
logger.GetLogger(ctx).Infof("MCP service config changed, connection closed for reconnect: %s (ID: %s)", name, service.ID)
|
||||
logger.GetLogger(ctx).Infof("MCP service config changed, connection closed: %s (ID: %s)", name, service.ID)
|
||||
} else if oldEnabled != existing.Enabled && existing.Enabled {
|
||||
// Service was just enabled (was disabled, now enabled)
|
||||
// Close any existing connection to ensure clean state
|
||||
s.mcpManager.CloseClient(service.ID)
|
||||
logger.GetLogger(ctx).Infof("MCP service enabled, existing connection closed for clean state: %s (ID: %s)", name, service.ID)
|
||||
logger.GetLogger(ctx).Infof("MCP service enabled, existing connection closed: %s (ID: %s)", name, service.ID)
|
||||
}
|
||||
|
||||
logger.GetLogger(ctx).Infof("MCP service updated: %s (ID: %s), enabled: %v", name, service.ID, existing.Enabled)
|
||||
|
||||
@@ -634,7 +634,8 @@ func (s *sessionService) selectChatModelID(
|
||||
return session.SummaryModelID, nil
|
||||
} else if err == nil && model != nil {
|
||||
// Session has a model but it's not Remote, we'll check knowledge bases for Remote models
|
||||
logger.Infof(ctx, "Session has summary model %s but it's not Remote, checking knowledge bases for Remote models", session.SummaryModelID)
|
||||
logger.Infof(ctx, "Session has summary model %s but it's not Remote, "+
|
||||
"checking knowledge bases for Remote models", session.SummaryModelID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -952,7 +953,8 @@ func (s *sessionService) AgentQA(
|
||||
len(agentConfig.KnowledgeBases), agentConfig.KnowledgeBases)
|
||||
}
|
||||
} else {
|
||||
logger.Infof(ctx, "Agent configured with %d knowledge base(s): %v", len(agentConfig.KnowledgeBases), agentConfig.KnowledgeBases)
|
||||
logger.Infof(ctx, "Agent configured with %d knowledge base(s): %v",
|
||||
len(agentConfig.KnowledgeBases), agentConfig.KnowledgeBases)
|
||||
}
|
||||
|
||||
summaryModelID := session.SummaryModelID
|
||||
@@ -1141,7 +1143,8 @@ func (s *sessionService) SaveWebSearchTempKBState(
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteWebSearchTempKBState deletes the temporary KB state for web search from Redis and cleans up associated knowledge base and knowledge items
|
||||
// DeleteWebSearchTempKBState deletes the temporary KB state for web search from Redis
|
||||
// and cleans up associated knowledge base and knowledge items.
|
||||
func (s *sessionService) DeleteWebSearchTempKBState(ctx context.Context, sessionID string) error {
|
||||
if s.redisClient == nil {
|
||||
return nil
|
||||
|
||||
@@ -63,6 +63,7 @@ func (p *DuckDuckGoProvider) Search(
|
||||
return nil, fmt.Errorf("duckduckgo API search failed: %w", apiErr)
|
||||
}
|
||||
|
||||
// searchHTML performs a web search using DuckDuckGo HTML endpoint
|
||||
func (p *DuckDuckGoProvider) searchHTML(
|
||||
ctx context.Context,
|
||||
query string,
|
||||
@@ -134,6 +135,7 @@ func (p *DuckDuckGoProvider) searchHTML(
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// searchAPI performs a web search using DuckDuckGo API endpoint
|
||||
func (p *DuckDuckGoProvider) searchAPI(
|
||||
ctx context.Context,
|
||||
query string,
|
||||
@@ -220,6 +222,7 @@ func (p *DuckDuckGoProvider) searchAPI(
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// cleanDDGURL cleans the URL from DuckDuckGo HTML endpoint
|
||||
func cleanDDGURL(urlStr string) string {
|
||||
if strings.HasPrefix(urlStr, "//duckduckgo.com/l/?uddg=") {
|
||||
trimmed := strings.TrimPrefix(urlStr, "//duckduckgo.com/l/?uddg=")
|
||||
@@ -241,6 +244,7 @@ func cleanDDGURL(urlStr string) string {
|
||||
return urlStr
|
||||
}
|
||||
|
||||
// extractTitle extracts the title from the text
|
||||
func extractTitle(text string) string {
|
||||
lines := strings.Split(text, "\n")
|
||||
if len(lines) > 0 {
|
||||
|
||||
@@ -52,7 +52,7 @@ func TestDuckDuckGoProvider_Name(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDuckDuckGoProvider_Search_HTMLSuccess(t *testing.T) {
|
||||
func TestDuckDuckGoProvider(t *testing.T) {
|
||||
// Minimal HTML page with two results, matching selectors used in searchHTML
|
||||
html := `
|
||||
<html>
|
||||
@@ -105,7 +105,7 @@ func TestDuckDuckGoProvider_Search_HTMLSuccess(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDuckDuckGoProvider_Search_APIFallback(t *testing.T) {
|
||||
func TestDuckDuckGoProvider_Fallback(t *testing.T) {
|
||||
// Simulate HTML returning non-OK to force API fallback, then a minimal API JSON
|
||||
apiResp := struct {
|
||||
AbstractText string `json:"AbstractText"`
|
||||
@@ -233,7 +233,7 @@ func TestDuckDuckGoProvider_Search_Real(t *testing.T) {
|
||||
|
||||
// TestDuckDuckGoProvider_Search_Real_Chinese tests the DuckDuckGo provider with Chinese query.
|
||||
// This verifies the Chinese language parameter (kl=cn-zh) works correctly.
|
||||
func TestDuckDuckGoProvider_Search_Real_Chinese(t *testing.T) {
|
||||
func TestDuckDuckGoProvider_SearchChinese(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping real DuckDuckGo integration test in short mode")
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ type FAQHandler struct {
|
||||
knowledgeService interfaces.KnowledgeService
|
||||
}
|
||||
|
||||
// NewFAQHandler creates a new FAQ handler.
|
||||
// NewFAQHandler creates a new FAQ handler
|
||||
func NewFAQHandler(knowledgeService interfaces.KnowledgeService) *FAQHandler {
|
||||
return &FAQHandler{knowledgeService: knowledgeService}
|
||||
}
|
||||
@@ -132,14 +132,17 @@ func (h *FAQHandler) UpdateEntryStatusBatch(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// faqDeleteRequest is a request for deleting FAQ entries in batch
|
||||
type faqDeleteRequest struct {
|
||||
IDs []string `json:"ids" binding:"required,min=1,dive,required"`
|
||||
}
|
||||
|
||||
// faqEntryStatusBatchRequest is a request for updating the enable status of FAQ entries in batch
|
||||
type faqEntryStatusBatchRequest struct {
|
||||
Updates map[string]bool `json:"updates" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
// faqEntryTagBatchRequest is a request for updating tags for FAQ entries in batch
|
||||
type faqEntryTagBatchRequest struct {
|
||||
Updates map[string]*string `json:"updates" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
+229
-178
@@ -351,12 +351,12 @@ func (h *InitializationHandler) InitializeByKB(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
kbIdStr := utils.SanitizeForLog(c.Param("kbId"))
|
||||
|
||||
var req InitializationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(ctx, "Failed to parse initialization request", err)
|
||||
c.Error(errors.NewBadRequestError(err.Error()))
|
||||
req, err := h.bindInitializationRequest(ctx, c)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(
|
||||
ctx,
|
||||
"Starting knowledge base configuration update, kbId: %s, request: %s",
|
||||
@@ -364,94 +364,152 @@ func (h *InitializationHandler) InitializeByKB(c *gin.Context) {
|
||||
utils.SanitizeForLog(utils.ToJSON(req)),
|
||||
)
|
||||
|
||||
// 获取指定知识库信息
|
||||
kb, err := h.getKnowledgeBaseForInitialization(ctx, kbIdStr)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.validateInitializationConfigs(ctx, req); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
processedModels, err := h.processInitializationModels(ctx, kb, kbIdStr, req)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
h.applyKnowledgeBaseInitialization(kb, req, processedModels)
|
||||
|
||||
if err := h.kbRepository.UpdateKnowledgeBase(ctx, kb); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{"kbId": utils.SanitizeForLog(kbIdStr)})
|
||||
c.Error(errors.NewInternalServerError("更新知识库配置失败: " + err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "知识库配置更新成功",
|
||||
"data": gin.H{
|
||||
"models": processedModels,
|
||||
"knowledge_base": kb,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (h *InitializationHandler) bindInitializationRequest(ctx context.Context, c *gin.Context) (*InitializationRequest, error) {
|
||||
var req InitializationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(ctx, "Failed to parse initialization request", err)
|
||||
return nil, errors.NewBadRequestError(err.Error())
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
func (h *InitializationHandler) getKnowledgeBaseForInitialization(ctx context.Context, kbIdStr string) (*types.KnowledgeBase, error) {
|
||||
kb, err := h.kbService.GetKnowledgeBaseByID(ctx, kbIdStr)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{"kbId": utils.SanitizeForLog(kbIdStr)})
|
||||
c.Error(errors.NewInternalServerError("获取知识库信息失败: " + err.Error()))
|
||||
return
|
||||
return nil, errors.NewInternalServerError("获取知识库信息失败: " + err.Error())
|
||||
}
|
||||
|
||||
if kb == nil {
|
||||
logger.Error(ctx, "Knowledge base not found")
|
||||
c.Error(errors.NewNotFoundError("知识库不存在"))
|
||||
return
|
||||
return nil, errors.NewNotFoundError("知识库不存在")
|
||||
}
|
||||
return kb, nil
|
||||
}
|
||||
|
||||
func (h *InitializationHandler) validateInitializationConfigs(ctx context.Context, req *InitializationRequest) error {
|
||||
if err := h.validateMultimodalConfig(ctx, req); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateRerankConfig(ctx, req); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateNodeExtractConfig(ctx, req)
|
||||
}
|
||||
|
||||
func (h *InitializationHandler) validateMultimodalConfig(ctx context.Context, req *InitializationRequest) error {
|
||||
if !req.Multimodal.Enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 验证多模态配置(如果启用)
|
||||
if req.Multimodal.Enabled {
|
||||
storageType := strings.ToLower(req.Multimodal.StorageType)
|
||||
if req.Multimodal.VLM == nil {
|
||||
logger.Error(ctx, "Multimodal enabled but missing VLM configuration")
|
||||
c.Error(errors.NewBadRequestError("启用多模态时需要配置VLM信息"))
|
||||
return
|
||||
}
|
||||
if req.Multimodal.VLM.InterfaceType == "ollama" {
|
||||
req.Multimodal.VLM.BaseURL = os.Getenv("OLLAMA_BASE_URL") + "/v1"
|
||||
}
|
||||
if req.Multimodal.VLM.ModelName == "" || req.Multimodal.VLM.BaseURL == "" {
|
||||
logger.Error(ctx, "VLM configuration incomplete")
|
||||
c.Error(errors.NewBadRequestError("VLM配置不完整"))
|
||||
return
|
||||
}
|
||||
switch storageType {
|
||||
case "cos":
|
||||
if req.Multimodal.COS == nil || req.Multimodal.COS.SecretID == "" || req.Multimodal.COS.SecretKey == "" ||
|
||||
req.Multimodal.COS.Region == "" || req.Multimodal.COS.BucketName == "" ||
|
||||
req.Multimodal.COS.AppID == "" {
|
||||
logger.Error(ctx, "COS configuration incomplete")
|
||||
c.Error(errors.NewBadRequestError("COS配置不完整"))
|
||||
return
|
||||
}
|
||||
case "minio":
|
||||
if req.Multimodal.Minio == nil || req.Multimodal.Minio.BucketName == "" ||
|
||||
os.Getenv("MINIO_ACCESS_KEY_ID") == "" || os.Getenv("MINIO_SECRET_ACCESS_KEY") == "" {
|
||||
logger.Error(ctx, "MinIO configuration incomplete")
|
||||
c.Error(errors.NewBadRequestError("MinIO配置不完整"))
|
||||
return
|
||||
}
|
||||
}
|
||||
storageType := strings.ToLower(req.Multimodal.StorageType)
|
||||
if req.Multimodal.VLM == nil {
|
||||
logger.Error(ctx, "Multimodal enabled but missing VLM configuration")
|
||||
return errors.NewBadRequestError("启用多模态时需要配置VLM信息")
|
||||
}
|
||||
if req.Multimodal.VLM.InterfaceType == "ollama" {
|
||||
req.Multimodal.VLM.BaseURL = os.Getenv("OLLAMA_BASE_URL") + "/v1"
|
||||
}
|
||||
if req.Multimodal.VLM.ModelName == "" || req.Multimodal.VLM.BaseURL == "" {
|
||||
logger.Error(ctx, "VLM configuration incomplete")
|
||||
return errors.NewBadRequestError("VLM配置不完整")
|
||||
}
|
||||
|
||||
// 验证Rerank配置(如果启用)
|
||||
if req.Rerank.Enabled {
|
||||
if req.Rerank.ModelName == "" || req.Rerank.BaseURL == "" {
|
||||
logger.Error(ctx, "Rerank configuration incomplete")
|
||||
c.Error(errors.NewBadRequestError("Rerank配置不完整"))
|
||||
return
|
||||
switch storageType {
|
||||
case "cos":
|
||||
if req.Multimodal.COS == nil || req.Multimodal.COS.SecretID == "" || req.Multimodal.COS.SecretKey == "" ||
|
||||
req.Multimodal.COS.Region == "" || req.Multimodal.COS.BucketName == "" ||
|
||||
req.Multimodal.COS.AppID == "" {
|
||||
logger.Error(ctx, "COS configuration incomplete")
|
||||
return errors.NewBadRequestError("COS配置不完整")
|
||||
}
|
||||
case "minio":
|
||||
if req.Multimodal.Minio == nil || req.Multimodal.Minio.BucketName == "" ||
|
||||
os.Getenv("MINIO_ACCESS_KEY_ID") == "" || os.Getenv("MINIO_SECRET_ACCESS_KEY") == "" {
|
||||
logger.Error(ctx, "MinIO configuration incomplete")
|
||||
return errors.NewBadRequestError("MinIO配置不完整")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 验证Node Extractor配置(如果启用)
|
||||
if strings.ToLower(os.Getenv("NEO4J_ENABLE")) != "true" && req.NodeExtract.Enabled {
|
||||
func validateRerankConfig(ctx context.Context, req *InitializationRequest) error {
|
||||
if !req.Rerank.Enabled {
|
||||
return nil
|
||||
}
|
||||
if req.Rerank.ModelName == "" || req.Rerank.BaseURL == "" {
|
||||
logger.Error(ctx, "Rerank configuration incomplete")
|
||||
return errors.NewBadRequestError("Rerank配置不完整")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateNodeExtractConfig(ctx context.Context, req *InitializationRequest) error {
|
||||
if !req.NodeExtract.Enabled {
|
||||
return nil
|
||||
}
|
||||
if strings.ToLower(os.Getenv("NEO4J_ENABLE")) != "true" {
|
||||
logger.Error(ctx, "Node Extractor configuration incomplete")
|
||||
c.Error(errors.NewBadRequestError("请正确配置环境变量NEO4J_ENABLE"))
|
||||
return
|
||||
return errors.NewBadRequestError("请正确配置环境变量NEO4J_ENABLE")
|
||||
}
|
||||
if req.NodeExtract.Enabled {
|
||||
if req.NodeExtract.Text == "" || len(req.NodeExtract.Tags) == 0 {
|
||||
logger.Error(ctx, "Node Extractor configuration incomplete")
|
||||
c.Error(errors.NewBadRequestError("Node Extractor配置不完整"))
|
||||
return
|
||||
}
|
||||
if len(req.NodeExtract.Nodes) == 0 || len(req.NodeExtract.Relations) == 0 {
|
||||
logger.Error(ctx, "Node Extractor configuration incomplete")
|
||||
c.Error(errors.NewBadRequestError("请先提取实体和关系"))
|
||||
return
|
||||
}
|
||||
if req.NodeExtract.Text == "" || len(req.NodeExtract.Tags) == 0 {
|
||||
logger.Error(ctx, "Node Extractor configuration incomplete")
|
||||
return errors.NewBadRequestError("Node Extractor配置不完整")
|
||||
}
|
||||
if len(req.NodeExtract.Nodes) == 0 || len(req.NodeExtract.Relations) == 0 {
|
||||
logger.Error(ctx, "Node Extractor configuration incomplete")
|
||||
return errors.NewBadRequestError("请先提取实体和关系")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 处理模型创建/更新
|
||||
modelsToProcess := []struct {
|
||||
modelType types.ModelType
|
||||
name string
|
||||
source types.ModelSource
|
||||
description string
|
||||
baseURL string
|
||||
apiKey string
|
||||
dimension int
|
||||
interfaceType string
|
||||
}{
|
||||
type modelDescriptor struct {
|
||||
modelType types.ModelType
|
||||
name string
|
||||
source types.ModelSource
|
||||
description string
|
||||
baseURL string
|
||||
apiKey string
|
||||
dimension int
|
||||
interfaceType string
|
||||
}
|
||||
|
||||
func buildModelDescriptors(req *InitializationRequest) []modelDescriptor {
|
||||
descriptors := []modelDescriptor{
|
||||
{
|
||||
modelType: types.ModelTypeKnowledgeQA,
|
||||
name: utils.SanitizeForLog(req.LLM.ModelName),
|
||||
@@ -471,18 +529,8 @@ func (h *InitializationHandler) InitializeByKB(c *gin.Context) {
|
||||
},
|
||||
}
|
||||
|
||||
// 如果启用Rerank,添加Rerank模型
|
||||
if req.Rerank.Enabled {
|
||||
modelsToProcess = append(modelsToProcess, struct {
|
||||
modelType types.ModelType
|
||||
name string
|
||||
source types.ModelSource
|
||||
description string
|
||||
baseURL string
|
||||
apiKey string
|
||||
dimension int
|
||||
interfaceType string
|
||||
}{
|
||||
descriptors = append(descriptors, modelDescriptor{
|
||||
modelType: types.ModelTypeRerank,
|
||||
name: utils.SanitizeForLog(req.Rerank.ModelName),
|
||||
source: types.ModelSourceRemote,
|
||||
@@ -492,18 +540,8 @@ func (h *InitializationHandler) InitializeByKB(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// 如果启用多模态,添加VLM模型
|
||||
if req.Multimodal.Enabled && req.Multimodal.VLM != nil {
|
||||
modelsToProcess = append(modelsToProcess, struct {
|
||||
modelType types.ModelType
|
||||
name string
|
||||
source types.ModelSource
|
||||
description string
|
||||
baseURL string
|
||||
apiKey string
|
||||
dimension int
|
||||
interfaceType string
|
||||
}{
|
||||
descriptors = append(descriptors, modelDescriptor{
|
||||
modelType: types.ModelTypeVLLM,
|
||||
name: utils.SanitizeForLog(req.Multimodal.VLM.ModelName),
|
||||
source: types.ModelSourceRemote,
|
||||
@@ -514,113 +552,116 @@ func (h *InitializationHandler) InitializeByKB(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// 处理模型
|
||||
return descriptors
|
||||
}
|
||||
|
||||
func (h *InitializationHandler) processInitializationModels(
|
||||
ctx context.Context,
|
||||
kb *types.KnowledgeBase,
|
||||
kbIdStr string,
|
||||
req *InitializationRequest,
|
||||
) ([]*types.Model, error) {
|
||||
descriptors := buildModelDescriptors(req)
|
||||
var processedModels []*types.Model
|
||||
for _, modelInfo := range modelsToProcess {
|
||||
model := &types.Model{
|
||||
Type: modelInfo.modelType,
|
||||
Name: modelInfo.name,
|
||||
Source: modelInfo.source,
|
||||
Description: modelInfo.description,
|
||||
Parameters: types.ModelParameters{
|
||||
BaseURL: modelInfo.baseURL,
|
||||
APIKey: modelInfo.apiKey,
|
||||
InterfaceType: modelInfo.interfaceType,
|
||||
},
|
||||
IsDefault: false,
|
||||
Status: types.ModelStatusActive,
|
||||
}
|
||||
|
||||
if modelInfo.modelType == types.ModelTypeEmbedding {
|
||||
model.Parameters.EmbeddingParameters = types.EmbeddingParameters{
|
||||
Dimension: modelInfo.dimension,
|
||||
}
|
||||
}
|
||||
for _, descriptor := range descriptors {
|
||||
model := descriptor.toModel()
|
||||
existingModelID := h.findExistingModelID(kb, descriptor.modelType)
|
||||
|
||||
// 根据模型类型从知识库获取已有的模型ID
|
||||
var existingModelID string
|
||||
switch modelInfo.modelType {
|
||||
case types.ModelTypeEmbedding:
|
||||
existingModelID = kb.EmbeddingModelID
|
||||
case types.ModelTypeKnowledgeQA:
|
||||
existingModelID = kb.SummaryModelID
|
||||
case types.ModelTypeVLLM:
|
||||
existingModelID = kb.VLMConfig.ModelID
|
||||
}
|
||||
|
||||
// 如果知识库中已经有对应类型的模型ID,尝试获取并更新
|
||||
var existingModel *types.Model
|
||||
if existingModelID != "" {
|
||||
var err error
|
||||
existingModel, err = h.modelService.GetModelByID(ctx, existingModelID)
|
||||
if err != nil {
|
||||
// 模型不存在或获取失败,记录日志但继续创建新模型
|
||||
logger.Warnf(ctx, "Failed to get existing model %s: %v, will create new one", existingModelID, err)
|
||||
existingModel = nil
|
||||
}
|
||||
}
|
||||
|
||||
if existingModel != nil {
|
||||
// 更新现有模型
|
||||
existingModel.Name = model.Name
|
||||
existingModel.Source = model.Source
|
||||
existingModel.Description = model.Description
|
||||
existingModel.Parameters = model.Parameters
|
||||
existingModel.UpdatedAt = time.Now()
|
||||
|
||||
err = h.modelService.UpdateModel(ctx, existingModel)
|
||||
if err != nil {
|
||||
if err := h.modelService.UpdateModel(ctx, existingModel); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"model_id": model.ID,
|
||||
"kb_id": kbIdStr,
|
||||
})
|
||||
c.Error(errors.NewInternalServerError("更新模型失败: " + err.Error()))
|
||||
return
|
||||
return nil, errors.NewInternalServerError("更新模型失败: " + err.Error())
|
||||
}
|
||||
processedModels = append(processedModels, existingModel)
|
||||
} else {
|
||||
// 创建新模型
|
||||
err = h.modelService.CreateModel(ctx, model)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"model_id": model.ID,
|
||||
"kb_id": kbIdStr,
|
||||
})
|
||||
c.Error(errors.NewInternalServerError("创建模型失败: " + err.Error()))
|
||||
return
|
||||
}
|
||||
processedModels = append(processedModels, model)
|
||||
}
|
||||
}
|
||||
|
||||
// 找到模型ID
|
||||
var embeddingModelID, llmModelID, vlmModelID string
|
||||
for _, model := range processedModels {
|
||||
if model == nil {
|
||||
continue
|
||||
}
|
||||
if model.Type == types.ModelTypeEmbedding {
|
||||
embeddingModelID = model.ID
|
||||
|
||||
if err := h.modelService.CreateModel(ctx, model); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"model_id": model.ID,
|
||||
"kb_id": kbIdStr,
|
||||
})
|
||||
return nil, errors.NewInternalServerError("创建模型失败: " + err.Error())
|
||||
}
|
||||
if model.Type == types.ModelTypeKnowledgeQA {
|
||||
llmModelID = model.ID
|
||||
}
|
||||
if model.Type == types.ModelTypeVLLM {
|
||||
vlmModelID = model.ID
|
||||
processedModels = append(processedModels, model)
|
||||
}
|
||||
|
||||
return processedModels, nil
|
||||
}
|
||||
|
||||
func (descriptor modelDescriptor) toModel() *types.Model {
|
||||
model := &types.Model{
|
||||
Type: descriptor.modelType,
|
||||
Name: descriptor.name,
|
||||
Source: descriptor.source,
|
||||
Description: descriptor.description,
|
||||
Parameters: types.ModelParameters{
|
||||
BaseURL: descriptor.baseURL,
|
||||
APIKey: descriptor.apiKey,
|
||||
InterfaceType: descriptor.interfaceType,
|
||||
},
|
||||
IsDefault: false,
|
||||
Status: types.ModelStatusActive,
|
||||
}
|
||||
|
||||
if descriptor.modelType == types.ModelTypeEmbedding {
|
||||
model.Parameters.EmbeddingParameters = types.EmbeddingParameters{
|
||||
Dimension: descriptor.dimension,
|
||||
}
|
||||
}
|
||||
|
||||
// 更新知识库配置
|
||||
return model
|
||||
}
|
||||
|
||||
func (h *InitializationHandler) findExistingModelID(kb *types.KnowledgeBase, modelType types.ModelType) string {
|
||||
switch modelType {
|
||||
case types.ModelTypeEmbedding:
|
||||
return kb.EmbeddingModelID
|
||||
case types.ModelTypeKnowledgeQA:
|
||||
return kb.SummaryModelID
|
||||
case types.ModelTypeVLLM:
|
||||
return kb.VLMConfig.ModelID
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (h *InitializationHandler) applyKnowledgeBaseInitialization(
|
||||
kb *types.KnowledgeBase,
|
||||
req *InitializationRequest,
|
||||
processedModels []*types.Model,
|
||||
) {
|
||||
embeddingModelID, llmModelID, vlmModelID := extractModelIDs(processedModels)
|
||||
|
||||
kb.SummaryModelID = llmModelID
|
||||
kb.EmbeddingModelID = embeddingModelID
|
||||
|
||||
// 更新文档分割配置
|
||||
kb.ChunkingConfig = types.ChunkingConfig{
|
||||
ChunkSize: req.DocumentSplitting.ChunkSize,
|
||||
ChunkOverlap: req.DocumentSplitting.ChunkOverlap,
|
||||
Separators: req.DocumentSplitting.Separators,
|
||||
}
|
||||
|
||||
// 更新多模态配置
|
||||
if req.Multimodal.Enabled {
|
||||
kb.VLMConfig = types.VLMConfig{
|
||||
Enabled: req.Multimodal.Enabled,
|
||||
@@ -677,22 +718,23 @@ func (h *InitializationHandler) InitializeByKB(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = h.kbRepository.UpdateKnowledgeBase(ctx, kb)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{"kbId": utils.SanitizeForLog(kbIdStr)})
|
||||
c.Error(errors.NewInternalServerError("更新知识库配置失败: " + err.Error()))
|
||||
return
|
||||
func extractModelIDs(processedModels []*types.Model) (embeddingModelID, llmModelID, vlmModelID string) {
|
||||
for _, model := range processedModels {
|
||||
if model == nil {
|
||||
continue
|
||||
}
|
||||
switch model.Type {
|
||||
case types.ModelTypeEmbedding:
|
||||
embeddingModelID = model.ID
|
||||
case types.ModelTypeKnowledgeQA:
|
||||
llmModelID = model.ID
|
||||
case types.ModelTypeVLLM:
|
||||
vlmModelID = model.ID
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "知识库配置更新成功",
|
||||
"data": gin.H{
|
||||
"models": processedModels,
|
||||
"knowledge_base": kb,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// CheckOllamaStatus 检查Ollama服务状态
|
||||
@@ -1787,6 +1829,7 @@ type TextRelationExtractionResponse struct {
|
||||
Relations []*types.GraphRelation `json:"relations"`
|
||||
}
|
||||
|
||||
// ExtractTextRelations extracts text relations from text
|
||||
func (h *InitializationHandler) ExtractTextRelations(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
@@ -1869,15 +1912,18 @@ func (h *InitializationHandler) extractRelationsFromText(
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// FabriTextRequest is a request for generating example text
|
||||
type FabriTextRequest struct {
|
||||
Tags []string `json:"tags"`
|
||||
LLMConfig LLMConfig `json:"llmConfig"`
|
||||
}
|
||||
|
||||
// FabriTextResponse is a response for generating example text
|
||||
type FabriTextResponse struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
// FabriText generates example text
|
||||
func (h *InitializationHandler) FabriText(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
@@ -1901,6 +1947,7 @@ func (h *InitializationHandler) FabriText(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// fabriText generates example text
|
||||
func (h *InitializationHandler) fabriText(ctx context.Context, tags []string, llm LLMConfig) (string, error) {
|
||||
chatModel, err := chat.NewChat(&chat.ChatConfig{
|
||||
ModelID: "initialization",
|
||||
@@ -1935,10 +1982,12 @@ func (h *InitializationHandler) fabriText(ctx context.Context, tags []string, ll
|
||||
return result.Content, nil
|
||||
}
|
||||
|
||||
// FabriTagRequest is a request for generating tags
|
||||
type FabriTagRequest struct {
|
||||
LLMConfig LLMConfig `json:"llmConfig"`
|
||||
}
|
||||
|
||||
// FabriTagResponse is a response for generating tags
|
||||
type FabriTagResponse struct {
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
@@ -1947,6 +1996,7 @@ var tagOptions = []string{
|
||||
"内容", "文化", "人物", "事件", "时间", "地点", "作品", "作者", "关系", "属性",
|
||||
}
|
||||
|
||||
// FabriTag generates tags
|
||||
func (h *InitializationHandler) FabriTag(c *gin.Context) {
|
||||
tagRandom := RandomSelect(tagOptions, rand.Intn(len(tagOptions)-1)+1)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -1955,6 +2005,7 @@ func (h *InitializationHandler) FabriTag(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// RandomSelect selects random strings
|
||||
func RandomSelect(strs []string, n int) []string {
|
||||
if n <= 0 {
|
||||
return []string{}
|
||||
|
||||
@@ -159,7 +159,7 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
if requestJSON, err := json.Marshal(request); err == nil {
|
||||
logger.Infof(ctx, "Agent QA request, request: %s", secutils.SanitizeForLog(string(requestJSON)))
|
||||
} else {
|
||||
logger.Warnf(ctx, "Agent QA request received but failed to marshal for logging: %s", secutils.SanitizeForLog(err.Error()))
|
||||
logger.Warnf(ctx, "failed to marshal for logging: %s", secutils.SanitizeForLog(err.Error()))
|
||||
}
|
||||
|
||||
// Validate query content
|
||||
|
||||
@@ -431,7 +431,7 @@ func (h *TenantHandler) UpdateTenantKV(c *gin.Context) {
|
||||
h.updateTenantWebSearchConfigInternal(c)
|
||||
return
|
||||
case "conversation-config":
|
||||
h.updateTenantConversationConfigInternal(c)
|
||||
h.updateTenantConversationInternal(c)
|
||||
return
|
||||
default:
|
||||
logger.Info(ctx, "KV key not supported", "key", key)
|
||||
@@ -664,9 +664,9 @@ func (h *TenantHandler) GetTenantConversationConfig(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// updateTenantConversationConfigInternal updates the conversation configuration for a tenant
|
||||
// updateTenantConversationInternal updates the conversation configuration for a tenant
|
||||
// This sets the global conversation configuration for normal mode sessions in this tenant
|
||||
func (h *TenantHandler) updateTenantConversationConfigInternal(c *gin.Context) {
|
||||
func (h *TenantHandler) updateTenantConversationInternal(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
logger.Info(ctx, "Start updating tenant conversation config")
|
||||
|
||||
|
||||
@@ -43,7 +43,8 @@ type ChunkRepository interface {
|
||||
CountChunksByKnowledgeBaseID(ctx context.Context, tenantID uint64, kbID string) (int64, error)
|
||||
// DeleteUnindexedChunks deletes unindexed chunks by knowledge id and chunk index range
|
||||
DeleteUnindexedChunks(ctx context.Context, tenantID uint64, knowledgeID string) ([]*types.Chunk, error)
|
||||
// ListAllFAQChunksByKnowledgeID lists all FAQ chunks for a knowledge ID (only ID and ContentHash fields for efficiency)
|
||||
// ListAllFAQChunksByKnowledgeID lists all FAQ chunks for a knowledge ID
|
||||
// only ID and ContentHash fields for efficiency
|
||||
ListAllFAQChunksByKnowledgeID(ctx context.Context, tenantID uint64, knowledgeID string) ([]*types.Chunk, error)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
// Extracter is a interface for extracting entities
|
||||
type Extracter interface {
|
||||
// Extract extracts entities from a task
|
||||
Extract(ctx context.Context, t *asynq.Task) error
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ type ModelRepository interface {
|
||||
Update(ctx context.Context, model *types.Model) error
|
||||
// Delete deletes a model
|
||||
Delete(ctx context.Context, tenantID uint64, id string) error
|
||||
// ClearDefaultByType clears the default flag for all models of a specific type, optionally excluding a specific model ID
|
||||
// ClearDefaultByType clears the default flag for all models of a specific type
|
||||
// optionally excluding a specific model ID.
|
||||
ClearDefaultByType(ctx context.Context, tenantID uint, modelType types.ModelType, excludeID string) error
|
||||
}
|
||||
|
||||
@@ -6,8 +6,12 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// RetrieveGraphRepository is a repository for retrieving graphs
|
||||
type RetrieveGraphRepository interface {
|
||||
// AddGraph adds a graph to the repository
|
||||
AddGraph(ctx context.Context, namespace types.NameSpace, graphs []*types.GraphData) error
|
||||
// DelGraph deletes a graph from the repository
|
||||
DelGraph(ctx context.Context, namespace []types.NameSpace) error
|
||||
// SearchNode searches for nodes in the repository
|
||||
SearchNode(ctx context.Context, namespace types.NameSpace, nodes []string) (*types.GraphData, error)
|
||||
}
|
||||
|
||||
@@ -32,7 +32,8 @@ type SessionService interface {
|
||||
// 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, webSearchEnabled bool, 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
|
||||
@@ -62,7 +63,8 @@ type SessionService interface {
|
||||
seenURLs map[string]bool,
|
||||
knowledgeIDs []string,
|
||||
)
|
||||
// DeleteWebSearchTempKBState deletes the temporary KB state for web search from Redis and cleans up associated knowledge base and knowledge items
|
||||
// DeleteWebSearchTempKBState deletes the temporary KB state for web search from Redis
|
||||
// and cleans up associated knowledge base and knowledge items
|
||||
DeleteWebSearchTempKBState(ctx context.Context, sessionID string) error
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,18 @@ import (
|
||||
|
||||
// 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 returns the name of the provider
|
||||
Name() string
|
||||
// Search performs a web search
|
||||
Search(ctx context.Context, query string, maxResults int, includeDate bool) ([]*types.WebSearchResult, error)
|
||||
}
|
||||
|
||||
// WebSearchService defines the interface for web search services
|
||||
type WebSearchService interface {
|
||||
// Search performs a web search
|
||||
Search(ctx context.Context, config *types.WebSearchConfig, query string) ([]*types.WebSearchResult, error)
|
||||
// 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.
|
||||
CompressWithRAG(ctx context.Context, sessionID string, tempKBID string, questions []string,
|
||||
webSearchResults []*types.WebSearchResult, cfg *types.WebSearchConfig,
|
||||
kbSvc KnowledgeBaseService, knowSvc KnowledgeService,
|
||||
|
||||
@@ -2,6 +2,7 @@ package utils
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// ToJSON converts a value to a JSON string
|
||||
func ToJSON(v interface{}) string {
|
||||
json, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user