Fix import error

This commit is contained in:
yangdx
2025-04-14 12:36:41 +08:00
parent 5c1d4201f9
commit c834570cfc
2 changed files with 28 additions and 7 deletions

View File

@@ -3,9 +3,11 @@ This module contains all graph-related routes for the LightRAG API.
""" """
from typing import Optional, Dict, Any from typing import Optional, Dict, Any
import traceback
from fastapi import APIRouter, Depends, Query, HTTPException from fastapi import APIRouter, Depends, Query, HTTPException
from pydantic import BaseModel from pydantic import BaseModel
from lightrag.utils import logger
from ..utils_api import get_combined_auth_dependency from ..utils_api import get_combined_auth_dependency
router = APIRouter(tags=["graph"]) router = APIRouter(tags=["graph"])
@@ -34,7 +36,12 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
Returns: Returns:
List[str]: List of graph labels List[str]: List of graph labels
""" """
try:
return await rag.get_graph_labels() return await rag.get_graph_labels()
except Exception as e:
logger.error(f"Error getting graph labels: {str(e)}")
logger.error(traceback.format_exc())
raise HTTPException(status_code=500, detail=f"Error getting graph labels: {str(e)}")
@router.get("/graphs", dependencies=[Depends(combined_auth)]) @router.get("/graphs", dependencies=[Depends(combined_auth)])
async def get_knowledge_graph( async def get_knowledge_graph(
@@ -56,11 +63,16 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
Returns: Returns:
Dict[str, List[str]]: Knowledge graph for label Dict[str, List[str]]: Knowledge graph for label
""" """
try:
return await rag.get_knowledge_graph( return await rag.get_knowledge_graph(
node_label=label, node_label=label,
max_depth=max_depth, max_depth=max_depth,
max_nodes=max_nodes, max_nodes=max_nodes,
) )
except Exception as e:
logger.error(f"Error getting knowledge graph for label '{label}': {str(e)}")
logger.error(traceback.format_exc())
raise HTTPException(status_code=500, detail=f"Error getting knowledge graph: {str(e)}")
@router.get("/graph/entity/exists", dependencies=[Depends(combined_auth)]) @router.get("/graph/entity/exists", dependencies=[Depends(combined_auth)])
async def check_entity_exists( async def check_entity_exists(
@@ -79,6 +91,8 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
exists = await rag.chunk_entity_relation_graph.has_node(name) exists = await rag.chunk_entity_relation_graph.has_node(name)
return {"exists": exists} return {"exists": exists}
except Exception as e: except Exception as e:
logger.error(f"Error checking entity existence for '{name}': {str(e)}")
logger.error(traceback.format_exc())
raise HTTPException( raise HTTPException(
status_code=500, detail=f"Error checking entity existence: {str(e)}" status_code=500, detail=f"Error checking entity existence: {str(e)}"
) )
@@ -106,8 +120,11 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
"data": result, "data": result,
} }
except ValueError as ve: except ValueError as ve:
logger.error(f"Validation error updating entity '{request.entity_name}': {str(ve)}")
raise HTTPException(status_code=400, detail=str(ve)) raise HTTPException(status_code=400, detail=str(ve))
except Exception as e: except Exception as e:
logger.error(f"Error updating entity '{request.entity_name}': {str(e)}")
logger.error(traceback.format_exc())
raise HTTPException( raise HTTPException(
status_code=500, detail=f"Error updating entity: {str(e)}" status_code=500, detail=f"Error updating entity: {str(e)}"
) )
@@ -134,8 +151,11 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
"data": result, "data": result,
} }
except ValueError as ve: except ValueError as ve:
logger.error(f"Validation error updating relation between '{request.source_id}' and '{request.target_id}': {str(ve)}")
raise HTTPException(status_code=400, detail=str(ve)) raise HTTPException(status_code=400, detail=str(ve))
except Exception as e: except Exception as e:
logger.error(f"Error updating relation between '{request.source_id}' and '{request.target_id}': {str(e)}")
logger.error(traceback.format_exc())
raise HTTPException( raise HTTPException(
status_code=500, detail=f"Error updating relation: {str(e)}" status_code=500, detail=f"Error updating relation: {str(e)}"
) )

View File

@@ -5,7 +5,8 @@ from typing import Any, cast
from .kg.shared_storage import get_graph_db_lock from .kg.shared_storage import get_graph_db_lock
from .prompt import GRAPH_FIELD_SEP from .prompt import GRAPH_FIELD_SEP
from .utils import compute_mdhash_id, logger, StorageNameSpace from .utils import compute_mdhash_id, logger
from .base import StorageNameSpace
async def adelete_by_entity( async def adelete_by_entity(