This commit is contained in:
Raphaël MANSUY 2025-12-04 19:18:38 +08:00
parent b7e1e59b91
commit edf9da2daa

View file

@ -299,6 +299,7 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
entity_data (dict): Entity properties including: entity_data (dict): Entity properties including:
- description (str): Textual description of the entity - description (str): Textual description of the entity
- entity_type (str): Category/type of the entity (e.g., PERSON, ORGANIZATION, LOCATION) - entity_type (str): Category/type of the entity (e.g., PERSON, ORGANIZATION, LOCATION)
- source_id (str): Related chunk_id from which the description originates
- Additional custom properties as needed - Additional custom properties as needed
Response Schema: Response Schema:
@ -309,6 +310,7 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
"entity_name": "Tesla", "entity_name": "Tesla",
"description": "Electric vehicle manufacturer", "description": "Electric vehicle manufacturer",
"entity_type": "ORGANIZATION", "entity_type": "ORGANIZATION",
"source_id": "chunk-123<SEP>chunk-456"
... (other entity properties) ... (other entity properties)
} }
} }
@ -329,24 +331,20 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
} }
""" """
try: try:
# Check if entity already exists # Use the proper acreate_entity method which handles:
exists = await rag.chunk_entity_relation_graph.has_node(request.entity_name) # - Graph lock for concurrency
if exists: # - Vector embedding creation in entities_vdb
raise ValueError(f"Entity '{request.entity_name}' already exists") # - Metadata population and defaults
# - Index consistency via _edit_entity_done
# Prepare entity data result = await rag.acreate_entity(
entity_data = request.entity_data.copy() entity_name=request.entity_name,
entity_data["entity_id"] = request.entity_name entity_data=request.entity_data,
# Create the entity
await rag.chunk_entity_relation_graph.upsert_node(
request.entity_name, entity_data
) )
return { return {
"status": "success", "status": "success",
"message": f"Entity '{request.entity_name}' created successfully", "message": f"Entity '{request.entity_name}' created successfully",
"data": entity_data, "data": result,
} }
except ValueError as ve: except ValueError as ve:
logger.error( logger.error(
@ -365,10 +363,11 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
""" """
Create a new relationship between two entities in the knowledge graph Create a new relationship between two entities in the knowledge graph
This endpoint establishes a directed relationship between two existing entities. This endpoint establishes an undirected relationship between two existing entities.
Both the source and target entities must already exist in the knowledge graph. The provided source/target order is accepted for convenience, but the backend
The system automatically generates vector embeddings for the relationship to stored edge is undirected and may be returned with the entities swapped.
enable semantic search and graph traversal. Both entities must already exist in the knowledge graph. The system automatically
generates vector embeddings for the relationship to enable semantic search and graph traversal.
Prerequisites: Prerequisites:
- Both source_entity and target_entity must exist in the knowledge graph - Both source_entity and target_entity must exist in the knowledge graph
@ -380,6 +379,7 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
relation_data (dict): Relationship properties including: relation_data (dict): Relationship properties including:
- description (str): Textual description of the relationship - description (str): Textual description of the relationship
- keywords (str): Comma-separated keywords describing the relationship type - keywords (str): Comma-separated keywords describing the relationship type
- source_id (str): Related chunk_id from which the description originates
- weight (float): Relationship strength/importance (default: 1.0) - weight (float): Relationship strength/importance (default: 1.0)
- Additional custom properties as needed - Additional custom properties as needed
@ -392,6 +392,7 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
"tgt_id": "Tesla", "tgt_id": "Tesla",
"description": "Elon Musk is the CEO of Tesla", "description": "Elon Musk is the CEO of Tesla",
"keywords": "CEO, founder", "keywords": "CEO, founder",
"source_id": "chunk-123<SEP>chunk-456"
"weight": 1.0, "weight": 1.0,
... (other relationship properties) ... (other relationship properties)
} }
@ -415,36 +416,22 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
} }
""" """
try: try:
# Check if both entities exist # Use the proper acreate_relation method which handles:
source_exists = await rag.chunk_entity_relation_graph.has_node( # - Graph lock for concurrency
request.source_entity # - Entity existence validation
) # - Duplicate relation checks
target_exists = await rag.chunk_entity_relation_graph.has_node( # - Vector embedding creation in relationships_vdb
request.target_entity # - Index consistency via _edit_relation_done
) result = await rag.acreate_relation(
source_entity=request.source_entity,
if not source_exists: target_entity=request.target_entity,
raise ValueError( relation_data=request.relation_data,
f"Source entity '{request.source_entity}' does not exist"
)
if not target_exists:
raise ValueError(
f"Target entity '{request.target_entity}' does not exist"
)
# Create the relationship
await rag.chunk_entity_relation_graph.upsert_edge(
request.source_entity, request.target_entity, request.relation_data
) )
return { return {
"status": "success", "status": "success",
"message": f"Relation created successfully between '{request.source_entity}' and '{request.target_entity}'", "message": f"Relation created successfully between '{request.source_entity}' and '{request.target_entity}'",
"data": { "data": result,
"source": request.source_entity,
"target": request.target_entity,
**request.relation_data,
},
} }
except ValueError as ve: except ValueError as ve:
logger.error( logger.error(