Spaces:
Sleeping
Sleeping
File size: 15,889 Bytes
c2ea5ed bcbd2ec c2ea5ed 1e144a5 c2ea5ed 1e144a5 c2ea5ed 1e144a5 c2ea5ed bcbd2ec c2ea5ed bcbd2ec c2ea5ed bcbd2ec c2ea5ed bcbd2ec |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
"""
Graph Comparison Router
API endpoints for comparing knowledge graphs in the database.
"""
from fastapi import APIRouter, HTTPException, Depends, Query
from fastapi.responses import JSONResponse
from sqlalchemy.orm import Session
from typing import List, Dict, Any, Optional
from pydantic import BaseModel, Field
import logging
from backend.database import get_db
from backend.database.models import KnowledgeGraph
from agentgraph.extraction.graph_utilities import KnowledgeGraphComparator, GraphComparisonMetrics
router = APIRouter(prefix="/api/graph-comparison", tags=["graph-comparison"])
logger = logging.getLogger(__name__)
class GraphComparisonRequest(BaseModel):
"""Request model for graph comparison"""
graph1_id: int
graph2_id: int
similarity_threshold: Optional[float] = Field(0.7, description="Threshold for semantic overlap detection (0.7 = 70%)")
use_cache: Optional[bool] = True
@router.get("/graphs")
async def list_available_graphs(db: Session = Depends(get_db)):
"""
Get hierarchically organized list of knowledge graphs for comparison.
Returns:
Hierarchically organized graphs with final graphs and their associated chunk graphs
"""
try:
all_graphs = db.query(KnowledgeGraph).order_by(
KnowledgeGraph.trace_id.asc(),
KnowledgeGraph.window_index.asc()
).all()
# Categorize graphs
final_graphs = []
chunk_graphs = []
for graph in all_graphs:
# Final graphs (has window_total but no window_index, or window_index is None, or no trace_id)
if (graph.window_total is not None and
graph.window_index is None) or not graph.trace_id:
final_graphs.append(graph)
# Chunk graphs (has window_index)
elif graph.window_index is not None:
chunk_graphs.append(graph)
else:
# Orphaned graphs - treat as final graphs
final_graphs.append(graph)
# Group chunk graphs by trace_id and processing_run_id
chunks_by_trace = {}
for chunk in chunk_graphs:
trace_key = chunk.trace_id
run_key = chunk.processing_run_id or 'default'
if trace_key not in chunks_by_trace:
chunks_by_trace[trace_key] = {}
if run_key not in chunks_by_trace[trace_key]:
chunks_by_trace[trace_key][run_key] = []
chunks_by_trace[trace_key][run_key].append(chunk)
# Build hierarchical structure
organized_graphs = {
"final_graphs": [],
"total_count": len(all_graphs)
}
# Process final graphs and associate their chunk graphs
for final_graph in final_graphs:
final_data = _format_graph_data(final_graph)
final_data["graph_type"] = "final"
# Find associated chunk graphs
chunk_list = []
if final_graph.trace_id in chunks_by_trace:
run_key = final_graph.processing_run_id or 'default'
associated_chunks = chunks_by_trace[final_graph.trace_id].get(run_key, [])
for chunk in sorted(associated_chunks, key=lambda x: x.window_index or 0):
chunk_data = _format_graph_data(chunk)
chunk_data["graph_type"] = "chunk"
chunk_data["window_info"] = {
"index": chunk.window_index,
"total": chunk.window_total,
"start_char": chunk.window_start_char,
"end_char": chunk.window_end_char
}
chunk_list.append(chunk_data)
final_data["chunk_graphs"] = chunk_list
organized_graphs["final_graphs"].append(final_data)
# Ground truth graphs are now treated as final graphs - no separate processing needed
return organized_graphs
except Exception as e:
logger.error(f"Error listing graphs: {str(e)}")
raise HTTPException(status_code=500, detail="An internal error occurred while listing graphs")
def _format_graph_data(graph: KnowledgeGraph) -> Dict[str, Any]:
"""Helper function to format graph data consistently"""
graph_data = {
"id": graph.id,
"filename": graph.filename,
"creation_timestamp": graph.creation_timestamp.isoformat() if graph.creation_timestamp else None,
"entity_count": graph.entity_count,
"relation_count": graph.relation_count,
"status": graph.status,
"trace_id": graph.trace_id,
"window_index": graph.window_index,
"window_total": graph.window_total,
"processing_run_id": graph.processing_run_id
}
# Surface human-friendly system_name and summary when available in stored graph_data
try:
gd = graph.graph_data or {}
if isinstance(gd, dict):
sys_name = gd.get("system_name")
sys_summary = gd.get("system_summary")
if sys_name:
graph_data["system_name"] = sys_name
if sys_summary:
graph_data["system_summary"] = sys_summary
except Exception:
# Non-fatal: ignore extraction issues
pass
# Add trace information if available
if graph.trace:
graph_data["trace_title"] = graph.trace.title
graph_data["trace_description"] = graph.trace.description
return graph_data
@router.post("/compare")
async def compare_graphs(
request: GraphComparisonRequest,
db: Session = Depends(get_db)
):
"""
Compare two knowledge graphs and return comprehensive metrics.
Args:
request: Graph comparison request containing graph IDs and settings
Returns:
Comprehensive comparison metrics between the two graphs
"""
try:
# Extract request data
graph1_id = request.graph1_id
graph2_id = request.graph2_id
similarity_threshold = request.similarity_threshold
use_cache = request.use_cache
# Fetch the two graphs
graph1 = db.query(KnowledgeGraph).filter(KnowledgeGraph.id == graph1_id).first()
graph2 = db.query(KnowledgeGraph).filter(KnowledgeGraph.id == graph2_id).first()
if not graph1:
raise HTTPException(status_code=404, detail=f"Graph with ID {graph1_id} not found")
if not graph2:
raise HTTPException(status_code=404, detail=f"Graph with ID {graph2_id} not found")
# Get graph data
graph1_data = graph1.graph_data or {}
graph2_data = graph2.graph_data or {}
if not graph1_data:
raise HTTPException(status_code=400, detail=f"Graph {graph1_id} has no data")
if not graph2_data:
raise HTTPException(status_code=400, detail=f"Graph {graph2_id} has no data")
# Add graph_info to enable same-trace detection
graph1_data = {
**graph1_data,
"graph_info": {
"id": graph1.id,
"trace_id": graph1.trace_id,
"filename": graph1.filename
}
}
graph2_data = {
**graph2_data,
"graph_info": {
"id": graph2.id,
"trace_id": graph2.trace_id,
"filename": graph2.filename
}
}
# Initialize comparator
# Use similarity_threshold as semantic_threshold for overlap detection
# and set similarity_threshold slightly lower for general semantic similarity
semantic_threshold = similarity_threshold # Use the user's threshold for overlap detection
general_threshold = max(0.5, similarity_threshold - 0.1) # Slightly lower for general similarity
comparator = KnowledgeGraphComparator(
similarity_threshold=general_threshold,
semantic_threshold=semantic_threshold,
use_cache=use_cache
)
# Perform comparison
logger.info(f"Comparing graphs {graph1_id} and {graph2_id} (trace_ids: {graph1.trace_id}, {graph2.trace_id})")
metrics = comparator.compare_graphs(graph1_data, graph2_data)
# Add metadata about the graphs being compared
comparison_result = metrics.to_dict()
comparison_result["metadata"] = {
"graph1": {
"id": graph1.id,
"filename": graph1.filename,
"creation_timestamp": graph1.creation_timestamp.isoformat() if graph1.creation_timestamp else None,
"trace_id": graph1.trace_id,
"trace_title": graph1.trace.title if graph1.trace else None,
"system_name": (graph1.graph_data or {}).get("system_name") if isinstance(graph1.graph_data, dict) else None,
},
"graph2": {
"id": graph2.id,
"filename": graph2.filename,
"creation_timestamp": graph2.creation_timestamp.isoformat() if graph2.creation_timestamp else None,
"trace_id": graph2.trace_id,
"trace_title": graph2.trace.title if graph2.trace else None,
"system_name": (graph2.graph_data or {}).get("system_name") if isinstance(graph2.graph_data, dict) else None,
},
"comparison_timestamp": metrics.graph1_stats.get('name', 'Unknown'), # Will be set properly
"similarity_threshold": general_threshold,
"semantic_threshold": semantic_threshold,
"user_requested_threshold": similarity_threshold,
"cache_used": use_cache
}
logger.info(f"Graph comparison completed. Overall similarity: {metrics.overall_similarity:.3f}")
return comparison_result
except HTTPException:
raise
except Exception as e:
logger.error(f"Error comparing graphs {graph1_id} and {graph2_id}: {str(e)}")
raise HTTPException(status_code=500, detail="An internal error occurred while comparing graphs")
@router.get("/compare/{graph1_id}/{graph2_id}")
async def get_comparison(
graph1_id: int,
graph2_id: int,
similarity_threshold: Optional[float] = Query(0.7, description="Threshold for semantic similarity matching"),
db: Session = Depends(get_db)
):
"""
GET endpoint for comparing two graphs (alternative to POST).
Args:
graph1_id: ID of the first knowledge graph
graph2_id: ID of the second knowledge graph
similarity_threshold: Threshold for semantic similarity matching
Returns:
Comprehensive comparison metrics between the two graphs
"""
# Create request object for the POST endpoint
request = GraphComparisonRequest(
graph1_id=graph1_id,
graph2_id=graph2_id,
similarity_threshold=similarity_threshold
)
return await compare_graphs(request, db)
@router.get("/graphs/{graph_id}")
async def get_graph_details(graph_id: int, db: Session = Depends(get_db)):
"""
Get detailed information about a specific knowledge graph.
Args:
graph_id: ID of the knowledge graph
Returns:
Detailed graph information including entities and relations
"""
try:
graph = db.query(KnowledgeGraph).filter(KnowledgeGraph.id == graph_id).first()
if not graph:
raise HTTPException(status_code=404, detail=f"Graph with ID {graph_id} not found")
graph_data = graph.graph_data or {}
# Generate basic statistics
entities = graph_data.get('entities', [])
relations = graph_data.get('relations', [])
# Entity type distribution
entity_types = {}
for entity in entities:
etype = entity.get('type', 'Unknown')
entity_types[etype] = entity_types.get(etype, 0) + 1
# Relation type distribution
relation_types = {}
for relation in relations:
rtype = relation.get('type', 'Unknown')
relation_types[rtype] = relation_types.get(rtype, 0) + 1
# Calculate basic metrics
n_entities = len(entities)
n_relations = len(relations)
density = (2 * n_relations) / (n_entities * (n_entities - 1)) if n_entities > 1 else 0.0
result = {
"graph_info": {
"id": graph.id,
"filename": graph.filename,
"creation_timestamp": graph.creation_timestamp.isoformat() if graph.creation_timestamp else None,
"entity_count": graph.entity_count,
"relation_count": graph.relation_count,
"status": graph.status,
"trace_id": graph.trace_id,
"window_index": graph.window_index,
"window_total": graph.window_total
},
"statistics": {
"entity_count": n_entities,
"relation_count": n_relations,
"density": density,
"entity_types": entity_types,
"relation_types": relation_types,
"avg_relations_per_entity": n_relations / n_entities if n_entities > 0 else 0.0
},
"entities": entities[:50], # Limit to first 50 for preview
"relations": relations[:50], # Limit to first 50 for preview
"has_more_entities": len(entities) > 50,
"has_more_relations": len(relations) > 50
}
# Add trace information if available
if graph.trace:
result["trace_info"] = {
"title": graph.trace.title,
"description": graph.trace.description,
"character_count": graph.trace.character_count,
"turn_count": graph.trace.turn_count
}
return result
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting graph details for {graph_id}: {str(e)}")
raise HTTPException(status_code=500, detail="An internal error occurred while getting graph details")
@router.get("/cache/info")
async def get_cache_info():
"""
Get information about the embedding cache.
Returns:
Cache information including size and statistics
"""
try:
# Create a temporary comparator to access cache info
comparator = KnowledgeGraphComparator()
cache_info = comparator.get_cache_info()
return {
"status": "success",
"cache_info": cache_info
}
except Exception as e:
logger.error(f"Error getting cache info: {str(e)}")
raise HTTPException(status_code=500, detail="An internal error occurred while getting cache info")
@router.delete("/cache/clear")
async def clear_cache():
"""
Clear the embedding cache.
Returns:
Success message if cache was cleared
"""
try:
# Create a temporary comparator to clear cache
comparator = KnowledgeGraphComparator()
success = comparator.clear_embedding_cache()
if success:
return {
"status": "success",
"message": "Embedding cache cleared successfully"
}
else:
raise HTTPException(status_code=500, detail="Failed to clear cache")
except HTTPException:
raise
except Exception as e:
logger.error(f"Error clearing cache: {str(e)}")
raise HTTPException(status_code=500, detail="An internal error occurred while clearing cache")
|