|
@@ -5,7 +5,7 @@ from uuid import UUID
|
|
|
|
|
|
from fastapi import Body, Depends, Path, Query
|
|
from fastapi import Body, Depends, Path, Query
|
|
|
|
|
|
-from core.base import KGEnrichmentStatus, R2RException, RunType, Workflow
|
|
|
|
|
|
+from core.base import KGEnrichmentStatus, R2RException, Workflow
|
|
from core.base.abstractions import KGRunType
|
|
from core.base.abstractions import KGRunType
|
|
from core.base.api.models import (
|
|
from core.base.api.models import (
|
|
GenericBooleanResponse,
|
|
GenericBooleanResponse,
|
|
@@ -19,15 +19,12 @@ from core.base.api.models import (
|
|
WrappedRelationshipResponse,
|
|
WrappedRelationshipResponse,
|
|
WrappedRelationshipsResponse,
|
|
WrappedRelationshipsResponse,
|
|
)
|
|
)
|
|
-from core.providers import (
|
|
|
|
- HatchetOrchestrationProvider,
|
|
|
|
- SimpleOrchestrationProvider,
|
|
|
|
-)
|
|
|
|
from core.utils import (
|
|
from core.utils import (
|
|
generate_default_user_collection_id,
|
|
generate_default_user_collection_id,
|
|
update_settings_from_dict,
|
|
update_settings_from_dict,
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+from ...abstractions import R2RProviders, R2RServices
|
|
from .base_router import BaseRouterV3
|
|
from .base_router import BaseRouterV3
|
|
|
|
|
|
logger = logging.getLogger()
|
|
logger = logging.getLogger()
|
|
@@ -36,22 +33,18 @@ logger = logging.getLogger()
|
|
class GraphRouter(BaseRouterV3):
|
|
class GraphRouter(BaseRouterV3):
|
|
def __init__(
|
|
def __init__(
|
|
self,
|
|
self,
|
|
- providers,
|
|
|
|
- services,
|
|
|
|
- orchestration_provider: (
|
|
|
|
- HatchetOrchestrationProvider | SimpleOrchestrationProvider
|
|
|
|
- ),
|
|
|
|
- run_type: RunType = RunType.KG,
|
|
|
|
|
|
+ providers: R2RProviders,
|
|
|
|
+ services: R2RServices,
|
|
):
|
|
):
|
|
- super().__init__(providers, services, orchestration_provider, run_type)
|
|
|
|
|
|
+ super().__init__(providers, services)
|
|
self._register_workflows()
|
|
self._register_workflows()
|
|
|
|
|
|
def _register_workflows(self):
|
|
def _register_workflows(self):
|
|
|
|
|
|
workflow_messages = {}
|
|
workflow_messages = {}
|
|
- if self.orchestration_provider.config.provider == "hatchet":
|
|
|
|
|
|
+ if self.providers.orchestration.config.provider == "hatchet":
|
|
workflow_messages["extract-triples"] = (
|
|
workflow_messages["extract-triples"] = (
|
|
- "Graph creation task queued successfully."
|
|
|
|
|
|
+ "Document extraction task queued successfully."
|
|
)
|
|
)
|
|
workflow_messages["build-communities"] = (
|
|
workflow_messages["build-communities"] = (
|
|
"Graph enrichment task queued successfully."
|
|
"Graph enrichment task queued successfully."
|
|
@@ -61,18 +54,18 @@ class GraphRouter(BaseRouterV3):
|
|
)
|
|
)
|
|
else:
|
|
else:
|
|
workflow_messages["extract-triples"] = (
|
|
workflow_messages["extract-triples"] = (
|
|
- "Document entities and relationships extracted successfully. To generate GraphRAG communities, POST to `/graphs/<collection_id>/communities/build` with a collection this document belongs to."
|
|
|
|
|
|
+ "Document entities and relationships extracted successfully."
|
|
)
|
|
)
|
|
workflow_messages["build-communities"] = (
|
|
workflow_messages["build-communities"] = (
|
|
- "Graph communities created successfully. You can view the communities at http://localhost:7272/v2/communities"
|
|
|
|
|
|
+ "Graph communities created successfully."
|
|
)
|
|
)
|
|
workflow_messages["entity-deduplication"] = (
|
|
workflow_messages["entity-deduplication"] = (
|
|
"KG Entity Deduplication completed successfully."
|
|
"KG Entity Deduplication completed successfully."
|
|
)
|
|
)
|
|
|
|
|
|
- self.orchestration_provider.register_workflows(
|
|
|
|
|
|
+ self.providers.orchestration.register_workflows(
|
|
Workflow.KG,
|
|
Workflow.KG,
|
|
- self.services["kg"],
|
|
|
|
|
|
+ self.services.graph,
|
|
workflow_messages,
|
|
workflow_messages,
|
|
)
|
|
)
|
|
|
|
|
|
@@ -126,7 +119,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
# Return cost estimate if requested
|
|
# Return cost estimate if requested
|
|
if run_type == KGRunType.ESTIMATE:
|
|
if run_type == KGRunType.ESTIMATE:
|
|
- return await self.services["kg"].get_deduplication_estimate(
|
|
|
|
|
|
+ return await self.services.graph.get_deduplication_estimate(
|
|
collection_id, server_settings
|
|
collection_id, server_settings
|
|
)
|
|
)
|
|
|
|
|
|
@@ -137,13 +130,13 @@ class GraphRouter(BaseRouterV3):
|
|
}
|
|
}
|
|
|
|
|
|
if run_with_orchestration:
|
|
if run_with_orchestration:
|
|
- return await self.orchestration_provider.run_workflow( # type: ignore
|
|
|
|
|
|
+ return await self.providers.orchestration.run_workflow( # type: ignore
|
|
"entity-deduplication", {"request": workflow_input}, {}
|
|
"entity-deduplication", {"request": workflow_input}, {}
|
|
)
|
|
)
|
|
else:
|
|
else:
|
|
from core.main.orchestration import simple_kg_factory
|
|
from core.main.orchestration import simple_kg_factory
|
|
|
|
|
|
- simple_kg = simple_kg_factory(self.services["kg"])
|
|
|
|
|
|
+ simple_kg = simple_kg_factory(self.services.graph)
|
|
await simple_kg["entity-deduplication"](workflow_input)
|
|
await simple_kg["entity-deduplication"](workflow_input)
|
|
return { # type: ignore
|
|
return { # type: ignore
|
|
"message": "Entity deduplication completed successfully.",
|
|
"message": "Entity deduplication completed successfully.",
|
|
@@ -161,6 +154,7 @@ class GraphRouter(BaseRouterV3):
|
|
def _setup_routes(self):
|
|
def _setup_routes(self):
|
|
@self.router.get(
|
|
@self.router.get(
|
|
"/graphs",
|
|
"/graphs",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
summary="List graphs",
|
|
summary="List graphs",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -213,7 +207,7 @@ class GraphRouter(BaseRouterV3):
|
|
le=1000,
|
|
le=1000,
|
|
description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
|
|
description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedGraphsResponse:
|
|
) -> WrappedGraphsResponse:
|
|
"""
|
|
"""
|
|
Returns a paginated list of graphs the authenticated user has access to.
|
|
Returns a paginated list of graphs the authenticated user has access to.
|
|
@@ -229,7 +223,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
graph_uuids = [UUID(graph_id) for graph_id in collection_ids]
|
|
graph_uuids = [UUID(graph_id) for graph_id in collection_ids]
|
|
|
|
|
|
- list_graphs_response = await self.services["kg"].list_graphs(
|
|
|
|
|
|
+ list_graphs_response = await self.services.graph.list_graphs(
|
|
# user_ids=requesting_user_id,
|
|
# user_ids=requesting_user_id,
|
|
graph_ids=graph_uuids,
|
|
graph_ids=graph_uuids,
|
|
offset=offset,
|
|
offset=offset,
|
|
@@ -243,6 +237,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.get(
|
|
@self.router.get(
|
|
"/graphs/{collection_id}",
|
|
"/graphs/{collection_id}",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
summary="Retrieve graph details",
|
|
summary="Retrieve graph details",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -292,7 +287,7 @@ class GraphRouter(BaseRouterV3):
|
|
@self.base_endpoint
|
|
@self.base_endpoint
|
|
async def get_graph(
|
|
async def get_graph(
|
|
collection_id: UUID = Path(...),
|
|
collection_id: UUID = Path(...),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedGraphResponse:
|
|
) -> WrappedGraphResponse:
|
|
"""
|
|
"""
|
|
Retrieves detailed information about a specific graph by ID.
|
|
Retrieves detailed information about a specific graph by ID.
|
|
@@ -307,7 +302,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- list_graphs_response = await self.services["kg"].list_graphs(
|
|
|
|
|
|
+ list_graphs_response = await self.services.graph.list_graphs(
|
|
# user_ids=None,
|
|
# user_ids=None,
|
|
graph_ids=[collection_id],
|
|
graph_ids=[collection_id],
|
|
offset=0,
|
|
offset=0,
|
|
@@ -317,6 +312,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.post(
|
|
@self.router.post(
|
|
"/graphs/{collection_id}/communities/build",
|
|
"/graphs/{collection_id}/communities/build",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
)
|
|
)
|
|
@self.base_endpoint
|
|
@self.base_endpoint
|
|
async def build_communities(
|
|
async def build_communities(
|
|
@@ -332,7 +328,7 @@ class GraphRouter(BaseRouterV3):
|
|
description="Settings for the graph enrichment process.",
|
|
description="Settings for the graph enrichment process.",
|
|
),
|
|
),
|
|
run_with_orchestration: Optional[bool] = Body(True),
|
|
run_with_orchestration: Optional[bool] = Body(True),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
):
|
|
):
|
|
"""
|
|
"""
|
|
Creates communities in the graph by analyzing entity relationships and similarities.
|
|
Creates communities in the graph by analyzing entity relationships and similarities.
|
|
@@ -391,14 +387,14 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
if run_with_orchestration:
|
|
if run_with_orchestration:
|
|
|
|
|
|
- return await self.orchestration_provider.run_workflow( # type: ignore
|
|
|
|
|
|
+ return await self.providers.orchestration.run_workflow( # type: ignore
|
|
"build-communities", {"request": workflow_input}, {}
|
|
"build-communities", {"request": workflow_input}, {}
|
|
)
|
|
)
|
|
else:
|
|
else:
|
|
from core.main.orchestration import simple_kg_factory
|
|
from core.main.orchestration import simple_kg_factory
|
|
|
|
|
|
logger.info("Running build-communities without orchestration.")
|
|
logger.info("Running build-communities without orchestration.")
|
|
- simple_kg = simple_kg_factory(self.services["kg"])
|
|
|
|
|
|
+ simple_kg = simple_kg_factory(self.services.graph)
|
|
await simple_kg["build-communities"](workflow_input)
|
|
await simple_kg["build-communities"](workflow_input)
|
|
return {
|
|
return {
|
|
"message": "Graph communities created successfully.",
|
|
"message": "Graph communities created successfully.",
|
|
@@ -407,6 +403,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.post(
|
|
@self.router.post(
|
|
"/graphs/{collection_id}/reset",
|
|
"/graphs/{collection_id}/reset",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
summary="Reset a graph back to the initial state.",
|
|
summary="Reset a graph back to the initial state.",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -456,7 +453,7 @@ class GraphRouter(BaseRouterV3):
|
|
@self.base_endpoint
|
|
@self.base_endpoint
|
|
async def reset(
|
|
async def reset(
|
|
collection_id: UUID = Path(...),
|
|
collection_id: UUID = Path(...),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedBooleanResponse:
|
|
) -> WrappedBooleanResponse:
|
|
"""
|
|
"""
|
|
Deletes a graph and all its associated data.
|
|
Deletes a graph and all its associated data.
|
|
@@ -479,13 +476,14 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- await self.services["kg"].reset_graph_v3(id=collection_id)
|
|
|
|
|
|
+ await self.services.graph.reset_graph_v3(id=collection_id)
|
|
# await _pull(collection_id, auth_user)
|
|
# await _pull(collection_id, auth_user)
|
|
return GenericBooleanResponse(success=True) # type: ignore
|
|
return GenericBooleanResponse(success=True) # type: ignore
|
|
|
|
|
|
# update graph
|
|
# update graph
|
|
@self.router.post(
|
|
@self.router.post(
|
|
"/graphs/{collection_id}",
|
|
"/graphs/{collection_id}",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
summary="Update graph",
|
|
summary="Update graph",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -542,7 +540,7 @@ class GraphRouter(BaseRouterV3):
|
|
description: Optional[str] = Body(
|
|
description: Optional[str] = Body(
|
|
None, description="An optional description of the graph"
|
|
None, description="An optional description of the graph"
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
):
|
|
):
|
|
"""
|
|
"""
|
|
Update an existing graphs's configuration.
|
|
Update an existing graphs's configuration.
|
|
@@ -564,7 +562,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- return await self.services["kg"].update_graph( # type: ignore
|
|
|
|
|
|
+ return await self.services.graph.update_graph( # type: ignore
|
|
collection_id,
|
|
collection_id,
|
|
name=name,
|
|
name=name,
|
|
description=description,
|
|
description=description,
|
|
@@ -572,6 +570,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.get(
|
|
@self.router.get(
|
|
"/graphs/{collection_id}/entities",
|
|
"/graphs/{collection_id}/entities",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
{
|
|
{
|
|
@@ -625,7 +624,7 @@ class GraphRouter(BaseRouterV3):
|
|
le=1000,
|
|
le=1000,
|
|
description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
|
|
description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedEntitiesResponse:
|
|
) -> WrappedEntitiesResponse:
|
|
"""Lists all entities in the graph with pagination support."""
|
|
"""Lists all entities in the graph with pagination support."""
|
|
if (
|
|
if (
|
|
@@ -638,7 +637,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- entities, count = await self.services["kg"].get_entities(
|
|
|
|
|
|
+ entities, count = await self.services.graph.get_entities(
|
|
parent_id=collection_id,
|
|
parent_id=collection_id,
|
|
offset=offset,
|
|
offset=offset,
|
|
limit=limit,
|
|
limit=limit,
|
|
@@ -648,7 +647,10 @@ class GraphRouter(BaseRouterV3):
|
|
"total_entries": count,
|
|
"total_entries": count,
|
|
}
|
|
}
|
|
|
|
|
|
- @self.router.post("/graphs/{collection_id}/entities")
|
|
|
|
|
|
+ @self.router.post(
|
|
|
|
+ "/graphs/{collection_id}/entities",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
|
|
+ )
|
|
@self.base_endpoint
|
|
@self.base_endpoint
|
|
async def create_entity(
|
|
async def create_entity(
|
|
collection_id: UUID = Path(
|
|
collection_id: UUID = Path(
|
|
@@ -667,7 +669,7 @@ class GraphRouter(BaseRouterV3):
|
|
metadata: Optional[dict] = Body(
|
|
metadata: Optional[dict] = Body(
|
|
None, description="The metadata of the entity to create."
|
|
None, description="The metadata of the entity to create."
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedEntityResponse:
|
|
) -> WrappedEntityResponse:
|
|
"""Creates a new entity in the graph."""
|
|
"""Creates a new entity in the graph."""
|
|
if (
|
|
if (
|
|
@@ -680,7 +682,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- return await self.services["kg"].create_entity(
|
|
|
|
|
|
+ return await self.services.graph.create_entity(
|
|
name=name,
|
|
name=name,
|
|
description=description,
|
|
description=description,
|
|
parent_id=collection_id,
|
|
parent_id=collection_id,
|
|
@@ -688,7 +690,10 @@ class GraphRouter(BaseRouterV3):
|
|
metadata=metadata,
|
|
metadata=metadata,
|
|
)
|
|
)
|
|
|
|
|
|
- @self.router.post("/graphs/{collection_id}/relationships")
|
|
|
|
|
|
+ @self.router.post(
|
|
|
|
+ "/graphs/{collection_id}/relationships",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
|
|
+ )
|
|
@self.base_endpoint
|
|
@self.base_endpoint
|
|
async def create_relationship(
|
|
async def create_relationship(
|
|
collection_id: UUID = Path(
|
|
collection_id: UUID = Path(
|
|
@@ -722,7 +727,7 @@ class GraphRouter(BaseRouterV3):
|
|
metadata: Optional[dict] = Body(
|
|
metadata: Optional[dict] = Body(
|
|
None, description="The metadata of the relationship to create."
|
|
None, description="The metadata of the relationship to create."
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedRelationshipResponse:
|
|
) -> WrappedRelationshipResponse:
|
|
"""Creates a new relationship in the graph."""
|
|
"""Creates a new relationship in the graph."""
|
|
if not auth_user.is_superuser:
|
|
if not auth_user.is_superuser:
|
|
@@ -739,7 +744,7 @@ class GraphRouter(BaseRouterV3):
|
|
"The currently authenticated user does not have access to the collection associated with the given graph.",
|
|
"The currently authenticated user does not have access to the collection associated with the given graph.",
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
- return await self.services["kg"].create_relationship(
|
|
|
|
|
|
+ return await self.services.graph.create_relationship(
|
|
subject=subject,
|
|
subject=subject,
|
|
subject_id=subject_id,
|
|
subject_id=subject_id,
|
|
predicate=predicate,
|
|
predicate=predicate,
|
|
@@ -753,6 +758,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.get(
|
|
@self.router.get(
|
|
"/graphs/{collection_id}/entities/{entity_id}",
|
|
"/graphs/{collection_id}/entities/{entity_id}",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
{
|
|
{
|
|
@@ -802,7 +808,7 @@ class GraphRouter(BaseRouterV3):
|
|
entity_id: UUID = Path(
|
|
entity_id: UUID = Path(
|
|
..., description="The ID of the entity to retrieve."
|
|
..., description="The ID of the entity to retrieve."
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedEntityResponse:
|
|
) -> WrappedEntityResponse:
|
|
"""Retrieves a specific entity by its ID."""
|
|
"""Retrieves a specific entity by its ID."""
|
|
if (
|
|
if (
|
|
@@ -826,7 +832,10 @@ class GraphRouter(BaseRouterV3):
|
|
raise R2RException("Entity not found", 404)
|
|
raise R2RException("Entity not found", 404)
|
|
return result[0][0]
|
|
return result[0][0]
|
|
|
|
|
|
- @self.router.post("/graphs/{collection_id}/entities/{entity_id}")
|
|
|
|
|
|
+ @self.router.post(
|
|
|
|
+ "/graphs/{collection_id}/entities/{entity_id}",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
|
|
+ )
|
|
@self.base_endpoint
|
|
@self.base_endpoint
|
|
async def update_entity(
|
|
async def update_entity(
|
|
collection_id: UUID = Path(
|
|
collection_id: UUID = Path(
|
|
@@ -848,7 +857,7 @@ class GraphRouter(BaseRouterV3):
|
|
metadata: Optional[dict] = Body(
|
|
metadata: Optional[dict] = Body(
|
|
None, description="The updated metadata of the entity."
|
|
None, description="The updated metadata of the entity."
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedEntityResponse:
|
|
) -> WrappedEntityResponse:
|
|
"""Updates an existing entity in the graph."""
|
|
"""Updates an existing entity in the graph."""
|
|
if not auth_user.is_superuser:
|
|
if not auth_user.is_superuser:
|
|
@@ -865,7 +874,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- return await self.services["kg"].update_entity(
|
|
|
|
|
|
+ return await self.services.graph.update_entity(
|
|
entity_id=entity_id,
|
|
entity_id=entity_id,
|
|
name=name,
|
|
name=name,
|
|
category=category,
|
|
category=category,
|
|
@@ -875,6 +884,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.delete(
|
|
@self.router.delete(
|
|
"/graphs/{collection_id}/entities/{entity_id}",
|
|
"/graphs/{collection_id}/entities/{entity_id}",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
summary="Remove an entity",
|
|
summary="Remove an entity",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -926,7 +936,7 @@ class GraphRouter(BaseRouterV3):
|
|
...,
|
|
...,
|
|
description="The ID of the entity to remove from the graph.",
|
|
description="The ID of the entity to remove from the graph.",
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedBooleanResponse:
|
|
) -> WrappedBooleanResponse:
|
|
"""Removes an entity from the graph."""
|
|
"""Removes an entity from the graph."""
|
|
if not auth_user.is_superuser:
|
|
if not auth_user.is_superuser:
|
|
@@ -944,7 +954,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- await self.services["kg"].delete_entity(
|
|
|
|
|
|
+ await self.services.graph.delete_entity(
|
|
parent_id=collection_id,
|
|
parent_id=collection_id,
|
|
entity_id=entity_id,
|
|
entity_id=entity_id,
|
|
)
|
|
)
|
|
@@ -953,6 +963,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.get(
|
|
@self.router.get(
|
|
"/graphs/{collection_id}/relationships",
|
|
"/graphs/{collection_id}/relationships",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
description="Lists all relationships in the graph with pagination support.",
|
|
description="Lists all relationships in the graph with pagination support.",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -1007,7 +1018,7 @@ class GraphRouter(BaseRouterV3):
|
|
le=1000,
|
|
le=1000,
|
|
description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
|
|
description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedRelationshipsResponse:
|
|
) -> WrappedRelationshipsResponse:
|
|
"""
|
|
"""
|
|
Lists all relationships in the graph with pagination support.
|
|
Lists all relationships in the graph with pagination support.
|
|
@@ -1022,7 +1033,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- relationships, count = await self.services["kg"].get_relationships(
|
|
|
|
|
|
+ relationships, count = await self.services.graph.get_relationships(
|
|
parent_id=collection_id,
|
|
parent_id=collection_id,
|
|
offset=offset,
|
|
offset=offset,
|
|
limit=limit,
|
|
limit=limit,
|
|
@@ -1034,6 +1045,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.get(
|
|
@self.router.get(
|
|
"/graphs/{collection_id}/relationships/{relationship_id}",
|
|
"/graphs/{collection_id}/relationships/{relationship_id}",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
description="Retrieves a specific relationship by its ID.",
|
|
description="Retrieves a specific relationship by its ID.",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -1084,7 +1096,7 @@ class GraphRouter(BaseRouterV3):
|
|
relationship_id: UUID = Path(
|
|
relationship_id: UUID = Path(
|
|
..., description="The ID of the relationship to retrieve."
|
|
..., description="The ID of the relationship to retrieve."
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedRelationshipResponse:
|
|
) -> WrappedRelationshipResponse:
|
|
"""Retrieves a specific relationship by its ID."""
|
|
"""Retrieves a specific relationship by its ID."""
|
|
if (
|
|
if (
|
|
@@ -1111,7 +1123,8 @@ class GraphRouter(BaseRouterV3):
|
|
return results[0][0]
|
|
return results[0][0]
|
|
|
|
|
|
@self.router.post(
|
|
@self.router.post(
|
|
- "/graphs/{collection_id}/relationships/{relationship_id}"
|
|
|
|
|
|
+ "/graphs/{collection_id}/relationships/{relationship_id}",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
)
|
|
)
|
|
@self.base_endpoint
|
|
@self.base_endpoint
|
|
async def update_relationship(
|
|
async def update_relationship(
|
|
@@ -1147,7 +1160,7 @@ class GraphRouter(BaseRouterV3):
|
|
metadata: Optional[dict] = Body(
|
|
metadata: Optional[dict] = Body(
|
|
None, description="The updated metadata of the relationship."
|
|
None, description="The updated metadata of the relationship."
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedRelationshipResponse:
|
|
) -> WrappedRelationshipResponse:
|
|
"""Updates an existing relationship in the graph."""
|
|
"""Updates an existing relationship in the graph."""
|
|
if not auth_user.is_superuser:
|
|
if not auth_user.is_superuser:
|
|
@@ -1165,7 +1178,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- return await self.services["kg"].update_relationship(
|
|
|
|
|
|
+ return await self.services.graph.update_relationship(
|
|
relationship_id=relationship_id,
|
|
relationship_id=relationship_id,
|
|
subject=subject,
|
|
subject=subject,
|
|
subject_id=subject_id,
|
|
subject_id=subject_id,
|
|
@@ -1179,6 +1192,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.delete(
|
|
@self.router.delete(
|
|
"/graphs/{collection_id}/relationships/{relationship_id}",
|
|
"/graphs/{collection_id}/relationships/{relationship_id}",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
description="Removes a relationship from the graph.",
|
|
description="Removes a relationship from the graph.",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -1230,7 +1244,7 @@ class GraphRouter(BaseRouterV3):
|
|
...,
|
|
...,
|
|
description="The ID of the relationship to remove from the graph.",
|
|
description="The ID of the relationship to remove from the graph.",
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedBooleanResponse:
|
|
) -> WrappedBooleanResponse:
|
|
"""Removes a relationship from the graph."""
|
|
"""Removes a relationship from the graph."""
|
|
if not auth_user.is_superuser:
|
|
if not auth_user.is_superuser:
|
|
@@ -1247,7 +1261,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- await self.services["kg"].delete_relationship(
|
|
|
|
|
|
+ await self.services.graph.delete_relationship(
|
|
parent_id=collection_id,
|
|
parent_id=collection_id,
|
|
relationship_id=relationship_id,
|
|
relationship_id=relationship_id,
|
|
)
|
|
)
|
|
@@ -1256,6 +1270,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.post(
|
|
@self.router.post(
|
|
"/graphs/{collection_id}/communities",
|
|
"/graphs/{collection_id}/communities",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
summary="Create a new community",
|
|
summary="Create a new community",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -1322,7 +1337,7 @@ class GraphRouter(BaseRouterV3):
|
|
rating_explanation: Optional[str] = Body(
|
|
rating_explanation: Optional[str] = Body(
|
|
default="", description="Explanation for the rating"
|
|
default="", description="Explanation for the rating"
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedCommunityResponse:
|
|
) -> WrappedCommunityResponse:
|
|
"""
|
|
"""
|
|
Creates a new community in the graph.
|
|
Creates a new community in the graph.
|
|
@@ -1353,7 +1368,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- return await self.services["kg"].create_community(
|
|
|
|
|
|
+ return await self.services.graph.create_community(
|
|
parent_id=collection_id,
|
|
parent_id=collection_id,
|
|
name=name,
|
|
name=name,
|
|
summary=summary,
|
|
summary=summary,
|
|
@@ -1364,6 +1379,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.get(
|
|
@self.router.get(
|
|
"/graphs/{collection_id}/communities",
|
|
"/graphs/{collection_id}/communities",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
summary="List communities",
|
|
summary="List communities",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -1418,7 +1434,7 @@ class GraphRouter(BaseRouterV3):
|
|
le=1000,
|
|
le=1000,
|
|
description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
|
|
description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedCommunitiesResponse:
|
|
) -> WrappedCommunitiesResponse:
|
|
"""
|
|
"""
|
|
Lists all communities in the graph with pagination support.
|
|
Lists all communities in the graph with pagination support.
|
|
@@ -1433,7 +1449,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- communities, count = await self.services["kg"].get_communities(
|
|
|
|
|
|
+ communities, count = await self.services.graph.get_communities(
|
|
parent_id=collection_id,
|
|
parent_id=collection_id,
|
|
offset=offset,
|
|
offset=offset,
|
|
limit=limit,
|
|
limit=limit,
|
|
@@ -1445,6 +1461,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.get(
|
|
@self.router.get(
|
|
"/graphs/{collection_id}/communities/{community_id}",
|
|
"/graphs/{collection_id}/communities/{community_id}",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
summary="Retrieve a community",
|
|
summary="Retrieve a community",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -1492,7 +1509,7 @@ class GraphRouter(BaseRouterV3):
|
|
...,
|
|
...,
|
|
description="The ID of the community to get.",
|
|
description="The ID of the community to get.",
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedCommunityResponse:
|
|
) -> WrappedCommunityResponse:
|
|
"""
|
|
"""
|
|
Retrieves a specific community by its ID.
|
|
Retrieves a specific community by its ID.
|
|
@@ -1507,14 +1524,14 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- results = await self.services[
|
|
|
|
- "kg"
|
|
|
|
- ].providers.database.graphs_handler.communities.get(
|
|
|
|
- parent_id=collection_id,
|
|
|
|
- community_ids=[community_id],
|
|
|
|
- store_type="graphs",
|
|
|
|
- offset=0,
|
|
|
|
- limit=1,
|
|
|
|
|
|
+ results = (
|
|
|
|
+ await self.providers.database.graphs_handler.communities.get(
|
|
|
|
+ parent_id=collection_id,
|
|
|
|
+ community_ids=[community_id],
|
|
|
|
+ store_type="graphs",
|
|
|
|
+ offset=0,
|
|
|
|
+ limit=1,
|
|
|
|
+ )
|
|
)
|
|
)
|
|
if len(results) == 0 or len(results[0]) == 0:
|
|
if len(results) == 0 or len(results[0]) == 0:
|
|
raise R2RException("Community not found", 404)
|
|
raise R2RException("Community not found", 404)
|
|
@@ -1522,6 +1539,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.delete(
|
|
@self.router.delete(
|
|
"/graphs/{collection_id}/communities/{community_id}",
|
|
"/graphs/{collection_id}/communities/{community_id}",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
summary="Delete a community",
|
|
summary="Delete a community",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -1573,7 +1591,7 @@ class GraphRouter(BaseRouterV3):
|
|
...,
|
|
...,
|
|
description="The ID of the community to delete.",
|
|
description="The ID of the community to delete.",
|
|
),
|
|
),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
):
|
|
):
|
|
if (
|
|
if (
|
|
not auth_user.is_superuser
|
|
not auth_user.is_superuser
|
|
@@ -1593,7 +1611,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- await self.services["kg"].delete_community(
|
|
|
|
|
|
+ await self.services.graph.delete_community(
|
|
parent_id=collection_id,
|
|
parent_id=collection_id,
|
|
community_id=community_id,
|
|
community_id=community_id,
|
|
)
|
|
)
|
|
@@ -1601,6 +1619,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.post(
|
|
@self.router.post(
|
|
"/graphs/{collection_id}/communities/{community_id}",
|
|
"/graphs/{collection_id}/communities/{community_id}",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
summary="Update community",
|
|
summary="Update community",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -1661,7 +1680,7 @@ class GraphRouter(BaseRouterV3):
|
|
findings: Optional[list[str]] = Body(None),
|
|
findings: Optional[list[str]] = Body(None),
|
|
rating: Optional[float] = Body(default=None, ge=1, le=10),
|
|
rating: Optional[float] = Body(default=None, ge=1, le=10),
|
|
rating_explanation: Optional[str] = Body(None),
|
|
rating_explanation: Optional[str] = Body(None),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedCommunityResponse:
|
|
) -> WrappedCommunityResponse:
|
|
"""
|
|
"""
|
|
Updates an existing community in the graph.
|
|
Updates an existing community in the graph.
|
|
@@ -1684,7 +1703,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- return await self.services["kg"].update_community(
|
|
|
|
|
|
+ return await self.services.graph.update_community(
|
|
community_id=community_id,
|
|
community_id=community_id,
|
|
name=name,
|
|
name=name,
|
|
summary=summary,
|
|
summary=summary,
|
|
@@ -1695,6 +1714,7 @@ class GraphRouter(BaseRouterV3):
|
|
|
|
|
|
@self.router.post(
|
|
@self.router.post(
|
|
"/graphs/{collection_id}/pull",
|
|
"/graphs/{collection_id}/pull",
|
|
|
|
+ dependencies=[Depends(self.rate_limit_dependency)],
|
|
summary="Pull latest entities to the graph",
|
|
summary="Pull latest entities to the graph",
|
|
openapi_extra={
|
|
openapi_extra={
|
|
"x-codeSamples": [
|
|
"x-codeSamples": [
|
|
@@ -1745,7 +1765,7 @@ class GraphRouter(BaseRouterV3):
|
|
# document_ids: list[UUID] = Body(
|
|
# document_ids: list[UUID] = Body(
|
|
# ..., description="List of document IDs to add to the graph."
|
|
# ..., description="List of document IDs to add to the graph."
|
|
# ),
|
|
# ),
|
|
- auth_user=Depends(self.providers.auth.auth_wrapper),
|
|
|
|
|
|
+ auth_user=Depends(self.providers.auth.auth_wrapper()),
|
|
) -> WrappedBooleanResponse:
|
|
) -> WrappedBooleanResponse:
|
|
"""
|
|
"""
|
|
Adds documents to a graph by copying their entities and relationships.
|
|
Adds documents to a graph by copying their entities and relationships.
|
|
@@ -1781,7 +1801,7 @@ class GraphRouter(BaseRouterV3):
|
|
403,
|
|
403,
|
|
)
|
|
)
|
|
|
|
|
|
- list_graphs_response = await self.services["kg"].list_graphs(
|
|
|
|
|
|
+ list_graphs_response = await self.services.graph.list_graphs(
|
|
# user_ids=None,
|
|
# user_ids=None,
|
|
graph_ids=[collection_id],
|
|
graph_ids=[collection_id],
|
|
offset=0,
|
|
offset=0,
|