feat: Add Graph Settings component for managing graph extraction configurations
This commit is contained in:
@@ -319,13 +319,19 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) {
|
||||
}
|
||||
|
||||
kb.ExtractConfig = &types.ExtractConfig{
|
||||
Enabled: req.NodeExtract.Enabled,
|
||||
Text: req.NodeExtract.Text,
|
||||
Tags: req.NodeExtract.Tags,
|
||||
Nodes: nodes,
|
||||
Relations: relations,
|
||||
}
|
||||
} else {
|
||||
kb.ExtractConfig = nil
|
||||
kb.ExtractConfig = &types.ExtractConfig{Enabled: false}
|
||||
}
|
||||
if err := validateExtractConfig(kb.ExtractConfig); err != nil {
|
||||
logger.Error(ctx, "Invalid extract configuration", err)
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 保存更新后的知识库
|
||||
@@ -1811,13 +1817,7 @@ func (h *InitializationHandler) ExtractTextRelations(c *gin.Context) {
|
||||
result, err := h.extractRelationsFromText(ctx, req.Text, req.Tags, req.LLMConfig)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "文本关系提取失败", err)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": gin.H{
|
||||
"success": false,
|
||||
"message": err.Error(),
|
||||
},
|
||||
})
|
||||
c.Error(errors.NewInternalServerError("文本关系提取失败: " + err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1885,13 +1885,7 @@ func (h *InitializationHandler) FabriText(c *gin.Context) {
|
||||
result, err := h.fabriText(ctx, req.Tags, req.LLMConfig)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "生成示例文本失败", err)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": gin.H{
|
||||
"success": false,
|
||||
"message": err.Error(),
|
||||
},
|
||||
})
|
||||
c.Error(errors.NewInternalServerError("生成示例文本失败: " + err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
@@ -78,6 +79,11 @@ func (h *KnowledgeBaseHandler) CreateKnowledgeBase(c *gin.Context) {
|
||||
c.Error(errors.NewBadRequestError("Invalid request parameters").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
if err := validateExtractConfig(req.ExtractConfig); err != nil {
|
||||
logger.Error(ctx, "Invalid extract configuration", err)
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Creating knowledge base, name: %s", req.Name)
|
||||
// Create knowledge base using the service
|
||||
@@ -267,3 +273,70 @@ func (h *KnowledgeBaseHandler) CopyKnowledgeBase(c *gin.Context) {
|
||||
"message": "Knowledge base copy successfully",
|
||||
})
|
||||
}
|
||||
|
||||
// validateExtractConfig validates the graph configuration parameters
|
||||
func validateExtractConfig(config *types.ExtractConfig) error {
|
||||
logger.Errorf(context.Background(), "Validating extract configuration: %+v", config)
|
||||
if config == nil {
|
||||
return nil
|
||||
}
|
||||
if !config.Enabled {
|
||||
*config = types.ExtractConfig{Enabled: false}
|
||||
return nil
|
||||
}
|
||||
// Validate text field
|
||||
if config.Text == "" {
|
||||
return errors.NewBadRequestError("text cannot be empty")
|
||||
}
|
||||
|
||||
// Validate tags field
|
||||
if len(config.Tags) == 0 {
|
||||
return errors.NewBadRequestError("tags cannot be empty")
|
||||
}
|
||||
for i, tag := range config.Tags {
|
||||
if tag == "" {
|
||||
return errors.NewBadRequestError("tag cannot be empty at index " + strconv.Itoa(i))
|
||||
}
|
||||
}
|
||||
|
||||
// Validate nodes
|
||||
if len(config.Nodes) == 0 {
|
||||
return errors.NewBadRequestError("nodes cannot be empty")
|
||||
}
|
||||
nodeNames := make(map[string]bool)
|
||||
for i, node := range config.Nodes {
|
||||
if node.Name == "" {
|
||||
return errors.NewBadRequestError("node name cannot be empty at index " + strconv.Itoa(i))
|
||||
}
|
||||
// Check for duplicate node names
|
||||
if nodeNames[node.Name] {
|
||||
return errors.NewBadRequestError("duplicate node name: " + node.Name)
|
||||
}
|
||||
nodeNames[node.Name] = true
|
||||
}
|
||||
|
||||
if len(config.Relations) == 0 {
|
||||
return errors.NewBadRequestError("relations cannot be empty")
|
||||
}
|
||||
// Validate relations
|
||||
for i, relation := range config.Relations {
|
||||
if relation.Node1 == "" {
|
||||
return errors.NewBadRequestError("relation node1 cannot be empty at index " + strconv.Itoa(i))
|
||||
}
|
||||
if relation.Node2 == "" {
|
||||
return errors.NewBadRequestError("relation node2 cannot be empty at index " + strconv.Itoa(i))
|
||||
}
|
||||
if relation.Type == "" {
|
||||
return errors.NewBadRequestError("relation type cannot be empty at index " + strconv.Itoa(i))
|
||||
}
|
||||
// Check if referenced nodes exist
|
||||
if !nodeNames[relation.Node1] {
|
||||
return errors.NewBadRequestError("relation references non-existent node1: " + relation.Node1)
|
||||
}
|
||||
if !nodeNames[relation.Node2] {
|
||||
return errors.NewBadRequestError("relation references non-existent node2: " + relation.Node2)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user