feat: Add tag management and FAQ management APIs, enhance knowledge base functionality with keyword search and custom file naming support

This commit is contained in:
wizardchen
2025-12-02 20:39:59 +08:00
parent cdf576eb18
commit 845d990a48
29 changed files with 1471 additions and 394 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ func ExampleUsage() {
"source": "local",
"type": "document",
}
knowledge, err := apiClient.CreateKnowledgeFromFile(context.Background(), createdKB.ID, filePath, metadata, nil)
knowledge, err := apiClient.CreateKnowledgeFromFile(context.Background(), createdKB.ID, filePath, metadata, nil, "")
if err != nil {
fmt.Printf("Failed to upload knowledge file: %v\n", err)
} else {
+4 -1
View File
@@ -113,7 +113,7 @@ type faqSimpleResponse struct {
// ListFAQEntries returns paginated FAQ entries under a knowledge base.
func (c *Client) ListFAQEntries(ctx context.Context,
knowledgeBaseID string, page, pageSize int, tagID string,
knowledgeBaseID string, page, pageSize int, tagID string, keyword string,
) (*FAQEntriesPage, error) {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries", knowledgeBaseID)
query := url.Values{}
@@ -126,6 +126,9 @@ func (c *Client) ListFAQEntries(ctx context.Context,
if tagID != "" {
query.Add("tag_id", tagID)
}
if keyword != "" {
query.Add("keyword", keyword)
}
resp, err := c.doRequest(ctx, http.MethodGet, path, nil, query)
if err != nil {
+22 -2
View File
@@ -22,8 +22,9 @@ import (
// Knowledge represents knowledge information
type Knowledge struct {
ID string `json:"id"`
TenantID uint `json:"tenant_id"`
TenantID uint64 `json:"tenant_id"`
KnowledgeBaseID string `json:"knowledge_base_id"`
TagID string `json:"tag_id"`
Type string `json:"type"`
Title string `json:"title"`
Description string `json:"description"`
@@ -34,7 +35,9 @@ type Knowledge struct {
FileName string `json:"file_name"`
FileType string `json:"file_type"`
FileSize int64 `json:"file_size"`
FileHash string `json:"file_hash"`
FilePath string `json:"file_path"`
StorageSize int64 `json:"storage_size"`
Metadata map[string]string `json:"metadata"` // Extensible metadata for storing machine information, paths, etc.
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
@@ -78,8 +81,14 @@ var ErrDuplicateFile = errors.New("file already exists")
var ErrDuplicateURL = errors.New("URL already exists")
// CreateKnowledgeFromFile creates a knowledge entry from a local file path
// Parameters:
// - knowledgeBaseID: The ID of the knowledge base
// - filePath: The local file path
// - metadata: Optional metadata for the knowledge entry
// - enableMultimodel: Optional flag to enable multimodal processing
// - customFileName: Optional custom file name (useful for folder uploads with path)
func (c *Client) CreateKnowledgeFromFile(ctx context.Context,
knowledgeBaseID string, filePath string, metadata map[string]string, enableMultimodel *bool,
knowledgeBaseID string, filePath string, metadata map[string]string, enableMultimodel *bool, customFileName string,
) (*Knowledge, error) {
// Open the local file
file, err := os.Open(filePath)
@@ -133,6 +142,13 @@ func (c *Client) CreateKnowledgeFromFile(ctx context.Context,
}
}
// Add custom file name if provided
if customFileName != "" {
if err := writer.WriteField("fileName", customFileName); err != nil {
return nil, fmt.Errorf("failed to write fileName field: %w", err)
}
}
// Close the multipart writer
err = writer.Close()
if err != nil {
@@ -249,12 +265,16 @@ func (c *Client) ListKnowledge(ctx context.Context,
knowledgeBaseID string,
page int,
pageSize int,
tagID string,
) ([]Knowledge, int64, error) {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/knowledge", knowledgeBaseID)
queryParams := url.Values{}
queryParams.Add("page", strconv.Itoa(page))
queryParams.Add("page_size", strconv.Itoa(pageSize))
if tagID != "" {
queryParams.Add("tag_id", tagID)
}
resp, err := c.doRequest(ctx, http.MethodGet, path, nil, queryParams)
if err != nil {
+69 -13
View File
@@ -8,7 +8,6 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"time"
)
@@ -17,34 +16,44 @@ type KnowledgeBase struct {
ID string `json:"id"`
Name string `json:"name"` // Name must be unique within the same tenant
Type string `json:"type"`
IsTemporary bool `json:"is_temporary"`
Description string `json:"description"`
TenantID uint `json:"tenant_id"` // Changed to uint type
TenantID uint64 `json:"tenant_id"`
ChunkingConfig ChunkingConfig `json:"chunking_config"`
ImageProcessingConfig ImageProcessingConfig `json:"image_processing_config"`
FAQConfig *FAQConfig `json:"faq_config"`
EmbeddingModelID string `json:"embedding_model_id"`
SummaryModelID string `json:"summary_model_id"` // Summary model ID
SummaryModelID string `json:"summary_model_id"`
VLMConfig VLMConfig `json:"vlm_config"`
StorageConfig StorageConfig `json:"cos_config"`
ExtractConfig *ExtractConfig `json:"extract_config"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// Computed fields (not stored in database)
KnowledgeCount int64 `json:"knowledge_count"`
ChunkCount int64 `json:"chunk_count"`
IsProcessing bool `json:"is_processing"`
ProcessingCount int64 `json:"processing_count"`
}
// KnowledgeBaseConfig represents knowledge base configuration
type KnowledgeBaseConfig struct {
ChunkingConfig ChunkingConfig `json:"chunking_config"`
ImageProcessingConfig ImageProcessingConfig `json:"image_processing_config"`
FAQConfig *FAQConfig `json:"faq_config"`
}
// ChunkingConfig represents document chunking configuration
type ChunkingConfig struct {
ChunkSize int `json:"chunk_size"` // Chunk size
ChunkOverlap int `json:"chunk_overlap"` // Overlap size
Separators []string `json:"separators"` // Separators
EnableMultimodal bool `json:"enable_multimodal"` // Whether to enable multimodal processing
ChunkSize int `json:"chunk_size"` // Chunk size
ChunkOverlap int `json:"chunk_overlap"` // Overlap size
Separators []string `json:"separators"` // Separators
}
// FAQConfig represents faq-specific configuration
type FAQConfig struct {
IndexMode string `json:"index_mode"`
IndexMode string `json:"index_mode"`
QuestionIndexMode string `json:"question_index_mode"`
}
// ImageProcessingConfig represents image processing configuration
@@ -52,6 +61,44 @@ type ImageProcessingConfig struct {
ModelID string `json:"model_id"` // Multimodal model ID
}
// VLMConfig represents the VLM configuration
type VLMConfig struct {
Enabled bool `json:"enabled"`
ModelID string `json:"model_id"`
}
// StorageConfig represents the storage configuration
type StorageConfig struct {
SecretID string `json:"secret_id"`
SecretKey string `json:"secret_key"`
Region string `json:"region"`
BucketName string `json:"bucket_name"`
AppID string `json:"app_id"`
PathPrefix string `json:"path_prefix"`
Provider string `json:"provider"`
}
// ExtractConfig represents the extract configuration for a knowledge base
type ExtractConfig struct {
Enabled bool `json:"enabled"`
Text string `json:"text,omitempty"`
Tags []string `json:"tags,omitempty"`
Nodes []*GraphNode `json:"nodes,omitempty"`
Relations []*GraphRelation `json:"relations,omitempty"`
}
// GraphNode represents a node in the graph extraction configuration
type GraphNode struct {
Name string `json:"name"`
}
// GraphRelation represents a relation in the graph extraction configuration
type GraphRelation struct {
Node1 string `json:"node1"`
Node2 string `json:"node2"`
Type string `json:"type"`
}
// KnowledgeBaseResponse knowledge base response
type KnowledgeBaseResponse struct {
Success bool `json:"success"`
@@ -181,14 +228,23 @@ func (c *Client) DeleteKnowledgeBase(ctx context.Context, knowledgeBaseID string
return parseResponse(resp, &response)
}
// SearchParams represents the search parameters for hybrid search
type SearchParams struct {
QueryText string `json:"query_text"`
VectorThreshold float64 `json:"vector_threshold"`
KeywordThreshold float64 `json:"keyword_threshold"`
MatchCount int `json:"match_count"`
DisableKeywordsMatch bool `json:"disable_keywords_match"`
DisableVectorMatch bool `json:"disable_vector_match"`
}
// HybridSearch performs hybrid search
func (c *Client) HybridSearch(ctx context.Context, knowledgeBaseID string, query string) ([]*SearchResult, error) {
// Note: The backend route is GET but expects JSON body, which is non-standard.
// This client uses POST with JSON body for better compatibility.
func (c *Client) HybridSearch(ctx context.Context, knowledgeBaseID string, params *SearchParams) ([]*SearchResult, error) {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/hybrid-search", knowledgeBaseID)
queryParams := url.Values{}
queryParams.Add("query", query)
resp, err := c.doRequest(ctx, http.MethodGet, path, nil, queryParams)
resp, err := c.doRequest(ctx, http.MethodGet, path, params, nil)
if err != nil {
return nil, err
}
+157
View File
@@ -0,0 +1,157 @@
package client
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
// Tag represents a knowledge base tag.
type Tag struct {
ID string `json:"id"`
TenantID uint64 `json:"tenant_id"`
KnowledgeBaseID string `json:"knowledge_base_id"`
Name string `json:"name"`
Color string `json:"color"`
SortOrder int `json:"sort_order"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TagWithStats represents tag information along with usage statistics.
type TagWithStats struct {
Tag
KnowledgeCount int64 `json:"knowledge_count"`
ChunkCount int64 `json:"chunk_count"`
}
// CreateTagPayload is used to create a new tag.
type CreateTagPayload struct {
Name string `json:"name"`
Color string `json:"color,omitempty"`
SortOrder int `json:"sort_order,omitempty"`
}
// UpdateTagPayload is used to update an existing tag.
type UpdateTagPayload struct {
Name *string `json:"name,omitempty"`
Color *string `json:"color,omitempty"`
SortOrder *int `json:"sort_order,omitempty"`
}
// TagsPage contains paginated tag results.
type TagsPage struct {
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
Tags []TagWithStats `json:"data"`
}
// TagsResponse wraps the paginated tags response.
type TagsResponse struct {
Success bool `json:"success"`
Data *TagsPage `json:"data"`
Message string `json:"message,omitempty"`
Code string `json:"code,omitempty"`
}
// TagResponse wraps a single tag response.
type TagResponse struct {
Success bool `json:"success"`
Data *Tag `json:"data"`
Message string `json:"message,omitempty"`
Code string `json:"code,omitempty"`
}
type tagSimpleResponse struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Code string `json:"code,omitempty"`
}
// ListTags returns paginated tags under a knowledge base.
func (c *Client) ListTags(ctx context.Context,
knowledgeBaseID string, page, pageSize int, keyword string,
) (*TagsPage, error) {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/tags", knowledgeBaseID)
query := url.Values{}
if page > 0 {
query.Add("page", strconv.Itoa(page))
}
if pageSize > 0 {
query.Add("page_size", strconv.Itoa(pageSize))
}
if keyword != "" {
query.Add("keyword", keyword)
}
resp, err := c.doRequest(ctx, http.MethodGet, path, nil, query)
if err != nil {
return nil, err
}
var response TagsResponse
if err := parseResponse(resp, &response); err != nil {
return nil, err
}
if response.Data == nil {
return &TagsPage{}, nil
}
return response.Data, nil
}
// CreateTag creates a new tag under a knowledge base.
func (c *Client) CreateTag(ctx context.Context,
knowledgeBaseID string, payload *CreateTagPayload,
) (*Tag, error) {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/tags", knowledgeBaseID)
resp, err := c.doRequest(ctx, http.MethodPost, path, payload, nil)
if err != nil {
return nil, err
}
var response TagResponse
if err := parseResponse(resp, &response); err != nil {
return nil, err
}
return response.Data, nil
}
// UpdateTag updates an existing tag.
func (c *Client) UpdateTag(ctx context.Context,
knowledgeBaseID, tagID string, payload *UpdateTagPayload,
) (*Tag, error) {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/tags/%s", knowledgeBaseID, tagID)
resp, err := c.doRequest(ctx, http.MethodPut, path, payload, nil)
if err != nil {
return nil, err
}
var response TagResponse
if err := parseResponse(resp, &response); err != nil {
return nil, err
}
return response.Data, nil
}
// DeleteTag deletes a tag. Set force to true to delete even if the tag is referenced.
func (c *Client) DeleteTag(ctx context.Context,
knowledgeBaseID, tagID string, force bool,
) error {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/tags/%s", knowledgeBaseID, tagID)
query := url.Values{}
if force {
query.Add("force", "true")
}
resp, err := c.doRequest(ctx, http.MethodDelete, path, nil, query)
if err != nil {
return err
}
var response tagSimpleResponse
return parseResponse(resp, &response)
}