Skip to content

Commit

Permalink
fixup crud
Browse files Browse the repository at this point in the history
  • Loading branch information
emrgnt-cmplxty committed Dec 1, 2024
1 parent 5ad3212 commit 8e36251
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion py/core/main/api/v2/kg_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def _register_workflows(self):
)
else:
workflow_messages["extract-triples"] = (
"Document entities and relationships extracted successfully. To generate GraphRAG communities, run cluster on the collection this document belongs to."
"Document entities and relationships extracted successfully. To generate GraphRAG communities, POST to `/graphs/<collection_id>/communities/build` with a collection this document belongs to."
)
workflow_messages["build-communities"] = (
"Graph communities created successfully. You can view the communities at http://localhost:7272/v2/communities"
Expand Down
3 changes: 2 additions & 1 deletion py/core/main/api/v3/documents_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
WrappedDocumentResponse,
WrappedDocumentsResponse,
WrappedEntitiesResponse,
WrappedGenericMessageResponse,
WrappedIngestionResponse,
WrappedRelationshipsResponse,
)
Expand Down Expand Up @@ -1259,7 +1260,7 @@ async def extract(
description="Whether to run the entities and relationships extraction process with orchestration.",
),
auth_user=Depends(self.providers.auth.auth_wrapper),
) -> WrappedIngestionResponse:
) -> WrappedGenericMessageResponse:
"""
Extracts entities and relationships from a document.
The entities and relationships extraction process involves:
Expand Down
4 changes: 2 additions & 2 deletions py/core/main/api/v3/graph_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ async def delete_entity(
403,
)

self.services["kg"].delete_entity(
await self.services["kg"].delete_entity(
parent_id=collection_id,
entity_id=entity_id,
)
Expand Down Expand Up @@ -1443,7 +1443,7 @@ async def get_community(
)
print(f"results: {results}")
if len(results) == 0 or len(results[0]) == 0:
raise R2RException("Relationship not found", 404)
raise R2RException("Community not found", 404)
return results[0][0]

@self.router.delete(
Expand Down
8 changes: 8 additions & 0 deletions py/core/pipes/retrieval/kg_search_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ async def search(
# "document_ids",
],
):
try:
# TODO - remove this nasty hack
search_result["metadata"] = json.loads(
search_result["metadata"]
)
except:
pass

yield GraphSearchResult(
content=KGRelationshipResult(
# name=search_result["name"],
Expand Down
40 changes: 37 additions & 3 deletions py/core/providers/database/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ async def delete(
WHERE id = ANY($1) AND parent_id = $2
RETURNING id
"""
print("QUERY = ", QUERY)
print("entity_ids = ", entity_ids)
print("parent_id = ", parent_id)
results = await self.connection_manager.fetch_query(
QUERY, [entity_ids, parent_id]
)
Expand Down Expand Up @@ -631,6 +634,8 @@ async def get(
)
except json.JSONDecodeError:
pass
elif not include_metadata:
relationship_dict.pop("metadata", None)
relationships.append(Relationship(**relationship_dict))

return relationships, count
Expand Down Expand Up @@ -992,13 +997,38 @@ async def delete(

query = f"""
DELETE FROM {self._get_table_name(table_name)}
WHERE id = $1 AND graph_id = $2
WHERE id = $1 AND collection_id = $2
"""
print("query = ", query)
print("parent_id = ", parent_id)
print("community_id = ", community_id)
params = [community_id, parent_id]

try:
results = await self.connection_manager.execute_query(
query, params
)
print("results = ", results)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"An error occurred while deleting the community: {e}",
)
table_name = "graph_community_info"
query = f"""
DELETE FROM {self._get_table_name(table_name)}
WHERE id = $1 AND collection_id = $2
"""
print("query = ", query)
print("parent_id = ", parent_id)
print("community_id = ", community_id)
params = [community_id, parent_id]

try:
await self.connection_manager.execute_query(query, params)
results = await self.connection_manager.execute_query(
query, params
)
print("results = ", results)
except Exception as e:
raise HTTPException(
status_code=500,
Expand Down Expand Up @@ -1251,7 +1281,7 @@ async def reset(self, graph_id: UUID) -> None:
# Delete all graph communities and community info
community_delete_queries = [
f"""DELETE FROM {self._get_table_name("graph_community_info")}
WHERE graph_id = $1""",
WHERE collection_id = $1""",
f"""DELETE FROM {self._get_table_name("graph_community")}
WHERE collection_id = $1""",
]
Expand Down Expand Up @@ -2182,6 +2212,7 @@ async def get_entities(
)
except json.JSONDecodeError:
pass

entities.append(Entity(**entity_dict))

return entities, count
Expand Down Expand Up @@ -2431,6 +2462,9 @@ async def get(
)
except json.JSONDecodeError:
pass
elif not include_metadata:
relationship_dict.pop("metadata", None)

relationships.append(Relationship(**relationship_dict))

return relationships, count
Expand Down

0 comments on commit 8e36251

Please sign in to comment.