Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat: 优化知识块分页查询功能,支持按知识类型区分搜索和排序逻辑

wizardchen3905b6fe5d6c
diff --git a/internal/agent/tools/get_document_info.go b/internal/agent/tools/get_document_info.go
index c5c7b9b2a..c4ddebd60 100644
--- a/internal/agent/tools/get_document_info.go
+++ b/internal/agent/tools/get_document_info.go
@@ -139,7 +139,7 @@ func (t *GetDocumentInfoTool) Execute(ctx context.Context, args map[string]inter
ListPagedChunksByKnowledgeID(ctx, t.tenantID, id, &types.Pagination{
Page: 1,
PageSize: 1000,
- }, []types.ChunkType{"text"}, "", "", "", "")
+ }, []types.ChunkType{"text"}, "", "", "", "", "")
if err != nil {
mu.Lock()
results[id] = &docInfo{
diff --git a/internal/agent/tools/knowledge_search.go b/internal/agent/tools/knowledge_search.go
index 5ef49053c..dbdcd8cc0 100644
--- a/internal/agent/tools/knowledge_search.go
+++ b/internal/agent/tools/knowledge_search.go
@@ -1052,7 +1052,7 @@ func (t *KnowledgeSearchTool) formatOutput(
_, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx,
t.tenantID, result.KnowledgeID,
&types.Pagination{Page: 1, PageSize: 1},
- []types.ChunkType{types.ChunkTypeText}, "", "", "", "",
+ []types.ChunkType{types.ChunkTypeText}, "", "", "", "", "",
)
if err != nil {
logger.Warnf(
diff --git a/internal/agent/tools/list_knowledge_chunks.go b/internal/agent/tools/list_knowledge_chunks.go
index 0747ee906..80998e9c9 100644
--- a/internal/agent/tools/list_knowledge_chunks.go
+++ b/internal/agent/tools/list_knowledge_chunks.go
@@ -117,7 +117,7 @@ func (t *ListKnowledgeChunksTool) Execute(ctx context.Context, args map[string]i
}
chunks, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx,
- t.tenantID, knowledgeID, pagination, []types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ}, "", "", "", "")
+ t.tenantID, knowledgeID, pagination, []types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ}, "", "", "", "", "")
if err != nil {
return &types.ToolResult{
Success: false,
diff --git a/internal/application/repository/chunk.go b/internal/application/repository/chunk.go
index 11b60ee28..919aa8bea 100644
--- a/internal/application/repository/chunk.go
+++ b/internal/application/repository/chunk.go
@@ -82,6 +82,7 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
keyword string,
searchField string,
sortOrder string,
+ knowledgeType string,
) ([]*types.Chunk, int64, error) {
var chunks []*types.Chunk
var total int64
@@ -98,6 +99,14 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
}
if keyword != "" {
like := "%" + keyword + "%"
+
+ // Document type: search content only
+ if knowledgeType != types.KnowledgeTypeFAQ {
+ db = db.Where("content LIKE ?", like)
+ return db
+ }
+
+ // FAQ type: search based on searchField
// 根据数据库类型使用不同的 JSON 查询语法
isPostgres := db.Dialector.Name() == "postgres"
@@ -147,10 +156,20 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
// Then query the paginated data
dataQuery := baseFilter(r.db.WithContext(ctx))
- // Default is time descending, "asc" for time ascending
- orderClause := "updated_at DESC"
- if sortOrder == "asc" {
- orderClause = "updated_at ASC"
+ // Determine sort order based on knowledge type
+ var orderClause string
+ if knowledgeType == types.KnowledgeTypeFAQ {
+ // FAQ: sort by updated_at
+ orderClause = "updated_at DESC"
+ if sortOrder == "asc" {
+ orderClause = "updated_at ASC"
+ }
+ } else {
+ // Document: sort by chunk_index
+ orderClause = "chunk_index ASC"
+ if sortOrder == "desc" {
+ orderClause = "chunk_index DESC"
+ }
}
if err := dataQuery.
diff --git a/internal/application/service/chunk.go b/internal/application/service/chunk.go
index c73107e71..6dc796765 100644
--- a/internal/application/service/chunk.go
+++ b/internal/application/service/chunk.go
@@ -153,6 +153,7 @@ func (s *chunkService) ListPagedChunksByKnowledgeID(ctx context.Context,
"",
"",
"",
+ "",
)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go
index 29a427698..a9594104a 100644
--- a/internal/application/service/knowledge.go
+++ b/internal/application/service/knowledge.go
@@ -2550,6 +2550,7 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl
"",
"",
"",
+ "",
)
chunkPage++
if err != nil {
@@ -2664,7 +2665,7 @@ func (s *knowledgeService) ListFAQEntries(ctx context.Context,
}
chunkType := []types.ChunkType{types.ChunkTypeFAQ}
chunks, total, err := s.chunkRepo.ListPagedChunksByKnowledgeID(
- ctx, tenantID, faqKnowledge.ID, page, chunkType, tagID, keyword, searchField, sortOrder,
+ ctx, tenantID, faqKnowledge.ID, page, chunkType, tagID, keyword, searchField, sortOrder, types.KnowledgeTypeFAQ,
)
if err != nil {
return nil, err
diff --git a/internal/types/custom_agent.go b/internal/types/custom_agent.go
index 914be167d..49f9c91c7 100644
--- a/internal/types/custom_agent.go
+++ b/internal/types/custom_agent.go
@@ -291,259 +291,6 @@ func GetBuiltinSmartReasoningAgent(tenantID uint64) *CustomAgent {
}
}
-// GetBuiltinDeepResearcherAgent returns the built-in deep researcher agent
-// This agent is optimized for in-depth research and comprehensive analysis
-func GetBuiltinDeepResearcherAgent(tenantID uint64) *CustomAgent {
- return &CustomAgent{
- ID: BuiltinDeepResearcherID,
- Name: "深度研究员",
- Description: "专注于深度研究和综合分析,能够制定研究计划、多维度检索信息、深入思考并给出全面的分析报告",
- Avatar: "🔬",
- IsBuiltin: true,
- TenantID: tenantID,
- Config: CustomAgentConfig{
- AgentMode: AgentModeSmartReasoning,
- SystemPrompt: `你是一位专业的深度研究员,擅长进行系统性的研究和综合分析。
-
-## 核心能力
-- 制定结构化的研究计划
-- 多维度信息检索和交叉验证
-- 深入思考和逻辑推理
-- 综合分析和报告撰写
-
-## 工作流程
-1. **理解问题**:深入分析用户的研究需求,明确研究目标和范围
-2. **制定计划**:使用 todo_write 工具制定详细的研究计划
-3. **信息收集**:
- - 使用 knowledge_search 进行语义搜索获取相关文档
- - 使用 grep_chunks 进行关键词精确搜索
- - 使用 query_knowledge_graph 探索实体关系
- - 必要时使用网络搜索获取最新信息
-4. **深度分析**:使用 thinking 工具进行深入思考和推理
-5. **综合报告**:整合所有信息,给出结构化的研究报告
-
-## 输出要求
-- 研究报告应包含:背景、方法、发现、分析、结论
-- 引用具体的信息来源
-- 指出信息的可靠性和局限性
-- 提供进一步研究的建议`,
- Temperature: 0.5,
- MaxCompletionTokens: 4096,
- MaxIterations: 30,
- KBSelectionMode: "all",
- AllowedTools: []string{"thinking", "todo_write", "knowledge_search", "grep_chunks", "list_knowledge_chunks", "query_knowledge_graph", "get_document_info"},
- WebSearchEnabled: true,
- WebSearchMaxResults: 10,
- ReflectionEnabled: true,
- MultiTurnEnabled: true,
- HistoryTurns: 10,
- // FAQ strategy
- FAQPriorityEnabled: true,
- FAQDirectAnswerThreshold: 0.9,
- FAQScoreBoost: 1.2,
- // Retrieval strategy - more comprehensive
- EmbeddingTopK: 20,
- KeywordThreshold: 0.2,
- VectorThreshold: 0.4,
- RerankTopK: 15,
- RerankThreshold: 0.25,
- },
- }
-}
-
-// GetBuiltinDataAnalystAgent returns the built-in data analyst agent
-// This agent is optimized for database queries and data analysis
-func GetBuiltinDataAnalystAgent(tenantID uint64) *CustomAgent {
- return &CustomAgent{
- ID: BuiltinDataAnalystID,
- Name: "数据分析师",
- Description: "专注于数据库查询和数据分析,能够理解业务需求、构建SQL查询、分析数据并提供洞察",
- Avatar: "📊",
- IsBuiltin: true,
- TenantID: tenantID,
- Config: CustomAgentConfig{
- AgentMode: AgentModeSmartReasoning,
- SystemPrompt: `你是一位专业的数据分析师,擅长数据库查询和数据分析。
-
-## 核心能力
-- 理解业务需求并转化为数据查询
-- 构建高效的 SQL 查询语句
-- 数据分析和可视化建议
-- 提供数据驱动的业务洞察
-
-## 工作流程
-1. **需求理解**:
- - 明确用户的数据分析目标
- - 确定需要查询的数据范围和维度
-2. **数据探索**:
- - 使用 database_query 查询相关数据
- - 使用 get_document_info 了解数据结构和元数据
-3. **数据分析**:
- - 使用 thinking 工具进行数据分析和推理
- - 识别数据模式和趋势
-4. **结果呈现**:
- - 清晰展示查询结果
- - 提供数据解读和业务建议
-
-## 输出要求
-- 解释 SQL 查询的逻辑
-- 以表格或结构化方式展示数据
-- 提供数据的业务含义解读
-- 指出数据的局限性和注意事项
-- 建议后续分析方向`,
- Temperature: 0.3,
- MaxCompletionTokens: 2048,
- MaxIterations: 20,
- KBSelectionMode: "all",
- AllowedTools: []string{"thinking", "todo_write", "database_query", "knowledge_search", "get_document_info"},
- WebSearchEnabled: false,
- WebSearchMaxResults: 5,
- ReflectionEnabled: false,
- MultiTurnEnabled: true,
- HistoryTurns: 5,
- // FAQ strategy
- FAQPriorityEnabled: true,
- FAQDirectAnswerThreshold: 0.9,
- FAQScoreBoost: 1.2,
- // Retrieval strategy
- EmbeddingTopK: 10,
- KeywordThreshold: 0.3,
- VectorThreshold: 0.5,
- RerankTopK: 5,
- RerankThreshold: 0.3,
- },
- }
-}
-
-// GetBuiltinKnowledgeGraphExpertAgent returns the built-in knowledge graph expert agent
-// This agent is optimized for knowledge graph exploration and relationship analysis
-func GetBuiltinKnowledgeGraphExpertAgent(tenantID uint64) *CustomAgent {
- return &CustomAgent{
- ID: BuiltinKnowledgeGraphExpertID,
- Name: "知识图谱专家",
- Description: "专注于知识图谱查询和关系分析,能够探索实体关系、发现隐藏联系并构建知识网络",
- Avatar: "🕸️",
- IsBuiltin: true,
- TenantID: tenantID,
- Config: CustomAgentConfig{
- AgentMode: AgentModeSmartReasoning,
- SystemPrompt: `你是一位知识图谱专家,擅长探索实体关系和构建知识网络。
-
-## 核心能力
-- 实体识别和关系发现
-- 知识图谱查询和遍历
-- 关系链分析和推理
-- 知识网络可视化建议
-
-## 工作流程
-1. **实体识别**:
- - 从用户问题中识别关键实体
- - 确定需要探索的关系类型
-2. **图谱查询**:
- - 使用 query_knowledge_graph 查询实体关系
- - 探索多跳关系和间接联系
-3. **关系分析**:
- - 使用 thinking 工具分析关系模式
- - 发现隐藏的关联和规律
-4. **知识整合**:
- - 结合 knowledge_search 获取更多上下文
- - 构建完整的知识图景
-
-## 输出要求
-- 清晰展示实体和关系
-- 使用图形化描述(如 A -> 关系 -> B)
-- 解释关系的含义和重要性
-- 指出可能的推理和假设
-- 建议进一步探索的方向`,
- Temperature: 0.5,
- MaxCompletionTokens: 2048,
- MaxIterations: 25,
- KBSelectionMode: "all",
- AllowedTools: []string{"thinking", "todo_write", "query_knowledge_graph", "knowledge_search", "grep_chunks", "get_document_info"},
- WebSearchEnabled: false,
- WebSearchMaxResults: 5,
- ReflectionEnabled: true,
- MultiTurnEnabled: true,
- HistoryTurns: 5,
- // FAQ strategy
- FAQPriorityEnabled: true,
- FAQDirectAnswerThreshold: 0.9,
- FAQScoreBoost: 1.2,
- // Retrieval strategy
- EmbeddingTopK: 15,
- KeywordThreshold: 0.3,
- VectorThreshold: 0.4,
- RerankTopK: 10,
- RerankThreshold: 0.3,
- },
- }
-}
-
-// GetBuiltinDocumentAssistantAgent returns the built-in document assistant agent
-// This agent is optimized for document retrieval, organization and summarization
-func GetBuiltinDocumentAssistantAgent(tenantID uint64) *CustomAgent {
- return &CustomAgent{
- ID: BuiltinDocumentAssistantID,
- Name: "文档助手",
- Description: "专注于文档检索和内容整理,能够快速定位文档、提取关键信息并生成摘要",
- Avatar: "📚",
- IsBuiltin: true,
- TenantID: tenantID,
- Config: CustomAgentConfig{
- AgentMode: AgentModeSmartReasoning,
- SystemPrompt: `你是一位专业的文档助手,擅长文档检索、信息提取和内容整理。
-
-## 核心能力
-- 快速定位相关文档和内容
-- 提取文档关键信息
-- 生成结构化摘要
-- 文档对比和整合
-
-## 工作流程
-1. **需求分析**:
- - 理解用户的文档检索需求
- - 确定搜索关键词和范围
-2. **文档检索**:
- - 使用 knowledge_search 语义搜索相关内容
- - 使用 grep_chunks 精确匹配关键词
- - 使用 get_document_info 获取文档元信息
-3. **内容处理**:
- - 使用 list_knowledge_chunks 查看完整内容
- - 使用 thinking 工具整理和分析信息
-4. **结果输出**:
- - 提供结构化的信息摘要
- - 标注信息来源和位置
-
-## 输出要求
-- 清晰标注信息来源(文档名、位置)
-- 使用结构化格式展示信息
-- 区分直接引用和总结内容
-- 指出信息的完整性和可能遗漏
-- 提供相关文档的导航建议`,
- Temperature: 0.3,
- MaxCompletionTokens: 2048,
- MaxIterations: 20,
- KBSelectionMode: "all",
- AllowedTools: []string{"thinking", "knowledge_search", "grep_chunks", "list_knowledge_chunks", "get_document_info"},
- WebSearchEnabled: false,
- WebSearchMaxResults: 5,
- ReflectionEnabled: false,
- MultiTurnEnabled: true,
- HistoryTurns: 5,
- // FAQ strategy
- FAQPriorityEnabled: true,
- FAQDirectAnswerThreshold: 0.9,
- FAQScoreBoost: 1.2,
- // Retrieval strategy - focused on precision
- EmbeddingTopK: 15,
- KeywordThreshold: 0.25,
- VectorThreshold: 0.45,
- RerankTopK: 10,
- RerankThreshold: 0.3,
- },
- }
-}
-
// Deprecated: Use GetBuiltinQuickAnswerAgent instead
func GetBuiltinNormalAgent(tenantID uint64) *CustomAgent {
return GetBuiltinQuickAnswerAgent(tenantID)
@@ -556,12 +303,8 @@ func GetBuiltinAgentAgent(tenantID uint64) *CustomAgent {
// BuiltinAgentRegistry provides a registry of all built-in agents for easy extension
var BuiltinAgentRegistry = map[string]func(uint64) *CustomAgent{
- BuiltinQuickAnswerID: GetBuiltinQuickAnswerAgent,
- BuiltinSmartReasoningID: GetBuiltinSmartReasoningAgent,
- BuiltinDeepResearcherID: GetBuiltinDeepResearcherAgent,
- BuiltinDataAnalystID: GetBuiltinDataAnalystAgent,
- BuiltinKnowledgeGraphExpertID: GetBuiltinKnowledgeGraphExpertAgent,
- BuiltinDocumentAssistantID: GetBuiltinDocumentAssistantAgent,
+ BuiltinQuickAnswerID: GetBuiltinQuickAnswerAgent,
+ BuiltinSmartReasoningID: GetBuiltinSmartReasoningAgent,
}
// builtinAgentIDsOrdered defines the fixed display order of built-in agents
diff --git a/internal/types/interfaces/chunk.go b/internal/types/interfaces/chunk.go
index 728260539..b9d5568ef 100644
--- a/internal/types/interfaces/chunk.go
+++ b/internal/types/interfaces/chunk.go
@@ -18,8 +18,11 @@ type ChunkRepository interface {
ListChunksByKnowledgeID(ctx context.Context, tenantID uint64, knowledgeID string) ([]*types.Chunk, error)
// ListPagedChunksByKnowledgeID lists paged chunks by knowledge id.
// When tagID is non-empty, results are filtered by tag_id.
- // sortOrder: "asc" for time ascending (updated_at ASC), default is time descending (updated_at DESC)
- // searchField: specifies which field to search in (for FAQ: "standard_question", "similar_questions", "answers", "" for all)
+ // knowledgeType: "faq" or "manual" - determines sort order and search behavior
+ // - FAQ: sorts by updated_at, searchField can be "standard_question", "similar_questions", "answers", or "" for all
+ // - Document (manual): sorts by chunk_index, keyword searches content only
+ // sortOrder: "asc" for ascending, default is descending
+ // searchField: specifies which field to search in (only applicable for FAQ type)
ListPagedChunksByKnowledgeID(
ctx context.Context,
tenantID uint64,
@@ -30,6 +33,7 @@ type ChunkRepository interface {
keyword string,
searchField string,
sortOrder string,
+ knowledgeType string,
) ([]*types.Chunk, int64, error)
ListChunkByParentID(ctx context.Context, tenantID uint64, parentID string) ([]*types.Chunk, error)
// UpdateChunk updates a chunk