feat: 为 FAQ 和标签引入 seq_id 支持

This commit is contained in:
wizardchen
2026-01-12 14:30:37 +08:00
parent 2e9793db8e
commit 14d7d3d995
21 changed files with 716 additions and 250 deletions
+1
View File
@@ -15,6 +15,7 @@ import (
// Chunks are the basic units of storage and indexing in the knowledge base
type Chunk struct {
ID string `json:"id"` // Unique identifier of the chunk
SeqID int64 `json:"seq_id"` // Auto-increment integer ID for external API usage
KnowledgeID string `json:"knowledge_id"` // Identifier of the parent knowledge
KnowledgeBaseID string `json:"knowledge_base_id"` // ID of the knowledge base
TenantID uint64 `json:"tenant_id"` // Tenant ID
+39 -33
View File
@@ -12,11 +12,11 @@ import (
// FAQEntry represents a FAQ item stored under a knowledge base.
type FAQEntry struct {
ID string `json:"id"`
ID int64 `json:"id"`
ChunkID string `json:"chunk_id"`
KnowledgeID string `json:"knowledge_id"`
KnowledgeBaseID string `json:"knowledge_base_id"`
TagID string `json:"tag_id"`
TagID int64 `json:"tag_id"`
TagName string `json:"tag_name"`
IsEnabled bool `json:"is_enabled"`
IsRecommended bool `json:"is_recommended"`
@@ -35,12 +35,14 @@ type FAQEntry struct {
// FAQEntryPayload is used to create or update a FAQ entry.
type FAQEntryPayload struct {
// ID is optional, used for data migration to specify seq_id (must be less than auto-increment start value 100000000)
ID *int64 `json:"id,omitempty"`
StandardQuestion string `json:"standard_question"`
SimilarQuestions []string `json:"similar_questions,omitempty"`
NegativeQuestions []string `json:"negative_questions,omitempty"`
Answers []string `json:"answers"`
AnswerStrategy *string `json:"answer_strategy,omitempty"`
TagID string `json:"tag_id,omitempty"`
TagID int64 `json:"tag_id,omitempty"`
TagName string `json:"tag_name,omitempty"`
IsEnabled *bool `json:"is_enabled,omitempty"`
IsRecommended *bool `json:"is_recommended,omitempty"`
@@ -57,9 +59,9 @@ type FAQBatchUpsertPayload struct {
// FAQEntryFieldsUpdate represents the fields that can be updated for a single FAQ entry.
type FAQEntryFieldsUpdate struct {
IsEnabled *bool `json:"is_enabled,omitempty"`
IsRecommended *bool `json:"is_recommended,omitempty"`
TagID *string `json:"tag_id,omitempty"`
IsEnabled *bool `json:"is_enabled,omitempty"`
IsRecommended *bool `json:"is_recommended,omitempty"`
TagID *int64 `json:"tag_id,omitempty"`
}
// FAQEntryFieldsBatchRequest updates multiple fields for FAQ entries in bulk.
@@ -67,31 +69,33 @@ type FAQEntryFieldsUpdate struct {
// 1. By entry ID: use ByID field
// 2. By Tag: use ByTag field to apply the same update to all entries under a tag
type FAQEntryFieldsBatchRequest struct {
// ByID updates by entry ID, key is entry ID
ByID map[string]FAQEntryFieldsUpdate `json:"by_id,omitempty"`
// ByTag updates all entries under a tag, key is tag ID (empty string for uncategorized)
ByTag map[string]FAQEntryFieldsUpdate `json:"by_tag,omitempty"`
// ExcludeIDs IDs to exclude from the ByTag update
ExcludeIDs []string `json:"exclude_ids,omitempty"`
// ByID updates by entry ID (seq_id), key is entry seq_id
ByID map[int64]FAQEntryFieldsUpdate `json:"by_id,omitempty"`
// ByTag updates all entries under a tag, key is tag seq_id (0 for uncategorized)
ByTag map[int64]FAQEntryFieldsUpdate `json:"by_tag,omitempty"`
// ExcludeIDs IDs (seq_id) to exclude from the ByTag update
ExcludeIDs []int64 `json:"exclude_ids,omitempty"`
}
// FAQEntryTagBatchRequest updates tags in bulk.
// key: entry seq_id, value: tag seq_id (nil to remove tag)
type FAQEntryTagBatchRequest struct {
Updates map[string]*string `json:"updates"`
Updates map[int64]*int64 `json:"updates"`
}
// FAQDeleteRequest deletes entries in bulk.
type FAQDeleteRequest struct {
IDs []string `json:"ids"`
IDs []int64 `json:"ids"`
}
// FAQSearchRequest represents the hybrid FAQ search request.
type FAQSearchRequest struct {
QueryText string `json:"query_text"`
VectorThreshold float64 `json:"vector_threshold"`
MatchCount int `json:"match_count"`
FirstPriorityTagIDs []string `json:"first_priority_tag_ids"` // First priority tag IDs, highest priority
SecondPriorityTagIDs []string `json:"second_priority_tag_ids"` // Second priority tag IDs, lower than first
QueryText string `json:"query_text"`
VectorThreshold float64 `json:"vector_threshold"`
MatchCount int `json:"match_count"`
FirstPriorityTagIDs []int64 `json:"first_priority_tag_ids"` // First priority tag seq_ids, highest priority
SecondPriorityTagIDs []int64 `json:"second_priority_tag_ids"` // Second priority tag seq_ids, lower than first
OnlyRecommended bool `json:"only_recommended"` // Only return recommended entries
}
// FAQEntriesPage contains paginated FAQ results.
@@ -146,10 +150,11 @@ type faqSimpleResponse struct {
}
// ListFAQEntries returns paginated FAQ entries under a knowledge base.
// tagSeqID: filter by tag seq_id (0 means no filter)
// searchField: specifies which field to search in ("standard_question", "similar_questions", "answers", "" for all)
// sortOrder: "asc" for time ascending (updated_at ASC), default is time descending (updated_at DESC)
func (c *Client) ListFAQEntries(ctx context.Context,
knowledgeBaseID string, page, pageSize int, tagID string, keyword string, searchField string, sortOrder string,
knowledgeBaseID string, page, pageSize int, tagSeqID int64, keyword string, searchField string, sortOrder string,
) (*FAQEntriesPage, error) {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries", knowledgeBaseID)
query := url.Values{}
@@ -159,8 +164,8 @@ func (c *Client) ListFAQEntries(ctx context.Context,
if pageSize > 0 {
query.Add("page_size", strconv.Itoa(pageSize))
}
if tagID != "" {
query.Add("tag_id", tagID)
if tagSeqID != 0 {
query.Add("tag_id", strconv.FormatInt(tagSeqID, 10))
}
if keyword != "" {
query.Add("keyword", keyword)
@@ -224,11 +229,11 @@ func (c *Client) CreateFAQEntry(ctx context.Context,
return response.Data, nil
}
// GetFAQEntry retrieves a single FAQ entry by ID.
// GetFAQEntry retrieves a single FAQ entry by seq_id.
func (c *Client) GetFAQEntry(ctx context.Context,
knowledgeBaseID, entryID string,
knowledgeBaseID string, entrySeqID int64,
) (*FAQEntry, error) {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%s", knowledgeBaseID, entryID)
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%d", knowledgeBaseID, entrySeqID)
resp, err := c.doRequest(ctx, http.MethodGet, path, nil, nil)
if err != nil {
return nil, err
@@ -243,9 +248,9 @@ func (c *Client) GetFAQEntry(ctx context.Context,
// UpdateFAQEntry updates a single FAQ entry.
func (c *Client) UpdateFAQEntry(ctx context.Context,
knowledgeBaseID, entryID string, payload *FAQEntryPayload,
knowledgeBaseID string, entrySeqID int64, payload *FAQEntryPayload,
) (*FAQEntry, error) {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%s", knowledgeBaseID, entryID)
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%d", knowledgeBaseID, entrySeqID)
resp, err := c.doRequest(ctx, http.MethodPut, path, payload, nil)
if err != nil {
return nil, err
@@ -261,10 +266,10 @@ func (c *Client) UpdateFAQEntry(ctx context.Context,
// UpdateFAQEntryFieldsBatch updates multiple fields for FAQ entries in bulk.
// Supports updating is_enabled, is_recommended, tag_id in a single call.
// Supports two modes:
// - byID: update by entry ID, key is entry ID
// - byTag: update all entries under a tag, key is tag ID (empty string for uncategorized)
// - byID: update by entry seq_id, key is entry seq_id
// - byTag: update all entries under a tag, key is tag seq_id (0 for uncategorized)
func (c *Client) UpdateFAQEntryFieldsBatch(ctx context.Context,
knowledgeBaseID string, byID map[string]FAQEntryFieldsUpdate, byTag map[string]FAQEntryFieldsUpdate, excludeIDs []string,
knowledgeBaseID string, byID map[int64]FAQEntryFieldsUpdate, byTag map[int64]FAQEntryFieldsUpdate, excludeIDs []int64,
) error {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/fields", knowledgeBaseID)
resp, err := c.doRequest(ctx, http.MethodPut, path, &FAQEntryFieldsBatchRequest{ByID: byID, ByTag: byTag, ExcludeIDs: excludeIDs}, nil)
@@ -277,8 +282,9 @@ func (c *Client) UpdateFAQEntryFieldsBatch(ctx context.Context,
}
// UpdateFAQEntryTagBatch updates FAQ entry tags in bulk.
// key: entry seq_id, value: tag seq_id (nil to remove tag)
func (c *Client) UpdateFAQEntryTagBatch(ctx context.Context,
knowledgeBaseID string, updates map[string]*string,
knowledgeBaseID string, updates map[int64]*int64,
) error {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/tags", knowledgeBaseID)
resp, err := c.doRequest(ctx, http.MethodPut, path, &FAQEntryTagBatchRequest{Updates: updates}, nil)
@@ -290,9 +296,9 @@ func (c *Client) UpdateFAQEntryTagBatch(ctx context.Context,
return parseResponse(resp, &response)
}
// DeleteFAQEntries deletes FAQ entries in bulk.
// DeleteFAQEntries deletes FAQ entries in bulk by seq_id.
func (c *Client) DeleteFAQEntries(ctx context.Context,
knowledgeBaseID string, ids []string,
knowledgeBaseID string, ids []int64,
) error {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries", knowledgeBaseID)
resp, err := c.doRequest(ctx, http.MethodDelete, path, &FAQDeleteRequest{IDs: ids}, nil)
+19 -2
View File
@@ -12,6 +12,7 @@ import (
// Tag represents a knowledge base tag.
type Tag struct {
ID string `json:"id"`
SeqID int64 `json:"seq_id"`
TenantID uint64 `json:"tenant_id"`
KnowledgeBaseID string `json:"knowledge_base_id"`
Name string `json:"name"`
@@ -121,6 +122,7 @@ func (c *Client) CreateTag(ctx context.Context,
}
// UpdateTag updates an existing tag.
// tagID can be either UUID or seq_id (as string).
func (c *Client) UpdateTag(ctx context.Context,
knowledgeBaseID, tagID string, payload *UpdateTagPayload,
) (*Tag, error) {
@@ -137,12 +139,20 @@ func (c *Client) UpdateTag(ctx context.Context,
return response.Data, nil
}
// UpdateTagBySeqID updates an existing tag by seq_id.
func (c *Client) UpdateTagBySeqID(ctx context.Context,
knowledgeBaseID string, tagSeqID int64, payload *UpdateTagPayload,
) (*Tag, error) {
return c.UpdateTag(ctx, knowledgeBaseID, strconv.FormatInt(tagSeqID, 10), payload)
}
// DeleteTag deletes a tag.
// tagID can be either UUID or seq_id (as string).
// Set force to true to delete even if the tag is referenced.
// Set contentOnly to true to only delete the content under the tag but keep the tag itself.
// excludeIDs: IDs of chunks to exclude from deletion.
// excludeIDs: seq_ids of chunks to exclude from deletion.
func (c *Client) DeleteTag(ctx context.Context,
knowledgeBaseID, tagID string, force bool, contentOnly bool, excludeIDs []string,
knowledgeBaseID, tagID string, force bool, contentOnly bool, excludeIDs []int64,
) error {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/tags/%s", knowledgeBaseID, tagID)
query := url.Values{}
@@ -168,3 +178,10 @@ func (c *Client) DeleteTag(ctx context.Context,
var response tagSimpleResponse
return parseResponse(resp, &response)
}
// DeleteTagBySeqID deletes a tag by seq_id.
func (c *Client) DeleteTagBySeqID(ctx context.Context,
knowledgeBaseID string, tagSeqID int64, force bool, contentOnly bool, excludeIDs []int64,
) error {
return c.DeleteTag(ctx, knowledgeBaseID, strconv.FormatInt(tagSeqID, 10), force, contentOnly, excludeIDs)
}