feat: Implement FAQ functionality in Knowledge Base
- Added support for creating and managing FAQ knowledge bases, including new API endpoints for listing, upserting, updating, and deleting FAQ entries. - Enhanced the frontend to allow users to create FAQ knowledge bases and manage FAQ entries with a dedicated interface. - Introduced new types and configurations for FAQ handling in the backend, ensuring proper indexing and retrieval of FAQ entries. - Updated localization files to include new strings related to FAQ management in multiple languages. - Refactored existing components to accommodate the new FAQ features, improving user experience and functionality.
This commit is contained in:
@@ -24,6 +24,8 @@ const (
|
||||
ChunkTypeEntity ChunkType = "entity"
|
||||
// ChunkTypeRelationship 表示关系类型的 Chunk
|
||||
ChunkTypeRelationship ChunkType = "relationship"
|
||||
// ChunkTypeFAQ 表示 FAQ 条目 Chunk
|
||||
ChunkTypeFAQ ChunkType = "faq"
|
||||
)
|
||||
|
||||
// ImageInfo 表示与 Chunk 关联的图片信息
|
||||
@@ -79,6 +81,8 @@ type Chunk struct {
|
||||
RelationChunks JSON `json:"relation_chunks" gorm:"type:json"`
|
||||
// 间接关系 Chunk ID,用于关联间接关系 Chunk 和原始文本 Chunk
|
||||
IndirectRelationChunks JSON `json:"indirect_relation_chunks" gorm:"type:json"`
|
||||
// Metadata 存储 chunk 级别的扩展信息,例如 FAQ 元数据
|
||||
Metadata JSON `json:"metadata" gorm:"type:json"`
|
||||
// 图片信息,存储为 JSON
|
||||
ImageInfo string `json:"image_info" gorm:"type:text"`
|
||||
// Chunk creation time
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// FAQChunkMetadata 定义 FAQ 条目在 Chunk.Metadata 中的结构
|
||||
type FAQChunkMetadata struct {
|
||||
StandardQuestion string `json:"standard_question"`
|
||||
SimilarQuestions []string `json:"similar_questions,omitempty"`
|
||||
NegativeQuestions []string `json:"negative_questions,omitempty"`
|
||||
Answers []string `json:"answers,omitempty"`
|
||||
Version int `json:"version,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
}
|
||||
|
||||
// Normalize 清理空白与重复项
|
||||
func (m *FAQChunkMetadata) Normalize() {
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
m.StandardQuestion = strings.TrimSpace(m.StandardQuestion)
|
||||
m.SimilarQuestions = normalizeStrings(m.SimilarQuestions)
|
||||
m.NegativeQuestions = normalizeStrings(m.NegativeQuestions)
|
||||
m.Answers = normalizeStrings(m.Answers)
|
||||
if m.Version <= 0 {
|
||||
m.Version = 1
|
||||
}
|
||||
}
|
||||
|
||||
// FAQMetadata 解析 Chunk 中的 FAQ 元数据
|
||||
func (c *Chunk) FAQMetadata() (*FAQChunkMetadata, error) {
|
||||
if c == nil || len(c.Metadata) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var meta FAQChunkMetadata
|
||||
if err := json.Unmarshal(c.Metadata, &meta); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
meta.Normalize()
|
||||
return &meta, nil
|
||||
}
|
||||
|
||||
// SetFAQMetadata 设置 Chunk 的 FAQ 元数据
|
||||
func (c *Chunk) SetFAQMetadata(meta *FAQChunkMetadata) error {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
if meta == nil {
|
||||
c.Metadata = nil
|
||||
return nil
|
||||
}
|
||||
meta.Normalize()
|
||||
bytes, err := json.Marshal(meta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Metadata = JSON(bytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
// FAQEntry 表示返回给前端的 FAQ 条目
|
||||
type FAQEntry struct {
|
||||
ID string `json:"id"`
|
||||
ChunkID string `json:"chunk_id"`
|
||||
KnowledgeID string `json:"knowledge_id"`
|
||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||
StandardQuestion string `json:"standard_question"`
|
||||
SimilarQuestions []string `json:"similar_questions"`
|
||||
NegativeQuestions []string `json:"negative_questions"`
|
||||
Answers []string `json:"answers"`
|
||||
IndexMode FAQIndexMode `json:"index_mode"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Score float64 `json:"score,omitempty"`
|
||||
MatchType MatchType `json:"match_type,omitempty"`
|
||||
ChunkType ChunkType `json:"chunk_type"`
|
||||
}
|
||||
|
||||
// FAQEntryPayload 用于创建/更新 FAQ 条目的 payload
|
||||
type FAQEntryPayload struct {
|
||||
StandardQuestion string `json:"standard_question" binding:"required"`
|
||||
SimilarQuestions []string `json:"similar_questions"`
|
||||
NegativeQuestions []string `json:"negative_questions"`
|
||||
Answers []string `json:"answers" binding:"required"`
|
||||
}
|
||||
|
||||
const (
|
||||
FAQBatchModeAppend = "append"
|
||||
FAQBatchModeReplace = "replace"
|
||||
)
|
||||
|
||||
// FAQBatchUpsertPayload 批量导入 FAQ 条目
|
||||
type FAQBatchUpsertPayload struct {
|
||||
Entries []FAQEntryPayload `json:"entries" binding:"required"`
|
||||
Mode string `json:"mode" binding:"oneof=append replace"`
|
||||
KnowledgeID string `json:"knowledge_id"`
|
||||
}
|
||||
|
||||
// FAQSearchRequest FAQ检索请求参数
|
||||
type FAQSearchRequest struct {
|
||||
QueryText string `json:"query_text" binding:"required"`
|
||||
VectorThreshold float64 `json:"vector_threshold"`
|
||||
MatchCount int `json:"match_count"`
|
||||
}
|
||||
|
||||
func normalizeStrings(values []string) []string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
dedup := make([]string, 0, len(values))
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
for _, v := range values {
|
||||
trimmed := strings.TrimSpace(v)
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := seen[trimmed]; exists {
|
||||
continue
|
||||
}
|
||||
seen[trimmed] = struct{}{}
|
||||
dedup = append(dedup, trimmed)
|
||||
}
|
||||
if len(dedup) == 0 {
|
||||
return nil
|
||||
}
|
||||
return dedup
|
||||
}
|
||||
@@ -50,6 +50,16 @@ type KnowledgeService interface {
|
||||
CloneKnowledgeBase(ctx context.Context, srcID, dstID string) error
|
||||
// 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)
|
||||
// UpsertFAQEntries imports or appends FAQ entries.
|
||||
UpsertFAQEntries(ctx context.Context, kbID string, payload *types.FAQBatchUpsertPayload) error
|
||||
// UpdateFAQEntry updates a single FAQ entry.
|
||||
UpdateFAQEntry(ctx context.Context, kbID string, entryID string, payload *types.FAQEntryPayload) error
|
||||
// DeleteFAQEntries deletes FAQ entries in batch.
|
||||
DeleteFAQEntries(ctx context.Context, kbID string, entryIDs []string) error
|
||||
// SearchFAQEntries searches FAQ entries using hybrid search.
|
||||
SearchFAQEntries(ctx context.Context, kbID string, req *types.FAQSearchRequest) ([]*types.FAQEntry, error)
|
||||
}
|
||||
|
||||
// KnowledgeRepository defines the interface for knowledge repositories.
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
const (
|
||||
KnowledgeTypeManual = "manual"
|
||||
KnowledgeTypeFAQ = "faq"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -12,12 +12,39 @@ const (
|
||||
InitDefaultKnowledgeBaseID = "kb-00000001"
|
||||
)
|
||||
|
||||
const (
|
||||
KnowledgeBaseTypeDocument = "document"
|
||||
KnowledgeBaseTypeFAQ = "faq"
|
||||
)
|
||||
|
||||
// FAQIndexMode 表示 FAQ 索引策略:仅索引标准问还是同时索引问答内容
|
||||
type FAQIndexMode string
|
||||
|
||||
const (
|
||||
// FAQIndexModeQuestionOnly 仅使用标准问及相似问向量化
|
||||
FAQIndexModeQuestionOnly FAQIndexMode = "question_only"
|
||||
// FAQIndexModeQuestionAnswer 将标准问与答案拼接后向量化
|
||||
FAQIndexModeQuestionAnswer FAQIndexMode = "question_answer"
|
||||
)
|
||||
|
||||
// FAQQuestionIndexMode 表示 FAQ 问题索引方式:一起索引还是分别索引
|
||||
type FAQQuestionIndexMode string
|
||||
|
||||
const (
|
||||
// FAQQuestionIndexModeCombined 将标准问和相似问一起索引(当前方式)
|
||||
FAQQuestionIndexModeCombined FAQQuestionIndexMode = "combined"
|
||||
// FAQQuestionIndexModeSeparate 将标准问和相似问分别索引
|
||||
FAQQuestionIndexModeSeparate FAQQuestionIndexMode = "separate"
|
||||
)
|
||||
|
||||
// KnowledgeBase represents a knowledge base
|
||||
type KnowledgeBase struct {
|
||||
// Unique identifier of the knowledge base
|
||||
ID string `yaml:"id" json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
// Name of the knowledge base
|
||||
Name string `yaml:"name" json:"name"`
|
||||
// Type of the knowledge base (document, faq, etc.)
|
||||
Type string `yaml:"type" json:"type" gorm:"type:varchar(32);default:'document'"`
|
||||
// Whether this knowledge base is temporary (ephemeral) and should be hidden from UI
|
||||
IsTemporary bool `yaml:"is_temporary" json:"is_temporary" gorm:"default:false"`
|
||||
// Description of the knowledge base
|
||||
@@ -42,6 +69,8 @@ type KnowledgeBase struct {
|
||||
StorageConfig StorageConfig `yaml:"cos_config" json:"cos_config" gorm:"column:cos_config;type:json"`
|
||||
// Extract config
|
||||
ExtractConfig *ExtractConfig `yaml:"extract_config" json:"extract_config" gorm:"column:extract_config;type:json"`
|
||||
// FAQConfig stores FAQ specific configuration such as indexing strategy
|
||||
FAQConfig *FAQConfig `yaml:"faq_config" json:"faq_config" gorm:"column:faq_config;type:json"`
|
||||
// Creation time of the knowledge base
|
||||
CreatedAt time.Time `yaml:"created_at" json:"created_at"`
|
||||
// Last updated time of the knowledge base
|
||||
@@ -56,6 +85,8 @@ type KnowledgeBaseConfig struct {
|
||||
ChunkingConfig ChunkingConfig `yaml:"chunking_config" json:"chunking_config"`
|
||||
// Image processing configuration
|
||||
ImageProcessingConfig ImageProcessingConfig `yaml:"image_processing_config" json:"image_processing_config"`
|
||||
// FAQ configuration (only for FAQ type knowledge bases)
|
||||
FAQConfig *FAQConfig `yaml:"faq_config" json:"faq_config"`
|
||||
}
|
||||
|
||||
// ChunkingConfig represents the document splitting configuration
|
||||
@@ -195,3 +226,53 @@ func (e *ExtractConfig) Scan(value interface{}) error {
|
||||
}
|
||||
return json.Unmarshal(b, e)
|
||||
}
|
||||
|
||||
// FAQConfig 存储 FAQ 知识库的特有配置
|
||||
type FAQConfig struct {
|
||||
IndexMode FAQIndexMode `yaml:"index_mode" json:"index_mode"`
|
||||
QuestionIndexMode FAQQuestionIndexMode `yaml:"question_index_mode" json:"question_index_mode"`
|
||||
}
|
||||
|
||||
// Value implements driver.Valuer
|
||||
func (f FAQConfig) Value() (driver.Value, error) {
|
||||
return json.Marshal(f)
|
||||
}
|
||||
|
||||
// Scan implements sql.Scanner
|
||||
func (f *FAQConfig) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
b, ok := value.([]byte)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(b, f)
|
||||
}
|
||||
|
||||
// EnsureDefaults 确保类型与配置具备默认值
|
||||
func (kb *KnowledgeBase) EnsureDefaults() {
|
||||
if kb == nil {
|
||||
return
|
||||
}
|
||||
if kb.Type == "" {
|
||||
kb.Type = KnowledgeBaseTypeDocument
|
||||
}
|
||||
if kb.Type != KnowledgeBaseTypeFAQ {
|
||||
kb.FAQConfig = nil
|
||||
return
|
||||
}
|
||||
if kb.FAQConfig == nil {
|
||||
kb.FAQConfig = &FAQConfig{
|
||||
IndexMode: FAQIndexModeQuestionAnswer,
|
||||
QuestionIndexMode: FAQQuestionIndexModeCombined,
|
||||
}
|
||||
return
|
||||
}
|
||||
if kb.FAQConfig.IndexMode == "" {
|
||||
kb.FAQConfig.IndexMode = FAQIndexModeQuestionAnswer
|
||||
}
|
||||
if kb.FAQConfig.QuestionIndexMode == "" {
|
||||
kb.FAQConfig.QuestionIndexMode = FAQQuestionIndexModeCombined
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,6 +73,11 @@ type IndexWithScore struct {
|
||||
MatchType MatchType
|
||||
}
|
||||
|
||||
// GetScore returns the score for ScoreComparable interface
|
||||
func (i *IndexWithScore) GetScore() float64 {
|
||||
return i.Score
|
||||
}
|
||||
|
||||
// RetrieveResult represents the result of retrieval
|
||||
type RetrieveResult struct {
|
||||
Results []*IndexWithScore // Retrieval results
|
||||
|
||||
@@ -50,10 +50,12 @@ type SearchResult struct {
|
||||
|
||||
// SearchParams represents the search parameters
|
||||
type SearchParams struct {
|
||||
QueryText string `json:"query_text"`
|
||||
VectorThreshold float64 `json:"vector_threshold"`
|
||||
KeywordThreshold float64 `json:"keyword_threshold"`
|
||||
MatchCount int `json:"match_count"`
|
||||
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"`
|
||||
}
|
||||
|
||||
// Value implements the driver.Valuer interface, used to convert SearchResult to database value
|
||||
|
||||
Reference in New Issue
Block a user