yangdx
commited on
Commit
·
c26d725
1
Parent(s):
dd6e078
Update graph retrival api
Browse files
lightrag/api/routers/graph_routes.py
CHANGED
@@ -2,53 +2,87 @@
|
|
2 |
This module contains all graph-related routes for the LightRAG API.
|
3 |
"""
|
4 |
|
5 |
-
from typing import Optional
|
6 |
-
from fastapi import APIRouter, Depends
|
|
|
7 |
|
8 |
from ..utils_api import get_combined_auth_dependency
|
9 |
|
10 |
router = APIRouter(tags=["graph"])
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def create_graph_routes(rag, api_key: Optional[str] = None):
|
14 |
combined_auth = get_combined_auth_dependency(api_key)
|
15 |
|
16 |
-
@router.get("/graph/label/list",
|
|
|
|
|
17 |
async def get_graph_labels():
|
18 |
"""
|
19 |
Get all graph labels
|
20 |
|
21 |
Returns:
|
22 |
-
|
23 |
"""
|
24 |
-
|
|
|
25 |
|
26 |
-
@router.get("/graphs",
|
|
|
|
|
27 |
async def get_knowledge_graph(
|
28 |
-
label: str
|
|
|
|
|
29 |
):
|
30 |
"""
|
31 |
Retrieve a connected subgraph of nodes where the label includes the specified label.
|
32 |
-
Maximum number of nodes is constrained by the environment variable `MAX_GRAPH_NODES` (default: 1000).
|
33 |
When reducing the number of nodes, the prioritization criteria are as follows:
|
34 |
-
1.
|
35 |
-
2.
|
36 |
-
3. Followed by nodes directly connected to the matching nodes
|
37 |
-
4. Finally, the degree of the nodes
|
38 |
-
Maximum number of nodes is limited to env MAX_GRAPH_NODES(default: 1000)
|
39 |
|
40 |
Args:
|
41 |
-
label (str): Label
|
42 |
-
max_depth (int, optional): Maximum depth of
|
43 |
-
|
44 |
-
min_degree (int, optional): Minimum degree of nodes. Defaults to 0. (Deprecated, always 0)
|
45 |
|
46 |
Returns:
|
47 |
-
|
48 |
"""
|
49 |
-
|
50 |
node_label=label,
|
51 |
max_depth=max_depth,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
)
|
53 |
|
54 |
return router
|
|
|
2 |
This module contains all graph-related routes for the LightRAG API.
|
3 |
"""
|
4 |
|
5 |
+
from typing import Optional, List, Dict, Any
|
6 |
+
from fastapi import APIRouter, Depends, Query
|
7 |
+
from pydantic import BaseModel, Field
|
8 |
|
9 |
from ..utils_api import get_combined_auth_dependency
|
10 |
|
11 |
router = APIRouter(tags=["graph"])
|
12 |
|
13 |
+
# Pydantic models for graph routes
|
14 |
+
class GraphLabelsResponse(BaseModel):
|
15 |
+
"""Response model: List of graph labels"""
|
16 |
+
labels: List[str] = Field(description="List of graph labels")
|
17 |
+
|
18 |
+
class KnowledgeGraphNode(BaseModel):
|
19 |
+
"""Model for a node in the knowledge graph"""
|
20 |
+
id: str = Field(description="Unique identifier of the node")
|
21 |
+
label: str = Field(description="Label of the node")
|
22 |
+
properties: Dict[str, Any] = Field(default_factory=dict, description="Properties of the node")
|
23 |
+
|
24 |
+
class KnowledgeGraphEdge(BaseModel):
|
25 |
+
"""Model for an edge in the knowledge graph"""
|
26 |
+
source: str = Field(description="Source node ID")
|
27 |
+
target: str = Field(description="Target node ID")
|
28 |
+
type: str = Field(description="Type of the relationship")
|
29 |
+
properties: Dict[str, Any] = Field(default_factory=dict, description="Properties of the edge")
|
30 |
+
|
31 |
+
class KnowledgeGraphResponse(BaseModel):
|
32 |
+
"""Response model: Knowledge graph data"""
|
33 |
+
nodes: List[KnowledgeGraphNode] = Field(description="List of nodes in the graph")
|
34 |
+
edges: List[KnowledgeGraphEdge] = Field(description="List of edges in the graph")
|
35 |
+
|
36 |
|
37 |
def create_graph_routes(rag, api_key: Optional[str] = None):
|
38 |
combined_auth = get_combined_auth_dependency(api_key)
|
39 |
|
40 |
+
@router.get("/graph/label/list",
|
41 |
+
dependencies=[Depends(combined_auth)],
|
42 |
+
response_model=GraphLabelsResponse)
|
43 |
async def get_graph_labels():
|
44 |
"""
|
45 |
Get all graph labels
|
46 |
|
47 |
Returns:
|
48 |
+
GraphLabelsResponse: List of graph labels
|
49 |
"""
|
50 |
+
labels = await rag.get_graph_labels()
|
51 |
+
return GraphLabelsResponse(labels=labels)
|
52 |
|
53 |
+
@router.get("/graphs",
|
54 |
+
dependencies=[Depends(combined_auth)],
|
55 |
+
response_model=KnowledgeGraphResponse)
|
56 |
async def get_knowledge_graph(
|
57 |
+
label: str = Query(..., description="Label to get knowledge graph for"),
|
58 |
+
max_depth: int = Query(3, description="Maximum depth of graph", ge=1),
|
59 |
+
max_nodes: int = Query(1000, description="Maxiumu nodes to return", ge=1),
|
60 |
):
|
61 |
"""
|
62 |
Retrieve a connected subgraph of nodes where the label includes the specified label.
|
|
|
63 |
When reducing the number of nodes, the prioritization criteria are as follows:
|
64 |
+
1. Hops(path) to the staring node take precedence
|
65 |
+
2. Followed by the degree of the nodes
|
|
|
|
|
|
|
66 |
|
67 |
Args:
|
68 |
+
label (str): Label of the starting node
|
69 |
+
max_depth (int, optional): Maximum depth of the subgraph,Defaults to 3
|
70 |
+
max_nodes: Maxiumu nodes to return
|
|
|
71 |
|
72 |
Returns:
|
73 |
+
KnowledgeGraphResponse: Knowledge graph containing nodes and edges
|
74 |
"""
|
75 |
+
graph_data = await rag.get_knowledge_graph(
|
76 |
node_label=label,
|
77 |
max_depth=max_depth,
|
78 |
+
max_nodes=max_nodes,
|
79 |
+
)
|
80 |
+
|
81 |
+
# Convert the returned dictionary to our response model format
|
82 |
+
# Assuming the returned dictionary has 'nodes' and 'edges' keys
|
83 |
+
return KnowledgeGraphResponse(
|
84 |
+
nodes=graph_data.get("nodes", []),
|
85 |
+
edges=graph_data.get("edges", [])
|
86 |
)
|
87 |
|
88 |
return router
|