feat: 增加简化版json数据

This commit is contained in:
2026-04-15 14:52:33 +08:00
parent 37cd2171b1
commit 8c5cd3705a
9 changed files with 310 additions and 1 deletions
+3 -1
View File
@@ -3,7 +3,9 @@
"allow": [ "allow": [
"Bash(curl -X POST \"http://localhost:3001/api/nodes\" -H \"Content-Type: application/json\" -d \"{\\\\\"id\\\\\":\\\\\"test_node_1\\\\\",\\\\\"label\\\\\":\\\\\"测试节点\\\\\",\\\\\"type\\\\\":\\\\\"测试类型\\\\\",\\\\\"x\\\\\":100,\\\\\"y\\\\\":200}\" -s)", "Bash(curl -X POST \"http://localhost:3001/api/nodes\" -H \"Content-Type: application/json\" -d \"{\\\\\"id\\\\\":\\\\\"test_node_1\\\\\",\\\\\"label\\\\\":\\\\\"测试节点\\\\\",\\\\\"type\\\\\":\\\\\"测试类型\\\\\",\\\\\"x\\\\\":100,\\\\\"y\\\\\":200}\" -s)",
"Bash(curl -X PUT \"http://localhost:3001/api/nodes/test_node_1\" -H \"Content-Type: application/json\" -d \"{\\\\\"label\\\\\":\\\\\"更新后的测试节点\\\\\",\\\\\"x\\\\\":150}\" -s)", "Bash(curl -X PUT \"http://localhost:3001/api/nodes/test_node_1\" -H \"Content-Type: application/json\" -d \"{\\\\\"label\\\\\":\\\\\"更新后的测试节点\\\\\",\\\\\"x\\\\\":150}\" -s)",
"Bash(curl:*)" "Bash(curl:*)",
"Bash(~/go/bin/swag init:*)",
"Bash(go run:*)"
] ]
} }
} }
+78
View File
@@ -131,6 +131,29 @@ const docTemplate = `{
} }
} }
}, },
"/api/graph/simpleJson": {
"get": {
"description": "获取知识图谱的全部节点和边数据(仅保留核心信息,适合LLM处理)",
"produces": [
"application/json"
],
"summary": "获取简化图数据",
"responses": {
"200": {
"description": "成功",
"schema": {
"$ref": "#/definitions/models.SimpleGraphData"
}
},
"500": {
"description": "内部错误",
"schema": {
"$ref": "#/definitions/models.ErrorResponse"
}
}
}
}
},
"/api/graph/stats": { "/api/graph/stats": {
"get": { "get": {
"description": "获取知识图谱的节点数、边数等统计信息", "description": "获取知识图谱的节点数、边数等统计信息",
@@ -694,6 +717,61 @@ const docTemplate = `{
} }
} }
}, },
"models.SimpleEdge": {
"type": "object",
"properties": {
"label": {
"type": "string",
"example": "包含"
},
"source": {
"type": "string",
"example": "1"
},
"target": {
"type": "string",
"example": "2"
},
"type": {
"type": "string",
"example": "CONTAINS"
}
}
},
"models.SimpleGraphData": {
"type": "object",
"properties": {
"edges": {
"type": "array",
"items": {
"$ref": "#/definitions/models.SimpleEdge"
}
},
"nodes": {
"type": "array",
"items": {
"$ref": "#/definitions/models.SimpleNode"
}
}
}
},
"models.SimpleNode": {
"type": "object",
"properties": {
"id": {
"type": "string",
"example": "1"
},
"label": {
"type": "string",
"example": "人工智能"
},
"type": {
"type": "string",
"example": "概念"
}
}
},
"models.UpdateNodeRequest": { "models.UpdateNodeRequest": {
"type": "object", "type": "object",
"properties": { "properties": {
+78
View File
@@ -125,6 +125,29 @@
} }
} }
}, },
"/api/graph/simpleJson": {
"get": {
"description": "获取知识图谱的全部节点和边数据(仅保留核心信息,适合LLM处理)",
"produces": [
"application/json"
],
"summary": "获取简化图数据",
"responses": {
"200": {
"description": "成功",
"schema": {
"$ref": "#/definitions/models.SimpleGraphData"
}
},
"500": {
"description": "内部错误",
"schema": {
"$ref": "#/definitions/models.ErrorResponse"
}
}
}
}
},
"/api/graph/stats": { "/api/graph/stats": {
"get": { "get": {
"description": "获取知识图谱的节点数、边数等统计信息", "description": "获取知识图谱的节点数、边数等统计信息",
@@ -688,6 +711,61 @@
} }
} }
}, },
"models.SimpleEdge": {
"type": "object",
"properties": {
"label": {
"type": "string",
"example": "包含"
},
"source": {
"type": "string",
"example": "1"
},
"target": {
"type": "string",
"example": "2"
},
"type": {
"type": "string",
"example": "CONTAINS"
}
}
},
"models.SimpleGraphData": {
"type": "object",
"properties": {
"edges": {
"type": "array",
"items": {
"$ref": "#/definitions/models.SimpleEdge"
}
},
"nodes": {
"type": "array",
"items": {
"$ref": "#/definitions/models.SimpleNode"
}
}
}
},
"models.SimpleNode": {
"type": "object",
"properties": {
"id": {
"type": "string",
"example": "1"
},
"label": {
"type": "string",
"example": "人工智能"
},
"type": {
"type": "string",
"example": "概念"
}
}
},
"models.UpdateNodeRequest": { "models.UpdateNodeRequest": {
"type": "object", "type": "object",
"properties": { "properties": {
+53
View File
@@ -196,6 +196,44 @@ definitions:
$ref: '#/definitions/models.Node' $ref: '#/definitions/models.Node'
type: array type: array
type: object type: object
models.SimpleEdge:
properties:
label:
example: 包含
type: string
source:
example: "1"
type: string
target:
example: "2"
type: string
type:
example: CONTAINS
type: string
type: object
models.SimpleGraphData:
properties:
edges:
items:
$ref: '#/definitions/models.SimpleEdge'
type: array
nodes:
items:
$ref: '#/definitions/models.SimpleNode'
type: array
type: object
models.SimpleNode:
properties:
id:
example: "1"
type: string
label:
example: 人工智能
type: string
type:
example: 概念
type: string
type: object
models.UpdateNodeRequest: models.UpdateNodeRequest:
properties: properties:
label: label:
@@ -302,6 +340,21 @@ paths:
schema: schema:
$ref: '#/definitions/models.ErrorResponse' $ref: '#/definitions/models.ErrorResponse'
summary: 获取图数据 summary: 获取图数据
/api/graph/simpleJson:
get:
description: 获取知识图谱的全部节点和边数据(仅保留核心信息,适合LLM处理)
produces:
- application/json
responses:
"200":
description: 成功
schema:
$ref: '#/definitions/models.SimpleGraphData'
"500":
description: 内部错误
schema:
$ref: '#/definitions/models.ErrorResponse'
summary: 获取简化图数据
/api/graph/stats: /api/graph/stats:
get: get:
description: 获取知识图谱的节点数、边数等统计信息 description: 获取知识图谱的节点数、边数等统计信息
+11
View File
@@ -38,3 +38,14 @@ func (h *GraphHandler) GetStats(c *gin.Context) {
stats := h.service.GetStats() stats := h.service.GetStats()
c.JSON(http.StatusOK, stats) c.JSON(http.StatusOK, stats)
} }
// @Summary 获取简化图数据
// @Description 获取知识图谱的全部节点和边数据(仅保留核心信息,适合LLM处理)
// @Produce json
// @Success 200 {object} models.SimpleGraphData "成功"
// @Failure 500 {object} models.ErrorResponse "内部错误"
// @Router /api/graph/simpleJson [get]
func (h *GraphHandler) GetSimpleGraphData(c *gin.Context) {
data := h.service.GetSimpleGraphData()
c.JSON(http.StatusOK, data)
}
+2
View File
@@ -65,6 +65,7 @@ func main() {
{ {
api.GET("/graph", graphHandler.GetGraphData) api.GET("/graph", graphHandler.GetGraphData)
api.GET("/graph/stats", graphHandler.GetStats) api.GET("/graph/stats", graphHandler.GetStats)
api.GET("/graph/simpleJson", graphHandler.GetSimpleGraphData)
api.GET("/search", searchHandler.SearchNodes) api.GET("/search", searchHandler.SearchNodes)
@@ -88,6 +89,7 @@ func main() {
log.Println(" GET /health - Health check") log.Println(" GET /health - Health check")
log.Println(" GET /api/graph - Get all graph data") log.Println(" GET /api/graph - Get all graph data")
log.Println(" GET /api/graph/stats - Get graph statistics") log.Println(" GET /api/graph/stats - Get graph statistics")
log.Println(" GET /api/graph/simpleJson - Get simplified graph data (for LLM)")
log.Println(" GET /api/search?q=query - Search nodes") log.Println(" GET /api/search?q=query - Search nodes")
log.Println(" GET /api/nodes/:id - Get node by ID") log.Println(" GET /api/nodes/:id - Get node by ID")
log.Println(" GET /api/nodes/:id/neighbors - Get node neighbors") log.Println(" GET /api/nodes/:id/neighbors - Get node neighbors")
+21
View File
@@ -24,3 +24,24 @@ type GraphData struct {
Nodes []Node `json:"nodes"` Nodes []Node `json:"nodes"`
Edges []Edge `json:"edges"` Edges []Edge `json:"edges"`
} }
// SimpleNode 简化版节点结构,仅保留核心信息,适合 LLM 处理
type SimpleNode struct {
ID string `json:"id" example:"1"`
Label string `json:"label" example:"人工智能"`
Type string `json:"type,omitempty" example:"概念"`
}
// SimpleEdge 简化版边结构,仅保留核心信息,适合 LLM 处理
type SimpleEdge struct {
Source string `json:"source" example:"1"`
Target string `json:"target" example:"2"`
Type string `json:"type,omitempty" example:"CONTAINS"`
Label string `json:"label,omitempty" example:"包含"`
}
// SimpleGraphData 简化版图数据,适合 LLM 处理
type SimpleGraphData struct {
Nodes []SimpleNode `json:"nodes"`
Edges []SimpleEdge `json:"edges"`
}
+63
View File
@@ -215,6 +215,69 @@ func (s *Neo4jService) GetStats() map[string]int {
} }
} }
// GetSimpleGraphData 获取简化的图数据,仅保留核心信息,适合 LLM 处理
func (s *Neo4jService) GetSimpleGraphData() models.SimpleGraphData {
ctx := context.Background()
var nodes []models.SimpleNode
var edges []models.SimpleEdge
nodeResult, err := neo4j.ExecuteQuery(ctx, s.driver,
"MATCH (n) RETURN n",
map[string]any{},
neo4j.EagerResultTransformer,
)
if err != nil {
fmt.Printf("Error querying nodes: %v\n", err)
return models.SimpleGraphData{}
}
for _, record := range nodeResult.Records {
if v, ok := record.Get("n"); ok {
if n, ok := v.(neo4j.Node); ok {
props := n.Props
node := models.SimpleNode{
ID: getStr(props, "id"),
Label: getStr(props, "label"),
Type: getStr(props, "type"),
}
nodes = append(nodes, node)
}
}
}
edgeResult, err := neo4j.ExecuteQuery(ctx, s.driver,
"MATCH (a)-[r]->(b) RETURN a.id AS source, b.id AS target, r",
map[string]any{},
neo4j.EagerResultTransformer,
)
if err != nil {
fmt.Printf("Error querying edges: %v\n", err)
return models.SimpleGraphData{Nodes: nodes}
}
for _, record := range edgeResult.Records {
source, _ := record.Get("source")
target, _ := record.Get("target")
v, _ := record.Get("r")
if rel, ok := v.(neo4j.Relationship); ok {
props := rel.Props
edge := models.SimpleEdge{
Source: getStr(props, "source"),
Target: getStr(props, "target"),
Type: rel.Type,
Label: getStr(props, "label"),
}
if s, ok := source.(string); ok {
edge.Source = s
}
if t, ok := target.(string); ok {
edge.Target = t
}
edges = append(edges, edge)
}
}
return models.SimpleGraphData{Nodes: nodes, Edges: edges}
}
func neo4jNodeToModel(n neo4j.Node) models.Node { func neo4jNodeToModel(n neo4j.Node) models.Node {
props := n.Props props := n.Props
node := models.Node{ node := models.Node{
+1
View File
@@ -4,6 +4,7 @@ import "knowledge-graph-backend/models"
type GraphService interface { type GraphService interface {
GetGraphData() models.GraphData GetGraphData() models.GraphData
GetSimpleGraphData() models.SimpleGraphData
GetNodeByID(id string) (models.Node, bool) GetNodeByID(id string) (models.Node, bool)
SearchNodes(query string) []models.Node SearchNodes(query string) []models.Node
GetNeighbors(nodeID string) (models.NeighborResponse, bool) GetNeighbors(nodeID string) (models.NeighborResponse, bool)