yangdx
commited on
Commit
·
69a5e66
1
Parent(s):
c26d725
Update graph retrival api(abandon pydantic model)
Browse files- lightrag/api/routers/graph_routes.py +8 -44
- lightrag/base.py +1 -1
- lightrag/lightrag.py +3 -19
lightrag/api/routers/graph_routes.py
CHANGED
@@ -2,61 +2,32 @@
|
|
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, 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 |
-
|
49 |
"""
|
50 |
-
|
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="
|
60 |
):
|
61 |
"""
|
62 |
Retrieve a connected subgraph of nodes where the label includes the specified label.
|
@@ -70,19 +41,12 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
|
|
70 |
max_nodes: Maxiumu nodes to return
|
71 |
|
72 |
Returns:
|
73 |
-
|
74 |
"""
|
75 |
-
|
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
|
|
|
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, Query
|
|
|
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", dependencies=[Depends(combined_auth)])
|
|
|
|
|
17 |
async def get_graph_labels():
|
18 |
"""
|
19 |
Get all graph labels
|
20 |
|
21 |
Returns:
|
22 |
+
List[str]: List of graph labels
|
23 |
"""
|
24 |
+
return await rag.get_graph_labels()
|
|
|
25 |
|
26 |
+
@router.get("/graphs", dependencies=[Depends(combined_auth)])
|
|
|
|
|
27 |
async def get_knowledge_graph(
|
28 |
label: str = Query(..., description="Label to get knowledge graph for"),
|
29 |
max_depth: int = Query(3, description="Maximum depth of graph", ge=1),
|
30 |
+
max_nodes: int = Query(1000, description="Maximum nodes to return", ge=1),
|
31 |
):
|
32 |
"""
|
33 |
Retrieve a connected subgraph of nodes where the label includes the specified label.
|
|
|
41 |
max_nodes: Maxiumu nodes to return
|
42 |
|
43 |
Returns:
|
44 |
+
Dict[str, List[str]]: Knowledge graph for label
|
45 |
"""
|
46 |
+
return await rag.get_knowledge_graph(
|
47 |
node_label=label,
|
48 |
max_depth=max_depth,
|
49 |
max_nodes=max_nodes,
|
50 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
return router
|
lightrag/base.py
CHANGED
@@ -341,7 +341,7 @@ class BaseGraphStorage(StorageNameSpace, ABC):
|
|
341 |
|
342 |
@abstractmethod
|
343 |
async def get_knowledge_graph(
|
344 |
-
self, node_label: str, max_depth: int = 3
|
345 |
) -> KnowledgeGraph:
|
346 |
"""Retrieve a subgraph of the knowledge graph starting from a given node."""
|
347 |
|
|
|
341 |
|
342 |
@abstractmethod
|
343 |
async def get_knowledge_graph(
|
344 |
+
self, node_label: str, max_depth: int = 3, max_nodes: int = 1000
|
345 |
) -> KnowledgeGraph:
|
346 |
"""Retrieve a subgraph of the knowledge graph starting from a given node."""
|
347 |
|
lightrag/lightrag.py
CHANGED
@@ -510,36 +510,20 @@ class LightRAG:
|
|
510 |
self,
|
511 |
node_label: str,
|
512 |
max_depth: int = 3,
|
513 |
-
|
514 |
-
inclusive: bool = False,
|
515 |
) -> KnowledgeGraph:
|
516 |
"""Get knowledge graph for a given label
|
517 |
|
518 |
Args:
|
519 |
node_label (str): Label to get knowledge graph for
|
520 |
max_depth (int): Maximum depth of graph
|
521 |
-
|
522 |
-
inclusive (bool, optional): Whether to use inclusive search mode. Defaults to False.
|
523 |
|
524 |
Returns:
|
525 |
KnowledgeGraph: Knowledge graph containing nodes and edges
|
526 |
"""
|
527 |
-
# get params supported by get_knowledge_graph of specified storage
|
528 |
-
import inspect
|
529 |
|
530 |
-
|
531 |
-
self.chunk_entity_relation_graph.get_knowledge_graph
|
532 |
-
).parameters
|
533 |
-
|
534 |
-
kwargs = {"node_label": node_label, "max_depth": max_depth}
|
535 |
-
|
536 |
-
if "min_degree" in storage_params and min_degree > 0:
|
537 |
-
kwargs["min_degree"] = min_degree
|
538 |
-
|
539 |
-
if "inclusive" in storage_params:
|
540 |
-
kwargs["inclusive"] = inclusive
|
541 |
-
|
542 |
-
return await self.chunk_entity_relation_graph.get_knowledge_graph(**kwargs)
|
543 |
|
544 |
def _get_storage_class(self, storage_name: str) -> Callable[..., Any]:
|
545 |
import_path = STORAGES[storage_name]
|
|
|
510 |
self,
|
511 |
node_label: str,
|
512 |
max_depth: int = 3,
|
513 |
+
max_nodes: int = 1000,
|
|
|
514 |
) -> KnowledgeGraph:
|
515 |
"""Get knowledge graph for a given label
|
516 |
|
517 |
Args:
|
518 |
node_label (str): Label to get knowledge graph for
|
519 |
max_depth (int): Maximum depth of graph
|
520 |
+
max_nodes (int, optional): Maximum number of nodes to return. Defaults to 1000.
|
|
|
521 |
|
522 |
Returns:
|
523 |
KnowledgeGraph: Knowledge graph containing nodes and edges
|
524 |
"""
|
|
|
|
|
525 |
|
526 |
+
return await self.chunk_entity_relation_graph.get_knowledge_graph(node_label, max_depth, max_nodes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
527 |
|
528 |
def _get_storage_class(self, storage_name: str) -> Callable[..., Any]:
|
529 |
import_path = STORAGES[storage_name]
|