cherry-pick b7c77396
This commit is contained in:
parent
fd7c3e269d
commit
fd109cdfcf
1 changed files with 23 additions and 35 deletions
|
|
@ -264,27 +264,25 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
|
|||
}
|
||||
"""
|
||||
try:
|
||||
# Check if entity already exists
|
||||
exists = await rag.chunk_entity_relation_graph.has_node(request.entity_name)
|
||||
if exists:
|
||||
raise ValueError(f"Entity '{request.entity_name}' already exists")
|
||||
|
||||
# Prepare entity data
|
||||
entity_data = request.entity_data.copy()
|
||||
entity_data["entity_id"] = request.entity_name
|
||||
|
||||
# Create the entity
|
||||
await rag.chunk_entity_relation_graph.upsert_node(
|
||||
request.entity_name, entity_data
|
||||
# Use the proper acreate_entity method which handles:
|
||||
# - Graph lock for concurrency
|
||||
# - Vector embedding creation in entities_vdb
|
||||
# - Metadata population and defaults
|
||||
# - Index consistency via _edit_entity_done
|
||||
result = await rag.acreate_entity(
|
||||
entity_name=request.entity_name,
|
||||
entity_data=request.entity_data,
|
||||
)
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": f"Entity '{request.entity_name}' created successfully",
|
||||
"data": entity_data,
|
||||
"data": result,
|
||||
}
|
||||
except ValueError as ve:
|
||||
logger.error(f"Validation error creating entity '{request.entity_name}': {str(ve)}")
|
||||
logger.error(
|
||||
f"Validation error creating entity '{request.entity_name}': {str(ve)}"
|
||||
)
|
||||
raise HTTPException(status_code=400, detail=str(ve))
|
||||
except Exception as e:
|
||||
logger.error(f"Error creating entity '{request.entity_name}': {str(e)}")
|
||||
|
|
@ -319,32 +317,22 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
|
|||
}
|
||||
"""
|
||||
try:
|
||||
# Check if both entities exist
|
||||
source_exists = await rag.chunk_entity_relation_graph.has_node(
|
||||
request.source_entity
|
||||
)
|
||||
target_exists = await rag.chunk_entity_relation_graph.has_node(
|
||||
request.target_entity
|
||||
)
|
||||
|
||||
if not source_exists:
|
||||
raise ValueError(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
|
||||
# Use the proper acreate_relation method which handles:
|
||||
# - Graph lock for concurrency
|
||||
# - Entity existence validation
|
||||
# - Duplicate relation checks
|
||||
# - Vector embedding creation in relationships_vdb
|
||||
# - Index consistency via _edit_relation_done
|
||||
result = await rag.acreate_relation(
|
||||
source_entity=request.source_entity,
|
||||
target_entity=request.target_entity,
|
||||
relation_data=request.relation_data,
|
||||
)
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": f"Relation created successfully between '{request.source_entity}' and '{request.target_entity}'",
|
||||
"data": {
|
||||
"source": request.source_entity,
|
||||
"target": request.target_entity,
|
||||
**request.relation_data,
|
||||
},
|
||||
"data": result,
|
||||
}
|
||||
except ValueError as ve:
|
||||
logger.error(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue