feat: Add statistics localization and enhance knowledge base functionality

- Introduced new localization strings for document count, FAQ entries, and chunk count in English, Russian, and Chinese.
- Updated the KnowledgeBase and KnowledgeBaseList components to display knowledge and chunk counts.
- Implemented backend functions to count knowledge items and chunks associated with each knowledge base, improving data management and user insights.
This commit is contained in:
wizardchen
2025-11-21 01:39:43 +08:00
parent c678094724
commit 9e98848d69
12 changed files with 72 additions and 4 deletions
+5
View File
@@ -750,6 +750,11 @@ export default {
features: {
knowledgeGraph: 'Knowledge Graph Enabled',
multimodal: 'Multimodal Enabled'
},
stats: {
documents: 'Document Count',
faqEntries: 'FAQ Entries',
chunks: 'Chunk Count'
}
},
knowledgeEditor: {
+5
View File
@@ -839,6 +839,11 @@ export default {
features: {
knowledgeGraph: 'Граф знаний включен',
multimodal: 'Мультимодальность включена'
},
stats: {
documents: 'Количество документов',
faqEntries: 'FAQ записи',
chunks: 'Количество фрагментов'
}
},
knowledgeEditor: {
+5
View File
@@ -1075,6 +1075,11 @@ export default {
knowledgeGraph: "已启用知识图谱",
multimodal: "已启用多模态",
},
stats: {
documents: "文档数量",
faqEntries: "问答条目",
chunks: "分块数量",
},
},
knowledgeEditor: {
titleCreate: "新建知识库",
@@ -678,7 +678,7 @@ const knowledgeDropdownOptions = computed(() =>
knowledgeList.value.map((item) => ({
content: item.name,
value: item.id,
prefixIcon: () => h(TIcon, { name: item.type === 'faq' ? 'help-circle' : 'folder', size: '16px' }),
prefixIcon: () => h(TIcon, { name: item.type === 'faq' ? 'chat-bubble-help' : 'folder', size: '16px' }),
}))
);
@@ -74,7 +74,10 @@
<div class="bottom-left">
<div class="type-badge" :class="{ 'document': (kb.type || 'document') === 'document', 'faq': kb.type === 'faq' }">
<t-icon :name="kb.type === 'faq' ? 'chat-bubble-help' : 'folder'" size="14px" />
<span>{{ kb.type === 'faq' ? $t('knowledgeEditor.basic.typeFAQ') : $t('knowledgeEditor.basic.typeDocument') }}</span>
<span>
{{ kb.type === 'faq' ? $t('knowledgeEditor.basic.typeFAQ') : $t('knowledgeEditor.basic.typeDocument') }}
({{ kb.type === 'faq' ? (kb.chunk_count || 0) : (kb.knowledge_count || 0) }})
</span>
</div>
<div class="feature-badges">
<t-tooltip v-if="kb.extract_config?.enabled" :content="$t('knowledgeList.features.knowledgeGraph')" placement="top">
@@ -167,6 +170,8 @@ interface KB {
vlm_config?: { enabled?: boolean; model_id?: string };
extract_config?: { enabled?: boolean };
cos_config?: { provider?: string; bucket_name?: string };
knowledge_count?: number;
chunk_count?: number;
}
const kbs = ref<KB[]>([])
@@ -1097,11 +1097,10 @@ const kbInfo = ref<any>(null)
const knowledgeList = ref<Array<{ id: string; name: string; type?: string }>>([])
const knowledgeDropdownOptions = computed(() =>
knowledgeList.value
.filter((item) => !item.type || item.type === 'document')
.map((item) => ({
content: item.name,
value: item.id,
prefixIcon: () => h(TIcon, { name: 'file', size: '16px' }),
prefixIcon: () => h(TIcon, { name: item.type === 'document' ? 'folder' : 'chat-bubble-help', size: '16px' }),
})),
)
+9
View File
@@ -154,3 +154,12 @@ func (r *chunkRepository) DeleteByKnowledgeList(ctx context.Context, tenantID ui
"tenant_id = ? AND knowledge_id in ?", tenantID, knowledgeIDs,
).Delete(&types.Chunk{}).Error
}
// CountChunksByKnowledgeBaseID counts the number of chunks in a knowledge base
func (r *chunkRepository) CountChunksByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) (int64, error) {
var count int64
err := r.db.WithContext(ctx).Model(&types.Chunk{}).
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
Count(&count).Error
return count, err
}
@@ -220,3 +220,12 @@ func (r *knowledgeRepository) UpdateKnowledgeColumn(ctx context.Context, id stri
err := r.db.WithContext(ctx).Model(&types.Knowledge{}).Where("id = ?", id).Update(column, value).Error
return err
}
// CountKnowledgeByKnowledgeBaseID counts the number of knowledge items in a knowledge base
func (r *knowledgeRepository) CountKnowledgeByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) (int64, error) {
var count int64
err := r.db.WithContext(ctx).Model(&types.Knowledge{}).
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
Count(&count).Error
return count, err
}
@@ -122,6 +122,29 @@ func (s *knowledgeBaseService) ListKnowledgeBases(ctx context.Context) ([]*types
return nil, err
}
// Query knowledge count and chunk count for each knowledge base
for _, kb := range kbs {
kb.EnsureDefaults()
// Get knowledge count
if kb.Type == types.KnowledgeBaseTypeDocument {
knowledgeCount, err := s.kgRepo.CountKnowledgeByKnowledgeBaseID(ctx, tenantID, kb.ID)
if err != nil {
logger.Warnf(ctx, "Failed to get knowledge count for knowledge base %s: %v", kb.ID, err)
} else {
kb.KnowledgeCount = knowledgeCount
}
} else if kb.Type == types.KnowledgeBaseTypeFAQ {
// Get chunk count
chunkCount, err := s.chunkRepo.CountChunksByKnowledgeBaseID(ctx, tenantID, kb.ID)
if err != nil {
logger.Warnf(ctx, "Failed to get chunk count for knowledge base %s: %v", kb.ID, err)
} else {
kb.ChunkCount = chunkCount
}
}
}
logger.Infof(
ctx,
"Knowledge base list retrieved successfully, tenant ID: %d, knowledge base count: %d",
+2
View File
@@ -37,6 +37,8 @@ type ChunkRepository interface {
DeleteChunksByKnowledgeID(ctx context.Context, tenantID uint, knowledgeID string) error
// DeleteByKnowledgeList deletes all chunks for a knowledge list
DeleteByKnowledgeList(ctx context.Context, tenantID uint, knowledgeIDs []string) error
// CountChunksByKnowledgeBaseID counts the number of chunks in a knowledge base.
CountChunksByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) (int64, error)
}
// ChunkService defines the interface for chunk service operations
+2
View File
@@ -102,4 +102,6 @@ type KnowledgeRepository interface {
// AminusB returns the difference set of A and B.
AminusB(ctx context.Context, Atenant uint, A string, Btenant uint, B string) ([]string, error)
UpdateKnowledgeColumn(ctx context.Context, id string, column string, value interface{}) error
// CountKnowledgeByKnowledgeBaseID counts the number of knowledge items in a knowledge base.
CountKnowledgeByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) (int64, error)
}
+4
View File
@@ -69,6 +69,10 @@ type KnowledgeBase struct {
UpdatedAt time.Time `yaml:"updated_at" json:"updated_at"`
// Deletion time of the knowledge base
DeletedAt gorm.DeletedAt `yaml:"deleted_at" json:"deleted_at" gorm:"index"`
// Knowledge count (not stored in database, calculated on query)
KnowledgeCount int64 `yaml:"knowledge_count" json:"knowledge_count" gorm:"-"`
// Chunk count (not stored in database, calculated on query)
ChunkCount int64 `yaml:"chunk_count" json:"chunk_count" gorm:"-"`
}
// KnowledgeBaseConfig represents the knowledge base configuration