feat: add knowldege tags

This commit is contained in:
wizardchen
2025-11-17 18:15:02 +08:00
parent adbbeb81fc
commit 822041d995
26 changed files with 668 additions and 45 deletions
+2
View File
@@ -59,6 +59,8 @@ type Chunk struct {
KnowledgeID string `json:"knowledge_id"`
// ID of the knowledge base, for quick location
KnowledgeBaseID string `json:"knowledge_base_id"`
// Optional tag ID for categorization within a knowledge base (used for FAQ)
TagID string `json:"tag_id" gorm:"type:varchar(36);index"`
// Actual text content of the chunk
Content string `json:"content"`
// Index position of the chunk in the original document
+2
View File
@@ -67,6 +67,7 @@ type FAQEntry struct {
ChunkID string `json:"chunk_id"`
KnowledgeID string `json:"knowledge_id"`
KnowledgeBaseID string `json:"knowledge_base_id"`
TagID string `json:"tag_id"`
StandardQuestion string `json:"standard_question"`
SimilarQuestions []string `json:"similar_questions"`
NegativeQuestions []string `json:"negative_questions"`
@@ -85,6 +86,7 @@ type FAQEntryPayload struct {
SimilarQuestions []string `json:"similar_questions"`
NegativeQuestions []string `json:"negative_questions"`
Answers []string `json:"answers" binding:"required"`
TagID string `json:"tag_id"`
}
const (
+5 -2
View File
@@ -16,13 +16,15 @@ type ChunkRepository interface {
ListChunksByID(ctx context.Context, tenantID uint, ids []string) ([]*types.Chunk, error)
// ListChunksByKnowledgeID lists chunks by knowledge id
ListChunksByKnowledgeID(ctx context.Context, tenantID uint, knowledgeID string) ([]*types.Chunk, error)
// ListPagedChunksByKnowledgeID lists paged chunks by knowledge id
// ListPagedChunksByKnowledgeID lists paged chunks by knowledge id.
// When tagID is non-empty, results are filtered by tag_id.
ListPagedChunksByKnowledgeID(
ctx context.Context,
tenantID uint,
knowledgeID string,
page *types.Pagination,
chunk_type []types.ChunkType,
chunkType []types.ChunkType,
tagID string,
) ([]*types.Chunk, int64, error)
ListChunkByParentID(ctx context.Context, tenantID uint, parentID string) ([]*types.Chunk, error)
// UpdateChunk updates a chunk
@@ -48,6 +50,7 @@ type ChunkService interface {
ctx context.Context,
knowledgeID string,
page *types.Pagination,
chunkType []types.ChunkType,
) (*types.PageResult, error)
// UpdateChunk updates a chunk
UpdateChunk(ctx context.Context, chunk *types.Chunk) error
+7 -2
View File
@@ -33,10 +33,12 @@ type KnowledgeService interface {
// ListKnowledgeByKnowledgeBaseID lists all knowledge under a knowledge base.
ListKnowledgeByKnowledgeBaseID(ctx context.Context, kbID string) ([]*types.Knowledge, error)
// ListPagedKnowledgeByKnowledgeBaseID lists all knowledge under a knowledge base with pagination.
// When tagID is non-empty, results are filtered by tag_id.
ListPagedKnowledgeByKnowledgeBaseID(
ctx context.Context,
kbID string,
page *types.Pagination,
tagID string,
) (*types.PageResult, error)
// DeleteKnowledge deletes knowledge by ID.
DeleteKnowledge(ctx context.Context, id string) error
@@ -51,7 +53,8 @@ type KnowledgeService interface {
// UpdateImageInfo updates image information for a knowledge chunk.
UpdateImageInfo(ctx context.Context, knowledgeID string, chunkID string, imageInfo string) error
// ListFAQEntries lists FAQ entries under a FAQ knowledge base.
ListFAQEntries(ctx context.Context, kbID string, page *types.Pagination) (*types.PageResult, error)
// When tagID is non-empty, results are filtered by tag_id on FAQ chunks.
ListFAQEntries(ctx context.Context, kbID string, page *types.Pagination, tagID string) (*types.PageResult, error)
// UpsertFAQEntries imports or appends FAQ entries.
UpsertFAQEntries(ctx context.Context, kbID string, payload *types.FAQBatchUpsertPayload) error
// UpdateFAQEntry updates a single FAQ entry.
@@ -67,8 +70,10 @@ type KnowledgeRepository interface {
CreateKnowledge(ctx context.Context, knowledge *types.Knowledge) error
GetKnowledgeByID(ctx context.Context, tenantID uint, id string) (*types.Knowledge, error)
ListKnowledgeByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) ([]*types.Knowledge, error)
// ListPagedKnowledgeByKnowledgeBaseID lists all knowledge in a knowledge base with pagination.
// When tagID is non-empty, results are filtered by tag_id.
ListPagedKnowledgeByKnowledgeBaseID(ctx context.Context,
tenantID uint, kbID string, page *types.Pagination,
tenantID uint, kbID string, page *types.Pagination, tagID string,
) ([]*types.Knowledge, int64, error)
UpdateKnowledge(ctx context.Context, knowledge *types.Knowledge) error
DeleteKnowledge(ctx context.Context, tenantID uint, id string) error
+30
View File
@@ -0,0 +1,30 @@
package interfaces
import (
"context"
"github.com/Tencent/WeKnora/internal/types"
)
// KnowledgeTagService defines operations on knowledge base scoped tags.
type KnowledgeTagService interface {
// ListTags lists all tags under a knowledge base with associated statistics.
ListTags(ctx context.Context, kbID string) ([]*types.KnowledgeTagWithStats, error)
// CreateTag creates a new tag under a knowledge base.
CreateTag(ctx context.Context, kbID string, name string, color string, sortOrder int) (*types.KnowledgeTag, error)
// UpdateTag updates tag basic information.
UpdateTag(ctx context.Context, id string, name *string, color *string, sortOrder *int) (*types.KnowledgeTag, error)
// DeleteTag deletes a tag.
DeleteTag(ctx context.Context, id string, force bool) error
}
// KnowledgeTagRepository defines persistence operations for tags.
type KnowledgeTagRepository interface {
Create(ctx context.Context, tag *types.KnowledgeTag) error
Update(ctx context.Context, tag *types.KnowledgeTag) error
GetByID(ctx context.Context, tenantID uint, id string) (*types.KnowledgeTag, error)
ListByKB(ctx context.Context, tenantID uint, kbID string) ([]*types.KnowledgeTag, error)
Delete(ctx context.Context, tenantID uint, id string) error
// CountReferences returns number of knowledges and chunks that reference the tag.
CountReferences(ctx context.Context, tenantID uint, kbID string, tagID string) (knowledgeCount int64, chunkCount int64, err error)
}
+2
View File
@@ -30,6 +30,8 @@ type Knowledge struct {
TenantID uint `json:"tenant_id"`
// ID of the knowledge base
KnowledgeBaseID string `json:"knowledge_base_id"`
// Optional tag ID for categorization within a knowledge base
TagID string `json:"tag_id" gorm:"type:varchar(36);index"`
// Type of the knowledge
Type string `json:"type"`
// Title of the knowledge
+32
View File
@@ -0,0 +1,32 @@
package types
import "time"
// KnowledgeTag represents a tag (category) under a specific knowledge base.
// Tags are scoped by knowledge base (and tenant) and are used to categorize
// Knowledge (documents) and FAQ Chunks.
type KnowledgeTag struct {
// Unique identifier of the tag (UUID)
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
// Tenant ID
TenantID uint `json:"tenant_id"`
// Knowledge base ID that this tag belongs to
KnowledgeBaseID string `json:"knowledge_base_id" gorm:"type:varchar(36);index"`
// Tag name, unique within the same knowledge base
Name string `json:"name" gorm:"type:varchar(128);not null"`
// Optional display color
Color string `json:"color" gorm:"type:varchar(32)"`
// Sort order within the same knowledge base
SortOrder int `json:"sort_order" gorm:"default:0"`
// Creation time
CreatedAt time.Time `json:"created_at"`
// Last updated time
UpdatedAt time.Time `json:"updated_at"`
}
// KnowledgeTagWithStats represents tag information along with usage statistics.
type KnowledgeTagWithStats struct {
KnowledgeTag
KnowledgeCount int64 `json:"knowledge_count"`
ChunkCount int64 `json:"chunk_count"`
}