yangdx
commited on
Commit
·
3cd8164
1
Parent(s):
f4d8de2
Fix import error
Browse files
lightrag/api/routers/graph_routes.py
CHANGED
@@ -3,9 +3,11 @@ This module contains all graph-related routes for the LightRAG API.
|
|
3 |
"""
|
4 |
|
5 |
from typing import Optional, Dict, Any
|
|
|
6 |
from fastapi import APIRouter, Depends, Query, HTTPException
|
7 |
from pydantic import BaseModel
|
8 |
|
|
|
9 |
from ..utils_api import get_combined_auth_dependency
|
10 |
|
11 |
router = APIRouter(tags=["graph"])
|
@@ -34,7 +36,12 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
|
|
34 |
Returns:
|
35 |
List[str]: List of graph labels
|
36 |
"""
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
@router.get("/graphs", dependencies=[Depends(combined_auth)])
|
40 |
async def get_knowledge_graph(
|
@@ -56,11 +63,16 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
|
|
56 |
Returns:
|
57 |
Dict[str, List[str]]: Knowledge graph for label
|
58 |
"""
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
@router.get("/graph/entity/exists", dependencies=[Depends(combined_auth)])
|
66 |
async def check_entity_exists(
|
@@ -79,6 +91,8 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
|
|
79 |
exists = await rag.chunk_entity_relation_graph.has_node(name)
|
80 |
return {"exists": exists}
|
81 |
except Exception as e:
|
|
|
|
|
82 |
raise HTTPException(
|
83 |
status_code=500, detail=f"Error checking entity existence: {str(e)}"
|
84 |
)
|
@@ -106,8 +120,11 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
|
|
106 |
"data": result,
|
107 |
}
|
108 |
except ValueError as ve:
|
|
|
109 |
raise HTTPException(status_code=400, detail=str(ve))
|
110 |
except Exception as e:
|
|
|
|
|
111 |
raise HTTPException(
|
112 |
status_code=500, detail=f"Error updating entity: {str(e)}"
|
113 |
)
|
@@ -134,8 +151,11 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
|
|
134 |
"data": result,
|
135 |
}
|
136 |
except ValueError as ve:
|
|
|
137 |
raise HTTPException(status_code=400, detail=str(ve))
|
138 |
except Exception as e:
|
|
|
|
|
139 |
raise HTTPException(
|
140 |
status_code=500, detail=f"Error updating relation: {str(e)}"
|
141 |
)
|
|
|
3 |
"""
|
4 |
|
5 |
from typing import Optional, Dict, Any
|
6 |
+
import traceback
|
7 |
from fastapi import APIRouter, Depends, Query, HTTPException
|
8 |
from pydantic import BaseModel
|
9 |
|
10 |
+
from lightrag.utils import logger
|
11 |
from ..utils_api import get_combined_auth_dependency
|
12 |
|
13 |
router = APIRouter(tags=["graph"])
|
|
|
36 |
Returns:
|
37 |
List[str]: List of graph labels
|
38 |
"""
|
39 |
+
try:
|
40 |
+
return await rag.get_graph_labels()
|
41 |
+
except Exception as e:
|
42 |
+
logger.error(f"Error getting graph labels: {str(e)}")
|
43 |
+
logger.error(traceback.format_exc())
|
44 |
+
raise HTTPException(status_code=500, detail=f"Error getting graph labels: {str(e)}")
|
45 |
|
46 |
@router.get("/graphs", dependencies=[Depends(combined_auth)])
|
47 |
async def get_knowledge_graph(
|
|
|
63 |
Returns:
|
64 |
Dict[str, List[str]]: Knowledge graph for label
|
65 |
"""
|
66 |
+
try:
|
67 |
+
return await rag.get_knowledge_graph(
|
68 |
+
node_label=label,
|
69 |
+
max_depth=max_depth,
|
70 |
+
max_nodes=max_nodes,
|
71 |
+
)
|
72 |
+
except Exception as e:
|
73 |
+
logger.error(f"Error getting knowledge graph for label '{label}': {str(e)}")
|
74 |
+
logger.error(traceback.format_exc())
|
75 |
+
raise HTTPException(status_code=500, detail=f"Error getting knowledge graph: {str(e)}")
|
76 |
|
77 |
@router.get("/graph/entity/exists", dependencies=[Depends(combined_auth)])
|
78 |
async def check_entity_exists(
|
|
|
91 |
exists = await rag.chunk_entity_relation_graph.has_node(name)
|
92 |
return {"exists": exists}
|
93 |
except Exception as e:
|
94 |
+
logger.error(f"Error checking entity existence for '{name}': {str(e)}")
|
95 |
+
logger.error(traceback.format_exc())
|
96 |
raise HTTPException(
|
97 |
status_code=500, detail=f"Error checking entity existence: {str(e)}"
|
98 |
)
|
|
|
120 |
"data": result,
|
121 |
}
|
122 |
except ValueError as ve:
|
123 |
+
logger.error(f"Validation error updating entity '{request.entity_name}': {str(ve)}")
|
124 |
raise HTTPException(status_code=400, detail=str(ve))
|
125 |
except Exception as e:
|
126 |
+
logger.error(f"Error updating entity '{request.entity_name}': {str(e)}")
|
127 |
+
logger.error(traceback.format_exc())
|
128 |
raise HTTPException(
|
129 |
status_code=500, detail=f"Error updating entity: {str(e)}"
|
130 |
)
|
|
|
151 |
"data": result,
|
152 |
}
|
153 |
except ValueError as ve:
|
154 |
+
logger.error(f"Validation error updating relation between '{request.source_id}' and '{request.target_id}': {str(ve)}")
|
155 |
raise HTTPException(status_code=400, detail=str(ve))
|
156 |
except Exception as e:
|
157 |
+
logger.error(f"Error updating relation between '{request.source_id}' and '{request.target_id}': {str(e)}")
|
158 |
+
logger.error(traceback.format_exc())
|
159 |
raise HTTPException(
|
160 |
status_code=500, detail=f"Error updating relation: {str(e)}"
|
161 |
)
|
lightrag/utils_graph.py
CHANGED
@@ -5,7 +5,8 @@ from typing import Any, cast
|
|
5 |
|
6 |
from .kg.shared_storage import get_graph_db_lock
|
7 |
from .prompt import GRAPH_FIELD_SEP
|
8 |
-
from .utils import compute_mdhash_id, logger
|
|
|
9 |
|
10 |
|
11 |
async def adelete_by_entity(
|
|
|
5 |
|
6 |
from .kg.shared_storage import get_graph_db_lock
|
7 |
from .prompt import GRAPH_FIELD_SEP
|
8 |
+
from .utils import compute_mdhash_id, logger
|
9 |
+
from .base import StorageNameSpace
|
10 |
|
11 |
|
12 |
async def adelete_by_entity(
|