2025-02-20 03:26:39 +08:00
|
|
|
"""
|
|
|
|
This module contains all graph-related routes for the LightRAG API.
|
|
|
|
"""
|
|
|
|
|
2025-04-12 00:48:19 +08:00
|
|
|
from typing import Optional, Dict, Any
|
|
|
|
from fastapi import APIRouter, Depends, Query, HTTPException
|
|
|
|
from pydantic import BaseModel
|
2025-02-20 03:26:39 +08:00
|
|
|
|
2025-03-24 05:23:40 +08:00
|
|
|
from ..utils_api import get_combined_auth_dependency
|
2025-02-20 03:26:39 +08:00
|
|
|
|
2025-03-24 05:23:40 +08:00
|
|
|
router = APIRouter(tags=["graph"])
|
2025-02-20 03:26:39 +08:00
|
|
|
|
2025-02-20 04:12:21 +08:00
|
|
|
|
2025-04-12 00:48:19 +08:00
|
|
|
class EntityUpdateRequest(BaseModel):
|
|
|
|
entity_name: str
|
|
|
|
updated_data: Dict[str, Any]
|
|
|
|
allow_rename: bool = False
|
|
|
|
|
|
|
|
|
2025-02-20 03:26:39 +08:00
|
|
|
def create_graph_routes(rag, api_key: Optional[str] = None):
|
2025-03-24 05:23:40 +08:00
|
|
|
combined_auth = get_combined_auth_dependency(api_key)
|
2025-02-20 03:26:39 +08:00
|
|
|
|
2025-04-02 18:32:03 +08:00
|
|
|
@router.get("/graph/label/list", dependencies=[Depends(combined_auth)])
|
2025-02-20 03:26:39 +08:00
|
|
|
async def get_graph_labels():
|
2025-03-02 12:52:25 +08:00
|
|
|
"""
|
|
|
|
Get all graph labels
|
|
|
|
|
|
|
|
Returns:
|
2025-04-02 18:32:03 +08:00
|
|
|
List[str]: List of graph labels
|
2025-03-02 12:52:25 +08:00
|
|
|
"""
|
2025-04-02 18:32:03 +08:00
|
|
|
return await rag.get_graph_labels()
|
2025-02-20 03:26:39 +08:00
|
|
|
|
2025-04-02 18:32:03 +08:00
|
|
|
@router.get("/graphs", dependencies=[Depends(combined_auth)])
|
2025-03-05 11:37:55 +08:00
|
|
|
async def get_knowledge_graph(
|
2025-04-02 17:21:45 +08:00
|
|
|
label: str = Query(..., description="Label to get knowledge graph for"),
|
|
|
|
max_depth: int = Query(3, description="Maximum depth of graph", ge=1),
|
2025-04-02 18:32:03 +08:00
|
|
|
max_nodes: int = Query(1000, description="Maximum nodes to return", ge=1),
|
2025-03-05 11:37:55 +08:00
|
|
|
):
|
2025-03-02 12:52:25 +08:00
|
|
|
"""
|
2025-03-02 17:32:25 +08:00
|
|
|
Retrieve a connected subgraph of nodes where the label includes the specified label.
|
|
|
|
When reducing the number of nodes, the prioritization criteria are as follows:
|
2025-04-02 17:21:45 +08:00
|
|
|
1. Hops(path) to the staring node take precedence
|
|
|
|
2. Followed by the degree of the nodes
|
2025-03-02 12:52:25 +08:00
|
|
|
|
|
|
|
Args:
|
2025-04-02 17:21:45 +08:00
|
|
|
label (str): Label of the starting node
|
|
|
|
max_depth (int, optional): Maximum depth of the subgraph,Defaults to 3
|
|
|
|
max_nodes: Maxiumu nodes to return
|
2025-03-02 12:52:25 +08:00
|
|
|
|
|
|
|
Returns:
|
2025-04-02 18:32:03 +08:00
|
|
|
Dict[str, List[str]]: Knowledge graph for label
|
2025-03-02 12:52:25 +08:00
|
|
|
"""
|
2025-04-02 18:32:03 +08:00
|
|
|
return await rag.get_knowledge_graph(
|
2025-03-05 11:37:55 +08:00
|
|
|
node_label=label,
|
|
|
|
max_depth=max_depth,
|
2025-04-02 17:21:45 +08:00
|
|
|
max_nodes=max_nodes,
|
|
|
|
)
|
2025-02-20 03:26:39 +08:00
|
|
|
|
2025-04-12 00:48:19 +08:00
|
|
|
@router.get("/graph/entity/exists", dependencies=[Depends(combined_auth)])
|
|
|
|
async def check_entity_exists(
|
|
|
|
name: str = Query(..., description="Entity name to check"),
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Check if an entity with the given name exists in the knowledge graph
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): Name of the entity to check
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dict[str, bool]: Dictionary with 'exists' key indicating if entity exists
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
exists = await rag.chunk_entity_relation_graph.has_node(name)
|
|
|
|
return {"exists": exists}
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=500, detail=f"Error checking entity existence: {str(e)}"
|
|
|
|
)
|
|
|
|
|
|
|
|
@router.post("/graph/entity/edit", dependencies=[Depends(combined_auth)])
|
|
|
|
async def update_entity(request: EntityUpdateRequest):
|
|
|
|
"""
|
|
|
|
Update an entity's properties in the knowledge graph
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (EntityUpdateRequest): Request containing entity name, updated data, and rename flag
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dict: Updated entity information
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
print(request.entity_name, request.updated_data, request.allow_rename)
|
|
|
|
result = await rag.aedit_entity(
|
|
|
|
entity_name=request.entity_name,
|
|
|
|
updated_data=request.updated_data,
|
|
|
|
allow_rename=request.allow_rename,
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
"status": "success",
|
|
|
|
"message": "Entity updated successfully",
|
|
|
|
"data": result,
|
|
|
|
}
|
|
|
|
except ValueError as ve:
|
|
|
|
raise HTTPException(status_code=400, detail=str(ve))
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=500, detail=f"Error updating entity: {str(e)}"
|
|
|
|
)
|
|
|
|
|
2025-02-20 03:26:39 +08:00
|
|
|
return router
|