feat(repository): add TagID and IsRecommended fields to vector embeddings
- Introduced `TagID` and `IsRecommended` fields across various repository implementations (Elasticsearch, Milvus, Postgres, Qdrant, SQLite) to enhance document categorization and recommendation capabilities. - Updated the logic for handling `IsEnabled` to ensure it reflects the actual state from the source data. - Adjusted the knowledge service to incorporate the new `IsRecommended` field, improving the indexing process for knowledge chunks. These changes enhance the flexibility and functionality of document retrieval and indexing across the application.
This commit is contained in:
@@ -15,8 +15,10 @@ type VectorEmbedding struct {
|
|||||||
ChunkID string `json:"chunk_id" gorm:"column:chunk_id"` // Unique ID of the text chunk
|
ChunkID string `json:"chunk_id" gorm:"column:chunk_id"` // Unique ID of the text chunk
|
||||||
KnowledgeID string `json:"knowledge_id" gorm:"column:knowledge_id"` // ID of the knowledge item
|
KnowledgeID string `json:"knowledge_id" gorm:"column:knowledge_id"` // ID of the knowledge item
|
||||||
KnowledgeBaseID string `json:"knowledge_base_id" gorm:"column:knowledge_base_id"` // ID of the knowledge base
|
KnowledgeBaseID string `json:"knowledge_base_id" gorm:"column:knowledge_base_id"` // ID of the knowledge base
|
||||||
|
TagID string `json:"tag_id" gorm:"column:tag_id"` // Tag ID for categorization
|
||||||
Embedding []float32 `json:"embedding" gorm:"column:embedding;not null"` // Vector embedding of the content
|
Embedding []float32 `json:"embedding" gorm:"column:embedding;not null"` // Vector embedding of the content
|
||||||
IsEnabled bool `json:"is_enabled"` // Whether the chunk is enabled
|
IsEnabled bool `json:"is_enabled"` // Whether the chunk is enabled
|
||||||
|
IsRecommended bool `json:"is_recommended"` // Whether the chunk is recommended
|
||||||
}
|
}
|
||||||
|
|
||||||
// VectorEmbeddingWithScore extends VectorEmbedding with similarity score
|
// VectorEmbeddingWithScore extends VectorEmbedding with similarity score
|
||||||
@@ -34,7 +36,9 @@ func ToDBVectorEmbedding(embedding *types.IndexInfo, additionalParams map[string
|
|||||||
ChunkID: embedding.ChunkID,
|
ChunkID: embedding.ChunkID,
|
||||||
KnowledgeID: embedding.KnowledgeID,
|
KnowledgeID: embedding.KnowledgeID,
|
||||||
KnowledgeBaseID: embedding.KnowledgeBaseID,
|
KnowledgeBaseID: embedding.KnowledgeBaseID,
|
||||||
IsEnabled: true, // Default to enabled
|
TagID: embedding.TagID,
|
||||||
|
IsEnabled: embedding.IsEnabled,
|
||||||
|
IsRecommended: embedding.IsRecommended,
|
||||||
}
|
}
|
||||||
// Add embedding data if available in additionalParams
|
// Add embedding data if available in additionalParams
|
||||||
if additionalParams != nil && slices.Contains(slices.Collect(maps.Keys(additionalParams)), "embedding") {
|
if additionalParams != nil && slices.Contains(slices.Collect(maps.Keys(additionalParams)), "embedding") {
|
||||||
@@ -65,8 +69,10 @@ func FromDBVectorEmbeddingWithScore(id string,
|
|||||||
ChunkID: embedding.ChunkID,
|
ChunkID: embedding.ChunkID,
|
||||||
KnowledgeID: embedding.KnowledgeID,
|
KnowledgeID: embedding.KnowledgeID,
|
||||||
KnowledgeBaseID: embedding.KnowledgeBaseID,
|
KnowledgeBaseID: embedding.KnowledgeBaseID,
|
||||||
|
TagID: embedding.TagID,
|
||||||
Content: embedding.Content,
|
Content: embedding.Content,
|
||||||
Score: embedding.Score,
|
Score: embedding.Score,
|
||||||
MatchType: matchType,
|
MatchType: matchType,
|
||||||
|
IsEnabled: embedding.IsEnabled,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -592,6 +592,11 @@ func (m *milvusRepository) getBaseFilterForQuery(params types.RetrieveParams) (s
|
|||||||
Value: params.ExcludeChunkIDs,
|
Value: params.ExcludeChunkIDs,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
filters = append(filters, &universalFilterCondition{
|
||||||
|
Field: fieldIsEnabled,
|
||||||
|
Operator: operatorEqual,
|
||||||
|
Value: true,
|
||||||
|
})
|
||||||
if len(filters) == 0 {
|
if len(filters) == 0 {
|
||||||
return "", nil, nil
|
return "", nil, nil
|
||||||
}
|
}
|
||||||
@@ -919,7 +924,7 @@ func toMilvusVectorEmbedding(embedding *types.IndexInfo, additionalParams map[st
|
|||||||
KnowledgeID: embedding.KnowledgeID,
|
KnowledgeID: embedding.KnowledgeID,
|
||||||
KnowledgeBaseID: embedding.KnowledgeBaseID,
|
KnowledgeBaseID: embedding.KnowledgeBaseID,
|
||||||
TagID: embedding.TagID,
|
TagID: embedding.TagID,
|
||||||
IsEnabled: true, // Default to enabled
|
IsEnabled: embedding.IsEnabled,
|
||||||
}
|
}
|
||||||
if additionalParams != nil && slices.Contains(slices.Collect(maps.Keys(additionalParams)), fieldEmbedding) {
|
if additionalParams != nil && slices.Contains(slices.Collect(maps.Keys(additionalParams)), fieldEmbedding) {
|
||||||
if embeddingMap, ok := additionalParams[fieldEmbedding].(map[string][]float32); ok {
|
if embeddingMap, ok := additionalParams[fieldEmbedding].(map[string][]float32); ok {
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ func toDBVectorEmbedding(indexInfo *types.IndexInfo, additionalParams map[string
|
|||||||
KnowledgeBaseID: indexInfo.KnowledgeBaseID,
|
KnowledgeBaseID: indexInfo.KnowledgeBaseID,
|
||||||
TagID: indexInfo.TagID,
|
TagID: indexInfo.TagID,
|
||||||
Content: common.CleanInvalidUTF8(indexInfo.Content),
|
Content: common.CleanInvalidUTF8(indexInfo.Content),
|
||||||
IsEnabled: true, // Default to enabled
|
IsEnabled: indexInfo.IsEnabled,
|
||||||
}
|
}
|
||||||
// Add embedding data if available in additionalParams
|
// Add embedding data if available in additionalParams
|
||||||
if additionalParams != nil && slices.Contains(slices.Collect(maps.Keys(additionalParams)), "embedding") {
|
if additionalParams != nil && slices.Contains(slices.Collect(maps.Keys(additionalParams)), "embedding") {
|
||||||
|
|||||||
@@ -797,6 +797,10 @@ func (q *qdrantRepository) CopyIndices(ctx context.Context,
|
|||||||
targetSourceID = uuid.New().String()
|
targetSourceID = uuid.New().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isEnabled := true
|
||||||
|
if v, ok := payload[fieldIsEnabled]; ok {
|
||||||
|
isEnabled = v.GetBoolValue()
|
||||||
|
}
|
||||||
newPayload := qdrant.NewValueMap(map[string]any{
|
newPayload := qdrant.NewValueMap(map[string]any{
|
||||||
fieldContent: payload[fieldContent].GetStringValue(),
|
fieldContent: payload[fieldContent].GetStringValue(),
|
||||||
fieldSourceID: targetSourceID,
|
fieldSourceID: targetSourceID,
|
||||||
@@ -804,7 +808,8 @@ func (q *qdrantRepository) CopyIndices(ctx context.Context,
|
|||||||
fieldChunkID: targetChunkID,
|
fieldChunkID: targetChunkID,
|
||||||
fieldKnowledgeID: targetKnowledgeID,
|
fieldKnowledgeID: targetKnowledgeID,
|
||||||
fieldKnowledgeBaseID: targetKnowledgeBaseID,
|
fieldKnowledgeBaseID: targetKnowledgeBaseID,
|
||||||
fieldIsEnabled: true,
|
fieldTagID: payload[fieldTagID].GetStringValue(),
|
||||||
|
fieldIsEnabled: isEnabled,
|
||||||
})
|
})
|
||||||
|
|
||||||
var vectors *qdrant.Vectors
|
var vectors *qdrant.Vectors
|
||||||
@@ -923,7 +928,7 @@ func toQdrantVectorEmbedding(embedding *types.IndexInfo, additionalParams map[st
|
|||||||
KnowledgeID: embedding.KnowledgeID,
|
KnowledgeID: embedding.KnowledgeID,
|
||||||
KnowledgeBaseID: embedding.KnowledgeBaseID,
|
KnowledgeBaseID: embedding.KnowledgeBaseID,
|
||||||
TagID: embedding.TagID,
|
TagID: embedding.TagID,
|
||||||
IsEnabled: true, // Default to enabled
|
IsEnabled: embedding.IsEnabled,
|
||||||
}
|
}
|
||||||
if additionalParams != nil && slices.Contains(slices.Collect(maps.Keys(additionalParams)), fieldEmbedding) {
|
if additionalParams != nil && slices.Contains(slices.Collect(maps.Keys(additionalParams)), fieldEmbedding) {
|
||||||
if embeddingMap, ok := additionalParams[fieldEmbedding].(map[string][]float32); ok {
|
if embeddingMap, ok := additionalParams[fieldEmbedding].(map[string][]float32); ok {
|
||||||
|
|||||||
@@ -437,7 +437,7 @@ func (r *sqliteRepository) vectorRetrieve(ctx context.Context, params types.Retr
|
|||||||
// --- Internal helpers ---
|
// --- Internal helpers ---
|
||||||
|
|
||||||
func toSQLiteEmbedding(info *types.IndexInfo) *sqliteEmbedding {
|
func toSQLiteEmbedding(info *types.IndexInfo) *sqliteEmbedding {
|
||||||
enabled := true
|
enabled := info.IsEnabled
|
||||||
return &sqliteEmbedding{
|
return &sqliteEmbedding{
|
||||||
SourceID: info.SourceID,
|
SourceID: info.SourceID,
|
||||||
SourceType: int(info.SourceType),
|
SourceType: int(info.SourceType),
|
||||||
|
|||||||
@@ -6224,6 +6224,7 @@ func (s *knowledgeService) buildFAQIndexInfoList(
|
|||||||
KnowledgeType: types.KnowledgeTypeFAQ,
|
KnowledgeType: types.KnowledgeTypeFAQ,
|
||||||
TagID: chunk.TagID,
|
TagID: chunk.TagID,
|
||||||
IsEnabled: chunk.IsEnabled,
|
IsEnabled: chunk.IsEnabled,
|
||||||
|
IsRecommended: chunk.Flags.HasFlag(types.ChunkFlagRecommended),
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@@ -6252,6 +6253,7 @@ func (s *knowledgeService) buildFAQIndexInfoList(
|
|||||||
KnowledgeType: types.KnowledgeTypeFAQ,
|
KnowledgeType: types.KnowledgeTypeFAQ,
|
||||||
TagID: chunk.TagID,
|
TagID: chunk.TagID,
|
||||||
IsEnabled: chunk.IsEnabled,
|
IsEnabled: chunk.IsEnabled,
|
||||||
|
IsRecommended: chunk.Flags.HasFlag(types.ChunkFlagRecommended),
|
||||||
})
|
})
|
||||||
|
|
||||||
// 每个相似问创建一个索引项
|
// 每个相似问创建一个索引项
|
||||||
@@ -6277,6 +6279,7 @@ func (s *knowledgeService) buildFAQIndexInfoList(
|
|||||||
KnowledgeType: types.KnowledgeTypeFAQ,
|
KnowledgeType: types.KnowledgeTypeFAQ,
|
||||||
TagID: chunk.TagID,
|
TagID: chunk.TagID,
|
||||||
IsEnabled: chunk.IsEnabled,
|
IsEnabled: chunk.IsEnabled,
|
||||||
|
IsRecommended: chunk.Flags.HasFlag(types.ChunkFlagRecommended),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user