2025-02-20 03:26:39 +08:00
|
|
|
"""
|
|
|
|
This module contains all graph-related routes for the LightRAG API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from typing import Optional
|
2025-02-20 04:12:21 +08:00
|
|
|
from fastapi import APIRouter, Depends
|
2025-02-20 03:26:39 +08:00
|
|
|
|
2025-03-05 10:39:27 +08:00
|
|
|
from ...utils import logger
|
2025-02-20 03:26:39 +08:00
|
|
|
from ..utils_api import get_api_key_dependency
|
|
|
|
|
|
|
|
router = APIRouter(tags=["graph"])
|
|
|
|
|
2025-02-20 04:12:21 +08:00
|
|
|
|
2025-02-20 03:26:39 +08:00
|
|
|
def create_graph_routes(rag, api_key: Optional[str] = None):
|
|
|
|
optional_api_key = get_api_key_dependency(api_key)
|
|
|
|
|
|
|
|
@router.get("/graph/label/list", dependencies=[Depends(optional_api_key)])
|
|
|
|
async def get_graph_labels():
|
2025-03-02 12:52:25 +08:00
|
|
|
"""
|
|
|
|
Get all graph labels
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List[str]: List of graph labels
|
|
|
|
"""
|
2025-02-20 03:26:39 +08:00
|
|
|
return await rag.get_graph_labels()
|
|
|
|
|
|
|
|
@router.get("/graphs", dependencies=[Depends(optional_api_key)])
|
2025-03-05 10:39:27 +08:00
|
|
|
async def get_knowledge_graph(label: str, max_depth: int = 3, inclusive: bool = False, min_degree: int = 0):
|
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.
|
|
|
|
Maximum number of nodes is constrained by the environment variable `MAX_GRAPH_NODES` (default: 1000).
|
|
|
|
When reducing the number of nodes, the prioritization criteria are as follows:
|
|
|
|
1. Label matching nodes take precedence
|
|
|
|
2. Followed by nodes directly connected to the matching nodes
|
|
|
|
3. Finally, the degree of the nodes
|
2025-03-02 12:52:25 +08:00
|
|
|
Maximum number of nodes is limited to env MAX_GRAPH_NODES(default: 1000)
|
|
|
|
|
|
|
|
Args:
|
|
|
|
label (str): Label to get knowledge graph for
|
|
|
|
max_depth (int, optional): Maximum depth of graph. Defaults to 3.
|
2025-03-05 10:39:27 +08:00
|
|
|
inclusive_search (bool, optional): If True, search for nodes that include the label. Defaults to False.
|
|
|
|
min_degree (int, optional): Minimum degree of nodes. Defaults to 0.
|
2025-03-02 12:52:25 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dict[str, List[str]]: Knowledge graph for label
|
|
|
|
"""
|
2025-03-05 10:39:27 +08:00
|
|
|
logger.info(f"Inclusive search : {inclusive}, Min degree: {min_degree}, Label: {label}")
|
|
|
|
return await rag.get_knowledge_graph(node_label=label, max_depth=max_depth, inclusive=inclusive, min_degree=min_degree)
|
2025-02-20 03:26:39 +08:00
|
|
|
|
|
|
|
return router
|