feat: add knowldege tags
This commit is contained in:
@@ -70,22 +70,36 @@ func (r *chunkRepository) ListChunksByKnowledgeID(
|
||||
|
||||
// ListPagedChunksByKnowledgeID lists chunks for a knowledge ID with pagination
|
||||
func (r *chunkRepository) ListPagedChunksByKnowledgeID(
|
||||
ctx context.Context, tenantID uint, knowledgeID string, page *types.Pagination, chunk_type []types.ChunkType,
|
||||
ctx context.Context,
|
||||
tenantID uint,
|
||||
knowledgeID string,
|
||||
page *types.Pagination,
|
||||
chunkType []types.ChunkType,
|
||||
tagID string,
|
||||
) ([]*types.Chunk, int64, error) {
|
||||
var chunks []*types.Chunk
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&types.Chunk{}).
|
||||
Where("tenant_id = ? AND knowledge_id = ? AND chunk_type IN (?)", tenantID, knowledgeID, chunkType)
|
||||
if tagID != "" {
|
||||
query = query.Where("tag_id = ?", tagID)
|
||||
}
|
||||
|
||||
// First query the total count
|
||||
if err := r.db.WithContext(ctx).Model(&types.Chunk{}).
|
||||
Where("tenant_id = ? AND knowledge_id = ? AND chunk_type IN (?)", tenantID, knowledgeID, chunk_type).
|
||||
Count(&total).Error; err != nil {
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Then query the paginated data
|
||||
if err := r.db.WithContext(ctx).
|
||||
Select("id, content, knowledge_id, knowledge_base_id, start_at, end_at, chunk_index, is_enabled, chunk_type, parent_chunk_id, image_info, metadata").
|
||||
Where("tenant_id = ? AND knowledge_id = ? AND chunk_type IN (?)", tenantID, knowledgeID, chunk_type).
|
||||
dataQuery := r.db.WithContext(ctx).
|
||||
Select("id, content, knowledge_id, knowledge_base_id, start_at, end_at, chunk_index, is_enabled, chunk_type, parent_chunk_id, image_info, metadata")
|
||||
dataQuery = dataQuery.Where("tenant_id = ? AND knowledge_id = ? AND chunk_type IN (?)", tenantID, knowledgeID, chunkType)
|
||||
if tagID != "" {
|
||||
dataQuery = dataQuery.Where("tag_id = ?", tagID)
|
||||
}
|
||||
|
||||
if err := dataQuery.
|
||||
Order("chunk_index ASC").
|
||||
Offset(page.Offset()).
|
||||
Limit(page.Limit()).
|
||||
|
||||
@@ -57,20 +57,30 @@ func (r *knowledgeRepository) ListPagedKnowledgeByKnowledgeBaseID(
|
||||
tenantID uint,
|
||||
kbID string,
|
||||
page *types.Pagination,
|
||||
tagID string,
|
||||
) ([]*types.Knowledge, int64, error) {
|
||||
var knowledges []*types.Knowledge
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&types.Knowledge{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID)
|
||||
if tagID != "" {
|
||||
query = query.Where("tag_id = ?", tagID)
|
||||
}
|
||||
|
||||
// Query total count first
|
||||
if err := r.db.WithContext(ctx).Model(&types.Knowledge{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
Count(&total).Error; err != nil {
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Then query paginated data
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
dataQuery := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID)
|
||||
if tagID != "" {
|
||||
dataQuery = dataQuery.Where("tag_id = ?", tagID)
|
||||
}
|
||||
|
||||
if err := dataQuery.
|
||||
Order("created_at DESC").
|
||||
Offset(page.Offset()).
|
||||
Limit(page.Limit()).
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type knowledgeTagRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewKnowledgeTagRepository creates a new tag repository.
|
||||
func NewKnowledgeTagRepository(db *gorm.DB) interfaces.KnowledgeTagRepository {
|
||||
return &knowledgeTagRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *knowledgeTagRepository) Create(ctx context.Context, tag *types.KnowledgeTag) error {
|
||||
return r.db.WithContext(ctx).Create(tag).Error
|
||||
}
|
||||
|
||||
func (r *knowledgeTagRepository) Update(ctx context.Context, tag *types.KnowledgeTag) error {
|
||||
return r.db.WithContext(ctx).Save(tag).Error
|
||||
}
|
||||
|
||||
func (r *knowledgeTagRepository) GetByID(ctx context.Context, tenantID uint, id string) (*types.KnowledgeTag, error) {
|
||||
var tag types.KnowledgeTag
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND id = ?", tenantID, id).
|
||||
First(&tag).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &tag, nil
|
||||
}
|
||||
|
||||
func (r *knowledgeTagRepository) ListByKB(ctx context.Context, tenantID uint, kbID string) ([]*types.KnowledgeTag, error) {
|
||||
var tags []*types.KnowledgeTag
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
Order("sort_order ASC, created_at ASC").
|
||||
Find(&tags).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
func (r *knowledgeTagRepository) Delete(ctx context.Context, tenantID uint, 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.
|
||||
func (r *knowledgeTagRepository) CountReferences(
|
||||
ctx context.Context,
|
||||
tenantID uint,
|
||||
kbID string,
|
||||
tagID string,
|
||||
) (knowledgeCount int64, chunkCount int64, err error) {
|
||||
if err = r.db.WithContext(ctx).
|
||||
Model(&types.Knowledge{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ? AND tag_id = ?", tenantID, kbID, tagID).
|
||||
Count(&knowledgeCount).Error; err != nil {
|
||||
return
|
||||
}
|
||||
if err = r.db.WithContext(ctx).
|
||||
Model(&types.Chunk{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ? AND tag_id = ?", tenantID, kbID, tagID).
|
||||
Count(&chunkCount).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -129,15 +129,14 @@ func (s *chunkService) ListChunksByKnowledgeID(ctx context.Context, knowledgeID
|
||||
// - *types.PageResult: Paginated result containing chunks and pagination metadata
|
||||
// - error: Any error encountered during retrieval
|
||||
func (s *chunkService) ListPagedChunksByKnowledgeID(ctx context.Context,
|
||||
knowledgeID string, page *types.Pagination,
|
||||
knowledgeID string, page *types.Pagination, chunkType []types.ChunkType,
|
||||
) (*types.PageResult, error) {
|
||||
logger.Info(ctx, "Start listing paged chunks by knowledge ID")
|
||||
logger.Infof(ctx, "Knowledge ID: %s, page: %d, page size: %d", knowledgeID, page.Page, page.PageSize)
|
||||
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
|
||||
logger.Infof(ctx, "Tenant ID: %d", tenantID)
|
||||
chunkType := []types.ChunkType{types.ChunkTypeText}
|
||||
chunks, total, err := s.chunkRepository.ListPagedChunksByKnowledgeID(ctx, tenantID, knowledgeID, page, chunkType)
|
||||
chunks, total, err := s.chunkRepository.ListPagedChunksByKnowledgeID(ctx, tenantID, knowledgeID, page, chunkType, "")
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"knowledge_id": knowledgeID,
|
||||
|
||||
@@ -553,10 +553,10 @@ func (s *knowledgeService) ListKnowledgeByKnowledgeBaseID(ctx context.Context,
|
||||
|
||||
// ListPagedKnowledgeByKnowledgeBaseID returns paginated knowledge entries in a knowledge base
|
||||
func (s *knowledgeService) ListPagedKnowledgeByKnowledgeBaseID(ctx context.Context,
|
||||
kbID string, page *types.Pagination,
|
||||
kbID string, page *types.Pagination, tagID string,
|
||||
) (*types.PageResult, error) {
|
||||
knowledges, total, err := s.repo.ListPagedKnowledgeByKnowledgeBaseID(ctx,
|
||||
ctx.Value(types.TenantIDContextKey).(uint), kbID, page)
|
||||
ctx.Value(types.TenantIDContextKey).(uint), kbID, page, tagID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1949,6 +1949,7 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl
|
||||
PageSize: chunkPageSize,
|
||||
},
|
||||
chunkType,
|
||||
"",
|
||||
)
|
||||
chunkPage++
|
||||
if err != nil {
|
||||
@@ -2023,7 +2024,7 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl
|
||||
|
||||
// ListFAQEntries lists FAQ entries under a FAQ knowledge base.
|
||||
func (s *knowledgeService) ListFAQEntries(ctx context.Context,
|
||||
kbID string, page *types.Pagination,
|
||||
kbID string, page *types.Pagination, tagID string,
|
||||
) (*types.PageResult, error) {
|
||||
if page == nil {
|
||||
page = &types.Pagination{}
|
||||
@@ -2042,7 +2043,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,
|
||||
ctx, tenantID, faqKnowledge.ID, page, chunkType, tagID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -2098,6 +2099,7 @@ func (s *knowledgeService) UpsertFAQEntries(ctx context.Context,
|
||||
faqKnowledge.ID,
|
||||
&types.Pagination{Page: 1, PageSize: 1},
|
||||
[]types.ChunkType{types.ChunkTypeFAQ},
|
||||
"",
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -2127,6 +2129,7 @@ func (s *knowledgeService) UpsertFAQEntries(ctx context.Context,
|
||||
ChunkIndex: startIndex + idx + 1,
|
||||
IsEnabled: true,
|
||||
ChunkType: types.ChunkTypeFAQ,
|
||||
TagID: entry.TagID,
|
||||
}
|
||||
if err := chunk.SetFAQMetadata(meta); err != nil {
|
||||
return err
|
||||
@@ -2187,6 +2190,7 @@ func (s *knowledgeService) UpdateFAQEntry(ctx context.Context,
|
||||
indexMode = kb.FAQConfig.IndexMode
|
||||
}
|
||||
chunk.Content = buildFAQChunkContent(meta, indexMode)
|
||||
chunk.TagID = payload.TagID
|
||||
chunk.UpdatedAt = time.Now()
|
||||
if err := s.chunkService.UpdateChunk(ctx, chunk); err != nil {
|
||||
return err
|
||||
@@ -2403,6 +2407,7 @@ func (s *knowledgeService) chunkToFAQEntry(chunk *types.Chunk, kb *types.Knowled
|
||||
ChunkID: chunk.ID,
|
||||
KnowledgeID: chunk.KnowledgeID,
|
||||
KnowledgeBaseID: chunk.KnowledgeBaseID,
|
||||
TagID: chunk.TagID,
|
||||
StandardQuestion: meta.StandardQuestion,
|
||||
SimilarQuestions: meta.SimilarQuestions,
|
||||
NegativeQuestions: meta.NegativeQuestions,
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
werrors "github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// knowledgeTagService implements KnowledgeTagService.
|
||||
type knowledgeTagService struct {
|
||||
kbService interfaces.KnowledgeBaseService
|
||||
repo interfaces.KnowledgeTagRepository
|
||||
}
|
||||
|
||||
// NewKnowledgeTagService creates a new tag service.
|
||||
func NewKnowledgeTagService(
|
||||
kbService interfaces.KnowledgeBaseService,
|
||||
repo interfaces.KnowledgeTagRepository,
|
||||
) (interfaces.KnowledgeTagService, error) {
|
||||
return &knowledgeTagService{
|
||||
kbService: kbService,
|
||||
repo: repo,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListTags lists all tags for a knowledge base with usage stats.
|
||||
func (s *knowledgeTagService) ListTags(ctx context.Context, kbID string) ([]*types.KnowledgeTagWithStats, error) {
|
||||
if kbID == "" {
|
||||
return nil, werrors.NewBadRequestError("知识库ID不能为空")
|
||||
}
|
||||
// Ensure KB exists and belongs to current tenant
|
||||
kb, err := s.kbService.GetKnowledgeBaseByID(ctx, kbID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tenantID := kb.TenantID
|
||||
|
||||
tags, err := s.repo.ListByKB(ctx, tenantID, kbID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results := make([]*types.KnowledgeTagWithStats, 0, len(tags))
|
||||
for _, tag := range tags {
|
||||
kCount, cCount, err := s.repo.CountReferences(ctx, tenantID, kbID, tag.ID)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"kb_id": kbID,
|
||||
"tag_id": tag.ID,
|
||||
})
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, &types.KnowledgeTagWithStats{
|
||||
KnowledgeTag: *tag,
|
||||
KnowledgeCount: kCount,
|
||||
ChunkCount: cCount,
|
||||
})
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// CreateTag creates a new tag under a KB.
|
||||
func (s *knowledgeTagService) CreateTag(ctx context.Context, kbID string, name string, color string, sortOrder int) (*types.KnowledgeTag, error) {
|
||||
name = strings.TrimSpace(name)
|
||||
if kbID == "" || name == "" {
|
||||
return nil, werrors.NewBadRequestError("知识库ID和标签名称不能为空")
|
||||
}
|
||||
kb, err := s.kbService.GetKnowledgeBaseByID(ctx, kbID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
now := time.Now()
|
||||
tag := &types.KnowledgeTag{
|
||||
ID: uuid.New().String(),
|
||||
TenantID: kb.TenantID,
|
||||
KnowledgeBaseID: kb.ID,
|
||||
Name: name,
|
||||
Color: strings.TrimSpace(color),
|
||||
SortOrder: sortOrder,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := s.repo.Create(ctx, tag); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tag, nil
|
||||
}
|
||||
|
||||
// UpdateTag updates tag basic information.
|
||||
func (s *knowledgeTagService) UpdateTag(ctx context.Context, id string, name *string, color *string, sortOrder *int) (*types.KnowledgeTag, error) {
|
||||
if id == "" {
|
||||
return nil, werrors.NewBadRequestError("标签ID不能为空")
|
||||
}
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
|
||||
tag, err := s.repo.GetByID(ctx, tenantID, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if name != nil {
|
||||
newName := strings.TrimSpace(*name)
|
||||
if newName == "" {
|
||||
return nil, werrors.NewBadRequestError("标签名称不能为空")
|
||||
}
|
||||
tag.Name = newName
|
||||
}
|
||||
if color != nil {
|
||||
tag.Color = strings.TrimSpace(*color)
|
||||
}
|
||||
if sortOrder != nil {
|
||||
tag.SortOrder = *sortOrder
|
||||
}
|
||||
tag.UpdatedAt = time.Now()
|
||||
if err := s.repo.Update(ctx, tag); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tag, nil
|
||||
}
|
||||
|
||||
// DeleteTag deletes a tag. When force=false, deletion is only allowed if no references exist.
|
||||
func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bool) error {
|
||||
if id == "" {
|
||||
return werrors.NewBadRequestError("标签ID不能为空")
|
||||
}
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
|
||||
tag, err := s.repo.GetByID(ctx, tenantID, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kCount, cCount, err := s.repo.CountReferences(ctx, tenantID, tag.KnowledgeBaseID, tag.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !force && (kCount > 0 || cCount > 0) {
|
||||
return werrors.NewBadRequestError("标签仍有知识或FAQ条目引用,无法删除")
|
||||
}
|
||||
return s.repo.Delete(ctx, tenantID, id)
|
||||
}
|
||||
@@ -87,6 +87,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
||||
must(container.Provide(repository.NewKnowledgeBaseRepository))
|
||||
must(container.Provide(repository.NewKnowledgeRepository))
|
||||
must(container.Provide(repository.NewChunkRepository))
|
||||
must(container.Provide(repository.NewKnowledgeTagRepository))
|
||||
must(container.Provide(repository.NewSessionRepository))
|
||||
must(container.Provide(repository.NewMessageRepository))
|
||||
must(container.Provide(repository.NewModelRepository))
|
||||
@@ -103,6 +104,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
||||
must(container.Provide(service.NewKnowledgeBaseService))
|
||||
must(container.Provide(service.NewKnowledgeService))
|
||||
must(container.Provide(service.NewChunkService))
|
||||
must(container.Provide(service.NewKnowledgeTagService))
|
||||
must(container.Provide(embedding.NewBatchEmbedder))
|
||||
must(container.Provide(service.NewModelService))
|
||||
must(container.Provide(service.NewDatasetService))
|
||||
@@ -149,6 +151,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
||||
must(container.Provide(handler.NewKnowledgeHandler))
|
||||
must(container.Provide(handler.NewChunkHandler))
|
||||
must(container.Provide(handler.NewFAQHandler))
|
||||
must(container.Provide(handler.NewTagHandler))
|
||||
must(container.Provide(session.NewHandler))
|
||||
must(container.Provide(handler.NewMessageHandler))
|
||||
must(container.Provide(handler.NewModelHandler))
|
||||
|
||||
@@ -100,11 +100,13 @@ func (h *ChunkHandler) ListKnowledgeChunks(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
chunkType := []types.ChunkType{types.ChunkTypeText}
|
||||
|
||||
logger.Infof(ctx, "Retrieving knowledge chunks list, knowledge ID: %s, page: %d, page size: %d",
|
||||
knowledgeID, pagination.Page, pagination.PageSize)
|
||||
|
||||
// Use pagination for query
|
||||
result, err := h.service.ListPagedChunksByKnowledgeID(ctx, knowledgeID, &pagination)
|
||||
result, err := h.service.ListPagedChunksByKnowledgeID(ctx, knowledgeID, &pagination, chunkType)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
|
||||
@@ -31,7 +31,9 @@ func (h *FAQHandler) ListEntries(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.knowledgeService.ListFAQEntries(ctx, c.Param("id"), &page)
|
||||
tagID := c.Query("tag_id")
|
||||
|
||||
result, err := h.knowledgeService.ListFAQEntries(ctx, c.Param("id"), &page, tagID)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
|
||||
@@ -285,11 +285,13 @@ func (h *KnowledgeHandler) ListKnowledge(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Retrieving knowledge list under knowledge base, knowledge base ID: %s, page: %d, page size: %d",
|
||||
kbID, pagination.Page, pagination.PageSize)
|
||||
tagID := c.Query("tag_id")
|
||||
|
||||
logger.Infof(ctx, "Retrieving knowledge list under knowledge base, knowledge base ID: %s, tag_id: %s, page: %d, page size: %d",
|
||||
kbID, tagID, pagination.Page, pagination.PageSize)
|
||||
|
||||
// Retrieve paginated knowledge entries
|
||||
result, err := h.kgService.ListPagedKnowledgeByKnowledgeBaseID(ctx, kbID, &pagination)
|
||||
result, err := h.kgService.ListPagedKnowledgeByKnowledgeBaseID(ctx, kbID, &pagination, tagID)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
|
||||
// TagHandler handles knowledge base tag operations.
|
||||
type TagHandler struct {
|
||||
tagService interfaces.KnowledgeTagService
|
||||
}
|
||||
|
||||
// NewTagHandler creates a new TagHandler.
|
||||
func NewTagHandler(tagService interfaces.KnowledgeTagService) *TagHandler {
|
||||
return &TagHandler{tagService: tagService}
|
||||
}
|
||||
|
||||
// ListTags returns all tags under a knowledge base with statistics.
|
||||
func (h *TagHandler) ListTags(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
kbID := c.Param("id")
|
||||
|
||||
tags, err := h.tagService.ListTags(ctx, kbID)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": tags,
|
||||
})
|
||||
}
|
||||
|
||||
type createTagRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Color string `json:"color"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
}
|
||||
|
||||
// CreateTag creates a new tag.
|
||||
func (h *TagHandler) CreateTag(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
kbID := c.Param("id")
|
||||
|
||||
var req createTagRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(ctx, "Failed to bind create tag payload", err)
|
||||
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
tag, err := h.tagService.CreateTag(ctx, kbID, req.Name, req.Color, req.SortOrder)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"kb_id": kbID,
|
||||
})
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": tag,
|
||||
})
|
||||
}
|
||||
|
||||
type updateTagRequest struct {
|
||||
Name *string `json:"name"`
|
||||
Color *string `json:"color"`
|
||||
SortOrder *int `json:"sort_order"`
|
||||
}
|
||||
|
||||
// UpdateTag updates an existing tag.
|
||||
func (h *TagHandler) UpdateTag(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
tagID := c.Param("tag_id")
|
||||
var req updateTagRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(ctx, "Failed to bind update tag payload", err)
|
||||
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
tag, err := h.tagService.UpdateTag(ctx, tagID, req.Name, req.Color, req.SortOrder)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"tag_id": tagID,
|
||||
})
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": tag,
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteTag deletes a tag. Use query param force=true to force delete even if referenced.
|
||||
func (h *TagHandler) DeleteTag(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
tagID := c.Param("tag_id")
|
||||
|
||||
force := c.Query("force") == "true"
|
||||
|
||||
if err := h.tagService.DeleteTag(ctx, tagID, force); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"tag_id": tagID,
|
||||
})
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
})
|
||||
}
|
||||
|
||||
// NOTE: TagHandler currently exposes CRUD for tags and statistics.
|
||||
// Knowledge / Chunk tagging is handled via dedicated knowledge and FAQ APIs.
|
||||
@@ -42,6 +42,7 @@ type RouterParams struct {
|
||||
MCPServiceHandler *handler.MCPServiceHandler
|
||||
WebSearchHandler *handler.WebSearchHandler
|
||||
FAQHandler *handler.FAQHandler
|
||||
TagHandler *handler.TagHandler
|
||||
}
|
||||
|
||||
// NewRouter 创建新的路由
|
||||
@@ -79,6 +80,7 @@ func NewRouter(params RouterParams) *gin.Engine {
|
||||
RegisterAuthRoutes(v1, params.AuthHandler)
|
||||
RegisterTenantRoutes(v1, params.TenantHandler)
|
||||
RegisterKnowledgeBaseRoutes(v1, params.KBHandler)
|
||||
RegisterKnowledgeTagRoutes(v1, params.TagHandler)
|
||||
RegisterKnowledgeRoutes(v1, params.KnowledgeHandler)
|
||||
RegisterFAQRoutes(v1, params.FAQHandler)
|
||||
RegisterChunkRoutes(v1, params.ChunkHandler)
|
||||
@@ -186,6 +188,20 @@ func RegisterKnowledgeBaseRoutes(r *gin.RouterGroup, handler *handler.KnowledgeB
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterKnowledgeTagRoutes 注册知识库标签相关路由
|
||||
func RegisterKnowledgeTagRoutes(r *gin.RouterGroup, tagHandler *handler.TagHandler) {
|
||||
if tagHandler == nil {
|
||||
return
|
||||
}
|
||||
kbTags := r.Group("/knowledge-bases/:id/tags")
|
||||
{
|
||||
kbTags.GET("", tagHandler.ListTags)
|
||||
kbTags.POST("", tagHandler.CreateTag)
|
||||
kbTags.PUT("/:tag_id", tagHandler.UpdateTag)
|
||||
kbTags.DELETE("/:tag_id", tagHandler.DeleteTag)
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterMessageRoutes 注册消息相关的路由
|
||||
func RegisterMessageRoutes(r *gin.RouterGroup, handler *handler.MessageHandler) {
|
||||
// 消息路由组
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
Reference in New Issue
Block a user